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
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.
//
// Racks redesign (specs/040-racks-redesign): what the reworked rack engine must keep and add.
// - v8 compatibility: rack blobs saved before the change (Fixtures/racks-v8) load into the
// same settings, and the Audio Effect Rack renders the same samples (FR-029, SC-004).
// The Instrument Rack is compared by its settings, not its audio: the built-in synths
// free-run their oscillators, so two renders of the same notes never match sample for sample.
// - mapping rules, the selector as a macro target, the key-parameter table.
// Fixtures are written once, on the engine from before the change:
// `dotnet run --project tests/Nota.SmokeTest -- --rack-fixtures`.
// Also runnable alone: `dotnet run --project tests/Nota.SmokeTest -- --racks`.
using System.Globalization;
using System.Text;
using Nota.Application;
using Nota.Infrastructure;
using Nota.Presentation;
namespace Nota.SmokeTest;
internal static class RackRedesignTests
{
private const int Rate = 48000, Block = 512, EffectFrames = 24 * Block; // ~0.26 s of audio
private const int Synth = 0, Physical = 2, Volt = 6, Pentad = 14; // instrument kinds
private const int Eq3 = 16, Reverb = 2, Utility = 4, EffectRack = 5; // device kinds
private const int RackControl = -3, Selector = 0; // NOTA_RACK_CONTROL target
/// <summary>The smoke-test sections, in run order.</summary>
public static IEnumerable<(string title, Func<IEnumerable<(bool ok, string label)>> run)> Sections()
{
yield return ("racks: v8 compatibility", V8Compatibility);
yield return ("racks: instrument mono/glide", MonoGlide);
yield return ("racks: duplicate chain", DuplicateChain);
yield return ("racks: edits keep playing state", EditsKeepState);
yield return ("racks: mapping rules", MappingRules);
yield return ("racks: macro curves and ranges", CurvesAndRanges);
yield return ("racks: audio rack select", AudioSelect);
yield return ("racks: key params table", KeyParamsTable);
}
// ---- fixtures -------------------------------------------------------------------
private static string FixtureDir()
{
var d = new DirectoryInfo(AppContext.BaseDirectory);
while (d is not null && !File.Exists(Path.Combine(d.FullName, "Nota.sln"))) d = d.Parent;
if (d is null) throw new InvalidOperationException("repo root not found");
return Path.Combine(d.FullName, "tests", "Nota.SmokeTest", "Fixtures", "racks-v8");
}
/// <summary>Writes the v8 fixtures. Run on the engine from before the racks redesign only.</summary>
public static void WriteFixtures()
{
string dir = FixtureDir();
Directory.CreateDirectory(dir);
using (var e = new NotaEngine())
{
int t = BuildInstrumentRack(e);
File.WriteAllBytes(Path.Combine(dir, "instrument.bin"), e.GetPluginState(t, -1));
File.WriteAllText(Path.Combine(dir, "instrument.txt"), DumpInstrumentRack(e, t));
}
string wav = ToneFile();
using (var e = new NotaEngine())
{
var (t, d) = BuildEffectRack(e, wav);
File.WriteAllBytes(Path.Combine(dir, "effect.bin"), e.GetPluginState(t, d));
File.WriteAllText(Path.Combine(dir, "effect.txt"), DumpEffectRack(e, t, d));
File.WriteAllBytes(Path.Combine(dir, "effect.f32"), FloatsToBytes(RenderEffect(e)));
}
Console.WriteLine($"rack fixtures written to {dir}");
}
// Three chains with zones, devices, names and mix; four macros, one of them with two
// targets on different curves (the "Mixed" case), one inverted range; glide set.
private static int BuildInstrumentRack(NotaEngine e)
{
int t = e.AddInstrumentRackTrack();
while (e.RackChainCount(t) > 0) e.RackRemoveChain(t, 0);
int a = e.RackAddChain(t, Synth), b = e.RackAddChain(t, Physical), c = e.RackAddChain(t, Volt);
e.RackSetChainZone(t, a, 0, 70, 0, 127);
e.RackSetChainZone(t, b, 58, 127, 0, 127);
e.RackSetChainZone(t, c, 36, 84, 96, 127);
e.RackSetChainName(t, a, "Low");
e.RackSetChainName(t, c, "Hard hits");
e.RackSetChainGain(t, a, 0.8f);
e.RackSetChainPan(t, b, 0.2f);
e.RackSetChainPan(t, c, -0.15f);
e.RackSetChainMute(t, b, true);
e.RackSetChainSolo(t, c, true);
e.RackChainInstrumentParamSet(t, a, 0, 0.62f);
int eq = e.RackAddChainDevice(t, a, Eq3);
e.RackChainDeviceParamSet(t, a, eq, 0, 3.5f);
e.RackAddChainDevice(t, c, Reverb);
e.RackSetMacroName(t, 0, "Bright");
e.RackSetMacroName(t, 1, "Space");
e.RackMacroSet(t, 0, 0.6f);
e.RackMacroSet(t, 1, 0.35f);
e.RackMacroSet(t, 3, 0.25f);
int m0 = e.RackAddMacroMapping(t, 0, a, -1, 0, 0.3f, 0.9f);
int m1 = e.RackAddMacroMapping(t, 0, b, -1, 1, 0.2f, 0.8f);
e.RackSetMappingCurve(t, m0, 0); // Lin
e.RackSetMappingCurve(t, m1, 2); // Log: macro 1 is "Mixed"
int m2 = e.RackAddMacroMapping(t, 1, c, 0, 0, 0.6f, 0.0f); // inverted
e.RackSetMappingCurve(t, m2, 1); // Exp
int m3 = e.RackAddMacroMapping(t, 3, a, 0, 0, -6f, 6f);
e.RackSetMappingCurve(t, m3, 3); // S
e.RackSetVolume(t, 0.8f);
e.RackSetGlide(t, 0.3f);
return t;
}
// Select mode, two deterministic chains (Utility gain, EQ-3), select ranges that overlap
// around the selector, dry/wet, latency compensation, one macro target.
private static (int t, int d) BuildEffectRack(NotaEngine e, string wav)
{
int t = e.AddAudioTrack();
e.AddAudioClip(t, wav, 0);
int d = e.AddBuiltinDevice(t, EffectRack);
while (e.RackDevChainCount(t, d) > 0) e.RackDevRemoveChain(t, d, 0);
int a = e.RackDevAddChain(t, d, -1), b = e.RackDevAddChain(t, d, -1);
int u = e.RackDevAddChainDevice(t, d, a, Utility);
e.RackDevChainDeviceParamSet(t, d, a, u, 0, -6f);
int q = e.RackDevAddChainDevice(t, d, b, Eq3);
e.RackDevChainDeviceParamSet(t, d, b, q, 0, 6f);
e.RackDevSetChainZone(t, d, a, 0, 63);
e.RackDevSetChainZone(t, d, b, 30, 127);
e.RackDevSetChainGain(t, d, b, 0.7f);
e.RackDevSetChainPan(t, d, a, -0.3f);
e.RackDevSetMode(t, d, 2);
e.RackDevSetChainSelect(t, d, 0.3f);
e.RackDevSetDryWet(t, d, 0.7f);
e.RackDevSetPdc(t, d, true);
e.RackDevSetVolume(t, d, 0.9f);
e.RackDevSetMacroName(t, d, 0, "Tone");
e.RackDevMacroSet(t, d, 0, 0.4f);
int m = e.RackDevAddMacroMapping(t, d, 0, b, q, 2, -12f, 12f);
e.RackDevSetMappingCurve(t, d, m, 3);
return (t, d);
}
private static string ToneFile()
{
string path = Path.Combine(Path.GetTempPath(), "nota-rack-fixture-tones.wav");
WavWriter.WriteTones(path, 2.0, new[] { 110.0, 880.0, 5000.0 }, 0.2, Rate);
return path;
}
private static float[] RenderEffect(NotaEngine e)
{
var all = new float[EffectFrames * 2];
var buf = new float[Block * 2];
e.Seek(0);
e.Play();
for (int done = 0; done < EffectFrames; done += Block)
{
int n = Math.Min(Block, EffectFrames - done);
e.RenderOffline(buf, n, Rate);
Array.Copy(buf, 0, all, done * 2, n * 2);
}
e.Stop();
return all;
}
// ---- settings dumps (the comparison key for v8 loads) ----------------------------
private static string F(float v) => v.ToString("0.#####", CultureInfo.InvariantCulture);
public static string DumpInstrumentRack(NotaEngine e, int t)
{
var s = new StringBuilder();
int n = e.RackChainCount(t);
s.Append($"chains {n}").Append('\n');
for (int c = 0; c < n; c++)
{
e.RackChainZone(t, c, out int kl, out int kh, out int vl, out int vh);
s.Append($"chain {c} name='{e.RackChainName(t, c)}' inst={e.RackChainInstrumentKind(t, c)} " +
$"gain={F(e.RackChainGain(t, c))} pan={F(e.RackChainPan(t, c))} mute={e.RackChainMute(t, c)} " +
$"solo={e.RackChainSolo(t, c)} keys={kl}-{kh} vel={vl}-{vh}").Append('\n');
int ip = Math.Min(8, e.RackChainInstrumentParamCount(t, c));
for (int p = 0; p < ip; p++) s.Append($" inst p{p}={F(e.RackChainInstrumentParamGet(t, c, p))}").Append('\n');
for (int d = 0; d < e.RackChainDeviceCount(t, c); d++)
{
s.Append($" dev {d} kind={e.RackChainDeviceBuiltinKind(t, c, d)}");
for (int p = 0; p < e.RackChainDeviceParamCount(t, c, d); p++)
if (e.RackChainDeviceParamName(t, c, d, p) != "View") // the card size (#531) is editor state, not a setting
s.Append(' ').Append(F(e.RackChainDeviceParamGet(t, c, d, p)));
s.Append('\n');
}
}
for (int m = 0; m < 8; m++) s.Append($"macro {m} '{e.RackMacroName(t, m)}' {F(e.RackMacroGet(t, m))}").Append('\n');
for (int i = 0; i < e.RackMappingCount(t); i++)
if (e.RackTryGetMapping(t, i, out var mm))
s.Append($"map {i} m{mm.Macro} c{mm.Chain} d{mm.DeviceIndex} p{mm.ParamIndex} {F(mm.RangeMin)}..{F(mm.RangeMax)} curve={e.RackMappingCurve(t, i)}").Append('\n');
s.Append($"volume {F(e.RackVolume(t))} glide {F(e.RackGlide(t))}").Append('\n');
return s.ToString();
}
public static string DumpEffectRack(NotaEngine e, int t, int d)
{
var s = new StringBuilder();
int n = e.RackDevChainCount(t, d);
s.Append($"chains {n}").Append('\n');
for (int c = 0; c < n; c++)
{
e.RackDevChainZone(t, d, c, out int lo, out int hi);
s.Append($"chain {c} gain={F(e.RackDevChainGain(t, d, c))} pan={F(e.RackDevChainPan(t, d, c))} " +
$"mute={e.RackDevChainMute(t, d, c)} solo={e.RackDevChainSolo(t, d, c)} select={lo}-{hi}").Append('\n');
for (int k = 0; k < e.RackDevChainDeviceCount(t, d, c); k++)
{
s.Append($" dev {k} kind={e.RackDevChainDeviceBuiltinKind(t, d, c, k)}");
for (int p = 0; p < e.RackDevChainDeviceParamCount(t, d, c, k); p++)
if (e.RackDevChainDeviceParamName(t, d, c, k, p) != "View") // the card size (#531) is editor state, not a setting
s.Append(' ').Append(F(e.RackDevChainDeviceParamGet(t, d, c, k, p)));
s.Append('\n');
}
}
for (int m = 0; m < 8; m++) s.Append($"macro {m} '{e.RackDevMacroName(t, d, m)}' {F(e.RackDevMacroGet(t, d, m))}").Append('\n');
for (int i = 0; i < e.RackDevMappingCount(t, d); i++)
if (e.RackDevTryGetMapping(t, d, i, out var mm))
s.Append($"map {i} m{mm.Macro} c{mm.Chain} d{mm.DeviceIndex} p{mm.ParamIndex} {F(mm.RangeMin)}..{F(mm.RangeMax)} curve={e.RackDevMappingCurve(t, d, i)}").Append('\n');
s.Append($"mode {e.RackDevMode(t, d)} select {F(e.RackDevChainSelect(t, d))} drywet {F(e.RackDevDryWet(t, d))} " +
$"pdc {e.RackDevPdc(t, d)} volume {F(e.RackDevVolume(t, d))}").Append('\n');
return s.ToString();
}
private static byte[] FloatsToBytes(float[] f)
{
var b = new byte[f.Length * 4];
Buffer.BlockCopy(f, 0, b, 0, b.Length);
return b;
}
private static float[] BytesToFloats(byte[] b)
{
var f = new float[b.Length / 4];
Buffer.BlockCopy(b, 0, f, 0, f.Length * 4);
return f;
}
// ---- sections -------------------------------------------------------------------
private static IEnumerable<(bool, string)> V8Compatibility()
{
string dir = FixtureDir();
if (!File.Exists(Path.Combine(dir, "instrument.bin")))
{
yield return (false, $"v8 fixtures missing in {dir}");
yield break;
}
using (var e = new NotaEngine())
{
int t = e.AddInstrumentRackTrack();
e.SetPluginState(t, -1, File.ReadAllBytes(Path.Combine(dir, "instrument.bin")));
string want = File.ReadAllText(Path.Combine(dir, "instrument.txt")).ReplaceLineEndings("\n"), got = DumpInstrumentRack(e, t);
yield return (got == want, "v8 Instrument Rack blob loads with every setting (zones, mix, macros, per-target curves, glide)"
+ (got == want ? "" : $"\n--- want\n{want}--- got\n{got}"));
}
using (var e = new NotaEngine())
{
int t = e.AddAudioTrack();
e.AddAudioClip(t, ToneFile(), 0);
int d = e.AddBuiltinDevice(t, EffectRack);
e.SetPluginState(t, d, File.ReadAllBytes(Path.Combine(dir, "effect.bin")));
string want = File.ReadAllText(Path.Combine(dir, "effect.txt")).ReplaceLineEndings("\n"), got = DumpEffectRack(e, t, d);
yield return (got == want, "v8 Audio Effect Rack blob loads with every setting (Select mode, select ranges, dry/wet, PDC)"
+ (got == want ? "" : $"\n--- want\n{want}--- got\n{got}"));
var refAudio = BytesToFloats(File.ReadAllBytes(Path.Combine(dir, "effect.f32")));
var audio = RenderEffect(e);
double maxDiff = 0, peak = 0;
for (int i = 0; i < Math.Min(audio.Length, refAudio.Length); i++)
{
maxDiff = Math.Max(maxDiff, Math.Abs(audio[i] - refAudio[i]));
peak = Math.Max(peak, Math.Abs(refAudio[i]));
}
yield return (audio.Length == refAudio.Length && peak > 0.01 && maxDiff < 1e-5,
string.Create(CultureInfo.InvariantCulture, $"v8 Audio Effect Rack renders the same samples as before (max diff {maxDiff:E1}, peak {peak:F3})"));
}
}
private static bool Near(float a, float b) => Math.Abs(a - b) < 1e-4f;
private static IEnumerable<(bool, string)> MappingRules()
{
using var e = new NotaEngine();
// One macro per target: mapping a mapped parameter again moves it, range kept.
int t = e.AddInstrumentRackTrack();
while (e.RackChainCount(t) > 0) e.RackRemoveChain(t, 0);
int a = e.RackAddChain(t, Synth), b = e.RackAddChain(t, Physical);
int m = e.RackAddMacroMapping(t, 0, a, -1, 5, 0.2f, 0.7f);
int again = e.RackAddMacroMapping(t, 3, a, -1, 5, 0f, 1f);
e.RackTryGetMapping(t, again, out var moved);
yield return (again == m && e.RackMappingCount(t) == 1 && moved.Macro == 3 && Near(moved.RangeMin, 0.2f) && Near(moved.RangeMax, 0.7f),
$"mapping a mapped parameter moves it to the new macro and keeps its range ({e.RackMappingCount(t)} mapping, macro {moved.Macro})");
yield return (e.Undo() && e.RackTryGetMapping(t, 0, out var back) && back.Macro == 0, "moving a mapping to another macro is one undo step");
// Deleting a chain drops its mappings and re-indexes the rest.
e.RackAddMacroMapping(t, 1, b, -1, 0, 0f, 1f);
e.RackRemoveChain(t, a);
e.RackTryGetMapping(t, 0, out var left);
yield return (e.RackMappingCount(t) == 1 && left.Chain == 0 && left.Macro == 1,
$"deleting a chain drops its mappings and shifts the others ({e.RackMappingCount(t)} left, on chain {left.Chain})");
// The selector as a macro target (Audio Effect Rack): the chain argument is ignored.
int at = e.AddAudioTrack();
int d = e.AddBuiltinDevice(at, EffectRack);
while (e.RackDevChainCount(at, d) < 2) e.RackDevAddChain(at, d, -1);
int sm = e.RackDevAddMacroMapping(at, d, 1, 99, RackControl, Selector, 0.2f, 0.8f);
e.RackDevMacroSet(at, d, 1, 1f);
float hi = e.RackDevChainSelect(at, d);
e.RackDevMacroSet(at, d, 1, 0f);
float lo = e.RackDevChainSelect(at, d);
yield return (sm >= 0 && Near(hi, 0.8f) && Near(lo, 0.2f),
string.Create(CultureInfo.InvariantCulture, $"a macro mapped to the selector drives it over its range ({lo:0.##}–{hi:0.##})"));
e.RackDevRemoveChain(at, d, 0);
yield return (e.RackDevMappingCount(at, d) == 1, "the selector mapping survives deleting a chain");
// Mappings, the selector target and curves survive the blob (v9 round trip).
e.RackDevSetMappingCurve(at, d, sm, 2);
string before = DumpEffectRack(e, at, d);
int at2 = e.AddAudioTrack();
int d2 = e.AddBuiltinDevice(at2, EffectRack);
e.SetPluginState(at2, d2, e.GetPluginState(at, d));
string after = DumpEffectRack(e, at2, d2);
yield return (before == after, "a rack blob round-trips its selector mapping and curves" + (before == after ? "" : $"\n{before}\n{after}"));
// A MIDI effect without a state blob reports none and refuses an empty one.
int it = e.AddInstrumentTrack();
int arp = e.AddMidiEffect(it, 0);
yield return (e.MidiEffectGetState(it, arp).Length == 0 && !e.MidiEffectSetState(it, arp, Array.Empty<byte>(), true),
"a params-only MIDI effect has no state blob");
}
private static IEnumerable<(bool, string)> KeyParamsTable()
{
using var e = new NotaEngine();
int t = e.AddInstrumentRackTrack();
while (e.RackChainCount(t) > 0) e.RackRemoveChain(t, 0);
foreach (int k in RackKeyParams.Kinds(RackDeviceFamily.Instrument))
{
int c = e.RackAddChain(t, k);
if (c < 0 || e.RackChainInstrumentKind(t, c) != k) { yield return (false, $"instrument kind {k} can be a rack chain"); continue; }
int n = e.RackChainInstrumentParamCount(t, c);
yield return Check(RackDeviceFamily.Instrument, k, e.RackChainInstrumentName(t, c), n, p => e.RackChainInstrumentParamName(t, c, p));
}
int host = e.RackAddChain(t, Synth);
foreach (int k in RackKeyParams.Kinds(RackDeviceFamily.AudioEffect))
{
int d = e.RackAddChainDevice(t, host, k);
if (d < 0) { yield return (false, $"audio effect kind {k} can be a chain device"); continue; }
int n = e.RackChainDeviceParamCount(t, host, d);
yield return Check(RackDeviceFamily.AudioEffect, k, e.RackChainDeviceName(t, host, d), n, p => e.RackChainDeviceParamName(t, host, d, p));
}
int it = e.AddInstrumentTrack();
foreach (int k in RackKeyParams.Kinds(RackDeviceFamily.MidiEffect))
{
int m = e.AddMidiEffect(it, k);
if (m < 0) { yield return (false, $"MIDI effect kind {k} exists"); continue; }
int n = e.MidiEffectParamCount(it, m);
yield return Check(RackDeviceFamily.MidiEffect, k, e.MidiEffectName(it, m), n, p => e.MidiEffectParamName(it, m, p));
}
// A kind without an entry (the Sampler) falls back to its first parameters.
var fb = RackKeyParams.Resolve(RackDeviceFamily.Instrument, 1, 5, p => $"P{p}");
yield return (fb.SequenceEqual(new[] { 0, 1, 2 }), "a kind without key params shows its first three");
}
// Every curated name is found on the device, and the picks are distinct and in range.
private static (bool, string) Check(RackDeviceFamily f, int kind, string name, int count, Func<int, string> paramName)
{
var want = RackKeyParams.Names(f, kind);
var got = RackKeyParams.Resolve(f, kind, count, paramName);
var missing = want.Where(w => !Enumerable.Range(0, count).Any(p => string.Equals(paramName(p), w, StringComparison.OrdinalIgnoreCase))).ToList();
bool ok = want.Count is > 0 and <= RackKeyParams.Max && missing.Count == 0 && got.Length == Math.Min(RackKeyParams.Max, count)
&& got.Distinct().Count() == got.Length && got.All(p => p >= 0 && p < count);
return (ok, $"{name}: key params {string.Join(", ", got.Select(paramName))}" + (missing.Count > 0 ? $" (missing: {string.Join(", ", missing)})" : ""));
}
// ---- mono / glide (US1) -----------------------------------------------------------
private static float[] RenderFrames(NotaEngine e, int frames)
{
var all = new float[frames * 2];
var buf = new float[Block * 2];
for (int done = 0; done < frames; done += Block)
{
int n = Math.Min(Block, frames - done);
e.RenderOffline(buf, n, Rate);
Array.Copy(buf, 0, all, done * 2, n * 2);
}
return all;
}
// Fundamental of the left channel by autocorrelation: the shortest lag within 90 % of the
// best one (so a lag of two periods never wins), 60–600 Hz.
private static double Pitch(float[] st, int from, int len)
{
int lo = Rate / 600, hi = Rate / 60;
var x = new double[len];
for (int i = 0; i < len; i++) x[i] = st[(from + i) * 2];
var r = new double[hi + 1];
double best = 0;
for (int lag = lo; lag <= hi; lag++)
{
double sum = 0, e0 = 0, e1 = 0;
for (int i = 0; i + lag < len; i++) { sum += x[i] * x[i + lag]; e0 += x[i] * x[i]; e1 += x[i + lag] * x[i + lag]; }
r[lag] = e0 > 0 && e1 > 0 ? sum / Math.Sqrt(e0 * e1) : 0;
best = Math.Max(best, r[lag]);
}
for (int lag = lo; lag <= hi; lag++)
if (r[lag] >= 0.9 * best && (lag == lo || r[lag] >= r[lag - 1]) && (lag == hi || r[lag] >= r[lag + 1])) return (double)Rate / lag;
return 0;
}
private static double Hz(int note) => 440.0 * Math.Pow(2, (note - 69) / 12.0);
private static int Held(NotaEngine e, int t, int c, out string list)
{
var buf = new int[16];
int n = e.RackChainInstrumentHeldNotes(t, c, buf);
list = string.Join(",", buf.Take(Math.Max(0, n)));
return n;
}
private static int PentadRack(NotaEngine e)
{
int t = e.AddInstrumentRackTrack();
while (e.RackChainCount(t) > 0) e.RackRemoveChain(t, 0);
int c = e.RackAddChain(t, Pentad);
for (int p = 0; p < e.RackChainInstrumentParamCount(t, c); p++)
{
string n = e.RackChainInstrumentParamName(t, c, p);
if (n == "Amp-Env Release" || n == "Amp-Env Attack") e.RackChainInstrumentParamSet(t, c, p, 0f);
if (n == "Glide Time") e.RackChainInstrumentParamSet(t, c, p, 0f); // the rack glides, not Pentad
}
return t;
}
private static IEnumerable<(bool, string)> MonoGlide()
{
string F2(double v) => v.ToString("0.#", CultureInfo.InvariantCulture);
using (var e = new NotaEngine())
{
int t = PentadRack(e);
e.TrackNoteOn(t, 48, 0.8f);
e.TrackNoteOn(t, 60, 0.8f);
RenderFrames(e, Block * 4);
int poly = Held(e, t, 0, out var pl);
e.TrackNoteOff(t, 48); e.TrackNoteOff(t, 60);
RenderFrames(e, Block * 8);
yield return (poly == 2 && !e.RackMono(t), $"poly by default: both keys sound ({pl})");
e.RackSetMono(t, true);
e.TrackNoteOn(t, 48, 0.8f);
RenderFrames(e, Block * 4);
e.TrackNoteOn(t, 60, 0.8f);
RenderFrames(e, Block * 4);
int mono = Held(e, t, 0, out var ml);
yield return (e.RackMono(t) && mono == 1 && ml == "60", $"mono: the last key takes over ({ml})");
e.TrackNoteOff(t, 60);
RenderFrames(e, Block * 4);
int back = Held(e, t, 0, out var bl);
yield return (back == 1 && bl == "48", $"mono: releasing it returns to the key still held ({bl})");
e.TrackNoteOff(t, 48);
RenderFrames(e, Block * 8);
yield return (Held(e, t, 0, out _) == 0, "mono: no note left hanging");
}
using (var e = new NotaEngine())
{
int t = PentadRack(e);
e.RackSetMono(t, true);
e.RackSetGlide(t, 0.6f); // 300 ms
e.TrackNoteOn(t, 48, 0.8f);
RenderFrames(e, Rate / 4);
e.TrackNoteOn(t, 60, 0.8f);
var a = RenderFrames(e, Rate / 2);
double start = Pitch(a, 0, 2048), end = Pitch(a, Rate * 2 / 5, 2048);
yield return (start > 0 && start < Hz(60) * 0.8 && Math.Abs(end - Hz(60)) < Hz(60) * 0.04,
$"glide: the new note starts near the old pitch and arrives in time ({F2(start)} → {F2(end)} Hz, target {F2(Hz(60))})");
e.TrackNoteOff(t, 60); e.TrackNoteOff(t, 48);
RenderFrames(e, Block * 8);
e.RackSetGlide(t, 0f);
e.TrackNoteOn(t, 48, 0.8f);
RenderFrames(e, Rate / 4);
e.TrackNoteOn(t, 60, 0.8f);
var b = RenderFrames(e, Rate / 10);
double now = Pitch(b, 0, 2048);
yield return (Math.Abs(now - Hz(60)) < Hz(60) * 0.04, $"no glide at 0 ms: the new note is in tune at once ({F2(now)} Hz)");
}
}
private static IEnumerable<(bool, string)> DuplicateChain()
{
using var e = new NotaEngine();
int t = e.AddInstrumentRackTrack();
while (e.RackChainCount(t) > 0) e.RackRemoveChain(t, 0);
int a = e.RackAddChain(t, Synth), b = e.RackAddChain(t, Volt);
e.RackSetChainZone(t, a, 10, 70, 5, 120);
e.RackSetChainGain(t, a, 0.6f);
e.RackSetChainName(t, a, "Low");
e.RackChainInstrumentParamSet(t, a, 5, 0.3f);
int eq = e.RackAddChainDevice(t, a, Eq3);
e.RackChainDeviceParamSet(t, a, eq, 1, 0.37f);
e.RackAddMacroMapping(t, 0, a, -1, 5, 0.1f, 0.9f);
e.RackAddMacroMapping(t, 1, b, -1, 0, 0f, 1f);
int d = e.RackDuplicateChain(t, a);
e.RackChainZone(t, d, out int kl, out int kh, out int vl, out int vh);
bool same = d == 1 && e.RackChainCount(t) == 3 && e.RackChainInstrumentKind(t, d) == Synth && e.RackChainName(t, d) == "Low"
&& kl == 10 && kh == 70 && vl == 5 && vh == 120 && Near(e.RackChainGain(t, d), 0.6f)
&& e.RackChainDeviceCount(t, d) == 1 && Near(e.RackChainDeviceParamGet(t, d, 0, 1), e.RackChainDeviceParamGet(t, a, eq, 1))
&& e.RackChainInstrumentKind(t, 2) == Volt;
yield return (same, "duplicate chain: the copy sits after the original with its instrument, devices, zone and mix"
+ (same ? "" : $" [d={d} n={e.RackChainCount(t)} kind={e.RackChainInstrumentKind(t, d)} name='{e.RackChainName(t, d)}' zone={kl}-{kh}/{vl}-{vh} gain={e.RackChainGain(t, d)} devs={e.RackChainDeviceCount(t, d)} eq={(e.RackChainDeviceCount(t, d) > 0 ? e.RackChainDeviceParamGet(t, d, 0, 1) : -1)} k2={e.RackChainInstrumentKind(t, 2)}]"));
int onCopy = 0, onVolt = 0;
for (int i = 0; i < e.RackMappingCount(t); i++)
if (e.RackTryGetMapping(t, i, out var m)) { if (m.Chain == d && m.Macro == 0) onCopy++; if (m.Chain == 2 && m.Macro == 1) onVolt++; }
yield return (e.RackMappingCount(t) == 3 && onCopy == 1 && onVolt == 1, "duplicate chain: the copy gets the chain's mappings, later chains keep theirs");
yield return (e.Undo() && e.RackChainCount(t) == 2 && e.RackMappingCount(t) == 2, "duplicate chain is one undo step");
}
// ---- curves, inverted ranges (US2) -------------------------------------------------
private static IEnumerable<(bool, string)> CurvesAndRanges()
{
using var e = new NotaEngine();
int t = e.AddInstrumentRackTrack();
while (e.RackChainCount(t) > 0) e.RackRemoveChain(t, 0);
int c = e.RackAddChain(t, Synth);
int a = e.RackAddMacroMapping(t, 0, c, -1, 5, 0f, 1f);
int b = e.RackAddMacroMapping(t, 0, c, -1, 6, 0f, 1f);
e.RackSetMappingCurve(t, a, 1);
e.RackSetMappingCurve(t, b, 2);
yield return (e.RackMappingCurve(t, a) != e.RackMappingCurve(t, b), "one macro can hold different curves per target (the Mixed case)");
e.BeginUndoGroup();
e.RackSetMappingCurve(t, a, 3);
e.RackSetMappingCurve(t, b, 3);
e.EndUndoGroup();
yield return (e.RackMappingCurve(t, a) == 3 && e.RackMappingCurve(t, b) == 3, "picking a curve for the macro sets every target");
yield return (e.Undo() && e.RackMappingCurve(t, a) == 1 && e.RackMappingCurve(t, b) == 2, "picking a curve is one undo step");
// min above max inverts: the macro at its top drives the parameter to the range's min end.
int inv = e.RackAddMacroMapping(t, 1, c, -1, 7, 0.8f, 0.2f);
e.RackMacroSet(t, 1, 1f);
float top = e.RackChainInstrumentParamGet(t, c, 7);
e.RackMacroSet(t, 1, 0f);
float bottom = e.RackChainInstrumentParamGet(t, c, 7);
yield return (inv >= 0 && Near(top, 0.2f) && Near(bottom, 0.8f),
string.Create(CultureInfo.InvariantCulture, $"a range with min above max inverts the response ({bottom:0.##} → {top:0.##})"));
}
// ---- Audio Effect Rack select (US3) -----------------------------------------------
private static IEnumerable<(bool, string)> AudioSelect()
{
using var e = new NotaEngine();
int t = e.AddAudioTrack();
e.AddAudioClip(t, ToneFile(), 0);
int d = e.AddBuiltinDevice(t, EffectRack);
while (e.RackDevChainCount(t, d) > 0) e.RackDevRemoveChain(t, d, 0);
int quiet = e.RackDevAddChain(t, d, -1), loud = e.RackDevAddChain(t, d, -1);
int u = e.RackDevAddChainDevice(t, d, quiet, Utility);
for (int p = 0; p < e.RackDevChainDeviceParamCount(t, d, quiet, u); p++)
if (e.RackDevChainDeviceParamName(t, d, quiet, u, p) == "Mute") e.RackDevChainDeviceParamSet(t, d, quiet, u, p, 1f);
e.RackDevSetChainZone(t, d, quiet, 0, 63);
e.RackDevSetChainZone(t, d, loud, 64, 127);
e.RackDevSetMode(t, d, 2);
e.RackDevSetDryWet(t, d, 1f);
e.RackDevAddMacroMapping(t, d, 0, 0, RackControl, Selector, 0f, 1f);
float Level(float macro)
{
e.RackDevMacroSet(t, d, 0, macro);
e.Seek(0); e.Play();
var buf = RenderFrames(e, Rate / 4);
e.Stop();
double sum = 0;
for (int i = Rate / 8 * 2; i < buf.Length; i++) sum += buf[i] * buf[i];
return (float)Math.Sqrt(sum / (buf.Length - Rate / 8 * 2));
}
float low = Level(0f), high = Level(1f);
yield return (low < 1e-4f && high > 0.02f,
string.Create(CultureInfo.InvariantCulture, $"a macro on the selector switches which chain plays (rms {low:0.####} → {high:0.###})"));
int dup = e.RackDevDuplicateChain(t, d, loud);
e.RackDevChainZone(t, d, dup, out int lo, out int hi);
yield return (dup == 2 && e.RackDevChainCount(t, d) == 3 && lo == 64 && hi == 127, "duplicate chain on an Audio Effect Rack keeps its select range");
}
// ---- review follow-ups: undo restores driven values, mono survives edits and switches ----
private static IEnumerable<(bool, string)> EditsKeepState()
{
using (var e = new NotaEngine())
{
int t = e.AddInstrumentRackTrack();
while (e.RackChainCount(t) > 0) e.RackRemoveChain(t, 0);
int c = e.RackAddChain(t, Synth);
e.RackMacroSet(t, 0, 0f);
e.RackMacroSet(t, 3, 1f);
e.RackAddMacroMapping(t, 0, c, -1, 5, 0.2f, 0.7f);
float before = e.RackChainInstrumentParamGet(t, c, 5);
e.RackAddMacroMapping(t, 3, c, -1, 5, 0.2f, 0.7f); // moves it to macro 3, at its top
float moved = e.RackChainInstrumentParamGet(t, c, 5);
e.Undo();
float undone = e.RackChainInstrumentParamGet(t, c, 5);
e.Redo();
float redone = e.RackChainInstrumentParamGet(t, c, 5);
yield return (Near(before, 0.2f) && Near(moved, 0.7f) && Near(undone, 0.2f) && Near(redone, 0.7f),
string.Create(CultureInfo.InvariantCulture, $"undo and redo restore what a macro drives ({before:0.##} → {moved:0.##}, undo {undone:0.##}, redo {redone:0.##})"));
}
using (var e = new NotaEngine())
{
int t = PentadRack(e);
e.RackSetMono(t, true);
e.TrackNoteOn(t, 48, 0.8f);
RenderFrames(e, Block * 4);
e.RackSetMacroName(t, 0, "Edited"); // an undoable edit swaps the rack object
e.TrackNoteOn(t, 60, 0.8f);
RenderFrames(e, Block * 4);
Held(e, t, 0, out var afterEdit);
e.TrackNoteOff(t, 60);
RenderFrames(e, Block * 4);
Held(e, t, 0, out var back);
e.TrackNoteOff(t, 48);
RenderFrames(e, Block * 8);
yield return (afterEdit == "60" && back == "48" && Held(e, t, 0, out _) == 0,
$"mono plays on through a rack edit (new key {afterEdit}, back to {back})");
}
using (var e = new NotaEngine())
{
int t = PentadRack(e);
foreach (int p in new[] { 48, 52, 55 }) e.TrackNoteOn(t, p, 0.8f);
RenderFrames(e, Block * 4);
e.RackSetMono(t, true);
RenderFrames(e, Block * 4);
Held(e, t, 0, out var onSwitch);
e.TrackNoteOn(t, 60, 0.8f);
RenderFrames(e, Block * 4);
Held(e, t, 0, out var next);
foreach (int p in new[] { 48, 52, 55, 60 }) e.TrackNoteOff(t, p);
RenderFrames(e, Block * 8);
yield return (onSwitch == "55" && next == "60" && Held(e, t, 0, out _) == 0,
$"switching mono on with a chord held keeps only the last key ({onSwitch}, then {next})");
}
}
}