Server/Spectrum/BasicSpectrumProvider.cs
author StephaneLenclud
Mon, 06 Mar 2017 15:11:27 +0100
changeset 282 29b4bdeda281
permissions -rw-r--r--
Published v1.4.7.0
Adding back runtime dependencies.
StephaneLenclud@273
     1
using System;
StephaneLenclud@273
     2
using System.Collections.Generic;
StephaneLenclud@273
     3
using CSCore.DSP;
StephaneLenclud@273
     4
StephaneLenclud@273
     5
namespace Visualization
StephaneLenclud@273
     6
{
StephaneLenclud@273
     7
    /// <summary>
StephaneLenclud@273
     8
    ///     BasicSpectrumProvider
StephaneLenclud@273
     9
    /// </summary>
StephaneLenclud@273
    10
    public class BasicSpectrumProvider : FftProvider, ISpectrumProvider
StephaneLenclud@273
    11
    {
StephaneLenclud@273
    12
        private readonly int _sampleRate;
StephaneLenclud@273
    13
        private readonly List<object> _contexts = new List<object>();
StephaneLenclud@273
    14
StephaneLenclud@273
    15
        public BasicSpectrumProvider(int channels, int sampleRate, FftSize fftSize)
StephaneLenclud@273
    16
            : base(channels, fftSize)
StephaneLenclud@273
    17
        {
StephaneLenclud@273
    18
            if (sampleRate <= 0)
StephaneLenclud@273
    19
                throw new ArgumentOutOfRangeException("sampleRate");
StephaneLenclud@273
    20
            _sampleRate = sampleRate;
StephaneLenclud@273
    21
        }
StephaneLenclud@273
    22
StephaneLenclud@273
    23
        public int GetFftBandIndex(float frequency)
StephaneLenclud@273
    24
        {
StephaneLenclud@273
    25
            int fftSize = (int)FftSize;
StephaneLenclud@273
    26
            double f = _sampleRate / 2.0;
StephaneLenclud@273
    27
            // ReSharper disable once PossibleLossOfFraction
StephaneLenclud@273
    28
            return (int)((frequency / f) * (fftSize / 2));
StephaneLenclud@273
    29
        }
StephaneLenclud@273
    30
StephaneLenclud@273
    31
        public bool GetFftData(float[] fftResultBuffer, object context)
StephaneLenclud@273
    32
        {
StephaneLenclud@273
    33
            if (_contexts.Contains(context))
StephaneLenclud@273
    34
                return false;
StephaneLenclud@273
    35
StephaneLenclud@273
    36
            _contexts.Add(context);
StephaneLenclud@273
    37
            GetFftData(fftResultBuffer);
StephaneLenclud@273
    38
            return true;
StephaneLenclud@273
    39
        }
StephaneLenclud@273
    40
StephaneLenclud@273
    41
        public override void Add(float[] samples, int count)
StephaneLenclud@273
    42
        {
StephaneLenclud@273
    43
            base.Add(samples, count);
StephaneLenclud@273
    44
            if (count > 0)
StephaneLenclud@273
    45
                _contexts.Clear();
StephaneLenclud@273
    46
        }
StephaneLenclud@273
    47
StephaneLenclud@273
    48
        public override void Add(float left, float right)
StephaneLenclud@273
    49
        {
StephaneLenclud@273
    50
            base.Add(left, right);
StephaneLenclud@273
    51
            _contexts.Clear();
StephaneLenclud@273
    52
        }
StephaneLenclud@273
    53
    }
StephaneLenclud@273
    54
}