Reputation: 145
I have Mp3 audio files with the same music, which are encoded at different sample rate and bit depth.
For example:
I want to convert all these Mp3 files to Wav format (PCM IEEE float format) using NAudio.
Before converting to Wav format, I want to ensure that all Mp3 files must be converted to a standard sample rate and bit depth: 192 Kbps, 48 KHz.
Do I need to do re-sampling of Mp3 to the required Mp3 rate and bit depth before finally converting it to Wav format? Or can it be done when converting to Wav format?
Appreciate if you can provide an example code.
Thanks.
Upvotes: 0
Views: 3905
Reputation: 8246
Yes you need to have all your .mp3
files re-sampled to your required rate before converting them to .wav
format. This is because the converting method NAudio.Wave.WaveFileWriter.CreateWaveFile(string filename, IWaveProvider sourceProvider)
has no parameters that corresponds to rate or frequency. The method signature (as taken from their code in GitHub) looks like:
/// <summary>
/// Creates a Wave file by reading all the data from a WaveProvider
/// BEWARE: the WaveProvider MUST return 0 from its Read method when it is finished,
/// or the Wave File will grow indefinitely.
/// </summary>
/// <param name="filename">The filename to use</param>
/// <param name="sourceProvider">The source WaveProvider</param>
public static void CreateWaveFile(string filename, IWaveProvider sourceProvider)
{
So as you can see you cannot pass the rate or frequency while converting to .wav. You can do it like this:
int outRate = 48000;
var inFile = @"test.mp3";
var outFile = @"test resampled MF.wav";
using (var reader = new Mp3FileReader(inFile))
{
var outFormat = new WaveFormat(outRate, reader.WaveFormat.Channels);
using (var resampler = new MediaFoundationResampler(reader, outFormat))
{
// resampler.ResamplerQuality = 48;
WaveFileWriter.CreateWaveFile(outFile, resampler);
}
}
Upvotes: 2