【问题标题】:How to decompress just one frame using NAudio?如何使用 NAudio 仅解压缩一帧?
【发布时间】:2018-07-30 12:17:25
【问题描述】:

我只是想从 mp3 文件中提取任何帧或第一帧,然后解压缩。

internal void Read3(string location) //take in file location
    {
        Mp3FileReader mp3 = new Mp3FileReader(location); //make a Mp3FileReader

        SECTIONA: //to jump back here when needed.

        byte[] _passedBuffer = Decompress(mp3.ReadNextFrame()); //passed the decompressed byte array here.
        int jump_size = mp3.WaveFormat.Channels *2; //just to get how many bytes to skip
        for (int i = 0; i < _passedBuffer.Length; i += jump_size)
        {
            short FinalSample = BitConverter.ToInt16(_passedBuffer, i);

            if (jump_size == 4) //converting the bytes to Int16,nothing special here.
            { 
               FinalSample = (short)(((BitConverter.ToInt16(_passedBuffer, i + 2)) + FinalSample) / 2); 
            }
            Console.Write(FinalSample+"|"); //and writing it down to Console.
        }
        Console.WriteLine("Frames are Written,continue to next frame?");
        if (Convert.ToChar(Console.Read()) == 'y') //asking to go on or not.
        { goto SECTIONA; }
    }


 private byte[] Decompress(Mp3Frame fm)
    {
        var buffer = new byte[16384 * 4]; //big enough buffer size
        WaveFormat wf = new Mp3WaveFormat(fm.SampleRate, fm.ChannelMode == ChannelMode.Mono ? 1 : 2, fm.FrameLength, fm.BitRate); //creating a new WaveFormat

        IMp3FrameDecompressor decompressor = new AcmMp3FrameDecompressor(wf); //passing in to AcmMp3FrameDecompressor.
        decompressor.DecompressFrame(fm, buffer, 0); //running the DecompressFrame method and then passing back the buffer.
        return buffer;

    }

现在Mp3FileReader 正在正确读取 Frame,因为我检查了 Frame 的 RawData。现在我正在尝试解压缩该帧,然后将其 PCM 数据转换为 Int16 仅用于循环,但每个 Int16 FinalSample 值都返回 0。 我知道只使用Mp3FileReader.Read(Buffer,Offset,Length) 就可以完成工作,但对于所有帧来说:

  • 如何只使用一帧?
  • 我的代码有什么问题导致我得到全零?
  • 我知道RawData没问题,所以Decompress方法肯定有问题,我该如何设置mp3文件的解压器?

【问题讨论】:

    标签: c# mp3 naudio pcm


    【解决方案1】:

    您可以使用AcmMp3FrameDecompressorDmoMp3FrameDecompressor 解压缩单个 MP3 帧。

    您需要检查Decompress 的返回值以查看返回了多少数据。第一帧可能不返回任何内容。

    您还应该创建一个单帧解压缩器并将其用于 MP3 文件中的所有帧,因为它会在调用之间保留状态。

    【讨论】:

    • 我对代码做了一些更改,我用DmoMp3FrameDecompressor 替换了AcmMp3FrameDecompressor,但出现了一个异常,说“不支持的输入格式”。
    • 欢迎来到调试编解码器的有趣世界!设置编解码器可以接受的正确 WaveFormat 总是很痛苦的。我解决这个问题的方法是使用 DmoMp3FrameDecompressor 设置一个 Mp3FileReader,然后在其中设置一个断点来检查它接收到的 WaveFormat 并确保你的完全匹配(包括任何额外的字节)。
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多