Help understanding mixer gain()

notahardwareguy

Active member
I have been experimenting with the audio library and with multiple mixers. I have also reviewed the PlaySynthMusic example but am still confused at to the relationship between combinations of mixer channels and the gain control. The documentation for mixers in the audio design tool is just not helping me.

For the primary mixer control "gain(channel, level)" the documentation says:

Adjust the amplification or attenuation. "channel" must be 0 to 3. (makes perfect sense as there are only 4 inputs to the mixer) "level" may be any floating point number from 0 to 32767.0. (awesome, I have a range to play within) 1.0 passes the signal through directly. (so 1.0 mean "no change", good) Level of 0 shuts the channel off completely. (also good, I can turn an individual channel on and off) Between 0 to 1.0 attenuates the signal, and above 1.0 amplifies it. (so any value above 0 but below 1 acts to dampen the signal to some degree and any value above one amplifies it - so for music 32767 would be bad?) Negative numbers may also be used, to invert the signal. All 4 channels have separate gain settings.

OK, that seems all reasonable and fair. In my case I plan to use all four mixer lines and at any one time have from one to four lines on. I also want every line and combination of lines to be at the original level, no attenuation or amplification by the mixer. Sounds like I would just set all the gain values to 1.0 if I want that channel on and set them to 0.0 if I want them off, yes? For example:

gain( 0, 1.00 ); // on
gain( 1, 0.00 ); // off
gain( 2, 1.00 ); // on
gain( 3, 0.00 ); // off

BUT then the documentation also says:

Signal clipping can occur when any channel has gain greater than 1.0, or when multiple signals add together to greater than 1.0.

NOW I am confused! If I am having more than one channel active, how do I adjust the gain so that they are all mixed at the same "gain" and don't exceed 1? I tried dividing 0.99 by the number of active channels but that does not sound right when I hear it as I change the number/mix of active channels. One channel at 0.99 sounds louder(?) and less muted than two at 0.45. How do I get any combination of active mixer lines to "sound" the same?

I want to take a single digital music signal, send it through various effects and filters, send all that into multiple mixers combining them all together and sending them to the same output. I do send the left and right signals down their own identical paths with identical filter, effect, and mixer settings.

Sorry if I am being an idiot and this should be obvious but I am just not getting it. Can someone explain this to me? Thanks much.
 
Setting the gain to 0.5 divides/damps the input signal by half which is -6dB. Setting the gain to 0.25 divides the input signal by a quarter which is -12dB.
So setting all 4 channels to a gain between 0.00 and maximum 0.25 will prevent the output to be clipped when all 4 signals are at max input voltage.
This is how I understand that it works (but I could be wrong).

Paul
 
Yeah, I struggle with that too, its just not a very elegant implementation.

It might help thinking about what happens under the hood, to better conceptualize it:

- All audio signals are 16 bit values.
- The "mixer" object simply adds the signals together.
- any part of the signal which exceeds these 16 bits is clipped.

Jumping back and forth between gain levels the way you suggested would lead to noticeable jumps in volume as you report. The alternative would be to set them all to 0.25 at input essentially limits you to 4 bit per signal, which I find non-ideal. Ideally you'd want so type of compressor/limiter object, but I'm not sure anything out of the box to address your problem exists.

(There have been some discussions about the limitations of the 16 bit processing in the past. I remember some grumpy audio-guy *demanding* that 32 bit processing be implemented on this forum, and then being completely unable to articulate why it is useful (that thread is actually pretty fun to read, if you manage to dig it out). This is one of the examples where it would be nice to have, cause you could then add up all the signals and then dynamically reduce the amplitude as needed, rather then resorting to reducing bit-depth and then adding the low-fi signals together)

Sorry, if this is not helpful, its just to say: You're not the only one who is struggling with this.
 
set them all to 0.25 at input essentially limits you to 4 bit per signal
Its not that bad. You loose a bit for each power of two number signals added, so you are down to 14 bits per signal.

There is a floating point version of the audio library. I have not looked at it so do not know how many of the audio objects have been converted. Using floating point would 'kick the problem down the road' as the result would still need to be converted back to 16 bit for the typical output codec.
 
Its not that bad. You loose a bit for each power of two number signals added, so you are down to 14 bits per signal.

There is a floating point version of the audio library. I have not looked at it so do not know how many of the audio objects have been converted. Using floating point would 'kick the problem down the road' as the result would still need to be converted back to 16 bit for the typical output codec.

That's the beauty of using the floating point (OpenAudio) library: you carry around ~24bits at each step; also, you can amplify & sum in mixers without worrying about clipping

Marc
 
Back
Top