简体   繁体   English

混合从两个源文件读取的音频

[英]Mix audio read from two source files

I want to mix audio read from two files (1 mp3 and 1 wav(recorded voice) file) into one file (mp3 or wav). 我想将两个文件(1个mp3和1个wav(录制的语音)文件)的音频读取混合到一个文件(mp3或wav)中。

I have read many relevant answers but non helped me to get what I wanted. 我已经阅读了许多相关答案,但没有帮助我得到我想要的东西。

Like this code below generate a steam as output. 像下面的代码一样生成一个蒸汽作为输出。 I do not know how to call this function properly and how to get the Output stream as an mp3/wav file at the end. 我不知道如何正确调用此函数以及如何在最后将输出流作为mp3 / wav文件。

    public static void Combine(string[] inputFiles, Stream output)
{
    foreach (string file in inputFiles)
    {
        Mp3FileReader reader = new Mp3FileReader(file);
        if ((output.Position == 0) && (reader.Id3v2Tag != null))
        {
            output.Write(reader.Id3v2Tag.RawData, 0, reader.Id3v2Tag.RawData.Length);
        }
        Mp3Frame frame;
        while ((frame = reader.ReadNextFrame()) != null)
        {
            output.Write(frame.RawData, 0, frame.RawData.Length);
        }
    }
}

It looks like you took the code from http://mark-dot-net.blogspot.com/2010/11/merging-mp3-files-with-naudio-in-c-and.html . 看起来您从http://mark-dot-net.blogspot.com/2010/11/merging-mp3-files-with-naudio-in-c-and.html获取了代码。 Check out http://markheath.net/post/mixing-and-looping-with-naudio as well. 查看http://markheath.net/post/mixing-and-looping-with-naudio This talks about looping as well as mixing. 这涉及循环和混合。 Since you're not looping, you don't need that part. 由于您没有循环,因此您不需要该部分。

Note that the example there sends the audio to the speaker. 请注意,那里的示例将音频发送给扬声器。 However, you can always replace that output with a (eg) WaveFileWriter . 但是,您始终可以使用(例如) WaveFileWriter替换该输出。

Note also that the MixingWaveProvider32 requires that all inputs be in the same WaveFormat . 另请注意, MixingWaveProvider32要求所有输入都在同一个WaveFormat If that's not the case, then you'll need to use a resampler. 如果情况并非如此,那么您将需要使用重新采样器。 Luckily, there's http://mark-dot-net.blogspot.com/2014/05/how-to-resample-audio-with-naudio.html to help you out there as well. 幸运的是,还有http://mark-dot-net.blogspot.com/2014/05/how-to-resample-audio-with-naudio.html来帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM