PaulStoffregen / Audio

Teensy Audio Library
http://www.pjrc.com/teensy/td_libs_Audio.html
1.07k stars 398 forks source link

AudioMixer4 behaves poorly when saturating #431

Open h4yn0nnym0u5e opened 2 years ago

h4yn0nnym0u5e commented 2 years ago

Description

When 2 or more inputs sum to values outside the ±1.0 (±32767) range, the mixer output can be erroneous at some (but not all) points in the waveform.

Steps To Reproduce Problem

Consider using just two inputs, with maximum-amplitude square waves of differing frequency applied to them. Input 0 has a gain of 2.0, input 1 a gain of 0.5. The only output values possible from such a system are 2.5, 1.5, -1.5 and -2.5; these should be output, therefore, as either 1.0 or -1.0, depending only on the state of input 0.

Because AudioMixer4 saturates on the fly, though, we can see an effect whereby input 0 is saturated to ±1.0; at a point where the existing value is +1.0, when input 1 is mixed in, if its value is +0.5 then the output remains saturated at +1.0, but if it's at -0.5 then the output becomes an erroneous +0.5. We therefore end up with an incorrect waveform containing values of +1.0, +0.5, -0.5 and -1.0.

Hardware & Software

Any

Arduino Sketch

Shouldn't be necessary, but I could do one if you really want it.

Errors or Incorrect Output

As noted in "steps to reproduce", above

Proposed solution

Mix into an intermediate 32-bit buffer, and only saturate once mixing is complete. This should suffice for inputs with the gains set to any value up to 8192.0, which is a reasonably unlikely value.

Or possibly don't solve it - this may be what would happen with an analogue mixer...