I am looking at creating a WAV file
in C and have seen an example here.
This looks good, but I'm interested in adding two buffers to make the audio stereo (the possibility to have different sound in each ear). If I set the number of channels to two, the audio plays out of the left channel only (which apparently is right, as the left channel is the first channel). I have read I must interleave it with the right channel.
Unfortunately I haven't found much online to help create a stereo WAV.
write_little_endian((unsigned int)(data[i]),bytes_per_sample, wav_file);
I've tried to create a second buffer, with half the amplitude to see if I could interleave.
for (j=0; i<BUF_SIZE; i++) {
phase +=freq_radians_per_sample;
buffertwo[i] = (int)((amplitude/2) * sin(phase));;
}
write_wav("test.wav", BUF_SIZE, buffer, buffertwo, S_RATE);
(changing the function to take two short integer buffers)
And just doing
write_little_endian((unsigned int)(data[i]),bytes_per_sample, wav_file);
write_little_endian((unsigned int)(datatwo[i]),bytes_per_sample, wav_file);
But that does not work. That should in theory be interleaved.
The WAV audio format was developed by Microsoft and has become one of the primary formats of uncompressed audio. It stores audio at about 10 MB per minute at a 44.1 kHz sample rate using stereo 16-bit samples.
Stereo Audio Files. Playback systems that make use of two speakers are referred to as stereo systems. Stereo audio files, such as stereo MP3 and WAV files, contain left channel and right channel information that tell the left and right speaker when to push and pull air.
Use File -> Export -> Export as WAV to convert the file to a WAV file. Audacity opens a Save File dialog. Save the WAV file where you'll remember it on your hard drive. Repeat the same process for all your MP3 files and you'll have a collection of WAV files suitable for use on microcontroller projects.
I think the problem is with the function "write_little_endian". You shouldn't need to use it on your laptop.
Endianness is architecture specific. The original example was likely for an Arduino microcontroller board. Arduino boards use Atmel microcontrollers which are big-endian. Thats why the code you cited explicitly needs to convert the 16-bit integers to little-endian format.
Your laptop on the other hand, uses x86 processors which are already little endian so no conversion is necessary. If you want robust portable code to convert endianness, you can use the function htole16
in Linux. Lookup the man pages to learn more about this function.
For a quick but non-portable fix, I would say just write out the entire 16bit value.
Also, I don't think you need to halve the amplitudes to go from mono to stereo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With