当声音输入达到一定水平时开始使用 naudio 录制波形
我正在尝试创建一个应用程序,允许我在每次输入音量大于给定音量时录制 wav 文件。
我有记录按钮声音的代码,但我想自动化它,我的代码如下:
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
waveIn_DataAvailable
中,您可以通过查看e.Buffer
中的字节来检查每个样本。 (假设您以 16 位录制,每一对字节都是一个样本 - 使用 BitConverter.ToInt16)。如果任何样本超过您指定的阈值,那么您可以使用 writer.WriteData 进行写入。要关闭记录,您可能需要检查是否已经通过了一定数量的“静默”样本。
in
waveIn_DataAvailable
you can examine each sample by looking at the bytes ine.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 withwriter.WriteData
.To switch off recording, you would probably want to check that a certain number of 'silent' samples had passed.