Mixing + Matching WAV files

Started by
2 comments, last by eq 17 years ago
I would like to create a program in C/C++ that can take in two WAV files and combine them together. Problem is, I don't really know where to start. I was thinking of using CSound, but it looks complicated (the API manual is close to 500 pages, and made no mention of the WAV filetype). I can program in C/C++ decently, I'm just not certain on what to use. The only thing I found on Google was http://people.msoe.edu/~taylor/examples/wav.htm . Unfortunately, I don't know how to modify that code to achieve my objectives. I think I would have to create two WAV_IN objects, but after that I'm not quite so sure. Can someone help?
Advertisement
If you simple plan to load the files and store the mixed one in another then it's easy, just load the WAV-data (search google for "wav file format"), add it together and store it in a new file.

If you wan't to play the sound mixed in realtime then it's a bit more tricky. If you're not too picky then I suggest the rude approach of simply playing them att the same time and the audio drivers will mix it automaticly.
.sehkteeah erthyahr gahro
You either meant you wanted to concatenate two waves together, in which case you simple slap the data of one onto the end of another

Or more likely, you wanted to mix the two wavs, then store them back as one file. While I don't have much experience with mixing sound files, I know there are many libraries that have sound mixers.

Here is the only one I have any experience with:
http://www.libsdl.org/projects/SDL_mixer/

Hopefully this will get you on the correct path.
Sure is a big 'ol world.
Quote:You either meant you wanted to concatenate two waves together, in which case you simple slap the data of one onto the end of another

Or more likely, you wanted to mix the two wavs, then store them back as one file. While I don't have much experience with mixing sound files, I know there are many libraries that have sound mixers.


Neither of thoose are simple since a .wav file is a container format, i.e there are many ways to represent the actual sound data in the file.
If you limit your self to uncompressed PCM samples (wav format tag 1) and files of the same frequency (i.e 44100 ), then it's fairly simple to do both concatention and simple mixing.

Loading a .wav file is quite simple, it uses the RIFF structure (file consists of a bunch of "hunks"). There's only two hunk's that you're interested in the 'fmt ' hunk wich contains a WAVEFORMATEX struct.
This describes the type of audio contained in the .wav, ignore the file if format isn't 1, here you also find the frequency, channel count etc.

The second hunk is the 'data' hunk that contains the actual PCM samples (as specified in the 'fmt' hunk). Typically this is a series of 16 bit signed integers (shorts), the channels are interleaved like this:

| S0 C0 | S0 C1 | S1 C0 | S1 C1 | S2 C0 | S2 C1 | .... | Sn C0 | Sn C1 |

Mixing two sound of the same frequency and channel counts could be a simple as just adding every short together (and scale down or handle overflows!), ie:

OutputSample[x] = short((int(InputSample1[x]) + int(InputSample2[x])) >> 1);

This is the most basic mixing though and there are many more fancier algorithms.

Just my 2c

This topic is closed to new replies.

Advertisement