/// <summary> /// 获取标签帧长度 /// </summary> /// <returns></returns> private void GetTAGFrameLength() { //必须为"ID3"否则认为标签不存在 byte[] mp3Header = MP3Buffer.GetRange(0, 3).ToArray(); //MP3Stream.Read(mp3Header, 0, mp3Header.Length); if (Encoding.ASCII.GetString(mp3Header) != "ID3") { throw new NotImplementedException("暂只能识别ID3的MP3音频"); } //版本号 ID3V2.3 就记录 3 byte ver = MP3Buffer[3]; //副版本号此版本记录为 byte Revision = MP3Buffer[4]; //存放标志的字节,这个版本只定义了三位 byte flag = MP3Buffer[5]; //标签大小,包括标签头的 10 个字节和所有的标签帧的大小 byte[] size = MP3Buffer.GetRange(6, 4).ToArray(); //MP3Stream.Read(size, 0, size.Length); MP3Buffer.RemoveRange(0, 10); TAGFrameLength = (size[0] & 0x7F) * 0x200000 + (size[1] & 0x7F) * 0x400 + (size[2] & 0x7F) * 0x80 + (size[3] & 0x7F); }
/// <summary> /// 获取标签帧 /// </summary> /// <returns></returns> public byte[] GetTAGFrame() { GetTAGFrameLength(); byte[] frameBuffer = MP3Buffer.GetRange(0, TAGFrameLength).ToArray(); MP3Buffer.RemoveRange(0, TAGFrameLength); //MP3Stream.Read(frameBuffer, 0, frameBuffer.Length); return(frameBuffer); }
/// <summary> /// 获取数据帧长度 /// </summary> /// <returns></returns> private int GetAuidoFrameLength(out double playTime) { playTime = 0; byte[] headerBuffer = MP3Buffer.GetRange(0, 4).ToArray(); //MP3Stream.Read(headerBuffer, 0, headerBuffer.Length); ////读取位置回到ff fb //MP3Stream.Position = MP3Stream.Position - 4; if (headerBuffer[0] != 0xFF) { Console.WriteLine("没有读取到FF头"); return(0); } MP3AudioFrameHeader header = new MP3AudioFrameHeader(headerBuffer); int length = 0; //每帧采样数 int frameCount = FrameCountDictionary[header.Version][header.Layer]; //比特率 int kbps = KBPSDictionary[header.Version][header.Layer][header.BitrateIndex]; //采样率 double khz = KHZDictionary[header.Version][header.SamplingFrequency]; switch (header.Layer) { //11 -> Layer 1, L1 case "11": break; //10 -> Layer 2, L2 //01 -> Layer 3, L3 case "10": case "01": length = (int)Math.Floor(((frameCount / 8 * kbps) / khz)) + (header.Padding == "0" ? 0 : 1); playTime = frameCount / khz; break; default: break; } return(length); }
public byte[] GetAudioFrame(out double playTime) { playTime = 0; if (MP3Buffer.Count == 0) { return(new byte[0]); } int audioFrameLength = GetAuidoFrameLength(out playTime); if (audioFrameLength == 0) { return(new byte[0]); } byte[] audioBuffer = MP3Buffer.GetRange(0, audioFrameLength).ToArray(); //MP3Stream.Read(audioBuffer, 0, audioFrameLength); MP3Buffer.RemoveRange(0, audioFrameLength); return(audioBuffer); }