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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.