Solved

# Mixing of wave files

Posted on 2006-03-30
552 Views
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.
0
Question by:HanuSoftware
1 Comment

LVL 4

Accepted Solution

Hi,

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/

chip3d

0

## Featured Post

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generatâ€¦
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more adâ€¦
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relatâ€¦
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.