Why does audio recording give Buffer full error after 5 seconds?

The following code gives me a "Buffer full" error and I have no idea why. My code was supposed to give me a diagram while recording audio. However, the waveform works and after 5 seconds the above error is displayed on the screen my program

Please help me. I am new to this.

private void btnRecord_Click(object sender, EventArgs e)
        {
            if (btnRecord.Text == "Record")
            {
                sources = new List<NAudio.Wave.WaveInCapabilities>();

                for (int i = 0; i < NAudio.Wave.WaveIn.DeviceCount; i++)
                {
                    sources.Add(NAudio.Wave.WaveIn.GetCapabilities(i));
                }

                input = new NAudio.Wave.WaveIn();
                input.DeviceNumber = 0;
                input.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(0).Channels);

                NAudio.Wave.WaveInProvider waveIn = new NAudio.Wave.WaveInProvider(input);

                waveOut = new NAudio.Wave.DirectSoundOut();
                waveOut.Init(waveIn);

                input.StartRecording();

                //waveOut.Play();
                input.DataAvailable += input_DataAvailable;
                btnRecord.Text = "Stop Record";
            }
            else if (btnRecord.Text == "Stop Record")
            {
                waveOut.Stop();
                input.StopRecording();
                btnRecord.Text = "Record";
                input.Dispose();

            }
        }

private void input_DataAvailable(object sender, WaveInEventArgs e)
        {
            MemoryStream memStream = new MemoryStream();
            memStream.Write(e.Buffer, 0, e.BytesRecorded);            


            RawSourceWaveStream rawSource = new RawSourceWaveStream(memStream, input.WaveFormat);
            customWaveViewer2.WaveStream = rawSource;
            rawSource.Flush();
            memStream.Flush();
        }

      

The next will be stacktrace

at NAudio.Wave.BufferedWaveProvider.AddSamples(Byte[] buffer, Int32 offset, Int32 count)
   at NAudio.Wave.WaveInProvider.waveIn_DataAvailable(Object sender, WaveInEventArgs e)
   at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
   at NAudio.Wave.WaveIn.RaiseDataAvailable(WaveInBuffer buffer)
   at NAudio.Wave.WaveIn.Callback(IntPtr waveInHandle, WaveMessage message, IntPtr userData, WaveHeader waveHeader, IntPtr reserved)
   at NAudio.Wave.WaveWindow.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Pitch_Comparer.Program.Main() in c:\Users\Nadeesha\Documents\Visual Studio 2012\Projects\Pitch_Comparer\Pitch_Comparer\Program.cs:line 19
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

      

+3


source to share


1 answer


WaveInProvider

records audio and puts it in BufferedWaveProvider

. So yours WaveOut

should be played or the buffer will fill up. (so uncomment waveOut.Play to fix the problem).



If you really don't want to play the audio you are recording, just use normal WaveIn

instead WaveInProvider

.

+2


source







All Articles