• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 576
  • Last Modified:

Mixing of wave files

Hi experts,
     I am developing a Multimedia application which is used to mix wave files. Upto now the mixing algorithm is implemented only for 16-bit wave files. If user is going to mix 8-bit,24-bit or 32-bit wave files our mixing algorithm fails. Is it possible to convert 8-bit ,24-bit,32-bit etc. wave files into 16-bit wave file without loss in Quality Or If there is general algoritms which is used to mix diffrent types of wave files. Source code or algorithm will be very helpful.
1 Solution

you can convert 8bit wavefiles to 16bit without loss, but not 24bit and 32bit files. The transformation is quite easy. Just shift the values to convert .

unsigned char *wave; // your wave signal
unsigned long val = 0;
unsigned char *pval = reinterpret_cast<unsigend char*>(&val);

// now copy the 8, 24 or 32bit value of wavestream (in this case a bytestream)
// 8bit
pval[0] = wave[x];
// shift to 16 bit
val << 8;

// 24bit
pval[0] = wave[x];
pval[1] = wave[x+1];
pval[2] = wave[x+2];
// shift to 16 bit
val >> 8;

// 32bit
pval[0] = wave[x];
pval[1] = wave[x+1];
pval[2] = wave[x+2];
pval[3] = wave[x+3];
// shift to 16 bit
val >> 16;

After conversion you can write back the value to a 16bit stream
wave16[x] = pval[0];
wave16[x+1] = pval[1];

the same way you can convert to any other bitrate.

To mix the streams, first convert all streams to the same format.
Then convert the bytes to one value. Same as above...
Now you can add the values. One way is just to make the sum. But you have to clamp the values if you get an overflow (not good, you will hear artefacts when playing the file.) One solution to avoid the artefacts, is to search for the highest peak and scale the hole stream, so the higest peak will fit in the valuerange. You can also just add the streams and part through the numbers of them: val = val1/3 + val2/3 + val3/3...

here you can download the source for a windows realtime mixer: http://www.codeguru.com/cpp/g-m/multimedia/audio/article.php/c4725/

hope this will help you


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now