Volume interpolation for sound effect overlay?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Volume interpolation for sound effect overlay?
by on (#159486)
I've run into an odd artifact in my sound engine, which only seems to be noticeable in certain situations. I've got a volume envelope in the square 1 channel that gradually increases in volume. During this song, a sound effect may play and overlay on the same square 1 channel with a different envelope, with different volume settings. Right now, it is overlaid directly, frame by frame, so when the sound effect ends, the music stream will supply where it would have been in its own envelope, potentially making a rude transition from one volume setting sharply to another. This results in an unpleasant "hiccup." And, I am certain this is not the effect described in MetalSlime's sound tutorial over on NA:

"Crackling Sounds: Writing to the 4th port of the Square channels ($4003/$4007) has the side effect of resetting the sequencer. If we write here too often, we will get a nasty crackling sound out of our Squares. This is not good."

I already followed his advice for preventing this effect when I upload all my apu register data. I think what I'm experiencing is purely due to abrupt shifting of the volume setting. If I'm right, it makes me wonder if others here who have written sound engines have any scheme to quickly interpolate volume values when overlaying sound effects, over a couple of frame perhaps---to smooth out this problem.
Re: Volume interpolation for sound effect overlay?
by on (#159487)
Mine silences the channel after a sound effect, and waits for the next NEW note before triggering.
Also, I keep the main song note on Sq 1 and square sound fxs on Sq 2, so the song doesn't sound interrupted.

(I'm still trying to think of a better engine, my sound effects are awfully simple)
Re: Volume interpolation for sound effect overlay?
by on (#159488)
The "crackling sound" refers to when you do $4003/7 it in the middle of a note, e.g. if you have vibrato near A-3 (see: Mega Man 2 ending).

Getting a single "pop" from the phase reset at the start of a new note or sound effect is normal, and shouldn't be a problem. There are many things that change all of a sudden (pitch, volume, duty) when you begin a new note or sound effect, so a interruption of phase via $4003/7 is more or less irrelevant. The "crackling" problem is when you are trying to have a smooth continuous sound, but get discontinuities because of the phase reset (like with the vibrato example I linked).

Usually the start of a sound effect interrupting something is not a problem; sound effects are supposed to sound like an interruption. They should stick out and notify the player that something has happened in the game. The usual problem area is what happens when the sound ends.

If you resume the music in the middle of a note, it doesn't sound very musical; it sounds like an interruption, like a sound effect. There are a number of ways to deal with this. My own approach is just to leave the channel silent after a sound effect ends, and only resume the music at the next note. That way I never hear a partial note out of place. (As a consequence of this, on channels that are to be interrupted by SFX, I try to compose with frequent notes instead of long sustained ones, so the period of silence isn't long and glaring.)

Other people do things like fade a playing note back in over several frames, or comparing the volume of SFX vs Music and just playing whichever was louder that frame. I'm not really a fan of these techniques, but they're an option if you like them.
Re: Volume interpolation for sound effect overlay?
by on (#159490)
Thanks rainwarrior, that's quite helpful. I tend to use a lot of sustained notes in my own music...so I'm gonna try the fade-in-after-sfx-terminate idea and see how I like it.
Re: Volume interpolation for sound effect overlay?
by on (#159492)
rainwarrior wrote:
comparing the volume of SFX vs Music and just playing whichever was louder that frame

That's what I do, especially because in my engine, drums can interrupt the note on the triangle channel in the same way that sound effects interrupt a note. This behavior ensures that if a sound effect has a long, quiet tail, a louder note on the same channel can interrupt that when prudent. And even though the triangle has no real volume control, it still uses the volume as a priority value between music and sound effects. For example, if a triangle kick drum has its volume bits set low for the last few frames, it will get cut slightly sooner when a note is playing than when not. Thanks to the the auditory phenomena of simultaneous masking, illusory continuity, and illusory discontinuity, the net effect is almost like having a mixer.
Re: Volume interpolation for sound effect overlay?
by on (#159496)
Alright...tried all three techniques. I'm not sure I did the sfx/music volume compare quite correctly. However, I was able to get fade-in-after-terminate to work and also silence-until-note. I think I prefer silence-until-note. This sounded the least disruptive of all, in the context of the music/sfx I was working with. Thanks guys!