[linux-audio-dev] Simple Jack mixing problem

David Olofson david at olofson.net
Fri Jan 5 08:45:07 UTC 2007

On Friday 05 January 2007 01:44, florentberthaut at no-log.org wrote:
> Hi everyone,
> I'm working on a sampler and i have a really silly problem.
> I simply want to mix sounds in a stereo buffer within the
> jack "process" function.
> So for each frame i sum the samples from each sound but then the
> values go above 1 and i get clipping.
> So my first thought was to divide by the number of samples playing,
> but it ends up with a decrease in volume.
> I tried to limit the values between -1 and 1 but it 's also
> clipping -> distortion.

Well, that's what JACK is doing anyway - or there would be wrapping in 
the float -> int conversion, which sounds a lot worse than clipping. 
It's just not possible to represent larger amplitudes than [-1, 1] in 
the integer domain (that is, [-32768, 32767] or whatever, depending 
on resolution), which is what DACs deal with.

> I looked into some sources and it seems i would actually just have
> to sum the samples.

Yep, that's all there is to it, really - and it's the user's 
responsibility to keep volumes low enough to avoid driving the output 
into clipping.

This is true on hardware synths and samplers too. The only reason some 
of them appears not to have this problem is that they use various 
tricks to hide it.

For example, the old Roland JV-1080 had 18 bit DACs, where the two 
extra bits were pretty much used for extra headroom above 
the "normal" max amplitude of a single voice. Even so, it was pretty 
easy to drive it into clipping by using resonant filters and/or lots 
of loud voices.

I wouldn't be surprised if some synths and samplers use 20 och 24 bit 
DACs for even more headroom, waveshaping to make the last 12 dB or so 
non-linear (soft saturation, sort of), dynamic output gain control 
and stuff like that.

Now, if you're dealing in digital, extra DAC bits and dynamic output 
gain control is out, obviously. (Goes for plugins and JACK clients as 
well as hardware devices with digital outputs.) So, either you set 
your sampler's 0 dB level at -12 dB or something (allows mixing four 
maximized samples at maximum volume without clipping), or you leave 
that to the user.

If you really want to make the impression sqeezing in more than 
there's room for, you'll have to add some dynamics processing. There 
is of course no way of doing this without some sort of distortion, 
but even a simple waveshaper to "flatten out" peaks above 12 dB below 
clipping sounds a great deal better than hard clipping.

However, I'd rather see that kind of stuff left to the user. Those of 
us who use 20+ bit sound cards would rather have it all linear and 
undistorted right into the amplifier. There is always the option of 
inserting some serious multiband compressor last thing in the JACK 
chain. Might actually be a good idea anyway, if you like playing loud 
and don't want to ruin your ears if some synth or effect freaks out.

//David Olofson - Programmer, Composer, Open Source Advocate

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'

More information about the Linux-audio-dev mailing list