Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将两个或多个mp3文件混合到一个文件中,保持高性能_C#_Audio_Mp3_Naudio - Fatal编程技术网

C# 将两个或多个mp3文件混合到一个文件中,保持高性能

C# 将两个或多个mp3文件混合到一个文件中,保持高性能,c#,audio,mp3,naudio,C#,Audio,Mp3,Naudio,我无法将两个mp3文件混合到一个文件中。事实上,我已经找到了一些使用NAudio库来实现这一点的测试代码,但它对我来说太慢了。我是“音频编程”新手,所以我需要你的帮助,建议如何使算法比下面的代码更快地解决这个问题 代码将两个文件在8秒以上的时间内混合成一个文件,这太慢了。实际上,我检查并转换WAVTOMP3()持续8秒。文件1.mp3和2.mp3大约有3分钟长 static void Main(string[] args) { var watch = Stopwa

我无法将两个mp3文件混合到一个文件中。事实上,我已经找到了一些使用NAudio库来实现这一点的测试代码,但它对我来说太慢了。我是“音频编程”新手,所以我需要你的帮助,建议如何使算法比下面的代码更快地解决这个问题

代码将两个文件在8秒以上的时间内混合成一个文件,这太慢了。实际上,我检查并转换WAVTOMP3()持续8秒。文件1.mp3和2.mp3大约有3分钟长

    static void Main(string[] args)
    {
        var watch = Stopwatch.StartNew();
        watch.Start();
        var path = @"C:\Users\gutek\Desktop\";
        // read mp3 files from disk
        Mp3FileReader mpbacground = new Mp3FileReader(path + "1.mp3");
        Mp3FileReader mpMessage = new Mp3FileReader(path + "2.mp3");

        //convert them into wave stream or decode the mp3 file
        WaveStream background = WaveFormatConversionStream.CreatePcmStream(mpbacground);
        WaveStream message = WaveFormatConversionStream.CreatePcmStream(mpMessage);

        var mixer = new WaveMixerStream32();
        mixer.AutoStop = true;

        var messageOffset = background.TotalTime;
        var messageOffsetted = new WaveOffsetStream(
        message, TimeSpan.FromSeconds(10), TimeSpan.Zero, message.TotalTime.Subtract(TimeSpan.FromSeconds(30)));

        var background32 = new WaveChannel32(background);
        background32.PadWithZeroes = false;
        // set the volume of background file
        background32.Volume = 0.4f;
        //add stream into the mixer
        mixer.AddInputStream(background32);

        var message32 = new WaveChannel32(messageOffsetted);
        message32.PadWithZeroes = false;
        // set the volume of 2nd mp3 song
        message32.Volume = 0.6f;
        mixer.AddInputStream(message32);
        var wave32 = new Wave32To16Stream(mixer);
        //encode the wave stream into mp3
        var mp3Stream = ConvertWavToMp3(wave32);
        // write mp3 on the disk
        File.WriteAllBytes(path + "output.mp3", mp3Stream.ToArray());
        watch.Stop();
        Console.WriteLine(watch.ElapsedMilliseconds);
        Console.ReadKey();
    }

    public static MemoryStream ConvertWavToMp3(Wave32To16Stream wavFile)
    {
        using (var retMs = new MemoryStream())
        using (var wtr = new LameMP3FileWriter(retMs, wavFile.WaveFormat, 128))
        {
            wavFile.CopyTo(wtr);
            return retMs;
        }
    }

也许应该像你在问题中所说的那样继续问这个问题,问题是解码MP3需要时间。您是否正在寻找一种更快的方法?这是一个计算密集型的过程,所以你可能无法做很多事情来加速它。