

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm trying to mix six sound clips together.

Imagine each clip is a single guitar string pluck sound and I want to mix them to produce a guitar chord.

Here, a clip is an array of real numbers in the range [-1,1], where each number is a mono sample.

double mixed_sample = mix(double sample1, ..., double sample6);

Please, implement mix!

share|improve this question
up vote 1 down vote accepted

You have got to be kidding.

Mixing is simple addition of signals.

double mix(double s1, double s2, double s3, double s4, double s5, double s6)
  return (s1 + s2 + s3 + s4 + s5 + s6);

Next step is to provide individual channel gains.

double variable_mix(double s1, double s2, double s3, double s4, double s5, double s6,
                      double g1, double g2, double g3, double g4, double g5, double g6)
  return (s1*g1 + s2*g2 + s3*g3 + s4*g4 + s5*g5 + s6*g6);

Of course, this is kind of a pain in the ass to code, and the parameter-passing overhead will eat you alive, but this is basically what you have to do.

share|improve this answer
So, if only one string produce sound and the five other are silent, the amplitude of the mixed clip will be 1/6 the amplitude of the original clip? – Nicolas Repiquet Nov 16 '10 at 19:59
@Nicolas: that's correct - it's no different than a real guitar in the real world - a chord is louder than a single string. – Paul R Nov 16 '10 at 20:03
Don't forget the time offsets. Guitar strings are not strummed or picked at the same time. – Gilbert Le Blanc Nov 16 '10 at 20:10
@Gilbert, managing the playback of the individual string sample sequences that feed the mixer is a completely different issue. You are correct that it is important: if they are all started at exactly the same time, you will hear a single complex sound, not a chord. (This experiment is easy to do on an electronic keyboard, and VERY instructive. I think I first heard about it from something Wendy Carlos wrote.) – John R. Strohm Nov 16 '10 at 21:33
Thanks for the additional information. It's been years since I've done signal processing. – Gilbert Le Blanc Nov 17 '10 at 15:44

Your Answer


