StephaneLenclud@273: using System; StephaneLenclud@273: using System.Collections.Generic; StephaneLenclud@273: using CSCore.DSP; StephaneLenclud@273: StephaneLenclud@273: namespace Visualization StephaneLenclud@273: { StephaneLenclud@273: /// StephaneLenclud@273: /// BasicSpectrumProvider StephaneLenclud@273: /// StephaneLenclud@273: public class BasicSpectrumProvider : FftProvider, ISpectrumProvider StephaneLenclud@273: { StephaneLenclud@273: private readonly int _sampleRate; StephaneLenclud@273: private readonly List _contexts = new List(); StephaneLenclud@273: StephaneLenclud@273: public BasicSpectrumProvider(int channels, int sampleRate, FftSize fftSize) StephaneLenclud@273: : base(channels, fftSize) StephaneLenclud@273: { StephaneLenclud@273: if (sampleRate <= 0) StephaneLenclud@273: throw new ArgumentOutOfRangeException("sampleRate"); StephaneLenclud@273: _sampleRate = sampleRate; StephaneLenclud@273: } StephaneLenclud@273: StephaneLenclud@273: public int GetFftBandIndex(float frequency) StephaneLenclud@273: { StephaneLenclud@273: int fftSize = (int)FftSize; StephaneLenclud@273: double f = _sampleRate / 2.0; StephaneLenclud@273: // ReSharper disable once PossibleLossOfFraction StephaneLenclud@273: return (int)((frequency / f) * (fftSize / 2)); StephaneLenclud@273: } StephaneLenclud@273: StephaneLenclud@273: public bool GetFftData(float[] fftResultBuffer, object context) StephaneLenclud@273: { StephaneLenclud@273: if (_contexts.Contains(context)) StephaneLenclud@273: return false; StephaneLenclud@273: StephaneLenclud@273: _contexts.Add(context); StephaneLenclud@273: GetFftData(fftResultBuffer); StephaneLenclud@273: return true; StephaneLenclud@273: } StephaneLenclud@273: StephaneLenclud@273: public override void Add(float[] samples, int count) StephaneLenclud@273: { StephaneLenclud@273: base.Add(samples, count); StephaneLenclud@273: if (count > 0) StephaneLenclud@273: _contexts.Clear(); StephaneLenclud@273: } StephaneLenclud@273: StephaneLenclud@273: public override void Add(float left, float right) StephaneLenclud@273: { StephaneLenclud@273: base.Add(left, right); StephaneLenclud@273: _contexts.Clear(); StephaneLenclud@273: } StephaneLenclud@273: } StephaneLenclud@273: }