当声音输入达到一定水平时开始使用 naudio 录制波形

发布于 2024-12-20 15:26:32 字数 1017 浏览 1 评论 0原文

我正在尝试创建一个应用程序,允许我在每次输入音量大于给定音量时录制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迎风吟唱 2024-12-27 15:26:32

waveIn_DataAvailable 中,您可以通过查看 e.Buffer 中的字节来检查每个样本。 (假设您以 16 位录制,每一对字节都是一个样本 - 使用 BitConverter.ToInt16)。如果任何样本超过您指定的阈值,那么您可以使用 writer.WriteData 进行写入。

要关闭记录,您可能需要检查是否已经通过了一定数量的“静默”样本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文