【问题标题】:Why is NAudio creating a WAV file that's twice the size it should be?为什么 NAudio 会创建一个两倍于应有大小的 WAV 文件?
【发布时间】:2019-05-15 15:13:29
【问题描述】:

我正在使用这个相当标准的代码示例将多个 WAV 文件混合到一个输出文件中(为清楚起见,省略了 Dispose 调用):

public static void MixAudioFiles(IEnumerable<FileObject> input, FileObject output)
{
    var mixer = new WaveMixerStream32 { AutoStop = true };

    foreach (var file in input)
    {
        var reader = new WaveFileReader(file.FullName);

        mixer.AddInputStream(new WaveChannel32(reader));
    }

    WaveFileWriter.CreateWaveFile(output.FullName, new Wave32To16Stream(mixer));
}

但输出文件的大小总是输入文件的两倍,尽管使用了Wave32To16Stream

我在这里错过了什么?

【问题讨论】:

  • 您可能要从单声道转为立体声?
  • 完全有可能,但我不知道怎么做。默认情况下,此链中的 WaveXXX 对象之一是否具有该效果?
  • @MarkHeath:似乎问题在于混合文件有2个通道,但输入文件只有1个。我认为这是因为WaveMixerStream32使用了WaveFormat有2个通道,但我看不到任何方法可以创建仅使用 1 个通道的混音器。
  • 看看MixingSampleProvider - WaveMixerStream32WaveChannel32 是非常古老的类,最好不要再使用了

标签: naudio


【解决方案1】:

谢谢Mark,它为我指明了正确的方向。对于遇到此问题的其他人,这是工作代码:

    public static void MixAudioFiles(IEnumerable<string> input, string output)
    {
        var wf = new WaveFormat(16000, 1);

        var mixer = new NAudio.Wave.SampleProviders.MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(wf.SampleRate, wf.Channels));

        foreach (var file in input)
        {
            var reader = new WaveFileReader(file);

            mixer.AddMixerInput(reader);
        }

        //write the mix file
        WaveFileWriter.CreateWaveFile16(output, mixer);
}

请注意,我省略了 Dispose 调用。确保添加这些以防止资源泄漏。

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 2017-03-20
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    相关资源
    最近更新 更多