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

Recording into WaveStream and wave form

Dec 8, 2011 at 10:12 AM

Hi, I'm new to NAudio. Unfortunately I didn't find any documentation, just couple oof articles and you tube videos showing how to do things.

What I want to do is record voice and show its waveform. I want to view volume meter as well and be able to change the volume of microphone. In the end I want to write the recorded stream to database. So far I was able to show the waveform and volume meter, but I would like to do this in a different way.

To show volume meter I draw on panel(I don't know how to use NAudio.Gui.VolumeMeter). To show waveForm I use NAudio.Gui.WaveFormPainter, but I would like to use NAudio.Gui.WaveViewer. But this control requires wave stream and I don't have any idea how to record to wave stream.

So far my code looks like that:

//this is one class

private WaveIn input = null;
private WaveFileWriter waveWriter = null;

public void StartRecord(int DeviceIndex, int sampleRate, int bits, int channels, string FileName)
{
   CloseAll(); ///disposes input and waveWriter

   input = new WaveIn();
   input.DeviceNumber = DeviceIndex;
   input.WaveFormat = new WaveFormat(sampleRate, bits, channels);
   input.DataAvailable += new EventHandler<WaveInEventArgs>(input_DataAvailable);
   waveWriter = new WaveFileWriter(FileName, input.WaveFormat);

   input.StartRecording();
   
}

private void input_DataAvailable(object sender, WaveInEventArgs e)
{
    if (waveWriter == null) return;
            
    if (OnVolumePeak != null) //onVolumePeak is my event
    {
        float max = 0;

        for (int index = 0; index < e.BytesRecorded; index += 2)
        {
            short sample = (short)((e.Buffer[index+1]<<8) | (e.Buffer[index]) );
            float sample16 = sample / 16384f;

            max = Math.Max(sample16, max);
        }

        OnVolumePeak(this, max);

    }
    waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
    waveWriter.Flush();
}

//and part of code from another class:
public void OnVolumePeak(object Sender, float max)
{
    waveformPainter.AddMax(max); //paint waveForm 
    int vu = (int)Math.Round(max*100);
    if(vu > 100) vu = 100;
    PaintPB(vu);
}

//this one paints my volume meter
private void PaintPB(int value)
{
    Graphics gr = vmPanel.CreateGraphics();
    gr.Clear(this.BackColor);
            
    gr.FillRectangle(Brushes.Green, 0, 100 - value, vmPanel.Width, vmPanel.Height);
}

 

I would like to use WaveViewer instead of WaveFormPainter, but I don't know how. I would also like to add volume control, but I don't know how. I have seen .Net Voice Recorder, but I didn't find source code anywhere. Just some parts here: http://channel9.msdn.com/coding4fun/articles/NET-Voice-Recorder which is not enough for me. Can someone help me? Because I try to do this for about a week.

Coordinator
Dec 8, 2011 at 10:16 AM

Hi, the source code for Voice Recorder is available here:

voicerecorder.codeplex.com

Dec 8, 2011 at 10:38 AM

No, I can't find source there.

This is list of all files in the download:

GalaSoft.MvvmLight.dll
GalaSoft.MvvmLight.Extras.dll
GalaSoft.MvvmLight.Extras.xml
GalaSoft.MvvmLight.xml
NAudio.dll
System.Windows.Interactivity.dll
System.Windows.Interactivity.xml
VoiceRecorder.application
VoiceRecorder.Audio.dll
VoiceRecorder.Audio.pdb
VoiceRecorder.Core.dll
VoiceRecorder.Core.pdb
VoiceRecorder.exe
VoiceRecorder.exe.config
VoiceRecorder.exe.manifest
VoiceRecorder.pdb
VoiceRecorder.vshost.application
VoiceRecorder.vshost.exe
VoiceRecorder.vshost.exe.config
VoiceRecorder.vshost.exe.manifest

plus some directories with I think language resources: System.Windows.Interactivity.resources.dll

Coordinator
Dec 8, 2011 at 10:48 AM

click the tab that says Source Code

Dec 8, 2011 at 11:01 AM

Ofcourse. I was there earlier but somehow I didn't see download ;) THX.

Dec 8, 2011 at 9:28 PM

 For  get Volume

// in the   volumeslider property

privite void  volumeSlider1_VolumeChanged(object sender,EventArg e)

if (this.waveOut != null)

{

this.WaveOut.Volume =volumeSlider.Volume;

}

}

 to  recive

var waveOut = new WaveOut();

waveOut.Volume=volumeSlider1;

wave