Skip to content

Commit

Permalink
Fix RoundToNearest
Browse files Browse the repository at this point in the history
  • Loading branch information
Nytra committed Dec 29, 2024
1 parent d061a65 commit af40eb4
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions ProjectObsidian/ProtoFlux/Math/FrequencyQuantize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit af40eb4

Please sign in to comment.