Пример #1
0
        public LineSpectrum GetLineSpectrum(int barCount, ScalingStrategy scalingStrategy)
        {
            if (_spectrumProvider == null || barCount <= 0)
            {
                return(null);
            }

            if (_lineSpectrum == null)
            {
                _lineSpectrum = new LineSpectrum(FftSize)
                {
                    SpectrumProvider = _spectrumProvider,
                    UseAverage       = true,
                    BarCount         = barCount,
                    IsXLogScale      = true,
                    ScalingStrategy  = scalingStrategy
                }
            }
            ;

            return(_lineSpectrum);
        }
Пример #2
0
        private void Start()
        {
            if (_starting)
            {
                return;
            }

            Logger.Debug("Starting audio capture for device: {0}", Device?.FriendlyName ?? "default");
            _starting = true;

            try
            {
                Stop();

                if (Type == MmDeviceType.Input)
                {
                    _soundIn = Device != null
                        ? new WasapiCapture {
                        Device = Device
                    }
                        : new WasapiCapture();
                }
                else
                {
                    _soundIn = Device != null
                        ? new WasapiLoopbackCapture {
                        Device = Device
                    }
                        : new WasapiLoopbackCapture();
                }

                _soundIn.Initialize();

                var soundInSource = new SoundInSource(_soundIn);
                _source = soundInSource.ToSampleSource().AppendSource(x => new GainSource(x), out _volume);

                // create a spectrum provider which provides fft data based on some input
                _spectrumProvider = new BasicSpectrumProvider(_source.WaveFormat.Channels, _source.WaveFormat.SampleRate,
                                                              FftSize);

                // the SingleBlockNotificationStream is used to intercept the played samples
                var notificationSource = new SingleBlockNotificationStream(_source);
                // pass the intercepted samples as input data to the spectrumprovider (which will calculate a fft based on them)
                notificationSource.SingleBlockRead += (s, a) => _spectrumProvider.Add(a.Left, a.Right);

                var waveSource = notificationSource.ToWaveSource(16);
                // We need to read from our source otherwise SingleBlockRead is never called and our spectrum provider is not populated
                var buffer = new byte[waveSource.WaveFormat.BytesPerSecond / 2];
                soundInSource.DataAvailable += (s, aEvent) =>
                {
                    while (waveSource.Read(buffer, 0, buffer.Length) > 0)
                    {
                    }
                };

                _lineSpectrum   = null;
                _singleSpectrum = new SingleSpectrum(FftSize, _spectrumProvider);
                _mayStop        = false;

                _disableTimer.Start();
                _volumeTimer.Start();
                _soundIn.Start();

                Running = true;
            }
            catch (Exception e)
            {
                Logger.Warn(e, "Failed to start WASAPI audio capture");
            }
            _starting = false;
        }