Re: Sound and mixing channels question

From: MSCHAEF.COM (mschaef_at_fnord.io.com)
Date: 02/29/04


Date: Sat, 28 Feb 2004 21:00:27 -0600

In article <JSa0c.50722$mU6.204665@newsb.telia.net>,
Mats <spamenot.mog.pettersson@telia.com> wrote:
  ...
>My question is though if say, you have a one byte per sample (or 8 bit
>per sample if you like) buffer and mixes say two channels into that
>buffer, the code i've seen just adds the two channels, but what happens
>if one or several bytes overflow? The output of say 240+128 is 368 which
>would set the mixed byte to 368-256=112?!? Wouldn't this distort the sound?

Yes, there'd be an incredible amount of distortion. One way to begin to
handle this is via saturation arithmetic. Rather than overflowing and
wrapping around as in your example, it caps the result at the maximum
representable value. This has the effect of clipping the tops of the
waveforms, which is more analagous to what happens in actual analog audio
hardware.

Of course, this still is pretty severe distortion, and the best way to
handle that is via mixing your sound in such a way that it does not
overflow at all. However, if you lower the amplitudes of the waveforms
before you add them together, you're throwing away information. It's
better to cast to a wider type, add the samples, and than scale down. I've
used double precision floats for this to good effect, but my application
was very lightweight.

-Mike

-- 
http://www.mschaef.com


Relevant Pages

  • Detailed analysis: Buffer overflow in Explorer.exe on Windows XP SP1
    ... Actually, I assume the overflowing file, no matter where it is located in ... it assumes the buffer to be as ... We do not know how this bug affects shell32.dll files on other Windows ... unintentionally execute arbitrary code. ...
    (Bugtraq)
  • Re: Advice on PC Based Logic Analyzer
    ... article in that I will rather buy 8 'good channels' with sufficient ... buffer depth than 32 channels and anyway not be able to use them all. ... busses are contained within the processor core, so the analyzer is ... needed is to delay the trigger based on an edge count or time. ...
    (sci.electronics.equipment)
  • Re: [PATCH 3/3] relay: Add buffer-only channels; useful for early logging.
    ... 20d8b67c06fa5e74f44e80b0a0fd68c8327f7c6a) broke LTTng. ... buffer file creation callback to be called when it calls relay_open. ... Therefore, such channels can be ...
    (Linux-Kernel)
  • Re: Once again multi channel sound...
    ... Learning advanced sound programming is quite a challenge! ... Splitting or mixing uncompressed PCM audio channels is not ... the buffer is 123412341234, where the numbers represent ... WAVEFORMATEX structure: if you have more than 2 channels, ...
    (microsoft.public.win32.programmer.directx.audio)
  • Re: Ancient history
    ... >> the boundary is between overflowing and access to an extended area ... Changing from the fixed sized queues implemented using an array ... When an entry is made a buffer is requested from the heap. ... paragraph for every line he writes. ...
    (sci.crypt)