使用 NAudio 将采样率设置为 16 kHz

发布于 2025-01-10 14:00:08 字数 678 浏览 0 评论 0原文

我目前正在开展一个业余项目,该项目将使用 NAudio 录制配乐。

下面的代码完成了这项工作,并且运行良好。但是,默认采样率不是我需要的。我需要采样率为 16 kHz。那么,如何根据下面的代码设置采样率呢?

var outputFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "NAudio");
Directory.CreateDirectory(outputFolder);
var outputFilePath = Path.Combine(outputFolder, "recorded.wav");
var capture = new WasapiLoopbackCapture();
var writer = new WaveFileWriter(outputFilePath, capture.WaveFormat);

capture.DataAvailable += (s, a) =>
{
    writer.Write(a.Buffer, 0, a.BytesRecorded);
    if (writer.Position > capture.WaveFormat.AverageBytesPerSecond * 20)
    {
        capture.StopRecording();
    }
};

I am currently working on a side project that will record a soundtrack using NAudio.

The code below does the job, and works well. However, the default sample rate is not the one I needed. I need the sample rate to be 16 kHz. So, how to set the sample rate given the codes below?

var outputFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "NAudio");
Directory.CreateDirectory(outputFolder);
var outputFilePath = Path.Combine(outputFolder, "recorded.wav");
var capture = new WasapiLoopbackCapture();
var writer = new WaveFileWriter(outputFilePath, capture.WaveFormat);

capture.DataAvailable += (s, a) =>
{
    writer.Write(a.Buffer, 0, a.BytesRecorded);
    if (writer.Position > capture.WaveFormat.AverageBytesPerSecond * 20)
    {
        capture.StopRecording();
    }
};

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

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

发布评论

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

评论(1

夜空下最亮的亮点 2025-01-17 14:00:08

如果您从未找到答案,这是可能的。 NAudio 演示应用程序中包含如何执行此操作的示例,可以从 NAudio GitHub 页面下载该应用程序。

您只需创建一个 WaveFormat 对象,传递所需的采样率和通道数作为参数,并将其分配给 WasapiLoopbackCaptureWaveFormat 属性。代码>对象。

例如,以下将采样率设置为 16,000 kHz,通道数设置为 1(单声道):

capture.WaveFormat = new WaveFormat(16000, 1);

In case you never found the answer, this is possible. An example of how to do it is included in the NAudio demo app which can be downloaded from the NAudio GitHub page.

You simply need to create a WaveFormat object, passing the desired sample rate and number of channels as parameters, and assign this to the WaveFormat property of the WasapiLoopbackCapture object.

As an example, the following sets the sample rate to 16,000 kHz and the number of channels to 1 (mono):

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