Cobe
Cobe

Reputation: 161

Playing a wav file after processing it with NAudio

I've converted a double array output[] to a wav file using NAudio. The file plays ok in VLC player and Windows Media Player, but when I try to open it in Winamp, or access it in Matlab using wavread() I fail.. (in Matlab I get the error: " Invalid Wave File. Reason: Incorrect chunk size information in WAV file." , which pretty obviously means something's wrong with the header). Any ideas on how to solve this? Here's the code for converting the array to a WAV:

float[] floatOutput = output.Select(s => (float)s).ToArray();
WaveFormat waveFormat = new WaveFormat(16000, 16, 1);
WaveFileWriter writer = new WaveFileWriter("C:\\track1.wav", waveFormat);
writer.WriteSamples(floatOutput, 0, floatOutput.Length);

Upvotes: 2

Views: 1083

Answers (1)

Mark Heath
Mark Heath

Reputation: 49522

You must dispose your WaveFileWriter so it can properly fix up the WAV file header. A using statement is the best way to do this:

float[] floatOutput = output.Select(s => (float)s).ToArray();
WaveFormat waveFormat = new WaveFormat(16000, 16, 1);
using (WaveFileWriter writer = new WaveFileWriter("C:\\track1.wav", waveFormat))
{
    writer.WriteSamples(floatOutput, 0, floatOutput.Length);
}

Upvotes: 2

Related Questions