开发者

Create Clapper software with Naudio

开发者 https://www.devze.com 2023-03-16 09:38 出处:网络
I\'d like to create a software that listens after claps thru microphone.. my first implementation will be to try to get the software to warn when i hears high volume sound.

I'd like to create a software that listens after claps thru microphone..

my first implementation will be to try to get the software to warn when i hears high volume sound.

but i was wondering if someone could help me in the right direction?

public partial class ClapperForm : Form
{
    WaveIn waveInStream;

    public ClapperForm()
    {
        InitializeComponent();
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        //start the streaming
        waveInStream = new WaveIn();
        waveInStream.DataAvailable += new EventHandler<WaveInEventArgs>(waveInStream_DataAvailable);
        waveInStream.StartRecording();
    }

    void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
    {
    开发者_如何转开发    //check out what volume it is
    }
    private void btnStop_Click(object sender, EventArgs e)
    {
        if (waveInStream != null)
        {
            //Stop streaming
            waveInStream.StopRecording();
            waveInStream.Dispose();

            waveInStream = null;
        }
    }
}


Assuming you are recording 16 bit audio (which is the default), then the contents of e.Buffer can be interpreted like this:

for (int n = 0; n < e.BytesRecorded; n += 2)
{
    short sampleValue = BitConverter.ToInt16(e.Buffer, n);        
}

Then you can look for high values of Math.Abs(sampleValue).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号