示例#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;
     }
 }