'Set Sample rate to 16 kHz using NAudio
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();
}
};
Solution 1:[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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jeremy Caney |