Reputation: 11
In C#, I am trying to save off PCM data to a WAV file. Using NAudio, I create a WaveFileWriter.
this.fileWriter = new WaveFileWriter(@"c:\temp\test.wav", new WaveFormat(this.audioParameters.SampleRate, this.channels));
I am capturing the PCM packet in a float array and write the samples.
var arraySize = noOfFrames * this.channels;
var buffer = new float[arraySize];
Marshal.Copy(data, buffer, 0, arraySize);
this.fileWriter?.WriteSamples(buffer, 0, buffer.Length);
The outputted audio file is of the proper length but the audio is sounds horrible. I can tell it is the same audio but it is not right.
// Non-interlaced 32-bit float format
// ChannelLayout = LayoutStereo
// FramesPerBuffer = 1024
// SampleRate = 44100
// channels = 2
In NAudio, how do I create a WAV file from a PCM stream with the above information?
Upvotes: 0
Views: 720
Reputation: 49482
You are putting 32 bit floating point audio samples into a 16 bit WAV file. Use WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channels)
for the WaveFormat
of the WaveFileWriter
.
Upvotes: 2