public MP3Stream(Stream stream, bool scanStream, int lengthOffset) { int frameLength = 0; if (scanStream) _frameInfoCollection = new FrameInfoCollection(); else _frameInfoCollection = null; _dataStartIndex = stream.Position; do { _frame = MP3Frame.FromStream(stream); if (_frame == null && stream.IsEndOfStream()) throw new FormatException("Stream is no MP3-stream. No MP3-Frame was found."); } while (_frame == null && !stream.IsEndOfStream()); frameLength = _frame.FrameLength; _sampleRate = _frame.SampleRate; XingHeader = XingHeader.FromFrame(_frame); //The first frame can contain a Xingheader if (XingHeader != null) { //Todo: dataInitPosition = stream.Position _dataStartIndex = stream.Position; } _dataLength = stream.Length - _dataStartIndex - lengthOffset; if (scanStream) { stream.Position = _dataStartIndex; PreScanFile(stream); CanSeek = true; } else { CanSeek = false; } stream.Position = _dataStartIndex; /* * bytes * 8 (8bits perbyte) / ms = totalbits / totalms = bits per ms */ if (scanStream) { _bitRate = ((_dataLength * 8.0) / ((double)_frameInfoCollection.TotalSamples / (double)_sampleRate)); } else { _bitRate = ((_frame.BitRate) / 1); } MP3Format = new MP3Format(_sampleRate, _frame.ChannelCount, frameLength, (int)Math.Round(_bitRate)); _converter = new AcmConverter(MP3Format); WaveFormat = _converter.DestinationFormat; //_bytesPerSample = (WaveFormat.BitsPerSample / 8 * WaveFormat.Channels); _pcmDstBuffer = new byte[SamplesPerFrame * WaveFormat.BytesPerBlock * 2]; stream.Position = _dataStartIndex; _stream = stream; }
private void BufferProc(object o) { if (_stream == null || _stream.CanRead == false) throw new Exception("Mp3WebStream not initialized"); MP3Frame frame = GetNextFrame(_stream); int channels = frame.ChannelMode == MP3ChannelMode.Stereo ? 2 : 1; AcmConverter converter = new AcmConverter(new MP3Format(frame.SampleRate, frame.ChannelCount, frame.FrameLength, frame.BitRate)); _waveFormat = converter.DestinationFormat; byte[] buffer = new byte[16384 * 4]; _buffer = new FixedSizeBuffer<byte>(converter.DestinationFormat.BytesPerSecond * 10); ManualResetEvent resetEvent = o as ManualResetEvent; resetEvent.Set(); do { if (_buffer.Buffered >= _buffer.Length * 0.85 && !_disposing) { Thread.Sleep(250); continue; } try { frame = GetNextFrame(_stream); //_frameBuffer is set in GetNextFrame int count = converter.Convert(_frameBuffer, frame.FrameLength, buffer, 0); if (count > 0) { int written = _buffer.Write(buffer, 0, count); } } catch (MmException) { _disposing = true; ThreadPool.QueueUserWorkItem((c) => { while (_bufferThread.ThreadState != System.Threading.ThreadState.Stopped) ; CreateStream(false); }); } catch (WebException) { InitializeConnection(); } catch (IOException) { InitializeConnection(); } } while (!_disposing); if (converter != null) converter.Dispose(); }
protected void Dispose(bool disposing) { lock (_lockObject) { if (disposing) { if (_converter != null) { _converter.Dispose(); _converter = null; } if (_stream != null) { _stream.Dispose(); _stream = null; } if (_frameInfoCollection != null) { _frameInfoCollection.Dispose(); _frameInfoCollection = null; } _pcmDstBuffer = null; _frame = null; } } }