private void StreamMP3() { HttpWebRequest request = null; try { var resp = ConnectionFactory.GetResponse(_source,false, out request); var buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame IMp3FrameDecompressor decompressor = null; using (var responseStream = resp.GetResponseStream()) { var readFullyStream = new ReadFullyStream(responseStream); while (!_stopEvent.WaitOne(10, false) && !MainForm.ShuttingDown) { if (_bufferedWaveProvider != null && _bufferedWaveProvider.BufferLength - _bufferedWaveProvider.BufferedBytes < _bufferedWaveProvider.WaveFormat.AverageBytesPerSecond/4) { //Debug.WriteLine("Buffer getting full, taking a break"); Thread.Sleep(100); } else { var da = DataAvailable; if (da != null) { Mp3Frame frame; try { frame = Mp3Frame.LoadFromStream(readFullyStream); } catch (EndOfStreamException) { // reached the end of the MP3 file / stream break; } catch (WebException) { // probably we have aborted download from the GUI thread break; } if (decompressor == null || _bufferedWaveProvider == null) { // don't think these details matter too much - just help ACM select the right codec // however, the buffered provider doesn't know what sample rate it is working at // until we have a frame WaveFormat waveFormat = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate); RecordingFormat = new WaveFormat(frame.SampleRate, 16, frame.ChannelMode == ChannelMode.Mono ? 1 : 2); decompressor = new AcmMp3FrameDecompressor(waveFormat); _bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat) {BufferDuration = TimeSpan.FromSeconds(5)}; _sampleChannel = new SampleChannel(_bufferedWaveProvider); _sampleChannel.PreVolumeMeter += SampleChannelPreVolumeMeter; } int decompressed = decompressor.DecompressFrame(frame, buffer, 0); _bufferedWaveProvider.AddSamples(buffer, 0, decompressed); var sampleBuffer = new float[buffer.Length]; _sampleChannel.Read(sampleBuffer, 0, buffer.Length); da.Invoke(this, new DataAvailableEventArgs((byte[]) buffer.Clone())); if (WaveOutProvider != null && Listening) { WaveOutProvider.AddSamples(buffer, 0, buffer.Length); } } } if (_stopEvent.WaitOne(0, false)) break; } AudioFinished?.Invoke(this, new PlayingFinishedEventArgs(ReasonToFinishPlaying.StoppedByUser)); // was doing this in a finally block, but for some reason // we are hanging on response stream .Dispose so never get there if (decompressor != null) { decompressor.Dispose(); decompressor = null; } } } catch (Exception ex) { var af = AudioFinished; af?.Invoke(this, new PlayingFinishedEventArgs(ReasonToFinishPlaying.DeviceLost)); MainForm.LogExceptionToFile(ex,"MP3Stream"); } finally { try { request?.Abort(); } catch { } request = null; } }
private void _buffer(Stream input) { Console.WriteLine("[Initial buffering] Meta interval: {0}", this.StationInformation.ContainsKey("icy-metaint") ? long.Parse(this.StationInformation["icy-metaint"]) : -1); // Auto-fix the stream and auto-parse metadata stream = new FixedBufferedStream( input, this.StationInformation.ContainsKey("icy-metaint") ? long.Parse(this.StationInformation["icy-metaint"]) : -1 ); // Read first frame this.Status = StreamStatus.Buffering; switch (Codec) { case StreamCodec.MP3: // Make a decoder which can decode that (and sequentially following) frames // The decoder will decode to 16-bit samples frame = Mp3Frame.LoadFromStream(stream); _decoderMp3 = new AcmMp3FrameDecompressor(new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate)); Console.WriteLine("[Initial buffering] MP3 decoder will encode to {0}-encoded wave format.", _decoderMp3.OutputFormat.Encoding); break; default: throw new NotSupportedException(); } }
private void StreamMP3() { var webRequest = (HttpWebRequest)WebRequest.Create(_source); HttpWebResponse resp = null; try { resp = (HttpWebResponse)webRequest.GetResponse(); } catch (WebException e) { if (e.Status != WebExceptionStatus.RequestCanceled) { } return; } byte[] buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame IMp3FrameDecompressor decompressor = null; try { using (var responseStream = resp.GetResponseStream()) { var readFullyStream = new ReadFullyStream(responseStream); while (!_stopEvent.WaitOne(0, false)) { if (_bufferedWaveProvider != null && _bufferedWaveProvider.BufferLength - _bufferedWaveProvider.BufferedBytes < _bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4) { //Debug.WriteLine("Buffer getting full, taking a break"); Thread.Sleep(100); } else { Mp3Frame frame = null; try { frame = Mp3Frame.LoadFromStream(readFullyStream); } catch (EndOfStreamException) { // reached the end of the MP3 file / stream break; } catch (WebException) { // probably we have aborted download from the GUI thread break; } if (decompressor == null) { // don't think these details matter too much - just help ACM select the right codec // however, the buffered provider doesn't know what sample rate it is working at // until we have a frame WaveFormat waveFormat = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate); decompressor = new AcmMp3FrameDecompressor(waveFormat); this._bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat) {BufferDuration = TimeSpan.FromSeconds(5)}; _waveOut = new DirectSoundOut(1000); _waveOut.Init(_bufferedWaveProvider); _waveOut.Play(); _waveOut.PlaybackStopped += wavePlayer_PlaybackStopped; } int decompressed = decompressor.DecompressFrame(frame, buffer, 0); //Debug.WriteLine(String.Format("Decompressed a frame {0}", decompressed)); _bufferedWaveProvider.AddSamples(buffer, 0, decompressed); } if (_stopEvent.WaitOne(0, false)) break; } Debug.WriteLine("Exiting"); // was doing this in a finally block, but for some reason // we are hanging on response stream .Dispose so never get there if (decompressor != null) { decompressor.Dispose(); decompressor = null; } } } finally { if (decompressor != null) { decompressor.Dispose(); } if (_waveOut!=null) { _waveOut.Stop(); } } }
private void DecompressFrames() { IMp3FrameDecompressor decompressor = null; IWavePlayer waveOut = null; try { BufferedWaveProvider bufferedWaveProvider = null; var buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame var firstLoop = true; do { //WaveBuffer getting full, taking a break if (bufferedWaveProvider != null && bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes < bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4) { Thread.Sleep(500); } //StreamBuffer empty, taking a break else if (stream.Length < 16384 * 2) { Thread.Sleep(500); } else { Mp3Frame frame; try { frame = Mp3Frame.LoadFromStream(stream); } catch (EndOfStreamException) { break; } if (frame == null) continue; if (decompressor == null) { WaveFormat waveFormat = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate); decompressor = new AcmMp3FrameDecompressor(waveFormat); bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat) { BufferDuration = TimeSpan.FromSeconds(5) }; // allow us to get well ahead of ourselves } var decompressed = decompressor.DecompressFrame(frame, buffer, 0); if (decompressed > 0) bufferedWaveProvider.AddSamples(buffer, 0, decompressed); if (firstLoop) { firstLoop = false; waveOut = CreateWaveOut(); if (onWaveProviderCreated != null) { var provider = onWaveProviderCreated(bufferedWaveProvider); waveOut.Init(provider); } waveOut.Play(); } } } while (IsPlaying); } finally { if (waveOut != null) { waveOut.Stop(); waveOut.Dispose(); } if (decompressor != null) { decompressor.Dispose(); } } }
private void StreamMP3(object state) { this.fullyDownloaded = false; string url = (string)state; webRequest = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse resp = null; try { resp = (HttpWebResponse)webRequest.GetResponse(); } catch(WebException e) { if (e.Status != WebExceptionStatus.RequestCanceled) { ShowError(e.Message); } return; } byte[] buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame IMp3FrameDecompressor decompressor = null; try { using (var responseStream = resp.GetResponseStream()) { var readFullyStream = new ReadFullyStream(responseStream); do { if (bufferedWaveProvider != null && bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes < bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4) { Debug.WriteLine("Buffer getting full, taking a break"); Thread.Sleep(500); } else { Mp3Frame frame = null; try { frame = Mp3Frame.LoadFromStream(readFullyStream); } catch (EndOfStreamException) { this.fullyDownloaded = true; // reached the end of the MP3 file / stream break; } catch (WebException) { // probably we have aborted download from the GUI thread break; } if (decompressor == null) { // don't think these details matter too much - just help ACM select the right codec // however, the buffered provider doesn't know what sample rate it is working at // until we have a frame WaveFormat waveFormat = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate); decompressor = new AcmMp3FrameDecompressor(waveFormat); this.bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat); this.bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20); // allow us to get well ahead of ourselves //this.bufferedWaveProvider.BufferedDuration = 250; } int decompressed = decompressor.DecompressFrame(frame, buffer, 0); //Debug.WriteLine(String.Format("Decompressed a frame {0}", decompressed)); bufferedWaveProvider.AddSamples(buffer, 0, decompressed); } } while (playbackState != StreamingPlaybackState.Stopped); Debug.WriteLine("Exiting"); // was doing this in a finally block, but for some reason // we are hanging on response stream .Dispose so never get there decompressor.Dispose(); } } finally { if (decompressor != null) { decompressor.Dispose(); } } }
private void BufferStream(SongData songData) { var stream = songData.Stream.Value.Item1; var bufferDuration = songData.Stream.Value.Item3; var buffer = new byte[16384 * 4]; var waitTime = 0; var wait = new AutoResetEvent(false); using (var throttledStream = new ThrottledStream(stream)) using (var readFullyStream = new ReadFullyStream(throttledStream)) { IMp3FrameDecompressor decompressor = null; _bufferingComplete = false; do { if (songData.Cts.Token.IsCancellationRequested) { songData.Cts.Token.ThrowIfCancellationRequested(); } wait.WaitOne(waitTime); try { var frame = Mp3Frame.LoadFromStream(readFullyStream); var progress = (int)(readFullyStream.Position / (double)songData.Stream.Value.Item2 * 100); if (progress % 5 == 0) { OnBuffering(new ValueProgressEventArgs<int>(progress, 100)); } if (frame == null) { Trace.WriteLine("Mp3Frame is null"); break; } if (decompressor == null) { var waveFormat = new Mp3WaveFormat( 44100, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate); double bufferTimeInSeconds = bufferDuration.TotalSeconds; if (bufferTimeInSeconds.LessThan(60)) { bufferTimeInSeconds = TimeSpan.FromMinutes(2).TotalSeconds; } decompressor = new AcmMp3FrameDecompressor(waveFormat); songData.WaveProvider = new BufferedWaveProvider(decompressor.OutputFormat) { BufferDuration = TimeSpan.FromSeconds(bufferTimeInSeconds), DiscardOnBufferOverflow = true }; // Adjust the maximum bytes to stream per second // This is done to avoid being blacklisted by Grooveshark for several hours throttledStream.MaximumBytesPerSecond = songData.WaveProvider.WaveFormat.AverageBytesPerSecond / 4; } if (songData.WaveProvider != null) { try { int decompressed = decompressor.DecompressFrame(frame, buffer, 0); songData.WaveProvider.AddSamples(buffer, 0, decompressed); if (songData.WaveProvider.BufferLength - songData.WaveProvider.BufferedBytes < songData.WaveProvider.WaveFormat.AverageBytesPerSecond / 4) { Trace.WriteLine("Buffer full. Waiting 500 ms"); waitTime = 500; } } catch (InvalidOperationException e) { Trace.WriteLine(e.Message + ". Wait time is 500ms"); waitTime = 500; } } } catch (Exception e) { Trace.WriteLine(e); break; } } while (_playerState != PlayerState.Stopped); if (decompressor != null) { decompressor.Dispose(); } } _bufferingComplete = true; Trace.WriteLine("Buffer-thread exiting"); }
//The StopPlaying/Recording functions which will call KillAll which kills both recording and playing streams. //public void StopPlaying(object sender, EventArgs e) public void StopPlayer() { //signal event we are shutting down if (backendHandler != null) backendHandler(this, false); if (decompressor != null) { decompressor.Dispose(); decompressor = null; } if (volumeHandler != null) { volumeHandler = null; } if (waveOut != null) { waveOut.Stop(); waveOut.Dispose(); waveOut = null; } KillAll(); }
public void SetupBackend(Mp3Frame frame) { //first cleanup the the backend. the old MP3 info could be different StopPlayer(); bFileEnding = false; //setup the output stream, using the provider & mp3Frame waveOut = new NAudio.Wave.DirectSoundOut(); SetWaveFormat(frame); decompressor = new AcmMp3FrameDecompressor(waveFormat); out_buffer = new BufferedWaveProvider(decompressor.OutputFormat); volumeHandler = new VolumeWaveProvider16(out_buffer); //1.0 = full volume, 0.0 = silence volumeHandler.Volume = volume; waveOut.Init(volumeHandler); //signal event we are set up if (backendHandler != null) backendHandler(this, true); waveOut.Play(); }
private void StreamMp3(object state) { _isBuffering(true); ThrottledStream responseStream = (ThrottledStream)state; byte[] buffer = new byte[16384 * 4]; // needs to be big enough to hold a decompressed frame IMp3FrameDecompressor decompressor = null; try { using (responseStream) { _playbackState = StreamingPlaybackState.Buffering; using (var readFullyStream = new ReadFullyStream(responseStream)) { do { if (_bufferedWaveProvider != null && _bufferedWaveProvider.BufferLength - _bufferedWaveProvider.BufferedBytes < _bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4) { Thread.Sleep(500); } Mp3Frame frame; try { frame = Mp3Frame.LoadFromStream(readFullyStream); if (frame == null) { _fullyDownloaded = true; break; } } catch (EndOfStreamException e) { _log.Log(e.Message, Category.Warn, Priority.Medium); _fullyDownloaded = true; // reached the end of the MP3 file / stream break; } catch (WebException e) { _log.Log(e.Message, Category.Warn, Priority.Medium); _fullyDownloaded = true; // probably we have aborted download from the GUI thread break; } catch (Exception e) { _log.Log(e.Message, Category.Exception, Priority.High); _fullyDownloaded = true; break; } if (decompressor == null) { WaveFormat waveFormat = new Mp3WaveFormat(44100, frame.ChannelMode == ChannelMode.Mono ? 1 : 2, frame.FrameLength, frame.BitRate); decompressor = new AcmMp3FrameDecompressor(waveFormat); _bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat); if (_track.TotalDuration != TimeSpan.Zero) { _bufferedWaveProvider.BufferDuration = _track.TotalDuration; } else { _bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(25); } responseStream.MaximumBytesPerSecond = _bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4; } if (_bufferedWaveProvider != null) { try { int decompressed = decompressor.DecompressFrame(frame, buffer, 0); _bufferedWaveProvider.AddSamples(buffer, 0, decompressed); } catch (Exception e) { _fullyDownloaded = true; _log.Log("Grooveshark: Error decompressing frame: " + e.Message, Category.Exception, Priority.Medium); break; } } } while (_playbackState != StreamingPlaybackState.Stopped); // was doing this in a finally block, but for some reason // we are hanging on response stream .Dispose so never get there if (decompressor != null) { decompressor.Dispose(); decompressor = null; } } } } finally { if (decompressor != null) { decompressor.Dispose(); decompressor = null; } } _log.Log("Grooveshark: Buffer thread exiting", Category.Info, Priority.Medium); }