Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

New Post: Recording into WaveStream and wave form

$
0
0

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.


Viewing all articles
Browse latest Browse all 5831

Trending Articles