How to mix audio samples?
Asked Answered
D

4

9

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.

Dreamadreamer answered 23/11, 2009 at 16:43 Comment(2)
What programming language and platform?Lubin
What language are you using? What libraries? Give some more details.Isadoraisadore
H
7

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.

Havelock answered 23/11, 2009 at 16:47 Comment(3)
Thanks for guessing right what I wanted to ask with my original question ;)Dreamadreamer
It's a pretty common problem, as you might imagine.Havelock
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!Darwen
H
5

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.

Harvison answered 23/11, 2009 at 16:48 Comment(2)
Clamping is hard-limiting, and sounds pretty bad. He's more likely to want a knee on that limiting. :-)Havelock
He might, but that's technically something else other than what he asked :)Harvison
V
4

dirty mix of two samples

mix = (a + b) - a * b * sign(a + b)
Vaccine answered 1/4, 2013 at 21:11 Comment(0)
L
0

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.

Lubin answered 23/11, 2009 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.