Exemple #1
0
        private void Update(EvaluationContext context)
        {
            var list = Input.GetValue(context);

            if (list == null || list.Count == 0)
            {
                return;
            }
            var lowerLimit = Math.Max(0, LowerLimit.GetValue(context));
            var upperLimit = Math.Min(list.Count, UpperLimit.GetValue(context));
            var sum        = 0f;

            for (var index = lowerLimit; index < upperLimit; index++)
            {
                sum += list[index];
            }
            Selected.Value = sum;
        }
Exemple #2
0
        private void Update(EvaluationContext context)
        {
            var fft = FftInput.GetValue(context);

            if (fft == null || fft.Count == 0)
            {
                return;
            }

            _bpmRangeMin = LowestBpm.GetValue(context);
            if (_bpmRangeMin < 50)
            {
                _bpmRangeMin = 50;
            }
            else if (_bpmRangeMin > 200)
            {
                _bpmRangeMin = 200;
            }

            _bpmRangeMax = HighestBpm.GetValue(context);
            if (_bpmRangeMax < _bpmRangeMin)
            {
                _bpmRangeMax = _bpmRangeMin + 1;
            }
            else if (_bpmRangeMax > 200)
            {
                _bpmRangeMax = 200;
            }

            if (_bpmEnergies.Count != _bpmRangeMax - _bpmRangeMin)
            {
                _bpmEnergies = new List <float>(new float[_bpmRangeMax - _bpmRangeMin]);
            }

            var bufferDuration = (int)BufferDurationSec.GetValue(context) * 60;

            if (bufferDuration < 60)
            {
                bufferDuration = 60;
            }
            else if (bufferDuration > 60 * 60)
            {
                bufferDuration = 60 * 60;
            }

            _bufferLength = bufferDuration;
            _lockInFactor = LockItFactor.GetValue(context);

            UpdateBuffer(fft, LowerLimit.GetValue(context), UpperLimit.GetValue(context));
            SmoothBuffer(ref _smoothedBuffer, _buffer);

            var bestBpm         = 0f;
            var bestMeasurement = float.PositiveInfinity;


            for (var bpm = _bpmRangeMin; bpm < _bpmRangeMax; bpm++)
            {
                var m = MeasureEnergyDifference(bpm) / ComputeFocusFactor(bpm, _currentBpm, 4, _lockInFactor);
                if (m < bestMeasurement)
                {
                    bestMeasurement = m;
                    bestBpm         = bpm;
                }

                _bpmEnergies[bpm - _bpmRangeMin] = m;
            }


            foreach (var offset in _searchOffsets)
            {
                var bpm = _currentBpm + offset;
                if (bpm < 70 || bpm > 160)
                {
                    continue;
                }

                var m = MeasureEnergyDifference(bpm) / ComputeFocusFactor(bpm, _currentBpm, 2, 0.01f);
                if (!(m < bestMeasurement))
                {
                    continue;
                }
                bestMeasurement = m;
                bestBpm         = bpm;
            }


            DetectedBpm.Value  = bestBpm;
            _currentBpm        = bestBpm;
            Measurements.Value = _bpmEnergies;
        }