使用 NAudio 将采样率设置为 16 kHz
我目前正在开展一个业余项目,该项目将使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您从未找到答案,这是可能的。 NAudio 演示应用程序中包含如何执行此操作的示例,可以从 NAudio GitHub 页面下载该应用程序。
您只需创建一个
WaveFormat
对象,传递所需的采样率和通道数作为参数,并将其分配给WasapiLoopbackCapture
的WaveFormat
属性。代码>对象。例如,以下将采样率设置为 16,000 kHz,通道数设置为 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 theWaveFormat
property of theWasapiLoopbackCapture
object.As an example, the following sets the sample rate to 16,000 kHz and the number of channels to 1 (mono):