Start recording wave using naudio when sound input reachs a certain level

荒凉一梦 提交于 2019-12-07 20:00:19

问题


I am trying to create an app that allows me to record a wav file everytime the input volume is greater than a given volume.

I have the code to record the sound off a button but i would like to automate it, my code below:

public partial class Form1 : Form
{
    private WaveIn waveIn;
    private WaveFileWriter writer;
    String outputFilename = @"c:\test.wav";

    public Form1()
    {
        InitializeComponent();

        int sampleRate = 22000;
        int channels = 1;
        waveIn = new WaveIn();
        waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
        waveIn.DeviceNumber = 0;
        waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(
            waveIn_DataAvailable);
        writer = new WaveFileWriter(outputFilename, waveIn.WaveFormat);        
        waveIn.StartRecording();   
    }

    void waveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        writer.WriteData(e.Buffer, 0, e.BytesRecorded);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        waveIn.StopRecording();
        waveIn.Dispose();
        writer.Close();
    }
}

回答1:


in waveIn_DataAvailable you can examine each sample by looking at the bytes in e.Buffer. (Assuming you recorded in 16 bit, each pair of bytes is one sample - use BitConverter.ToInt16). If any sample goes above the threshold you specified then you can write with writer.WriteData.

To switch off recording, you would probably want to check that a certain number of 'silent' samples had passed.



来源:https://stackoverflow.com/questions/8480556/start-recording-wave-using-naudio-when-sound-input-reachs-a-certain-level

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!