0
votes

This is a new area of C# coding that I am diving into so please forgive my ignorance on this. I attempting to learn more about digital signal processing using NAudio. I am starting by only attempting to create a simple console app. Using the code below, the NAudio "DataAvailable" event is only utilized for 5 seconds, at which point it is no longer called. When the data is saved to a .WAV file, the length of the file is also only 5 seconds.

I manually timed a voice test through my microphone by saying "Mississippi 1, Mississippi 2, etc." up to 10 times to make sure that I was at least trying to record for more than 5 seconds. After "Mississippi 5" the recording is stopping automatically on its own and triggers the "RecordingStopped" event. Inside of the "DataAvailable" event I am simply having it write Guids to the output window to make sure the program is still running but after 5 seconds, it stops writing to the output console.

Is there a step I missing to tell NAudio to record endlessly until an event is triggered by the user which then forces it to stop?

This is the code I am attempting to use:

static void Main(string[] args)
{
    int deviceCount = WaveIn.DeviceCount;

    if (deviceCount > 0)
    {
        waveFileWriter = new WaveFileWriter(@"F:/" + Guid.NewGuid() + ".wav", new WaveFormat(44100, 24, WaveInEvent.GetCapabilities(deviceCount - 1).Channels));

        waveInEvent = new WaveInEvent();
        waveInEvent.DeviceNumber = deviceCount - 1;
        waveInEvent.WaveFormat = new WaveFormat(44100, 24, WaveInEvent.GetCapabilities(deviceCount - 1).Channels);

        WaveInProvider waveIn = new WaveInProvider(waveInEvent);
        waveInEvent.DataAvailable += new EventHandler<WaveInEventArgs>(waveInEvent_DataAvailable);
        waveInEvent.StartRecording();
    }

    // used to keep the console window open for testing...
    Console.ReadLine();
}

private static void waveInEvent_DataAvailable(object sender, WaveInEventArgs e)
{
    if (waveFileWriter == null)
    {
        return;
    }

    Console.WriteLine(Guid.NewGuid());
}

private static void waveInEvent_RecordingStopped(Object sender, StoppedEventArgs e)
{
    Console.Write("This is getting called automatically after 5 seconds... IDK why???");
}

I would expect the above code to run indefinitely until an event is triggered that stops the recording, however that is not the case.

Thank you.

UPDATE:

Renamed "waveIn" variable to "waveInProvider" for clarity.

1
there's an exception property on StoppedEventArgs - so you might want to check that to see if there's a problem causing recording to stopMark Heath
I checked the event args. A "Buffer Full" exception is occurring. From some research it appears that you are suppose to read from it which is not what I was doing. Once I added the "waveInProvider.Read(e.Buffer, 0, e.BytesRecorded);" line of code to the "DataAvailable" event, it continuously ran without issue. Thank you for your help!osswmi

1 Answers

0
votes

Please see the comments from the above post from @Mark Heath and myself. It appears that you are supposed to read the buffer from the WaveInProvider or it fills up and throws an exception. Use "waveInProvider.Read(e.Buffer, 0, e.BytesRecorded);" inside of your "DataAvailable" callback event to prevent this from happening.