Esempio n. 1
0
        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Provides a pcm stream that can be used to a waveout device (i.e. speakers)
        /// or can write pcm samples to a System.IO.Stream
        /// </summary>
        public void Start()
        {
            //declares
            IMp3FrameDecompressor decompressor = null;
            HttpWebResponse       resp         = null;

            IsActive = true;

            //init state to buffering
            playbackState = StreamingPlaybackState.Buffering;

            //do web request
            var webRequest = (HttpWebRequest)WebRequest.Create(this.mp3url);

            try
            {
                resp = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (Exception ex)
            {
                DebugEx.Assert(ex);
            }
            if (resp == null)
            {
                return;
            }

            //local buffer that is overriden each  time a mp3 frame is decompressed
            var buffer          = new byte[16384 * 4];
            var respStream      = resp.GetResponseStream();
            var readFullyStream = new ReadFullyStream(respStream);

            //Streaming Loop
            try
            {
                while (playbackState != StreamingPlaybackState.Stopped)
                {
                    //get frame
                    Mp3Frame frame;
                    try
                    {
                        frame = Mp3Frame.LoadFromStream(readFullyStream);
                    }
                    catch (Exception ex)
                    {
                        DebugEx.TraceError(ex.Message);
                        break;
                    }

                    //get the codec info from the first frame
                    if (decompressor == null)
                    {
                        decompressor = Tools.CreateFrameDecompressor(frame);
                        audioFormat  = new AudioFormat(decompressor.OutputFormat.SampleRate, (ushort)decompressor.OutputFormat.BitsPerSample, (ushort)decompressor.OutputFormat.Channels);
                        if (OnPCMStart != null)
                        {
                            OnPCMStart(this, decompressor.OutputFormat);
                        }
                    }

                    //write decompressed (pcm) samples to local buffer
                    int decompressed = decompressor.DecompressFrame(frame, buffer, 0);
                    if (IsActive)
                    {
                        //fire event
                        if (OnAudioDataCaptured != null)
                        {
                            OnAudioDataCaptured(this, new AudioEventArgs(buffer, decompressed));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DebugEx.TraceError(ex.Message);
            }
        }
Esempio n. 2
0
        //------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Provides a pcm stream that can be used to a waveout device (i.e. speakers) 
        /// or can write pcm samples to a System.IO.Stream
        /// </summary>
        public void Start()
        {
            //declares
            IMp3FrameDecompressor decompressor = null;
            HttpWebResponse resp = null;
            IsActive = true;

            //init state to buffering
            playbackState = StreamingPlaybackState.Buffering;

            //do web request
            var webRequest = (HttpWebRequest)WebRequest.Create(this.mp3url);
            try
            {
                resp = (HttpWebResponse)webRequest.GetResponse();
            }
            catch (Exception ex)
            {
                DebugEx.Assert(ex);
            }
            if (resp == null)
                return;

            //local buffer that is overriden each  time a mp3 frame is decompressed
            var buffer = new byte[16384 * 4];
            var respStream = resp.GetResponseStream();
            var readFullyStream = new ReadFullyStream(respStream);

            //Streaming Loop
            try
            {
                while (playbackState != StreamingPlaybackState.Stopped)
                {
                    //get frame
                    Mp3Frame frame;
                    try
                    {
                        frame = Mp3Frame.LoadFromStream(readFullyStream);
                    }
                    catch (Exception ex)
                    {
                        DebugEx.TraceError(ex.Message);
                        break;
                    }

                    //get the codec info from the first frame
                    if (decompressor == null)
                    {
                        decompressor = Tools.CreateFrameDecompressor(frame);
                        audioFormat = new AudioFormat(decompressor.OutputFormat.SampleRate, (ushort)decompressor.OutputFormat.BitsPerSample, (ushort)decompressor.OutputFormat.Channels);
                        if (OnPCMStart != null)
                            OnPCMStart(this, decompressor.OutputFormat);
                    }

                    //write decompressed (pcm) samples to local buffer
                    int decompressed = decompressor.DecompressFrame(frame, buffer, 0);
                    if (IsActive)
                    {
                        //fire event
                        if (OnAudioDataCaptured != null)
                            OnAudioDataCaptured(this, new AudioEventArgs(buffer, decompressed));
                    }
                }
            }
            catch (Exception ex)
            {
                DebugEx.TraceError(ex.Message);
            }
        }