简体   繁体   English

如何使用NAudio解压缩一帧?

[英]How to decompress just one frame using NAudio?

I am simply trying to take any or first frame from an mp3 file and then decompress it. 我只是想从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;

    }

Now the Mp3FileReader is reading the Frame correctly as I checked the Frame's RawData. 现在,当我检查框架的RawData时, Mp3FileReader正在正确读取框架。 Now I am trying to decompress that Frame and then convert its PCM data into Int16 in that only For Loop but every Int16 FinalSample value is returning 0. I know that just using Mp3FileReader.Read(Buffer,Offset,Length) will get the job done but for all the frames so: 现在,我尝试解压缩该Frame,然后将其PCM数据转换为Int16 ,因为只有For Loop,但每个Int16 FinalSample值都返回0。我知道仅使用Mp3FileReader.Read(Buffer,Offset,Length)完成工作但对于所有框架,则:

  • how do I do it for just one frame? 我如何只用一帧呢?
  • what is wrong with my code because of which I am getting all zeros? 我的代码出了什么问题,因为我得到了全零?
  • I know that RawData is ok, so something must be wrong with Decompress method, How do I setup a decompressor for mp3 file? 我知道RawData可以,所以Decompress方法一定有问题,如何为mp3文件设置解压缩器?

You can use the AcmMp3FrameDecompressor or DmoMp3FrameDecompressor to decompress individual MP3 frames. 您可以使用AcmMp3FrameDecompressorDmoMp3FrameDecompressor解压缩单个MP3帧。

You need to check the return value of Decompress to see how much data was returned. 您需要检查Decompress的返回值以查看返回了多少数据。 It's possible for the first frame not to return anything. 第一帧可能不返回任何内容。

You should also create a single frame decompressor and use it for all frames in the MP3 file, as it retains state between calls. 您还应该创建一个单帧解压缩器,并将其用于MP3文件中的所有帧,因为它保留了两次调用之间的状态。

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

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