【发布时间】: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文件的解压器?
【问题讨论】: