0

I am making a music application where I need to join the user's voice with the instrumental music. In achieving this, I need to decode the instrumental music into PCM and combine it with user's voice. The algorithm I used to combine the music is this:

int sample1 = ...;
int sample2 = ...;
int combinedSample = sample1+sample2;
combinedSample /= 2;

However, the result is very annoying. The sound is not audible, the voice cannot be listened. When I open the resulting PCM file in Audacity, it looks like this (weird):

Screenshot 1

Screenshot 2

Also, I tried this formula and the result is not expected.

int sample1 = ....;
int sample2 = ....;
int combinedSample = sample1+sample2;
if (combinedSample >= 32767) {
    combinedSample = 32767;
} else if (combinedSample  <= -32768) {
    combinedSample = -32768;
}
Dana Prakoso
  • 51
  • 1
  • 4
  • The add and divide should work. There's nothing unusual about that. I'd suggest the problem is elsewhere. Are you reconstructing the samples from the file yourself or using some library? – Radiodef Jan 31 '18 at 02:36
  • In the screen shot its shows the sample as 32bit float. Are you opening it in the right format? (or was is converted to float after loading the PCM values) – slipperyseal Jan 31 '18 at 02:41
  • agreed that the first algorithm is correct. the second will only introduce clipping. when you output the samples are you only writing the bottom 16 bits of the integer? (and the same for reading) – slipperyseal Jan 31 '18 at 02:47

0 Answers0