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

Recording from Microphone

Feb 13, 2011 at 11:38 AM

Hi there,

I'm trying to record the microphone channel on Windows 7.

My output file "test.wav" is growing during the recording, but the file couldn't be played after stopping the record.

I've tried it with all connected lines ( devices[0], devices[1] and so on...), but its's still not working.

Heres my code:

 

 	private void buttonrec_Click(object sender, RoutedEventArgs e)
        {
         
            MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator();
            MMDeviceCollection deviceCol = deviceEnum.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);

            Collection<MMDevice> devices = new Collection<MMDevice>();
            foreach (MMDevice device in deviceCol)
            {
                devices.Add(device);
            }

            //0=Mic,1=Digital,2=digital,3=Mic,4=LineIn,
           //MessageBox.Show(devices[0].ToString());
            
            waveIn = new WasapiCapture((MMDevice)devices[0]);
            
            
            //waveIn.WaveFormat = new WaveFormat(8000, 1);
            
            writer = new WaveFileWriter(@"c:\test\test.wav", waveIn.WaveFormat);

            waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
            
            waveIn.StartRecording();
            waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
        }

        void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            
                
                writer.WriteData(e.Buffer, 0, e.BytesRecorded);
                int secondsRecorded = (int)(writer.Length / writer.WaveFormat.AverageBytesPerSecond);
                if (secondsRecorded >= 60)
                {
                    StopRecording();
                }    
        }
        

        void StopRecording()
        {
           
            waveIn.StopRecording();

        }
       
    	 void waveIn_RecordingStopped(object sender, EventArgs e)
        {      
           waveIn.Dispose();
           waveIn = null;
           writer.Close();
           writer = null;
                
         }


        private void button_stop_rec_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                waveIn.StopRecording();
                waveIn.Dispose();
                waveIn = null;
                writer.Close();
                writer = null;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Thanks for your advices and NAudio ;)
Björn
Feb 19, 2011 at 12:33 PM

I am trying to do the exact same thing, with the same result. Also running windows 7.

I suspect that there is some problem with the WaveFormat used by WasapiCapture so I am trying to figure out how to get the data into a stream so that I can try to use the conversion stream on the data. Any ideas?

Coordinator
Feb 21, 2011 at 10:27 AM

have a look at the NAudio demo project for an example of using WASAPI for recording

Feb 21, 2011 at 6:24 PM
markheath wrote:

have a look at the NAudio demo project for an example of using WASAPI for recording

If you are refering to the Recording form, thats the one I did not get to work..

However I found the device selection on WaveIn so I switched to it instead, not really problem solved, but problem gone at least.

Thanks for a great lib

May 9, 2011 at 2:10 PM

Can you upload the whole project here? I am interested in trying this in Windows 7.

Coordinator
May 9, 2011 at 2:37 PM

I suspect the issue might be that WASAPI likes to use WAVEFORMATEXTENSIBLE for its headers. It will be PCM or IEEE under the hood though, so you could create your own standard WaveFormat to use. Can you upload a small portion of the WAV file created?

Mark

May 10, 2011 at 8:11 PM
webideas wrote:

Can you upload the whole project here? I am interested in trying this in Windows 7.

Assuming that it is my project you are looking for, you can download a .rar here

http://www.filedropper.com/remotesound

It is a client server setup which records sound on one machine and plays it on a remote host, feel free to use it however you like

 

May 10, 2011 at 8:46 PM

Thanks for the link! There is a missing file, Converters.cs under RemoteSoundServer, so I cannot build it.

May 10, 2011 at 9:30 PM

Also, is this the same projects: http://www.codeproject.com/KB/cs/Streaming_wave_audio.aspx

May 11, 2011 at 7:45 PM
webideas wrote:

Thanks for the link! There is a missing file, Converters.cs under RemoteSoundServer, so I cannot build it.

http://www.filedropper.com/remotesound2

Sorry about that, usual silly problem that one forgets to add files to the repository and since you are working alone on it no one notices

May 11, 2011 at 7:48 PM
webideas wrote:

Also, is this the same projects: http://www.codeproject.com/KB/cs/Streaming_wave_audio.aspx

Nice find, seems like the same idea but it looks like he spent more time on it