Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.
C# audioaudio-unitavaloniacpp20cross-platformdawdigital-audio-workstationdspjucelinux-audiomcp-servermidimusicmusic-productionpiano-rollplugin-hostsequencersynthesizervstvst3
(feat) Convert to harmony algorithm update
Egor Khindikaynene05a31a131a8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ee385e90..14c17aea 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,9 @@ created, and the build-and-publish run starts.Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.### Added+- **Remove out of scale** in the MIDI clip editor, next to the Scale key and mode: with+ Scale on it deletes every note outside the scale in one undo step. The button is live+ only while the clip has such notes.- Dropping an effect from the browser onto Devices puts it where you point: between cards(an accent bar marks the spot) or in place of the card under the pointer (the card isoutlined). Dropping an instrument there replaces the track's instrument. Either way it is@@ -37,6 +40,10 @@ Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.shows its first automated parameter instead of Volume.- While dragging from the browser over the arrangement, the target track is outlined in itsheader too, and the outline stays visible over the lane's clips.+- **Convert Harmony to New MIDI Track** is much cleaner. Overtones no longer come out as extra+ notes. Held chords no longer break into short fragments. Slightly detuned recordings map to+ the right notes. The shimmer of chorused or detuned sounds no longer adds neighbouring+ semitones. Notes start on the audio's attacks, and a re-struck chord comes out as new notes.### Fixed- Reordering or removing effects in Devices no longer scrambles their automation: each lanediff --git a/src/managed/Nota.App/ClipEditorView.cs b/src/managed/Nota.App/ClipEditorView.csindex 6f7c8d9a..685ab053 100644--- a/src/managed/Nota.App/ClipEditorView.cs+++ b/src/managed/Nota.App/ClipEditorView.cs@@ -2,8 +2,8 @@// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.//// Detail · Clip for MIDI clips (nota-design "Nota Clip Editor" 1a): a 38px header — the clip-// cell, Notes | Envelopes, then the Scale switch with its key and mode, and the Tools-// toggle — over the 232px inspector (ClipPropsView), the piano roll (PianoRollView) and+// cell, Notes | Envelopes, then the Scale switch with its key, mode and "Remove out of+// scale", and the Tools toggle — over the 232px inspector (ClipPropsView), the piano roll (PianoRollView) and// the 272px clip-tools panel (MidiToolsView). Envelopes is a mode of the same canvas: the// notes dim under a wash and the clip envelope (Velocity / Volume) is drawn over them in// brass, on the roll's own beat axis so zoom and scroll carry over.@@ -99,6 +99,7 @@ public sealed class ClipEditorView : UserControl// ---- scale overlay ("Set Scale"): a switch, then the key and the mode ----private Action _syncScale = () => { };private TextBlock _keyText = null!, _modeText = null!;+ private Button _dropOutOfScale = null!;private Control ScaleControls(){@@ -107,8 +108,14 @@ public sealed class ClipEditorView : UserControlvar sw = SwitchRow("Scale", () => Roll.ScaleOn, () => { Roll.SetScale(!Roll.ScaleOn, Roll.ScaleRoot, Roll.ScaleIndex); PaintScale(); }, out _syncScale);var key = Dropdown(_keyText, a => ShowMenu(a, PianoRollView.KeyNames, Roll.ScaleRoot, i => { Roll.SetScale(true, i, Roll.ScaleIndex); PaintScale(); }));var mode = Dropdown(_modeText, a => ShowMenu(a, PianoRollView.ScaleNames, Roll.ScaleIndex, i => { Roll.SetScale(true, Roll.ScaleRoot, i); PaintScale(); }));+ // Deletes the notes the overlay dims; live only while some note is out of scale.+ _dropOutOfScale = ClipEditorKit.Button("Remove out of scale", () => Roll.RemoveOutOfScaleNotes());+ _dropOutOfScale.HorizontalAlignment = HorizontalAlignment.Left;+ _dropOutOfScale.VerticalAlignment = VerticalAlignment.Center;+ ToolTip.SetTip(_dropOutOfScale, "Delete every note outside the scale");+ Roll.Changed += () => _dropOutOfScale.IsEnabled = Roll.HasOutOfScaleNotes;PaintScale();- return new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8, Children = { sw, key, mode } };+ return new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8, Children = { sw, key, mode, _dropOutOfScale } };}private void PaintScale()@@ -119,6 +126,7 @@ public sealed class ClipEditorView : UserControlvar ink = Roll.ScaleOn ? NotaPalette.TextPrimary : NotaPalette.TextDisabled;_keyText.Foreground = ink;_modeText.Foreground = ink;+ _dropOutOfScale.IsEnabled = Roll.HasOutOfScaleNotes;}// ---- envelope overlay: a wash over the notes, the curve, a target picker ----------diff --git a/src/managed/Nota.App/MainWindow.Convert.cs b/src/managed/Nota.App/MainWindow.Convert.csindex 228d4cb0..3cf3ce69 100644--- a/src/managed/Nota.App/MainWindow.Convert.cs+++ b/src/managed/Nota.App/MainWindow.Convert.cs@@ -201,12 +201,14 @@ public partial class MainWindowStatus($"Converted melody: {notes.Count} notes → new track {t}");}- // Convert Harmony: STFT peak-picking (AudioHarmony) → polyphonic note spans on a new Nota Synth- // track. Approximate (MVP) — best on sustained chords / pads.+ // Convert Harmony: harmonic-sum pitch estimation (AudioHarmony) → polyphonic note spans on a new+ // Nota Synth track. Approximate — best on sustained chords / pads.private async Task ConvertHarmonyAsync(float[] mono, double sr, double startBeat, double clipBeats, IProgress<ProgressReport> prog){prog.Report(ProgressReport.Indeterminate("Detecting harmony…"));- var spans = await Task.Run(() => AudioHarmony.Detect(mono, sr));+ // Shortest kept note: an eighth (half a beat) at the clip's own beats-per-second.+ double secPerBeat = clipBeats > 0 ? mono.Length / sr / clipBeats : 0.5;+ var spans = await Task.Run(() => AudioHarmony.Detect(mono, sr, minNoteSec: 0.5 * secPerBeat));if (spans.Count == 0) { Status("No harmony found."); return; }double toBeat = mono.Length > 0 ? clipBeats / mono.Length : 0;var notes = new List<NotaNote>(spans.Count);diff --git a/src/managed/Nota.App/PianoRollView.cs b/src/managed/Nota.App/PianoRollView.csindex dfc81d10..e0a33713 100644--- a/src/managed/Nota.App/PianoRollView.cs+++ b/src/managed/Nota.App/PianoRollView.cs@@ -455,6 +455,22 @@ public sealed class PianoRollView : UserControlreturn true;}+ /// <summary>True when the scale overlay is on and some note falls outside it.</summary>+ public bool HasOutOfScaleNotes => _scaleOn && _notes.Exists(n => !InScale(n.Pitch));++ /// <summary>Removes every note outside the scale overlay. No-op with the scale off.+ /// Undoable (one Commit).</summary>+ public bool RemoveOutOfScaleNotes()+ {+ if (!HasOutOfScaleNotes) return false;+ _notes.RemoveAll(n => !InScale(n.Pitch));+ _selection.Clear();+ Invalidate();+ CommitNotes();+ Changed?.Invoke();+ return true;+ }+// --- clip-tool preview -------------------------------------------------------// A tool's result is shown in the roll and streamed to the engine with no undo// checkpoint, exactly like a note drag, so the knobs are audible while they move.diff --git a/src/managed/Nota.Infrastructure/Analysis/AudioHarmony.cs b/src/managed/Nota.Infrastructure/Analysis/AudioHarmony.csindex 74a73b71..b8e049a7 100644--- a/src/managed/Nota.Infrastructure/Analysis/AudioHarmony.cs+++ b/src/managed/Nota.Infrastructure/Analysis/AudioHarmony.cs@@ -1,10 +1,22 @@// SPDX-License-Identifier: AGPL-3.0-only// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.//-// Polyphonic pitch estimation for "Convert Harmony to New MIDI Track". STFT peak--// picking with crude harmonic suppression, then per-pitch temporal segmentation.-// Pure, permissive DSP (in-house FFT). Approximate by design (MVP) — polyphonic-// transcription is hard; this catches sustained vertical chords reasonably.+// Polyphonic pitch estimation for "Convert Harmony to New MIDI Track". Pure, permissive DSP+// (in-house FFT), in four stages:+// 1. Tuning — the recording's offset from A=440 (weighted circular mean of peak cents), so a+// slightly sharp/flat source doesn't flip between neighbouring semitones.+// 2. Per frame, iterative estimate-and-cancel (after Klapuri): score every pitch by a weighted+// sum of its harmonics, take the best, subtract its (smoothed) partials, repeat. A harmonic+// of a chosen note no longer reads as its own note, and missing-fundamental ghosts are+// rejected.+// 3. Per pitch, hysteresis segmentation: a note starts on a strong frame and sustains through+// weak ones; a median filter + gap bridging + minimum length kill flicker fragments. A note+// must also sit in tune on average: glides, and the smeared partial clouds of chorused /+// detuned-unison / shimmer sounds, wander 25–50 cents off-centre, real notes stay within ~15.+// 4. Starts snap to detected onsets (the long STFT window smears attacks), and a clear level+// jump at an onset inside a held note splits it into a re-strike. Only onsets with a real+// energy rise count — the drum-tuned detector also fires on chorus/tremolo pulsing.+// Still approximate — best on sustained chords / pads.using System;using System.Collections.Generic;@@ -16,93 +28,320 @@ public readonly record struct MidiNoteSpan(int Pitch, int StartSample, int EndSapublic static class AudioHarmony{- public static List<MidiNoteSpan> Detect(float[] mono, double sr, double minHz = 55, double maxHz = 2000)+ const int LowPitch = 28, HighPitch = 96; // ~E1 … C7+ const int Pitches = HighPitch - LowPitch + 1;+ const int MaxVoices = 6, Harmonics = 10;+ const double MaxPartialHz = 5000;+ const double StableCents = 20; // max mean |cents off| over a note's frames++ /// <param name="minNoteSec">Shortest note kept (shorter runs are flicker).</param>+ public static List<MidiNoteSpan> Detect(float[] mono, double sr, double minHz = 55, double maxHz = 2000,+ double minNoteSec = 0.1){var notes = new List<MidiNoteSpan>();- const int n = 8192, hop = 2048;+ int n = sr > 60000 ? 16384 : 8192, hop = n / 8;if (mono is null || mono.Length < n || sr <= 0) return notes;int bins = n / 2;+ int frames = (mono.Length - n) / hop + 1;var win = new double[n];for (int i = 0; i < n; i++) win[i] = 0.5 * (1.0 - Math.Cos(2.0 * Math.PI * i / (n - 1)));-- const int lowPitch = 28, highPitch = 96; // ~E1 … C7- const int maxVoices = 6;- int frames = (mono.Length - n) / hop + 1;- var act = new bool[frames][];- var lvl = new float[frames][];- int loBin = Math.Max(1, (int)(minHz * n / sr));- int hiBin = Math.Min(bins - 2, (int)(maxHz * n / sr));-var re = new double[n];var im = new double[n];var m = new double[bins];- for (int f = 0; f < frames; f++)++ void Spectrum(int f){- act[f] = new bool[highPitch - lowPitch + 1];- lvl[f] = new float[highPitch - lowPitch + 1];int off = f * hop;for (int i = 0; i < n; i++) { re[i] = mono[off + i] * win[i]; im[i] = 0; }AudioFft.Forward(re, im);+ for (int b = 0; b < bins; b++) m[b] = Math.Sqrt(re[b] * re[b] + im[b] * im[b]);+ }++ // ---- 1. Tuning --------------------------------------------------------------------+ double tuneCents = EstimateTuning(frames, bins, n, sr, m, Spectrum);++ // Per pitch / harmonic: the bin window searched for that partial (±~⅓ semitone, ≥ ±1 bin),+ // and Klapuri's harmonic weight (f0 + 27) / (h·f0 + 320).+ var lo = new int[Pitches, Harmonics];+ var hi = new int[Pitches, Harmonics];+ var w = new double[Pitches, Harmonics];+ // Expected partial position (fractional bin) where the window is clamped to ±1 bin and so+ // overlaps the semitone neighbours' (low partials); 0 elsewhere.+ var centre = new double[Pitches, Harmonics];+ var inRange = new bool[Pitches];+ for (int p = 0; p < Pitches; p++)+ {+ double f0 = 440.0 * Math.Pow(2, (LowPitch + p - 69) / 12.0 + tuneCents / 1200.0);+ inRange[p] = f0 >= minHz && f0 <= maxHz;+ for (int h = 0; h < Harmonics; h++)+ {+ double fh = f0 * (h + 1);+ double c = fh * n / sr, half = Math.Max(1.0, c * 0.02);+ centre[p, h] = c * 0.02 < 1.0 ? c : 0;+ lo[p, h] = (int)Math.Floor(c - half);+ hi[p, h] = (int)Math.Ceiling(c + half);+ if (fh > MaxPartialHz || hi[p, h] >= bins - 1 || lo[p, h] < 1) { lo[p, h] = -1; continue; }+ w[p, h] = (f0 + 27) / ((h + 1) * f0 + 320);+ }+ }+ // ---- 2. Per-frame estimate-and-cancel ---------------------------------------------+ var level = new float[frames][]; // salience of each chosen pitch (0 = not chosen)+ var frameTop = new float[frames]; // salience of the frame's first (strongest) pick+ var cents = new float[frames][]; // chosen pitch's measured offset from its centre+ var amp = new double[Harmonics];+ var taken = new bool[Pitches];+ for (int f = 0; f < frames; f++)+ {+ level[f] = new float[Pitches];+ cents[f] = new float[Pitches];+ Spectrum(f);double mmax = 0;- for (int b = 1; b < bins; b++) { m[b] = Math.Sqrt(re[b] * re[b] + im[b] * im[b]); if (m[b] > mmax) mmax = m[b]; }+ for (int b = 1; b < bins; b++) if (m[b] > mmax) mmax = m[b];if (mmax < 1e-6) continue;- double thr = mmax * 0.12;+ Array.Clear(taken);- // Spectral peaks (parabolic-interpolated frequency), strongest first.- var peaks = new List<(double hz, double amp)>();- for (int b = loBin; b <= hiBin; b++)+ double first = 0;+ for (int voice = 0; voice < MaxVoices; voice++){- if (m[b] > thr && m[b] >= m[b - 1] && m[b] > m[b + 1])+ int best = -1; double bestSal = 0;+ for (int p = 0; p < Pitches; p++){- double a0 = m[b - 1], a1 = m[b], a2 = m[b + 1];- double denom = a0 - 2 * a1 + a2;- double delta = Math.Abs(denom) > 1e-12 ? 0.5 * (a0 - a2) / denom : 0;- peaks.Add(((b + delta) * sr / n, a1));+ if (!inRange[p] || taken[p] || lo[p, 0] < 0) continue;+ double s = 0;+ for (int h = 0; h < Harmonics && lo[p, h] >= 0; h++) s += w[p, h] * Partial(m, lo[p, h], hi[p, h], centre[p, h]);+ if (s > bestSal) { bestSal = s; best = p; }}- }- peaks.Sort((x, y) => y.amp.CompareTo(x.amp));+ if (best < 0 || bestSal < 0.08 * first) break;- // Map peaks to pitches, skipping ones that look like a harmonic of an already-chosen- // (stronger, lower) pitch, and dedupe by pitch. Keep up to maxVoices.- var chosen = new List<(int pitch, double hz, double amp)>();- foreach (var pk in peaks)- {- bool harmonic = false;- foreach (var c in chosen)- for (int h = 2; h <= 6; h++)- if (Math.Abs(pk.hz - c.hz * h) < c.hz * h * 0.03) { harmonic = true; break; }- if (harmonic) continue;- int pitch = (int)Math.Round(69 + 12 * Math.Log2(pk.hz / 440.0));- if (pitch < lowPitch || pitch > highPitch) continue;- bool dup = false;- foreach (var c in chosen) if (c.pitch == pitch) { dup = true; break; }- if (dup) continue;- chosen.Add((pitch, pk.hz, pk.amp));- if (chosen.Count >= maxVoices) break;+ // A real note has energy at its fundamental; otherwise it's a missing-fundamental+ // ghost assembled from other notes' partials — drop the candidate, keep looking.+ taken[best] = true;+ if (Peak(m, lo[best, 0], hi[best, 0]) < 0.05 * mmax) { voice--; continue; }++ if (first == 0) first = bestSal;+ level[f][best] = (float)bestSal;+ cents[f][best] = (float)CentsOff(m, best, lo, hi, n, sr, tuneCents);++ // Cancel its partials. Spectral smoothing (min with the local mean) leaves most of a+ // partial that coincides with another note's, so that note is still found.+ int hc = 0;+ for (; hc < Harmonics && lo[best, hc] >= 0; hc++) amp[hc] = Peak(m, lo[best, hc], hi[best, hc]);+ for (int h = 0; h < hc; h++)+ {+ double a = amp[h];+ if (a <= 0) continue;+ double take = a;+ if (h > 0) { double mean = (amp[h - 1] + a + (h + 1 < hc ? amp[h + 1] : a)) / 3; take = Math.Min(a, mean); }+ double keep = 1 - take / a;+ for (int b = lo[best, h]; b <= hi[best, h]; b++) m[b] *= keep;+ }}- foreach (var c in chosen) { act[f][c.pitch - lowPitch] = true; lvl[f][c.pitch - lowPitch] = (float)(c.amp / mmax); }+ frameTop[f] = (float)first;}- // Per-pitch temporal segmentation: runs of active frames (bridging 1-frame gaps), min 2 frames.- const int minFrames = 2, gapMerge = 1;- for (int p = 0; p <= highPitch - lowPitch; p++)+ // ---- 3. Hysteresis segmentation ---------------------------------------------------+ float gmax = 0;+ foreach (var t in frameTop) gmax = Math.Max(gmax, t);+ if (gmax <= 0) return notes;++ double frameSec = hop / sr;+ int minFrames = Math.Max(2, (int)Math.Round(minNoteSec / frameSec));+ int gapFrames = Math.Max(1, (int)Math.Round(0.06 / frameSec));+ var onsets = StrongOnsets(mono, sr);+ var weak = new bool[frames];+ var strong = new bool[frames];+ var on = new bool[frames];+ var runs = new List<(int s, int e)>();++ for (int p = 0; p < Pitches; p++){- int f = 0;- while (f < frames)+ bool any = false;+ for (int f = 0; f < frames; f++){- if (!act[f][p]) { f++; continue; }- int start = f, end = f, gap = 0; float peak = 0;- while (f < frames)+ float l = level[f][p];+ weak[f] = l > 0 && l >= 0.01f * gmax;+ strong[f] = l >= 0.2f * frameTop[f] && l >= 0.04f * gmax;+ any |= strong[f];+ }+ if (!any) continue;+ MedianBool(weak, on, 2);++ runs.Clear();+ int fi = 0;+ while (fi < frames)+ {+ if (!on[fi]) { fi++; continue; }+ int s = fi, e = fi, gap = 0;+ for (fi++; fi < frames; fi++){- if (act[f][p]) { end = f; peak = Math.Max(peak, lvl[f][p]); gap = 0; f++; }- else { gap++; if (gap > gapMerge) break; f++; }+ if (on[fi]) { e = fi; gap = 0; }+ else if (++gap > gapFrames) break;}- if (end - start + 1 >= minFrames)- notes.Add(new MidiNoteSpan(lowPitch + p, start * hop, (end + 1) * hop, Math.Clamp(0.3f + peak * 0.7f, 0.3f, 1f)));+ bool hasStrong = false;+ for (int k = s; k <= e && !hasStrong; k++) hasStrong = strong[k];+ if (hasStrong && e - s + 1 >= minFrames && InTune(level, cents, p, s, e)) runs.Add((s, e));}++ foreach (var (s, e) in runs) EmitRun(notes, level, onsets, LowPitch + p, p, s, e, n, hop, minFrames, mono.Length, gmax);}+ notes.Sort((a, b) => a.StartSample != b.StartSample ? a.StartSample.CompareTo(b.StartSample) : a.Pitch.CompareTo(b.Pitch));return notes;}++ // ---- 4. Onset split + snap, then emit -------------------------------------------------+ static void EmitRun(List<MidiNoteSpan> notes, float[][] level, List<int> onsets, int pitch, int p,+ int s, int e, int n, int hop, int minFrames, int length, float gmax)+ {+ // A frame's centre is f·hop + n/2; an onset is felt ~half a window either side of it.+ int reach = n / (2 * hop);+ var cuts = new List<int>(); // sample positions where a re-strike begins+ foreach (int o in onsets)+ {+ int fo = (int)Math.Round((o - n / 2.0) / hop);+ if (fo - reach < s || fo + reach > e) continue;+ if (fo - (cuts.Count > 0 ? FrameOf(cuts[^1], n, hop) : s) < minFrames || e - fo + 1 < minFrames) continue;+ float before = level[fo - reach][p], after = level[fo + reach][p];+ if (after > 1.5f * before) cuts.Add(o);+ }++ int start = SnapStart(s, onsets, n, hop);+ int end = Math.Min(length, e * hop + n / 2 + hop / 2);+ foreach (int c in cuts)+ {+ if (c > start) notes.Add(Span(pitch, start, c, level, p, FrameOf(start, n, hop), FrameOf(c, n, hop), gmax));+ start = c;+ }+ if (end > start) notes.Add(Span(pitch, start, end, level, p, FrameOf(start, n, hop), e, gmax));+ }++ static MidiNoteSpan Span(int pitch, int start, int end, float[][] level, int p, int fs, int fe, float gmax)+ {+ float peak = 0;+ for (int f = Math.Max(0, fs); f <= Math.Min(level.Length - 1, fe); f++) peak = Math.Max(peak, level[f][p]);+ float v = 0.25f + 0.75f * MathF.Sqrt(peak / gmax);+ return new MidiNoteSpan(pitch, start, end, Math.Clamp(v, 0.25f, 1f));+ }++ // Onsets whose energy over the next 60 ms is ≥ 2× (+3 dB) that of the previous 60 ms.+ static List<int> StrongOnsets(float[] mono, double sr)+ {+ var all = AudioOnsets.Detect(mono, sr);+ var keep = new List<int>(all.Count);+ int w = Math.Max(1, (int)(sr * 0.060));+ foreach (int o in all)+ {+ if (o < w || o + w > mono.Length) continue;+ double before = 0, after = 0;+ for (int i = o - w; i < o; i++) before += (double)mono[i] * mono[i];+ for (int i = o; i < o + w; i++) after += (double)mono[i] * mono[i];+ if (after >= 2 * before) keep.Add(o);+ }+ return keep;+ }++ static int FrameOf(int sample, int n, int hop) => Math.Max(0, (int)Math.Round((sample - n / 2.0) / hop));++ // Estimated start = a little before the first detected frame's centre; snapped to the nearest+ // onset within half a window, since the window smears the attack by that much.+ static int SnapStart(int s, List<int> onsets, int n, int hop)+ {+ if (s == 0) return 0;+ int est = s * hop + n / 2 - hop / 2;+ int best = est, bestD = n / 2 + 1;+ foreach (int o in onsets)+ {+ int d = Math.Abs(o - est);+ if (d < bestD) { bestD = d; best = o; }+ }+ return Math.Max(0, best);+ }++ // Weighted circular mean of spectral peaks' deviation from the 12-TET grid, in cents (−50..50).+ static double EstimateTuning(int frames, int bins, int n, double sr, double[] m, Action<int> spectrum)+ {+ int loBin = Math.Max(2, (int)(100.0 * n / sr)), hiBin = Math.Min(bins - 2, (int)(4000.0 * n / sr));+ int step = Math.Max(1, frames / 200);+ double sx = 0, sy = 0;+ for (int f = 0; f < frames; f += step)+ {+ spectrum(f);+ double mmax = 0;+ for (int b = loBin; b <= hiBin; b++) if (m[b] > mmax) mmax = m[b];+ if (mmax < 1e-6) continue;+ double thr = mmax * 0.1;+ for (int b = loBin; b <= hiBin; b++)+ {+ if (m[b] <= thr || m[b] < m[b - 1] || m[b] <= m[b + 1]) continue;+ double a0 = m[b - 1], a1 = m[b], a2 = m[b + 1];+ double denom = a0 - 2 * a1 + a2;+ double delta = Math.Abs(denom) > 1e-12 ? 0.5 * (a0 - a2) / denom : 0;+ double cents = 1200 * Math.Log2((b + delta) * sr / n / 440.0);+ double ang = 2 * Math.PI * cents / 100.0;+ sx += a1 * Math.Cos(ang); sy += a1 * Math.Sin(ang);+ }+ }+ if (sx == 0 && sy == 0) return 0;+ return Math.Atan2(sy, sx) / (2 * Math.PI) * 100.0;+ }++ static bool InTune(float[][] level, float[][] cents, int p, int s, int e)+ {+ double sum = 0; int k = 0;+ for (int f = s; f <= e; f++) if (level[f][p] > 0) { sum += Math.Abs(cents[f][p]); k++; }+ return k > 0 && sum / k <= StableCents;+ }++ // How far (cents) the chosen pitch's strongest low partial sits from the pitch's tuned centre.+ // Uses the loudest of harmonics 1–4 (parabolic-interpolated) so low notes, whose fundamental+ // spans few bins, are measured on a better-resolved partial.+ static double CentsOff(double[] m, int p, int[,] lo, int[,] hi, int n, double sr, double tuneCents)+ {+ int bh = -1, bb = 0; double ba = 0;+ for (int h = 0; h < 4 && lo[p, h] >= 0; h++)+ for (int b = lo[p, h]; b <= hi[p, h]; b++)+ if (m[b] > ba) { ba = m[b]; bb = b; bh = h; }+ if (bh < 0) return 0;+ double a0 = m[bb - 1], a1 = m[bb], a2 = m[bb + 1];+ double denom = a0 - 2 * a1 + a2;+ double delta = Math.Abs(denom) > 1e-12 ? 0.5 * (a0 - a2) / denom : 0;+ double hz = (bb + delta) * sr / n / (bh + 1);+ double centre = 440.0 * Math.Pow(2, (LowPitch + p - 69) / 12.0 + tuneCents / 1200.0);+ return 1200 * Math.Log2(hz / centre);+ }++ // A partial's amplitude, discounted by how far its interpolated peak sits from where it should+ // be (full at 0 cents, none at ≥ 50). Low notes' semitone neighbours share bins; this keeps the+ // one whose partials actually line up ahead.+ static double Partial(double[] m, int lo, int hi, double expected)+ {+ if (expected <= 0) return Peak(m, lo, hi);+ int bb = lo;+ for (int b = lo + 1; b <= hi; b++) if (m[b] > m[bb]) bb = b;+ double a1 = m[bb];+ if (a1 <= 0) return 0;+ double a0 = m[bb - 1], a2 = m[bb + 1];+ double denom = a0 - 2 * a1 + a2;+ double delta = Math.Abs(denom) > 1e-12 ? 0.5 * (a0 - a2) / denom : 0;+ double off = Math.Abs(1200 * Math.Log2((bb + delta) / expected));+ return a1 * Math.Max(0, 1 - off / 50);+ }++ static double Peak(double[] m, int lo, int hi)+ {+ double v = 0;+ for (int b = lo; b <= hi; b++) if (m[b] > v) v = m[b];+ return v;+ }++ static void MedianBool(bool[] src, bool[] dst, int r)+ {+ for (int i = 0; i < src.Length; i++)+ {+ int cnt = 0, tot = 0;+ for (int k = Math.Max(0, i - r); k <= Math.Min(src.Length - 1, i + r); k++) { tot++; if (src[k]) cnt++; }+ dst[i] = cnt * 2 > tot;+ }+ }}diff --git a/tests/Nota.SmokeTest/Program.cs b/tests/Nota.SmokeTest/Program.csindex 8aa73f2e..b3324580 100644--- a/tests/Nota.SmokeTest/Program.cs+++ b/tests/Nota.SmokeTest/Program.cs@@ -12924,6 +12924,33 @@ Console.WriteLine("-- MCP tools --");var pitches = new HashSet<int>(); foreach (var s in spans) pitches.Add(s.Pitch);Check(pitches.Contains(60) && pitches.Contains(64) && pitches.Contains(67),$"harmony detects C-major triad (pitches: {string.Join(",", pitches)})");++ // Harmony on harmonic-rich tones: 8-partial saws (C2 + C4/E4/G4), 45 cents sharp with a+ // ±15-cent 5 Hz vibrato, struck three times (decaying). Expect exactly the four pitches —+ // no overtone ghosts, no semitone flips — each split into three re-strikes.+ float[] Saws(double sec, int[] midi, double[] strikes)+ {+ var x = new float[(int)(sec * psr)];+ var ph = new double[midi.Length];+ for (int i = 0; i < x.Length; i++)+ {+ double t = (double)i / psr, last = 0, v = 0;+ foreach (var st in strikes) if (t >= st) last = st;+ for (int k = 0; k < midi.Length; k++)+ {+ ph[k] += 2 * Math.PI * 440 * Math.Pow(2, (midi[k] - 69) / 12.0 + (45 + 15 * Math.Sin(2 * Math.PI * 5 * t)) / 1200.0) / psr;+ for (int h = 1; h <= 8; h++) v += Math.Sin(ph[k] * h) / h;+ }+ x[i] = (float)(v * Math.Exp(-(t - last) * 1.5) * 0.15);+ }+ return x;+ }+ var hs = Nota.Infrastructure.AudioHarmony.Detect(Saws(3, new[] { 36, 60, 64, 67 }, new[] { 0.0, 1.0, 2.0 }), psr, minNoteSec: 0.25);+ var hp = new SortedSet<int>(); foreach (var s in hs) hp.Add(s.Pitch);+ Check(hp.SetEquals(new[] { 36, 60, 64, 67 }), $"harmony: saw chord, no overtone/detune ghosts (pitches: {string.Join(",", hp)})");+ bool threeEach = true;+ foreach (int p in hp) { int c = 0; foreach (var s in hs) if (s.Pitch == p) c++; threeEach &= c == 3; }+ Check(threeEach && hs.Count == 12, $"harmony: re-struck chord splits into 3 notes per pitch (got {hs.Count})");}finally { try { if (File.Exists(clickWav)) File.Delete(clickWav); } catch { } }}