private void Update(EvaluationContext context) { var t = context.Playback.FxTimeInBars; var wasEvaluatedThisFrame = Math.Abs(t - _lastEvalTime) < 0.001f; if (wasEvaluatedThisFrame) { return; } _lastEvalTime = t; var value = Value.GetValue(context); var increase = (value - _lastValue);//.Clamp(0, 10000); var timeSinceLastPeak = Playback.RunTimeInSecs - _lastPeakTime; if (timeSinceLastPeak < 0) { _lastPeakTime = Double.NegativeInfinity; } if (increase > Threshold.GetValue(context) && timeSinceLastPeak > MinTimeBetweenPeaks.GetValue(context)) { _lastPeakTime = Playback.RunTimeInSecs; FoundPeak.Value = true; } else { FoundPeak.Value = false; } var previousSum = MovingSum.GetValue(context); const float precisionThreshold = 30000f; if (Math.Abs(previousSum) > precisionThreshold) { previousSum %= precisionThreshold; } MovingSum.Value = previousSum + increase; AttackLevel.Value = increase; TimeSincePeak.Value = (float)timeSinceLastPeak; _lastValue = value; FoundPeak.DirtyFlag.Clear(); TimeSincePeak.DirtyFlag.Clear(); AttackLevel.DirtyFlag.Clear(); }
private void Update(EvaluationContext context) { var needsRecalcAverage = false; var filePath = FilePath.GetValue(context); if (filePath != _filepath) { _filepath = filePath; if (File.Exists(_filepath)) { using (var reader = new StreamReader(filePath)) { var jsonString = reader.ReadToEnd(); _upLevels = JsonConvert.DeserializeObject <float[]>(jsonString); if (_upLevels == null || _upLevels.Length == 0) { Log.Warning("Loading sound levels failed"); return; } } } else { Log.Warning("File doesn't exist: " + _filepath); } needsRecalcAverage = true; } var smoothWindow = (int)Clamp(Smooth.GetValue(context), 1, MaxSmoothWindow); needsRecalcAverage |= smoothWindow != _smoothWindow; if (needsRecalcAverage) { _minTimeBetweenPeaks = MinTimeBetweenPeaks.GetValue(context); _smoothWindow = smoothWindow; _maxLevel = 0f; _averageLevels = new float[_upLevels.Length]; _averageLevels2 = new float[_upLevels.Length]; foreach (var l in _upLevels) { _maxLevel = Math.Max(l, _maxLevel); } SmoothBuffer(ref _upLevels, ref _averageLevels2, _smoothWindow, 1); SmoothBuffer(ref _averageLevels2, ref _averageLevels, _smoothWindow, 2); SmoothBuffer(ref _averageLevels, ref _averageLevels2, _smoothWindow, 4); SmoothBuffer(ref _averageLevels2, ref _averageLevels, _smoothWindow, 8); } bool needsRescanBeats = needsRecalcAverage; var threshold = Threshold.GetValue(context); needsRescanBeats |= threshold != _threshold; var minTimeBetweenPeaks = MinTimeBetweenPeaks.GetValue(context); needsRescanBeats |= minTimeBetweenPeaks != _minTimeBetweenPeaks; if (needsRescanBeats) { _threshold = threshold; _minTimeBetweenPeaks = minTimeBetweenPeaks; UpdateAllBeatNumbers(); } //var index = (int)(Time.GetValue(context) * SampleResolutionPerSecond); var index = (int)(Playback.Current.TimeInBars * SampleResolutionPerSecond); // Log.Debug("INdex:" + index); var needToFindNewBoundaries = (index <= _beatStartIndex || index >= _beatEndIndex); needToFindNewBoundaries |= needsRescanBeats; if (needToFindNewBoundaries) { FindBoundarysFromBeatNumbers(index, out _beatStartIndex, out _beatEndIndex); } //FindBeatBoundaries(index, ref _beatStartIndex, ref _beatEndIndex); if (_upLevels == null || index >= _upLevels.Length || index <= 0 || !(_maxLevel > 0.001f)) { return; } Level.Value = _upLevels[index]; // Peaks... // BeatTime.Value = (float)Math.Max(0, _uplevels[index] - _averageLevels[index]); // Flashes: var t = ((float)index - _beatStartIndex) / SampleResolutionPerSecond; BeatTime.Value = (float)Math.Pow(2.71f, -FlashDecay.GetValue(context) * t); // FlashesWithIntensity... // var t2 = ((float)index - _beatStartIndex) / (float)SAMPLE_RESOLUTION_PER_SECOND; // BeatTime.Value = (float)Math.Pow(2.71f, -FlashDecay* t2) * _uplevels[_beatStartIndex]; // TimeSincePeak... // BeatTime.Value = ((float)index - _beatStartIndex) / (float)SAMPLE_RESOLUTION_PER_SECOND; // TimeToPeak... // BeatTime.Value = (_beatEndIndex - (float)index) / (float)SAMPLE_RESOLUTION_PER_SECOND; // BeatIndex.Value = (float)_beatStartIndex; BeatIndex.Value = _beatNumbers[index]; Loudness.Value = _averageLevels[index]; }