views:

521

answers:

4

My question is not completely programming-related, but nevertheless I think SO is the right place to ask.

In my program I generate some audio data and save the track to a WAV file. Everything works fine with one sound generator. But now I want to add more generators and mix the generated audio data into one file. Unfortunately it is more complicated than it seems at first sight. Moreover I didn't find much useful information on how to mix a set of audio samples.

So is there anyone who can give me advice?

edit:

I'm programming in C++. But it doesn't matter, since I was interested in the theory behind mixing two audio tracks. The problem I have is that I cannot just sum up the samples, because this often produces distorted sound.

+1  A: 

You never said what programming language and platform, however for now I'll assume Windows using C#.

http://www.codeplex.com/naudio

Great open source library that really covers off lots of the stuff you'd encounter during most audio operations.

Nissan Fan
+2  A: 

I assume your problem is that for every audio source you're adding in, you're having to lower the levels.

If the app gives control to a user, just let them control the levels directly. Hotness is their responsibility, not yours. This is "summing."

If the mixing is automated, you're about to go on a journey. You'll probably need compression, if not limiting. (Limiting is an extreme version of compression.)

Note that anything you do to the audio (including compression and limiting) is a form of distortion, so you WILL have coloration of the audio. Your choice of compression and limiting algorithms will affect the sound.

Since you're not generating the audio in real time, you have the possibility of doing "brick wall" limiting. That's because you have foreknowledge of the levels. Realtime limiting is more limited because you can't know what's coming up--you have to be reactive.

Is this music, sound effects, voices, what?

Programmers here deal with this all the time.

Nosredna
Thanks for guessing right what I wanted to ask with my original question ;)
ralle
It's a pretty common problem, as you might imagine.
Nosredna
It certainly is, I'm running into the same problem mixing two generated sounds in actionscript 3. Thanks for the links, looks like I've got some reading to do on compressors!
vitch
+2  A: 

Mixing audio samples means adding them together, that's all. Typically you do add them into a larger data type so that you can detect overflow and clamp the values before casting back into your destination buffer. If you know beforehand that you will have overflow then you can scale their amplitudes prior to addition - simply multiply by a floating point value between 0 and 1, again keeping in mind the issue of precision, perhaps converting to a larger data type first.

If you have a specific problem that is not addressed by this, feel free to update your original question.

Kylotan
Clamping is hard-limiting, and sounds pretty bad. He's more likely to want a knee on that limiting. :-)
Nosredna
He might, but that's technically something else other than what he asked :)
Kylotan
+1  A: 

Use Audactiy.

http://audacity.sourceforge.net/

  1. Record from microphone, line input, or other sources.
  2. Dub over existing tracks to create multi-track recordings.
  3. Record up to 16 channels at once (requires multi-channel hardware).
  4. Level meters can monitor volume levels before, during, and after recording
Yada
And audacity can use VST compression and limiting plugins.
Nosredna