convert .raw to .wav

maxx_sc

New member
Hello everyone,

for a project I want to record sound with the teensy and save it on a SD card. with the audio library of paul, i'm already capable of recording the sounds in the .raw format. For further processing however I need the files in the Wav format. Is there a way to directly convert .raw files to .wav files on the teensy 4.1?

Thanks for your help
max
 
You definitely can. You just have to add a valid WAV header to the .raw file. The wav header format is described here: https://isip.piconepress.com/projec.../fundamentals/v1.0/section_02/s02_01_p05.html
Its a group of (usually? always?) 44 bytes tacked on to the beginning of the raw wav data.

You'd use the SD library to open and read the bytes of the .raw file into an array. Then you need to create
a new file called whatever.wav, write your header data bytes in, and then just write in the bytes that you pulled out of the .raw file.
 
One minor change to that suggestion - Don't read the raw into an array, for large files that would be a massive amount of memory.
Instead open both the raw and the wav files at the same time. Load a block of data from the raw (say 512 bytes, that's the size of a filesystem block), write it to the wav. Repeat until the end of the file.

As a general rule whenever possible always avoid loading the whole file at once if you can avoid it. Especially if you are only going to use each part of the file once and in order.

Or even better modify the library to write .wav files to start with. This could be as simple as changing it to append to a file if it already exists rather than replacing it (assuming it doesn't already have this option). Then your code can write the header before the library starts to write the audio data.
 
You could write 44 zero bytes to the file before you start recording. Then when you're done recording (and you know how much data you wrote), seek back to the beginning of the file and write the 44 byte WAV header.

Or if you later want to convert RAW to WAV, if you're willing to lose those first 44 bytes (or you know they are zeros), just get the file's total length and use it to write a 44 byte WAV header at the beginning. You'll just overwrite the zeros, or if the first 44 bytes are actual audio samples, you'll end up deleting only half a millisecond of sound. Of course if you *really* care about that first half millisecond, you could write a program to copy the entire data to a new file after the 44 byte header.
 
Back
Top