Mixing of wave files

Posted on 2006-03-30
Last Modified: 2008-03-17
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.
Question by:HanuSoftware
    1 Comment
    LVL 4

    Accepted 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:

    hope this will help you



    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    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.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now