コード例 #1
0
        /// <summary>
        /// Creates a stream that can convert to PCM
        /// </summary>
        /// <param name="sourceStream">The source stream</param>
        /// <returns>A PCM stream</returns>
        public static WaveStream CreatePcmStream(WaveStream sourceStream)
        {
            if (sourceStream.WaveFormat.Encoding == WaveFormatEncoding.Pcm)
            {
                return(sourceStream);
            }
            WaveFormat pcmFormat = AcmStream.SuggestPcmFormat(sourceStream.WaveFormat);

            return(new WaveFormatConversionStream(pcmFormat, sourceStream));
        }
コード例 #2
0
 /// <summary>
 /// Creates a new ACM frame decompressor
 /// </summary>
 /// <param name="sourceFormat">The MP3 source format</param>
 public AcmMp3FrameDecompressor(WaveFormat sourceFormat)
 {
     this.pcmFormat = AcmStream.SuggestPcmFormat(sourceFormat);
     try
     {
         conversionStream = new AcmStream(sourceFormat, pcmFormat);
     }
     catch (Exception)
     {
         disposed = true;
         GC.SuppressFinalize(this);
         throw;
     }
 }
コード例 #3
0
        public static WaveStream CreatePcmStream(WaveStream sourceStream)
        {
            if (sourceStream.WaveFormat.Encoding == WaveFormatEncoding.Pcm)
            {
                return(sourceStream);
            }
            WaveFormat waveFormat = AcmStream.SuggestPcmFormat(sourceStream.WaveFormat);

            if (waveFormat.SampleRate < 8000)
            {
                if (sourceStream.WaveFormat.Encoding != WaveFormatEncoding.G723)
                {
                    throw new InvalidOperationException("Invalid suggested output format, please explicitly provide a target format");
                }
                waveFormat = new WaveFormat(8000, 16, 1);
            }
            return(new WaveFormatConversionStream(waveFormat, sourceStream));
        }
コード例 #4
0
 /// <summary>
 /// Creates a new ACM frame decompressor
 /// </summary>
 /// <param name="sourceFormat">The MP3 source format</param>
 public AcmMp3FrameDecompressor(WaveFormat sourceFormat)
 {
     this.pcmFormat   = AcmStream.SuggestPcmFormat(sourceFormat);
     conversionStream = new AcmStream(sourceFormat, pcmFormat);
 }
コード例 #5
0
ファイル: Player.cs プロジェクト: JustOxlamon/TwoRatChat
            private void StreamMP3_New(object state)
            {
                Thread.CurrentThread.Name = state.ToString();
                string url = (string)state;

                byte[] buffer = new byte[16384 * 4];

                Dictionary <int, IMp3FrameDecompressor> Decompressors = new Dictionary <int, IMp3FrameDecompressor>();
                WaveFormat outputFormat = new WaveFormat(44100, 16, 2);

                this.bufferedWaveProvider = new BufferedWaveProvider(outputFormat);
                this.bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(2);

//                WaveToSampleProvider wav2sample = new WaveToSampleProvider(this.bufferedWaveProvider);

                ISampleProvider sampleProvider = new Pcm16BitToSampleProvider(this.bufferedWaveProvider);

                SampleAggregator sa = new SampleAggregator(128);

                sa.NotificationCount = 882;
                sa.PerformFFT        = true;
                sa.FftCalculated    += sa_FftCalculated;
                NotifyingSampleProvider notifyProvider = new NotifyingSampleProvider(sampleProvider);

                notifyProvider.Sample += (a, b) => sa.Add(b.Left);

                volumeProvider = new VolumeSampleProvider(notifyProvider);
                //volumeProvider = new SampleChannel(this.bufferedWaveProvider, true);
                volumeProvider.Volume = 0.0f;
                //volumeProvider.PreVolumeMeter += waveChannel_PreVolumeMeter;

                for (int j = 0; j < 5; ++j)
                {
                    try {
                        using (IWavePlayer waveOut = new WaveOut()) {
                            waveOut.PlaybackStopped += waveOut_PlaybackStopped;
                            waveOut.Init(volumeProvider);

                            using (var readFullyStream = new ShoutcastStream(url)) {
                                waveOut.Play();
                                if (OnStartPlay != null)
                                {
                                    OnStartPlay(this);
                                }

                                do
                                {
                                    if (bufferedWaveProvider != null && bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes < bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4)
                                    {
                                        int x = 0;
                                        while (playbackState != StreamingPlaybackState.Stopped && x < 5)
                                        {
                                            x++;
                                            Thread.Sleep(50);
                                        }
                                    }
                                    else
                                    {
                                        Mp3Frame frame = Mp3Frame.LoadFromStream(readFullyStream, true);

                                        if (currentTrack != readFullyStream.StreamTitle)
                                        {
                                            currentTrack = readFullyStream.StreamTitle;
                                            if (!string.IsNullOrEmpty(currentTrack))
                                            {
                                                ThreadPool.QueueUserWorkItem(Search, currentTrack);
                                            }
                                            else
                                            {
                                                CurrentTrack = null;
                                                if (OnNewTrack != null)
                                                {
                                                    OnNewTrack(this, null);
                                                }
                                            }
                                        }

                                        IMp3FrameDecompressor dec;
                                        if (!Decompressors.TryGetValue(frame.SampleRate, out dec))
                                        {
                                            WaveFormat waveFormat = new Mp3WaveFormat(frame.SampleRate,
                                                                                      frame.ChannelMode == ChannelMode.Mono ? 1 : 2,
                                                                                      frame.FrameLength, frame.BitRate);

                                            var suggFromat = AcmStream.SuggestPcmFormat(waveFormat);

                                            dec = new VbrAcmMp3FrameDecompressor(waveFormat, outputFormat);
                                            Decompressors[frame.SampleRate] = dec;
                                        }


                                        int decompressed = dec.DecompressFrame(frame, buffer, 0);
                                        bufferedWaveProvider.AddSamples(buffer, 0, decompressed);
                                    }
                                } while (playbackState != StreamingPlaybackState.Stopped);

                                waveOut.Stop();
                            }
                        }

                        return;
                    } catch (Exception exe) {
                        int x = 0;
                        while (playbackState != StreamingPlaybackState.Stopped && x < 20)
                        {
                            x++;
                            Thread.Sleep(50);
                        }
                        if (playbackState == StreamingPlaybackState.Stopped)
                        {
                            return;
                        }
                    } finally {
                        foreach (var dc in Decompressors)
                        {
                            dc.Value.Dispose();
                        }

                        Decompressors.Clear();
                    }
                }

                if (OnError != null)
                {
                    OnError(this);
                }
            }