记录来自 NAudio WaveIn 的输入,并输出到 NAudio WaveOut

发布于 2024-11-01 21:31:22 字数 88 浏览 1 评论 0原文

我希望能够通过 NAudio.WaveIn 从麦克风设备获取输入,然后通过 NAudio.WaveOut 将该精确输入输出到输出设备。

我该怎么做?

I want to be able to get input from a microphone device via NAudio.WaveIn, and then output that exact input to an output device via NAudio.WaveOut.

How would I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

作业与我同在 2024-11-08 21:31:22

这是对我有用的代码:

using NAudio.Wave;
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        private BufferedWaveProvider bwp;
        WaveIn wi;
        WaveOut wo;

        public Form4()
        {
            InitializeComponent();
            wo = new WaveOut();
            wi = new WaveIn();
            wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);

            bwp = new BufferedWaveProvider(wi.WaveFormat);
            bwp.DiscardOnBufferOverflow = true;

            wo.Init(bwp);
            wi.StartRecording();
            wo.Play();
        }

        void wi_DataAvailable(object sender, WaveInEventArgs e)
        {
            bwp.AddSamples(e.Buffer, 0, e.BytesRecorded);
        }
    }
}

Here is the code that worked for me:

using NAudio.Wave;
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form4 : Form
    {
        private BufferedWaveProvider bwp;
        WaveIn wi;
        WaveOut wo;

        public Form4()
        {
            InitializeComponent();
            wo = new WaveOut();
            wi = new WaveIn();
            wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable);

            bwp = new BufferedWaveProvider(wi.WaveFormat);
            bwp.DiscardOnBufferOverflow = true;

            wo.Init(bwp);
            wi.StartRecording();
            wo.Play();
        }

        void wi_DataAvailable(object sender, WaveInEventArgs e)
        {
            bwp.AddSamples(e.Buffer, 0, e.BytesRecorded);
        }
    }
}
樱花细雨 2024-11-08 21:31:22

最好的方法是使用 BufferedWaveProvider< /a> 作为 WaveOut 的输入。然后在 WaveInDataAvailable 回调中,将记录的数据提供给 BufferedWaveProvider

void DataAvailable(object sender, WaveInEventArgs args)
{
    bufferedWaveProvider.AddSamples(args.Buffer, 0, args.BytesRecorded);
}

您需要注意,默认缓冲区大小将导致明显的延迟,因此如果您希望低延迟,您可能需要对缓冲区大小进行一些实验,看看可以将其降低到多低。

The best way would be to use a BufferedWaveProvider as the input to WaveOut. Then in the DataAvailable callback of WaveIn, supply the data recorded to the BufferedWaveProvider

void DataAvailable(object sender, WaveInEventArgs args)
{
    bufferedWaveProvider.AddSamples(args.Buffer, 0, args.BytesRecorded);
}

You need to be aware that the default buffer sizes will result in a noticeable delay, so if you were hoping for low latency you might need to experiment a bit with buffer sizes to see how low you can get it.

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