Problem with WaveFileReader(inputStream)

Dec 24, 2009 at 2:18 PM
Edited Dec 24, 2009 at 2:19 PM

Hello,

I have a problem with WaveFileReader. I'm trying to pass MemoryStream with recorded audio to WaveFileReader:

waveStream = new WaveFileReader(inputStream);

And on this line I have Exception: The non-negative number is required. Parametre name: value. Can you tell me, where I was mistaken?

Coordinator
Jan 8, 2010 at 3:40 PM

WaveFileReader expects a WAV file header, not just recorded audio. Does your memory stream contain a correctly formatted WAV header?

Jan 8, 2010 at 5:03 PM
Edited Jan 8, 2010 at 5:07 PM

I so think, because at creation of object WaveFileWriter I set MemoryStream as argument.

var writer = new WaveFileWriter(inputStream, recordingFormat);

It means that WaveFileWriter writes down a data from a microphone in MemoryStream. And after recording sound I try to read data from MemoryStream:

 var waveStream = new WaveFileReader(inputStream);

As I have written earlier, here I have an exception.

If I was mistaken, and MemoryStream does not contain  WAV header, what you can suggest me to solve my problem, where and how I can get WAV header? And if I write down a sound in a file, no exceptions arise. What distinction of writing in a file and in a stream?

Coordinator
Jan 8, 2010 at 6:58 PM

do you set the inputStream position back to 0 before you call WaveFileReader?

Jan 9, 2010 at 7:11 AM
Of cource yes,
inputStream.Seek(0, SeekOrigin.Begin);
Anyway, I can not understand defference between writing in a file and writing in a stream.
Coordinator
Jan 11, 2010 at 10:10 AM

I have written applications that do exactly what you are trying to do myself, so I'm not sure what exactly is going wrong.

Are you able to discover which line of code within the NAudio source code you are getting the exception from? (does the stack trace go any deeper?)

 

Mark

Aug 18, 2010 at 3:04 PM

Same thing here, I've recorded audio and put it in a MemoryStream 

                try
                {
                    waveInStream = new WaveIn();
                    ms = new MemoryStream();
                    writer = new WaveFileWriter(ms, waveInStream.WaveFormat);

                    waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(waveInStream_DataAvailable);
                    waveInStream.StartRecording();
                    btnGravar.Text = "STOP";
                    btnGravar.BackColor = Color.Gray;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Erro!",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

Later, after the user stops the record.

 

            if (waveInStream != null)
            {
                waveInStream.StopRecording();    

                btnGravar.Text = "REC";
                btnGravar.BackColor = Color.Red;

                sfdSalvarArquivo.ShowDialog();
            }

 

after, when the user selects where to save the file (.wav)

 

                FileStream fs = null;
                try
                {
                    ms.Position = 0;
                    fs = new FileStream(sfdSalvarArquivo.FileName, FileMode.Create, FileAccess.Write);
                    ms.WriteTo(fs);
                    
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.Close();
                        fs.Dispose();
                    }
                    if (waveInStream != null)
                    {
                        waveInStream.Dispose();
                        waveInStream = null;
                    }
                    if (writer != null)
                    {
                        writer.Close();
                        writer = null;
                    }

                    ms.Close();
                    ms.Dispose();
                }

The Stream is wrote in the file but I can't reproduce it.

 

What I'm mistaking ?