private static bool IsValidHeader(byte[] headerBytes, Mp3Frame frame) { if (headerBytes[0] != 255 || (headerBytes[1] & 224) != 224) { return(false); } frame.MpegVersion = (MpegVersion)((headerBytes[1] & 24) >> 3); if (frame.MpegVersion == MpegVersion.Reserved) { return(false); } frame.MpegLayer = (MpegLayer)((headerBytes[1] & 6) >> 1); if (frame.MpegLayer == MpegLayer.Reserved) { return(false); } int num = (frame.MpegLayer == MpegLayer.Layer1) ? 0 : ((frame.MpegLayer == MpegLayer.Layer2) ? 1 : 2); frame.CrcPresent = ((headerBytes[1] & 1) == 0); frame.BitRateIndex = (headerBytes[2] & 240) >> 4; if (frame.BitRateIndex == 15) { return(false); } int num2 = (frame.MpegVersion == MpegVersion.Version1) ? 0 : 1; frame.BitRate = Mp3Frame.bitRates[num2, num, frame.BitRateIndex] * 1000; if (frame.BitRate == 0) { return(false); } int num3 = (headerBytes[2] & 12) >> 2; if (num3 == 3) { return(false); } if (frame.MpegVersion == MpegVersion.Version1) { frame.SampleRate = Mp3Frame.sampleRatesVersion1[num3]; } else if (frame.MpegVersion == MpegVersion.Version2) { frame.SampleRate = Mp3Frame.sampleRatesVersion2[num3]; } else { frame.SampleRate = Mp3Frame.sampleRatesVersion25[num3]; } bool flag = (headerBytes[2] & 2) == 2; byte arg_123_0 = headerBytes[2]; frame.ChannelMode = (ChannelMode)((headerBytes[3] & 192) >> 6); frame.ChannelExtension = (headerBytes[3] & 48) >> 4; if (frame.ChannelExtension != 0 && frame.ChannelMode != ChannelMode.JointStereo) { return(false); } frame.Copyright = ((headerBytes[3] & 8) == 8); byte arg_167_0 = headerBytes[3]; byte arg_16B_0 = headerBytes[3]; int num4 = flag ? 1 : 0; frame.SampleCount = Mp3Frame.samplesPerFrame[num2, num]; int num5 = frame.SampleCount / 8; if (frame.MpegLayer == MpegLayer.Layer1) { frame.FrameLength = (num5 * frame.BitRate / frame.SampleRate + num4) * 4; } else { frame.FrameLength = num5 * frame.BitRate / frame.SampleRate + num4; } return(frame.FrameLength <= 16384); }
public static Mp3Frame LoadFromStream(Stream input) { return(Mp3Frame.LoadFromStream(input, true)); }
private Mp3FileReader(Stream inputStream, Mp3FileReader.FrameDecompressorBuilder frameDecompressorBuilder, bool ownInputStream) { if (inputStream == null) { throw new ArgumentNullException("inputStream"); } if (frameDecompressorBuilder == null) { throw new ArgumentNullException("frameDecompressorBuilder"); } this.ownInputStream = ownInputStream; try { this.mp3Stream = inputStream; this.Id3v2Tag = Id3v2Tag.ReadTag(this.mp3Stream); this.dataStartPosition = this.mp3Stream.Position; Mp3Frame mp3Frame = Mp3Frame.LoadFromStream(this.mp3Stream); if (mp3Frame == null) { throw new InvalidDataException("Invalid MP3 file - no MP3 Frames Detected"); } double num = (double)mp3Frame.BitRate; this.xingHeader = XingHeader.LoadXingHeader(mp3Frame); if (this.xingHeader != null) { this.dataStartPosition = this.mp3Stream.Position; } Mp3Frame mp3Frame2 = Mp3Frame.LoadFromStream(this.mp3Stream); if (mp3Frame2 != null && (mp3Frame2.SampleRate != mp3Frame.SampleRate || mp3Frame2.ChannelMode != mp3Frame.ChannelMode)) { this.dataStartPosition = mp3Frame2.FileOffset; mp3Frame = mp3Frame2; } this.mp3DataLength = this.mp3Stream.Length - this.dataStartPosition; this.mp3Stream.Position = this.mp3Stream.Length - 128L; byte[] array = new byte[128]; this.mp3Stream.Read(array, 0, 128); if (array[0] == 84 && array[1] == 65 && array[2] == 71) { this.Id3v1Tag = array; this.mp3DataLength -= 128L; } this.mp3Stream.Position = this.dataStartPosition; this.Mp3WaveFormat = new Mp3WaveFormat(mp3Frame.SampleRate, (mp3Frame.ChannelMode == ChannelMode.Mono) ? 1 : 2, mp3Frame.FrameLength, (int)num); this.CreateTableOfContents(); this.tocIndex = 0; num = (double)this.mp3DataLength * 8.0 / this.TotalSeconds(); this.mp3Stream.Position = this.dataStartPosition; this.Mp3WaveFormat = new Mp3WaveFormat(mp3Frame.SampleRate, (mp3Frame.ChannelMode == ChannelMode.Mono) ? 1 : 2, mp3Frame.FrameLength, (int)num); this.decompressor = frameDecompressorBuilder(this.Mp3WaveFormat); this.waveFormat = this.decompressor.OutputFormat; this.bytesPerSample = this.decompressor.OutputFormat.BitsPerSample / 8 * this.decompressor.OutputFormat.Channels; this.bytesPerDecodedFrame = 1152 * this.bytesPerSample; this.decompressBuffer = new byte[this.bytesPerDecodedFrame * 2]; } catch (Exception) { if (ownInputStream) { inputStream.Dispose(); } throw; } }
public override int Read(byte[] sampleBuffer, int offset, int numBytes) { int i = 0; object obj = this.repositionLock; lock (obj) { if (this.decompressLeftovers != 0) { int num = Math.Min(this.decompressLeftovers, numBytes); Array.Copy(this.decompressBuffer, this.decompressBufferOffset, sampleBuffer, offset, num); this.decompressLeftovers -= num; if (this.decompressLeftovers == 0) { this.decompressBufferOffset = 0; } else { this.decompressBufferOffset += num; } i += num; offset += num; } int num2 = this.tocIndex; if (this.repositionedFlag) { this.decompressor.Reset(); this.tocIndex = Math.Max(0, this.tocIndex - 3); this.mp3Stream.Position = this.tableOfContents[this.tocIndex].FilePosition; this.repositionedFlag = false; } while (i < numBytes) { Mp3Frame mp3Frame = this.ReadNextFrame(true); if (mp3Frame == null) { break; } int num3 = this.decompressor.DecompressFrame(mp3Frame, this.decompressBuffer, 0); if (this.tocIndex > num2 && num3 != 0) { if (this.tocIndex == num2 + 1 && num3 == this.bytesPerDecodedFrame * 2) { Array.Copy(this.decompressBuffer, this.bytesPerDecodedFrame, this.decompressBuffer, 0, this.bytesPerDecodedFrame); num3 = this.bytesPerDecodedFrame; } int num4 = Math.Min(num3 - this.decompressBufferOffset, numBytes - i); Array.Copy(this.decompressBuffer, this.decompressBufferOffset, sampleBuffer, offset, num4); if (num4 + this.decompressBufferOffset < num3) { this.decompressBufferOffset = num4 + this.decompressBufferOffset; this.decompressLeftovers = num3 - this.decompressBufferOffset; } else { this.decompressBufferOffset = 0; this.decompressLeftovers = 0; } offset += num4; i += num4; } } } this.position += (long)i; return(i); }
public static XingHeader LoadXingHeader(Mp3Frame frame) { XingHeader xingHeader = new XingHeader(); xingHeader.frame = frame; int num; if (frame.MpegVersion == MpegVersion.Version1) { if (frame.ChannelMode != ChannelMode.Mono) { num = 36; } else { num = 21; } } else { if (frame.MpegVersion != MpegVersion.Version2) { return(null); } if (frame.ChannelMode != ChannelMode.Mono) { num = 21; } else { num = 13; } } if (frame.RawData[num] == 88 && frame.RawData[num + 1] == 105 && frame.RawData[num + 2] == 110 && frame.RawData[num + 3] == 103) { xingHeader.startOffset = num; num += 4; int arg_9E_0 = XingHeader.ReadBigEndian(frame.RawData, num); num += 4; if ((arg_9E_0 & 1) != 0) { xingHeader.framesOffset = num; num += 4; } if ((arg_9E_0 & 2) != 0) { xingHeader.bytesOffset = num; num += 4; } if ((arg_9E_0 & 4) != 0) { xingHeader.tocOffset = num; num += 100; } if ((arg_9E_0 & 8) != 0) { xingHeader.vbrScale = XingHeader.ReadBigEndian(frame.RawData, num); num += 4; } xingHeader.endOffset = num; return(xingHeader); } return(null); }