gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Audio > Mixing

#16084 - DarkPhantom - Sat Feb 07, 2004 9:00 pm

I've been playing with sound mixing. I just looked up how to feed samples to the hardware and then I used what I learned from highschool physics about wave interference to mix the samples and I was pretty amazed (and proud of myself) that it worked. :) I didn't really think this would work but all I did wave add the PCM codes because that's how we used to interfere waves in science class. Anyway, are there other approaches to this or did I hit paydirt here? If this is the "proper" technique than one question, what do I do in the event of an overflow?

Say I'm mixing two samples with PCM codes of 64 and 63. If I add them I get a new value of 127 which is the maximum since GBA has 8-bit signed samples. That's fine but what if the samples are 64 and 64? That's an overflow and I endup with a sample value of -128 which is bad, right?

#16088 - poslundc - Sat Feb 07, 2004 9:30 pm

You've got the right strategy. There are two things you can do to handle overflow: clipping and scaling.

Clipping is where you check to see if the value exceeds its bound, and then cut it off. So you would mix (add) your two channels into a 16-bit value, then if that value was greater than 127 you would assign 127 to it. Then you would check to see if it was less than -128, and if so assign -128 to it.

Scaling is where you divide the samples by a constant amount before adding them, so they can't overflow. So if you are mixing two 8-bit samples together, you can divide both of them by 2 before adding and you can be guaranteed not to overflow.

Scaling is usually much faster than clipping, but can also result in significant volume loss because you are reducing the amplitude of your wave (this gets worse the more channels you mix in). Whether or not it's a problem depends on you: my mixer does 4 channels in the left side and 4 channels in the right side using scaling, and it sounds just fine.

Dan.

#16131 - FluBBa - Mon Feb 09, 2004 2:37 pm

And dont scale before you add them, scale them afterwards.
For 4 channels you can lose up to 4 steps in the final output, might not look much but its really unnecessary to scale before you add.
_________________
I probably suck, my not is a programmer.

#16134 - poslundc - Mon Feb 09, 2004 3:29 pm

FluBBa wrote:
And dont scale before you add them, scale them afterwards.


That's correct; I don't know why I thought it should be done the other way around.

Dan.

#16135 - FluBBa - Mon Feb 09, 2004 3:57 pm

I just thought that maybe you can add 4 bytes at once if scale them before you add them...
Dont know if its worth it though.
_________________
I probably suck, my not is a programmer.

#16143 - poslundc - Mon Feb 09, 2004 8:34 pm

A lot of it depends on how you want to optimize. In my mixer's case, I'm mixing 64 volume levels with 4 channels per side; using those restrictions I can implement scaling for four samples at a time extremely quickly.

I just use two registers to mix two 16-bit samples each (using mla for pseudo-vector processing), then when it comes time to scale I just set up a masking register with 0x00FF00FF in it. Then I AND the mask-register with both of my mix-registers shifted down by 8 (which divides by 256, or 4 channels * 64 volume levels). Then I just OR one mix register with the other shifted left by 8 to get my completed word.

Four mixed samples take approximately four arithmetic instructions to scale and combine. Versus 5 to 7 instructions per sample using clipping, so about 20-30 instructions to do all 4 samples.

Of course, everything depends on what your needs are. I'm glad I realized early on that my game didn't require things like full panning and that the volume drop from scaling wouldn't be a problem.

Dan.

#16144 - animension - Mon Feb 09, 2004 8:41 pm

Mix with a minimum of 16-bits to allow for overflow of 8-bit samples. This will allow for up to 256 channels to overflow and still retain accuracy, ie, you will be able to mix in as many channels as you need and not wrap around the values with overflow. When you're ready to send the sample to hardware, then scale or clip the final sample value.

Most of the time you won't get distortion even when mixing many samples in unless the waveforms are too close in shape to eachother.
_________________
"Beer is proof that God loves us and wants us to be happy."
-- Benjamin Franklin