Example #1
0
 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;
     }
 }
Example #2
0
        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);
        }