コード例 #1
0
ファイル: MainWindow.cs プロジェクト: yazici/AudioLab
        private void button1_Click(object sender, EventArgs e)
        {
            var ofn = new OpenFileDialog();

            ofn.Filter = CodecFactory.SupportedFilesFilterEN;
            if (ofn.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Stop();

                if (WasapiOut.IsSupportedOnCurrentPlatform)
                {
                    _soundOut = new WasapiOut();
                }
                else
                {
                    _soundOut = new DirectSoundOut();
                }

                var source = CodecFactory.Instance.GetCodec(ofn.FileName);
                source = new LoopStream(source)
                {
                    EnableLoop = false
                };
                (source as LoopStream).StreamFinished += (s, args) => Stop();

                _eq = Equalizer.Create10BandEqualizer(source);
                _soundOut.Initialize(_eq.ToWaveSource(16));
                _soundOut.Play();
            }
        }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: rustforfuture/Wale
        private void Equal()
        {
            //CSCore.SoundOut.WaveOutDevice.DefaultDevice;
            const string    filename   = @"C:\Temp\test.mp3";
            EventWaitHandle waitHandle = new AutoResetEvent(false);

            try
            {
                //create a source which provides audio data
                using (var source = CodecFactory.Instance.GetCodec(filename))
                {
                    //create the equalizer.
                    //You can create a custom eq with any bands you want, or you can just use the default 10 band eq.
                    Equalizer equalizer = Equalizer.Create10BandEqualizer(FluentExtensions.ToSampleSource(source));

                    //create a soundout to play the source
                    ISoundOut soundOut;
                    if (WasapiOut.IsSupportedOnCurrentPlatform)
                    {
                        soundOut = new WasapiOut();
                    }
                    else
                    {
                        soundOut = new DirectSoundOut();
                    }

                    soundOut.Stopped += (s, e) => waitHandle.Set();

                    IWaveSource finalSource = equalizer.ToWaveSource(16); //since the equalizer is a samplesource, you have to convert it to a raw wavesource
                    soundOut.Initialize(finalSource);                     //initialize the soundOut with the previously created finalSource
                    soundOut.Play();

                    /*
                     * You can change the filter configuration of the equalizer at any time.
                     */
                    equalizer.SampleFilters[0].AverageGainDB = 20; //eq set the gain of the first filter to 20dB (if needed, you can set the gain value for each channel of the source individually)

                    //wait until the playback finished
                    //of course that is optional
                    waitHandle.WaitOne();

                    //remember to dispose and the soundout and the source
                    soundOut.Dispose();
                }
            }
            catch (NotSupportedException ex)
            {
                Console.WriteLine("Fileformat not supported: " + ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unexpected exception: " + ex.Message);
            }
        }
コード例 #3
0
        public void Open(string filename)
        {
            CleanupPlayback();

            var source = CodecFactory.Instance.GetCodec(filename);

            volumeSource = new VolumeSource(source);

            equalizer = Equalizer.Create10BandEqualizer(volumeSource);

            finalSource = equalizer
                          .ToStereo()
                          .ChangeSampleRate(44100)
                          .AppendSource(Equalizer.Create10BandEqualizer, out equalizer)
                          .ToWaveSource(16);

            if (WasapiOut.IsSupportedOnCurrentPlatform)
            {
                soundOut = new WasapiOut()
                {
                    Latency = 100, Device = PlaybackDevice
                }
            }
            ;
            else
            {
                soundOut = new DirectSoundOut();
            }

            soundOut.Initialize(finalSource);

            soundOut.Volume = deviceVolume;

            if (this.OpenCompleted != null)
            {
                this.OpenCompleted(this, new EventArgs());
            }
        }
コード例 #4
0
        public void TestFlatten()
        {
            EventWaitHandle waitHandle = new AutoResetEvent(false);

            using (MemoryStream stream = new MemoryStream())
                using (SpeechSynthesizer synth = new SpeechSynthesizer())
                {
                    synth.SetOutputToWaveStream(stream);
                    synth.Speak("This is a test for flattening");
                    stream.Seek(0, SeekOrigin.Begin);

                    IWaveSource source    = new WaveFileReader(stream);
                    Equalizer   equalizer = Equalizer.Create10BandEqualizer(source);
                    equalizer.SampleFilters[0].SetGain(-9.6f);
                    equalizer.SampleFilters[1].SetGain(-9.6f);
                    equalizer.SampleFilters[2].SetGain(-9.6f);
                    equalizer.SampleFilters[3].SetGain(-3.9f);
                    equalizer.SampleFilters[4].SetGain(2.4f);
                    equalizer.SampleFilters[5].SetGain(11.1f);
                    equalizer.SampleFilters[6].SetGain(15.9f);
                    equalizer.SampleFilters[7].SetGain(15.9f);
                    equalizer.SampleFilters[8].SetGain(15.9f);
                    equalizer.SampleFilters[9].SetGain(16.7f);

                    var soundOut = new WasapiOut();
                    soundOut.Stopped += (s, e) => waitHandle.Set();

                    soundOut.Initialize(equalizer.ToWaveSource());
                    soundOut.Play();

                    waitHandle.WaitOne();

                    soundOut.Dispose();
                    equalizer.Dispose();
                    source.Dispose();
                }
        }
コード例 #5
0
        protected override IWaveSource GetSource()
        {
            var source = GetMockSource();

            return(Equalizer.Create10BandEqualizer(source.ToSampleSource()).ToWaveSource());
        }
コード例 #6
0
        public async Task <bool> OpenTrack(PlayableBase track)
        {
            if (!IsEnabled)
            {
                OnSoundOutErrorOccurred(Application.Current.Resources["NoSoundOutDeviceFound"].ToString());
                return(false);
            }

            _playAfterLoading = false;
            IsLoading         = true;
            StopPlayback();
            if (CurrentTrack != null)
            {
                CurrentTrack.IsOpened = false;
                CurrentTrack.Unload();
            }
            if (SoundSource != null && !_crossfade.IsCrossfading)
            {
                SoundSource.Dispose();
            }
            track.IsOpened = true;
            CurrentTrack   = track;
            var       t = Task.Run(() => track.Load());
            Equalizer equalizer;

            var result = await SetSoundSource(track);

            switch (result.State)
            {
            case State.False:
                track.IsOpened = false;
                return(false);

            case State.Exception:
                track.IsOpened = false;
                IsLoading      = false;
                CurrentTrack   = null;
                if (ExceptionOccurred != null)
                {
                    ExceptionOccurred(this, (Exception)result.CustomState);
                }
                StopPlayback();
                return(false);
            }

            if (Settings.SampleRate == -1 && SoundSource.WaveFormat.SampleRate < 44100)
            {
                SoundSource = SoundSource.ChangeSampleRate(44100);
            }
            else if (Settings.SampleRate > -1)
            {
                SoundSource = SoundSource.ChangeSampleRate(Settings.SampleRate);
            }

            SoundSource = SoundSource
                          .AppendSource(x => Equalizer.Create10BandEqualizer(x.ToSampleSource()), out equalizer)
                          .AppendSource(x => new SingleBlockNotificationStream(x), out _singleBlockNotificationStream)
                          .AppendSource(x => new SimpleNotificationSource(x)
            {
                Interval = 100
            }, out _simpleNotificationSource)
                          .ToWaveSource(Settings.WaveSourceBits);

            MusicEqualizer = equalizer;
            SetAllEqualizerSettings();
            _simpleNotificationSource.BlockRead            += notifysource_BlockRead;
            _singleBlockNotificationStream.SingleBlockRead += notificationSource_SingleBlockRead;

            _analyser = new SampleAnalyser(FFTSize);
            _analyser.Initialize(SoundSource.WaveFormat);

            try
            {
                _soundOut.Initialize(SoundSource);
            }
            catch (Exception ex)
            {
                track.IsOpened = false;
                IsLoading      = false;
                OnSoundOutErrorOccurred(ex.Message);
                return(false);
            }

            OnPropertyChanged("TrackLength");
            OnPropertyChanged("CurrentTrackLength");

            CurrentStateChanged();
            _soundOut.Volume = Volume;
            if (StartVisualization != null)
            {
                StartVisualization(this, EventArgs.Empty);
            }
            track.LastTimePlayed = DateTime.Now;
            if (_crossfade.IsCrossfading)
            {
                _fader.CrossfadeIn(_soundOut, Volume);
            }
            IsLoading = false;
            if (_playAfterLoading)
            {
                TogglePlayPause();
            }
            await t;

            return(true);
        }
コード例 #7
0
        public async Task <bool> OpenTrack(IPlaySource track, bool openCrossfading, long position)
        {
            IsLoading = true;
            if (!openCrossfading)
            {
                StopPlayback();
            }

            if (_crossfadeService.IsFading)
            {
                _crossfadeService.Cancel();
            }

            if (_soundSource != null && !openCrossfading)
            {
                _soundSource.Dispose();
            }

            if (openCrossfading && _soundSource != null)
            {
                _soundOut.Stopped                   -= SoundOut_Stopped;
                _loopStream.StreamFinished          -= LoopStream_StreamFinished;
                _simpleNotificationSource.BlockRead -= SimpleNotificationSource_BlockRead;
                _crossfadeService.CrossfadeOut(_soundOut, CrossfadeDuration).Forget();
                _soundOut = null;
            }

            var tempSource = await GetSoundSource(track, position);

            if (tempSource == null)
            {
                return(false);
            }
            _soundSource = tempSource;

            if (_soundSource.WaveFormat.SampleRate < 44100) //Correct sample rate
            {
                _soundSource = _soundSource.ChangeSampleRate(44100);
            }

            _soundSource = _soundSource
                           .AppendSource(x => new LoopStream(x), out _loopStream)
                           .AppendSource(x => Equalizer.Create10BandEqualizer(x.ToSampleSource()), out _equalizer)
                           .AppendSource(x => new SimpleNotificationSource(x)
            {
                Interval = 100
            }, out _simpleNotificationSource)
                           .ToWaveSource();

            _loopStream.EnableLoop               = IsLooping;
            _loopStream.StreamFinished          += LoopStream_StreamFinished;
            _simpleNotificationSource.BlockRead += SimpleNotificationSource_BlockRead;

            for (var i = 0; i < EqualizerBands.Count; i++)
            {
                SetEqualizerBandValue(EqualizerBands.Bands[i].Value, i);
            }

            if (_soundOut == null)
            {
                _soundOut          = _soundOutProvider.GetSoundOut();
                _soundOut.Stopped += SoundOut_Stopped;
            }
            _soundOut.Initialize(_soundSource);
            _soundOut.Volume = Volume;
            IsLoading        = false;

            OnTrackLengthChanged();
            _playTimeStopwatch.Reset();

            if (openCrossfading)
            {
                await TogglePlayPause();

                _fadingService.FadeIn(_soundOut, Volume).Forget();
            }

            CurrentStateChanged();
            OnPositionChanged();
            return(true);
        }