Question

Can we use waveViever to plot waveform while recording?

    private NAudio.Wave.WaveIn wi = null;
    int waveInDevices = NAudio.Wave.WaveIn.DeviceCount;
    WaveFileWriter waveWriter = null;
    SaveFileDialog save;
    private void button2_Click(object sender, EventArgs e)
    {
        save = new SaveFileDialog();
        save.Filter = "Wave File (*.wav)|*.wav;";
        if (save.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

        int deviceNumber = 0;

        wi = new WaveIn();
        wi.DeviceNumber = deviceNumber;
        wi.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(deviceNumber).Channels);
        wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);
        waveWriter = new NAudio.Wave.WaveFileWriter(save.FileName, wi.WaveFormat);
        wi.StartRecording();
        timer1.Start();
    }
    private void wi_DataAvailable(object sender, WaveInEventArgs e)
    {
        if (waveWriter == null) return;

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

    }
    private void timer1_Tick(object sender, EventArgs e)
    {

        waveViewer1.WaveStream = new WaveFileReader(save.Filename);
    }

I am using that code statement to record file and want to read file every tick interval of timer and plot waveform in waveViewer.However it gives an error that "File is in use by another process". Is there anyway to do this. Should i use first overload of WaveFileReader(Stream streamInput)? If yes, please can you give an example about first overload.

Was it helpful?

Solution

I wouldn't recommend trying to use waveViewer, although you could take a copy of the code and use it as a basis. If you look at the WPF demo code in the NAudio repository, it supports drawing the waveform while you are recording.

The basic principle is every time the DataAvailable event fires, calculate the max peaks of the recorded audio and add that to your waveform display.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top