This project has moved. For the latest updates, please go here.

WaveFileWriter save as pcm

Feb 5, 2015 at 11:52 AM
Edited Feb 6, 2015 at 8:17 AM
hi..
i am recording from my sound device.
iam using the WaveFileWriter to record the incoming data to a wav file.
it should be saved as pcm format
now i am doing the following
WaveFileWriter writer;
this.writer = new WaveFileWriter(this.outputFilePath, this.waveIn.WaveFormat);
this.bufferedWaveProvider = new BufferedWaveProvider(this.waveIn.WaveFormat);
// should be enough to buffer 1 minute in front
this.bufferedWaveProvider.BufferDuration = new TimeSpan(0, 1, 0);
this.bufferedWaveProvider.DiscardOnBufferOverflow = true;
this.waveIn.ShareMode = WaveInShareMode;
this.waveIn.StartRecording();
...
private void OnDataAvailable(object sender, WaveInEventArgs e)
        {
            try
            {
                if (saveToFile)
                {
                    this.writer.Write(e.Buffer, 0, e.BytesRecorded);
                }
            }

            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex.InnerException);
            }
        }
whereas the waveIn.WaveFormat looks like this:

this.waveIn.WaveFormat = {32 bit PCM: 48kHz 2 channels wBitsPerSample:32 dwChannelMask:3 subFormat:00000003-0000-0010-8000-00aa00389b71 extraSize:22}
AverageBytesPerSecond = 384000
BitsPerSample = 32
BlockAlign = 8
Channels = 2
Encoding = Extensible
ExtraSize = 22
SampleRate = 48000

now when i try to play the recorded wav file on my playback sound device, which has the following properties

MixFormat = {32 bit PCM: 48kHz 2 channels wBitsPerSample:32 dwChannelMask:3 subFormat:00000003-0000-0010-8000-00aa00389b71 extraSize:22}

i tried using the AudioFileReader but it gives an exception no driver calling acmformatsuggest

and if i try the MediaFoundationReader i get
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in NAudio.dll

Additional information: Exception from HRESULT: 0xC00D5212
at NAudio.MediaFoundation.IMFSourceReader.SetCurrentMediaType(Int32 dwStreamIndex, IntPtr pdwReserved, IMFMediaType pMediaType)
at NAudio.Wave.MediaFoundationReader.CreateReader(MediaFoundationReaderSettings settings)

and the wave file i recorded has the following properties:
Stereo, 48kHz 32-bit float
Can be downloaded here

if i use audacity to change the float to pcm, then it works.

so my question is
can i tell the writer to write directly to a pcm format?
I mean int and not floating format? or do i have to convert the file before using it?

iam setting the writer waveformat as you can see above, but still it is not working probably.

Any help is really appreciated

thanks in advance
Coordinator
Feb 7, 2015 at 7:25 AM
WASAPI uses the WAVEFORMATEXTENSIBLE wave format structure, which is not typically used in a WAV file. Instead, I would suggest creating a regular PCM / IEEE float wave format with WaveFormat.CreateIEEEWaveFormat() for example. There is a "ToStandardWaveFormat" method on the WaveFormatExtensible class in NAudio that will perform this conversion for you.

I should probably try to fix the AudioFileReader though for the future, as it doesn't actually need to do any conversion if it gets one of these files.
Feb 9, 2015 at 8:06 AM
Edited Feb 9, 2015 at 8:07 AM
Mark, thank you sooo much... that did the trick...
i think this auto convserion is not requiered as long as you provide the format you want to read/write your file with.

p.s.: just for anyone having the same problem, the function mark meant is CreateIeeeFloatWaveFormat();