Order of sound effects in FamiTone

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Order of sound effects in FamiTone
by on (#165421)
When you play sound effects on FT_SFX_CH0 to FT_SFX_CH3 in FamiTone at the same time, which of the sounds is played if all of them use the same NES audio channel?

Does FT_SFX_CH0 always have the highest priority? Or does the one that I call last have the highest priority?

I know that when you play two sounds on the same FT_SFX_CH, then the later sound stops the former one.
But I'm indeed talking about the fact that, at the same time, we play one sound effect on FT_SFX_CH0, one on FT_SFX_CH1, one on FT_SFX_CH2 and one on FT_SFX_CH3.

Which one is played over the other ones if they overlap? Do these values have an internal priority system with 0 having a higher priority than 3? Or is it always the last one that overrides all others?
Re: Order of sound effects in FamiTone
by on (#165424)
You know. I've been trying to figure out all the workings of Famitone for a while...

I know that ldx #FT_SFX_CH0 doesn't mean that the sound will go to Pulse channel, but rather is a way to separate Famitone sound effects so they don't collide. A new FT_SFX_CH0 will overwrite any current FT_SFX_CH0 sound effect....EDIT...it definitely says 'priority', but not too clear on how that's implemented, since you can play a CH0 effect while a CH1 effect is sounded, as long as they don't use the same PPU channel.

This comment in the source code...

Quote:
;compare effect output buffer with main output buffer
;if volume of pulse 1 of effect is higher than that of the
;main buffer, overwrite the main buffer value with the new one


Makes me think that the sound effect will only play if it is LOUDER than the music currently playing on that channel, which doesn't make sense to me....but it seems that the nsf2data tool writes the output of all 4 channels into the data file, and puts a volume of zero for channels not used...so I think this is a way to make sure only PPU channels specified in the SoundFx file will effect the sound output. But, it makes me also think that if you specified a very quiet sound effect to play on the same PPU channel of loud music, that the sound effect would never play.



EDIT 2 - It first writes the music to a BUFFER, then it goes through the SFX 'channels' in this order...

Code:
   .if FT_SFX_STREAMS>0
   ldx #FT_SFX_CH0
   jsr _FT2SfxUpdate
   .endif
   .if FT_SFX_STREAMS>1
   ldx #FT_SFX_CH1
   jsr _FT2SfxUpdate
   .endif
   .if FT_SFX_STREAMS>2
   ldx #FT_SFX_CH2
   jsr _FT2SfxUpdate
   .endif
   .if FT_SFX_STREAMS>3
   ldx #FT_SFX_CH3
   jsr _FT2SfxUpdate
   .endif


But, each pass, it compares the volume of the FT_OUT_BUF (output buffer) to the sound effect, and plays whichever has the highest volume...if at same volume, it will use sound effect. SFX CH3, therefore, at full volume will have priority over all others. Next CH2, CH1, CH0, then the music.


Someone please correct me if I'm wrong.
Re: Order of sound effects in FamiTone
by on (#165425)
dougeff wrote:
I know that ldx #FT_SFX_CH0 doesn't mean that the sound will go to Pulse channel, but rather is a way to separate Famitone sound effects so they don't collide.

Yes, I know this. The "channel" isn't one of the APU audio channels here, but just a "layer" for simultaneous sounds.

What I wanted to know:
Let's say I have two sound effects.
One sound effect plays the note C on pulse 1.
And one sound effect plays the note D on pulse 1.

I.e., both sound effects use the same audio channel and therefore cannot be played at the same time.

Now I let one of the sound effects play on FT_SFX_CH0 and one on FT_SFX_CH1.

My question: Which sound is played?

Is it the sound for FT_SFX_CH0? (Which means that FT_SFX_CH0 to FT_SFX_CH3 have a priority order in which FT_SFX_CH0 is always the most primary sound layer if two sound effects overlap.)

Or is it the sound that was played last? (Which means that FT_SFX_CH0 to FT_SFX_CH3 are equal among each other since every one of them can potentially mute the sound every another layer.)


Edit: About your last part: So, can we be sure that FT_SFX_CH3 always trumps the other sound layers?
Re: Order of sound effects in FamiTone
by on (#165427)
I'd have to test it to be sure. I'm kind of busy today.
Re: Order of sound effects in FamiTone
by on (#165438)
1. Volume has priority (applies each frame)
2. Channel has secondary priority (i.e. channel 1 has priority over channel 0 if same volume)

Edit: apparently the secondary priority is reversed from what I was led to believe.
Re: Order of sound effects in FamiTone
by on (#165442)
rainwarrior wrote:
(i.e. channel 0 has priority over channel 1 if same volume)

But doesn't the previously posted code:
Code:
   .if FT_SFX_STREAMS>0
   ldx #FT_SFX_CH0
   jsr _FT2SfxUpdate
   .endif
   .if FT_SFX_STREAMS>1
   ldx #FT_SFX_CH1
   jsr _FT2SfxUpdate
   .endif
   .if FT_SFX_STREAMS>2
   ldx #FT_SFX_CH2
   jsr _FT2SfxUpdate
   .endif
   .if FT_SFX_STREAMS>3
   ldx #FT_SFX_CH3
   jsr _FT2SfxUpdate
   .endif
imply that CH3 has priority over all the others?

Maybe I should try it out myself:

Since I don't know that much about FamiTracker, can somebody please create an FTM file that includes two songs?
Each song should be only one single note that is played for a bunch of seconds (or infinitely if that still works with FamiTone).
Both songs should play their note on the same APU channel and with the same volume.
One song should play a low note, the other a high note, so that both songs are immediately distinguishable.

Then I can try out and see for myself which FamiTone channel has priority when I play the sounds. Or if one sound automatically overrides the other one.
Re: Order of sound effects in FamiTone
by on (#165451)
I've confirmed my earlier conclusion...

SFX CH3 = highest priority
SFX CH2 = next
SFX CH1 = next
SFX CH0 = next
song = lowest

I'll post source code in a little bit...
Re: Order of sound effects in FamiTone
by on (#165452)
At least for me, source code is not so important. I can quickly write some function in my game to test it.
One thing that would help me: An FTM file with two sound effects that are equal in volume and APU channel and that play only one note each, but a different note in each song, for the duration of a few seconds.
Re: Order of sound effects in FamiTone
by on (#165454)
Oddly, one thing that was tripping me up, was how to define a thing as going to CH1 or CH2...

FT_SFX_CH0 = 0
FT_SFX_CH1 = $0f
FT_SFX_CH2 = $1e
FT_SFX_CH3 = $2d

the X register must be loaded with this value before going to FamiToneSfxPlay
Re: Order of sound effects in FamiTone
by on (#165457)
What's so odd about it? This shows the function on which "channel" you want to play the sound effect.
Re: Order of sound effects in FamiTone
by on (#165458)
DRW wrote:
But doesn't the previously posted code imply that CH3 has priority over all the others?

It depends on what this meant:
Quote:
if volume of pulse 1 of effect is higher than that of the main buffer, overwrite the main buffer value with the new one

If this is a > test, then no, if this is a >= test then yes. The wording implies > but check the source code if you want to know for sure.

DRW wrote:
Maybe I should try it out myself.

Yes.
Re: Order of sound effects in FamiTone
by on (#165464)
Dann fress ich einen Besen!


Roughly translated... "I'll eat a broom if I'm wrong!"
Re: Order of sound effects in FamiTone
by on (#165469)
rainwarrior wrote:
DRW wrote:
Maybe I should try it out myself.

Yes.

I'd need some test FTM file for this, though. Who knows FamiTracker well enough to create a sample file as I described it above?

Reading the sound code will probably not help me much. But I can make my program play a sound when I press A and another sound when I press B. And then I can see if one of them always has priority or if the sound that I play later overwrites the one that I played earlier. And if one always has priority, I can see whether it's the lowest or the highest FamiTone "channel".

I can basically find this out myself, but I need a fitting sample FTM file.
Re: Order of sound effects in FamiTone
by on (#165472)
Here's my source code...(modified from a different source code I've used)...included FTM files.

http://dl.dropboxusercontent.com/s/vzqf ... M_TEST.zip

It takes some explaining...
I'm playing a constant C-3 as the song...on Pulse 1 channel.
SFX 0 = C-1 note. SFX 1 = C-2 note. SFX 2 = C-4 note. SFX 3 = C-5 note. All on Pulse 1 channel.

I have START set to play all the SFX at the same time, in their respective 'channel', in order.
SELECT reverses which effect mapped to channel.
A_BUTTON reverses their order.

It works as expected. CH3 is highest priority. It's always the one mapped to CH3 that sounds.
Re: Order of sound effects in FamiTone
by on (#165473)
Ah, so it's >= after all. Revised my earlier post.