我想创建一个在拍手后通过麦克风听的软件。
我的第一个实施方法是尝试让软件在听到大音量声音时发出警告。
但我想知道是否有人可以在正确的方向帮助我?
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;
}
}
}
最佳答案
假设您正在录制16位音频(这是默认设置),那么e.Buffer的内容可以这样解释:
for (int n = 0; n < e.BytesRecorded; n += 2)
{
short sampleValue = BitConverter.ToInt16(e.Buffer, n);
}
然后,您可以寻找Math.Abs(sampleValue)的高值。