Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recording microphone audio using NAudio in a console application

my code runs and creates a test.wav but this file dosent contain aything. i am trying to run this code in a console application. please help

        using System;
        using System.Media;
        using NAudio;
        using NAudio.Wave;

        class sound
        {
            public static void Main()
            {
                WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(0);
                Console.WriteLine("Now recording...");
                WaveInEvent waveSource = new WaveInEvent();
                waveSource.DeviceNumber = 0;
                waveSource.WaveFormat = new WaveFormat(16000, deviceInfo.Channels);

                //waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);

                string tempFile = (@"C:\Users\user\Desktop\test1.wav");
                WaveFileWriter waveFile = new WaveFileWriter(tempFile, waveSource.WaveFormat);
                waveSource.StartRecording();

            }

            //void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
            //{
            //    wavefile.WriteData(e.Buffer, 0, e.BytesRecorded);
            //}

        }

and can someone please explain what do the lines which are commented mean. i am a beginner in programming.

when i compile the program it gives 2 errors: Error 1: An object reference is required for the non-static field, method, or property 'sound.waveSource_DataAvailable(object, NAudio.Wave.WaveInEventArgs)' C:\Users\user\Documents\Visual Studio 2008\Projects\sound\sound\Program.cs 18 49 sound

Error 2 The name 'wavefile' does not exist in the current context C:\Users\user\Documents\Visual Studio 2008\Projects\sound\sound\Program.cs 28 21 sound

like image 940
Pawan Avatar asked Jan 28 '26 17:01

Pawan


2 Answers

Try this:

using System;
using System.Media;
using NAudio;
using NAudio.Wave;

class sound
    {
        static WaveFileWriter waveFile;
        public static void Main()
        {
            //WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(0);
            Console.WriteLine("Now recording...");
            WaveInEvent waveSource = new WaveInEvent();
            //waveSource.DeviceNumber = 0;
            waveSource.WaveFormat = new WaveFormat(44100, 1);

            waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);

            string tempFile = (@"C:\Users\user\Desktop\test1.wav");
            waveFile = new WaveFileWriter(tempFile, waveSource.WaveFormat);
            waveSource.StartRecording();
            Console.WriteLine("Press enter to stop");
            Console.ReadLine();
            waveSource.StopRecording();
            waveFile.Dispose();
        }

        static void waveSource_DataAvailable(object sender, WaveInEventArgs e)
        {
            waveFile.WriteData(e.Buffer, 0, e.BytesRecorded);
        }

    }
like image 157
JoeAdeoye Avatar answered Jan 30 '26 09:01

JoeAdeoye


Apparently the StartRecording methods start some capture loops that periodically raises the DataAvailable event to allow the user collecting the recorded data. In your example code the event handle properly append the recorded data to the file tempFile. Both the function waveInStream_DataAvailable and the waveFile must be declared as static.

like image 28
Felice Pollano Avatar answered Jan 30 '26 09:01

Felice Pollano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!