/// <summary> /// Reads the MP3 stream as PCM-encoded bytes. Decodes a portion of the stream if necessary. /// Returns the number of bytes read. /// </summary> public override int Read(byte[] buffer, int offset, int count) { // Copy from queue buffers, reading new ones as necessary, // until we can't read more or we have read "count" bytes int bytesRead = 0; while (true) { if (QueueOBuffer.bytesLeft <= 0) { if (!ReadFrame()) // out of frames or end of stream? { break; } } // Copy as much as we can from the current buffer: bytesRead += QueueOBuffer.Read(buffer, offset + bytesRead, count - bytesRead); if (bytesRead >= count) { break; } } return(bytesRead); }
/// <summary> /// Reads the MP3 stream as PCM-encoded bytes. Decodes a portion of the stream if necessary. /// </summary> public override int Read(byte[] buffer, int offset, int count) { bool aFrameWasRead = true; while (QueueOBuffer.QueuedByteCount < count && aFrameWasRead) { aFrameWasRead = ReadFrame(); } int bytesToReturn = Math.Min(QueueOBuffer.QueuedByteCount, count); int bytesRead = 0; switch (Format) { case SoundFormat.Pcm16BitMono: bytesRead = QueueOBuffer.DequeueAs16BitPcmMono(buffer, offset, bytesToReturn); break; case SoundFormat.Pcm16BitStereo: bytesRead = QueueOBuffer.DequeueAs16BitPcmStereo(buffer, offset, bytesToReturn); break; default: throw new ApplicationException("Unknown sound format in Mp3Stream Read call: " + Format); } return(bytesRead); }
/// <summary> /// Creates a new stream instance using the provided stream as a source. /// </summary> public Mp3Stream(Stream sourceStream, int chunkSize) { SourceStream = sourceStream; JZBitStream = new javazoom.jl.decoder.Bitstream(new javazoom.jl.decoder.BackStream(SourceStream, chunkSize)); QueueOBuffer = new QueueOBuffer(); JZDecoder.OutputBuffer = QueueOBuffer; }
/// <summary> /// Reads the MP3 stream as PCM-encoded bytes. Decodes a portion of the stream if necessary. /// Returns the number of bytes read. /// </summary> public override int Read(byte[] buffer, int offset, int count) { ////System.Diagnostics.Trace.WriteLine("Mp3Stream - Read"); // Copy from queue buffers, reading new ones as necessary, // until we can't read more or we have read "count" bytes int bytesRead = 0; //int attempts = 0; while (true)//&& attempts < 1000 { ////System.Diagnostics.Trace.WriteLine("Mp3Stream - While Start attempts=" + attempts.ToString()); if (QueueOBuffer.bytesLeft <= 0) { ////System.Diagnostics.Trace.WriteLine("Mp3Stream - ReadFrame start"); if (!ReadFrame()) // out of frames or end of stream? { break; } ////System.Diagnostics.Trace.WriteLine("Mp3Stream - ReadFrame end"); } ////System.Diagnostics.Trace.WriteLine("Mp3Stream - QueueOBuffer.Read"); // Copy as much as we can from the current buffer: bytesRead += QueueOBuffer.Read(buffer, offset + bytesRead, count - bytesRead); if (bytesRead >= count) { break; } ////System.Diagnostics.Trace.WriteLine("Mp3Stream - QueueOBuffer.Read - end"); //attempts++; } //System.Diagnostics.Trace.WriteLine("Mp3Stream - Finished reading " + bytesRead.ToString() + " bytes"); return(bytesRead); }