From af40eb487194ef0f7088bc719a8e7b4ab823abe9 Mon Sep 17 00:00:00 2001 From: Nytra <14206961+Nytra@users.noreply.github.com> Date: Sun, 29 Dec 2024 13:59:12 +0000 Subject: [PATCH] Fix RoundToNearest --- .../ProtoFlux/Math/FrequencyQuantize.cs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/ProjectObsidian/ProtoFlux/Math/FrequencyQuantize.cs b/ProjectObsidian/ProtoFlux/Math/FrequencyQuantize.cs index 1005183..6f2be63 100644 --- a/ProjectObsidian/ProtoFlux/Math/FrequencyQuantize.cs +++ b/ProjectObsidian/ProtoFlux/Math/FrequencyQuantize.cs @@ -115,15 +115,29 @@ protected override void ComputeOutputs(FrooxEngineContext context) { if (RoundToNearest.Evaluate(context)) { + int scaleDegree = (midi - rootNote) % 12; + if (scaleDegree < 0) + scaleDegree += 12; + int closestSemitone = scaleList[0]; - var scaleDegree = (midi - rootNote) % 12; - if (scaleDegree < 0) scaleDegree += 12; + int minDifference = int.MaxValue; + foreach (int scaleSemitone in scaleList) { - if (MathX.Abs(scaleDegree - scaleSemitone) < MathX.Abs(scaleDegree - closestSemitone)) + int difference = MathX.Abs(scaleDegree - scaleSemitone); + if (difference < minDifference) + { + minDifference = difference; closestSemitone = scaleSemitone; + } } - midi += (scaleDegree - closestSemitone); + + int octave = (midi - rootNote) / 12; + + midi += closestSemitone - scaleDegree; + + //UniLog.Log($"midi: {midi} octave: {octave} rootNote: {rootNote} closestSemitone: {closestSemitone} scaleDegree: {scaleDegree}"); + inScale = true; } }