0
votes

I want to export a double[] array to a wav file. I'm having some trouble manipulating the headers (RIFF, fmt, etc) . Any ideas how I should approach this? Maybe use BitConverter to turn the double[] array into a byte[] array and then use NAudio's waveFileWriter? Any sample code much appreciated!

2

2 Answers

1
votes

Just use WaveFileWriter's WriteSample method to write each sample one by one, or WriteSamples to do it in one hit. Since you have an array of doubles, you can use a bit of LINQ to convert to a float array first.

float[] floatArray = doubleArray.Select(s => (float)s).ToArray();
writer.WriteSamples(floatArray, 0, floatArray.Length);

This will write each sample to the WaveFileWriter, converting to the correct bit depth (e.g. 16 bit or 32 bit IEEE float), which depends on the WaveFormat you selected when you opened the WaveFileWriter

1
votes

You are going to have to write all of the necessary bits of the WAV file according to standards. You will have issues with mono vs. stereo and sampling rate issues too. Strongly recommended you look form some source code that already exists that deals with this issues (I did not find a any C# version in a quick search, though there were several C/C++ version). Trying to write the code just based on a header spec will be hit or miss