我拥有带有相同音乐的Mp3音频文件,这些文件以不同的采样率和位深度进行编码。
例如:
我想使用NAudio将所有这些Mp3文件转换为Wav格式(PCM IEEE浮动格式)。
在转换为Wav格式之前,我想确保所有Mp3文件必须转换为标准采样率和位深度: 192 Kbps,48 KHz 。
在最终将Mp3转换为Wav格式之前,是否需要将Mp3重新采样到所需的Mp3速率和位深度?或者可以在转换为Wav格式时完成吗?
感谢您是否可以提供示例代码。
感谢。
答案 0 :(得分:2)
是的,您需要将所有.mp3
个文件重新采样到所需的费率,然后再将其转换为.wav
格式。这是因为转换方法NAudio.Wave.WaveFileWriter.CreateWaveFile(string filename, IWaveProvider sourceProvider)
没有与速率或频率对应的参数。方法签名(取自GitHub中的代码)如下所示:
/// <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)
{
因此,您可以看到在转换为.wav时无法传递速率或频率。你可以这样做:
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);
}
}