如何将2个wav文件混合在一起?

时间:2017-02-07 19:12:51

标签: c# audio naudio

我试图录制输入并将其与歌曲合并(不连接)。我有一把吉他,我在听一首歌时录制,我想把吉他放在歌曲上(如音响)。 这样做有什么办法吗?如果实时混音不可能 - 我可以在录制后将它们合并吗?就像我录制吉他后,现在它是一个wav文件,我想把两个wav文件混合在一起。 那是输入设备:

 private void Capture()
    {
        input = new WasapiCapture((MMDevice)inputCombo.SelectedItem);
        bufferedWaveProvider = new BufferedWaveProvider(input.WaveFormat);
        input.DataAvailable += WaveInOnDataAvailable;
        input.StartRecording();
        write = new WaveFileWriter(System.IO.Path.GetTempFileName(), input.WaveFormat);
    }

 private void WaveInOnDataAvailable(object sender, WaveInEventArgs e)
    {
        bufferedWaveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
        write.Write(e.Buffer, 0, e.BytesRecorded);
        write.Flush();
    }

不是将其写入空白文件,而是将其写入已存在的wav文件中,而不是覆盖它。是否可以使用MixingSampleProvider?

2 个答案:

答案 0 :(得分:3)

这应该可以使用WaveMixerStream32,例如像

var mixer = new WaveMixerStream32 { AutoStop = true};

var wav1 = new WaveFileReader(@"c:\...\1.wav");
var wav2 = new WaveFileReader(@"c:\...\2.wav");

mixer.AddInputStream(new WaveChannel32(wav1));
mixer.AddInputStream(new WaveChannel32(wav2));
WaveFileWriter.CreateWaveFile("mixed.wav", new Wave32To16Stream(mixer));

答案 1 :(得分:2)

要使用ISampleProvider混合多个MixingSampleProvider来源,您可以执行以下操作:

此处SignalGenerator具有Gain属性,可以指定混音中应该有多大声。

using System;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ISampleProvider provider1 = new SignalGenerator
            {
                Frequency = 1000.0f,
                Gain = 0.5f
            };

            ISampleProvider provider2 = new SignalGenerator
            {
                Frequency = 1250.0f,
                Gain = 0.5f
            };

            var takeDuration1 = TimeSpan.FromSeconds(5); // otherwise it would emit indefinitely
            var takeDuration2 = TimeSpan.FromSeconds(10);

            var sources = new[]
            {
                provider1.Take(takeDuration1),
                provider2.Take(takeDuration2)
            };

            var mixingSampleProvider = new MixingSampleProvider(sources);

            var waveProvider = mixingSampleProvider.ToWaveProvider();

            WaveFileWriter.CreateWaveFile("test.wav", waveProvider);
        }
    }
}