Question

Can I convert a wav file to a simple c double or int array and convert back to a new wav file? I want to use some c filter in the wav file. I red that with lame it is possible. But I don't know how. Can someone link some documentation or code snippet, how I can use it?

wav file -> array -> some operation -> wav file

Thank you for the answers.

Was it helpful?

Solution

This has nothing to do with Lame. Lame is a tool for encoding MP3 files.

There are many libraries for reading and writing WAV files. You can even write your own, but that's inconvenient because WAV files have so many different sample formats: 8-bit, 16-bit, 24-bit, floating point, ADPCM, µlaw, etc.

You can use libsndfile to read WAV files.

Fields in WAV files do not have fixed offsets

The reason this is bold + large is because a lot of people think that the data in a WAV file always starts at 44 bytes. This is simply not true. A WAV file is a RIFF file, and can contain an arbitrary number of chunks of data, one or more of which contains sample data. You'd be forgiven for being unable to handle esoteric WAV files, but if you make assumptions about the locations of data in the file you can end up with garbage.

OTHER TIPS

Yes, it's possible. Wav format is quite well specified and it uses typically 2 channels of 16 bit samples. Those can be represented exactly both as integers, floats and doubles.

As told by simonc in the comment, the typical format is so simple, that it really doesn't pay to learn using a library. If the length of the wav doesn't change, the conversion / filtering can be done simply by:

fread(header,1,44,in_file); fwrite(header,1,44,out_file);
short int block[1024];  // left and right channel will be interleaved.
while (c=fread(block, 2, 1024, fp))
{  
    filter_in_place(block,c); 
    fwrite(block, 2,c, out_file);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top