3

I have a simple question regarding mixing multiple PCM samples.

I read that best way to mix multiple audio PCM samples is to take the average of the samples each frame.

So if I am adding together say 5 16 bit samples before dividing by 5, there is obviously a good chance it will have a value greater than a 16bit short can hold.

So when mixing together multiple 16 bit samples, do I store them all in int first and add them and average them, then convert back to short?

user3083522
  • 201
  • 2
  • 13
  • Where did you read it's best to average samples when mixing? Mixing is normally done by adding samples together. Clipping can be a problem but the best solution there depends on the application itself. – Shannon Matthews Feb 02 '14 at 12:50
  • https://stackoverflow.com/questions/6879534/mixing-multiple-signals-using-audio-units-on-the-ios?rq=1 – Shannon Matthews Feb 02 '14 at 12:53

2 Answers2

6

If you want to mix audio samples you just add them together. Building an average is not the correct way to do this.

Think about it: If someone plays a violin and a second violin joins the music, will the first violin become less loud? No. It would not. The second violin just adds to the signal.

When adding PCM samples you have to deal with integer overflows. One way to do it is to have a global 'master volume' that gets applied to the mixed PCM sample. Using such a global multiplier can help you to make sure your final signal is mostly within the 16 bits of your output data.

You'll probably also want a per channel volume control.

In the end overflows will still occur here and there and the best way to deal with them is to clamp the output value to the maximum and minimum representable value of your 16 bit output stream. The ear will tolerate that and it will go unnoticed as long as it doesn't occur to often.

Nils Pipenbrinck
  • 77,289
  • 24
  • 142
  • 216
1

If you talk about mixing, I would suggest you to use floats. Anyway, if you want to use shorts, you can use 32 or 64 bit integers or you simple divide the samples first and add them afterwards. That is possible since this

enter image description here

equals this

enter image description here

Florian
  • 5,728
  • 3
  • 41
  • 75
  • Does it? Not if you are using integers (the first equation rounds each term, while the second result only rounds once). – Dithermaster Feb 02 '14 at 17:03
  • Well believe me. You won't hear any difference. But if you want a better result, use floats as it is common in dsp. @Dithermaster – Florian Feb 02 '14 at 17:37
  • With floats should I use first or second method? I guess it wouldn't matter since there would be no rounding error. – user3083522 Feb 02 '14 at 18:06
  • Also if I am converting wave RIFF PCM data, I will need to convert short little endian to short big endian, then to float correct? – user3083522 Feb 02 '14 at 19:50
  • If i say float, I mean IeeeFloat. This is specified by floats in the range from -1.0 to 1.0. I don't even know which language you are using... so I can't really tell you how exactly to do that. But I can give an example. http://cscore.codeplex.com/SourceControl/latest#CSCore/Streams/SampleConverter/Pcm16BitToSample.cs – Florian Feb 02 '14 at 20:53
  • I am using java. I am also wondering if I sum samples together and at one point one of the samples has no sound at all, won't that reduce the overall volume of the mix since you are adding a 0 and dividing by a number 1 larger? – user3083522 Feb 03 '14 at 01:35