diff --git a/README.md b/README.md index ff6479b..85bf1d8 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ dotnet test - `FaustEmitter.Emit(patch)` emits Faust source. - `FaustCompiler.ValidateAsync(source)` compile-checks with Faust when present. - `FaustCompiler.CompileAsync(source, options)` writes generated backend code. +- `BuiltInScripts` carries the current Rust-side classic SFXR, 808, FM bell, + and wobble bass golf scripts so the migration has executable baggage checks. Faust `2.85.5` supports `-lang csharp`, so the intended hot path is: @@ -33,7 +35,9 @@ await FaustCompiler.CompileAsync( ## Status -This is the first scaffolded slice, not the whole cathedral. It covers the graph -surface needed by the current wobble/FM/formant scripts and validates generated -Faust when the compiler is installed. Next job is tightening parity against the -Rust crate and porting the remaining preset/macro conveniences. +The current slice covers the modular graph surface needed by the Rust golfed +scripts, emits Faust, and validates generated Faust when the compiler is +installed. Migration coverage is tracked in +[`docs/migration-checklist.md`](docs/migration-checklist.md), because leaving +important things behind in the old repo would be a very efficient way to become +our own haunted house. diff --git a/docs/migration-checklist.md b/docs/migration-checklist.md new file mode 100644 index 0000000..ddfdbd6 --- /dev/null +++ b/docs/migration-checklist.md @@ -0,0 +1,45 @@ +# Migration Checklist + +This repo is now the authoring-side home for Aquarium Synth patch scripts and +Faust generation. The Rust crate remains useful as a reference lab, but the +current C# surface should not depend on somebody remembering what got built +there. + +## Ported + +- Patch graph model: voices, envelopes, pitch motion, duty motion, filters, + phasers, arpeggios, FM, color shaping, formants, repeat, and control lanes. +- Patch script parser: `patch`, `default`, `def`, `voice`, `mod`/`bus`, and + `lfo`/`control`. +- Rust DSL shorthand aliases used by the golfed scripts, including `p`, `d`, + `v`, `u`, `w`, `f`, `g`, `s`, `pr`, `pdr`, `du`, `dur`, `l`, `h`, `drv`, + `fl`, `fmix`, `fmi`, `mods`, and `m`. +- Modulation routing in both direct style (`mod n=wob g=.4 l=.5`) and routed + style (`bus n=sway to=g:.1,l:-.2,fmix:.3`). +- Built-in script catalog for classic SFXR, classic 808, FM bell, and wobble + bass primitive golf scripts. +- Abstract SFXR golf script using defaults and templates. +- Faust source emitter and installed-compiler wrapper for C, C++, C#, and Rust + target code generation. +- Tests that parse/export all built-in primitive scripts and validate Faust + source when Faust is installed. + +## Still Deliberate + +- The C# repo does not yet include an audio renderer, analysis metric suite, or + exhaustive Rust-vs-Faust audio parity harness. Faust is the renderer target, + so C# should own authoring and validation first. +- SFXR preset atoms such as `sfxr preset=laser mutate_seed=...` are not carried + over yet. The current migration preserves the primitive patch construction + path, which is the part we want in the engine. +- Random mutation, parameter-space search, and readability/code-golf scoring are + still Rust-reference features until we decide which ones belong in C# tools + versus offline authoring utilities. + +## Next Pull + +- Add JSON serialization contracts for `SynthPatch` once the Vortice-side load + boundary is known. +- Add a Faust compilation cache keyed by script/options/source hash. +- Add a small CLI so build scripts can compile `.aqs` patch files into Faust or + generated C# without writing host code first. diff --git a/src/AquariumSynth.Dsl/BuiltInScripts.cs b/src/AquariumSynth.Dsl/BuiltInScripts.cs new file mode 100644 index 0000000..a265404 --- /dev/null +++ b/src/AquariumSynth.Dsl/BuiltInScripts.cs @@ -0,0 +1,88 @@ +namespace AquariumSynth.Dsl; + +public static class BuiltInScripts +{ + public const string PatchScriptExample = """ + # One command per line. Comments start with #. + patch gain=0.7 soft_clip=true + mod name=wobble wave=sine hz=5 pitch=0.01 formant=0.08 + bus n=shimmer w=triangle hz=2 to=gain:0.08,lpf:-0.04,fmix:0.12 + voice wave=sine freq=220 gain=0.12 attack=0.002 sustain=0.03 decay=0.2 vibrato=0.02 vibrato_hz=5 formants=620:90:1,1040:150:0.8 formant_mix=0.45 mods=formant:sine:2:0.12,pitch:triangle:3:0.015 + voice wave=triangle freq=440 gain=0.04 attack=0 sustain=0.02 decay=0.18 lpf=0.7 hpf=0.02 mods=gain:sine:8:0.18,lpf:hold:12:0.08 + """; + + public static readonly IReadOnlyList ClassicSfxrNames = + [ + "pickup", + "laser", + "explosion", + "powerup", + "hit", + "jump", + "blip" + ]; + + public const string ClassicSfxrPrimitiveGolfScript = "pickup;laser;explosion;powerup;hit;jump;blip"; + + public static readonly IReadOnlyList<(string Name, string Script)> ClassicSfxrPrimitiveGolfScripts = + [ + ("pickup", "v w=sq f=148.7934 g=.22 s=.01451247166 d=.1306122449 pu=.45 drv=.201 ad=.081844 am=1.116121255"), + ("laser", "v w=saw f=229.0554 g=.22 s=.08185941043 d=.07346938776 pr=.703836 du=.31 dur=.056 h=.04 ph=.0001458 phr=-.000504 drv=.12"), + ("explosion", "p r=.174744;v w=n f=20 g=.22 s=.1907029478 d=.293877551 pu=.52 pr=.0320625 ph=-.0008712 phr=-.00035 vi=.11 vh=3.4112 nz=.35 drv=.2136 tr=.0264 th=13.04"), + ("powerup", "p r=.11315;v w=sin f=57.5946 g=.22 s=.1306122449 d=.1777777778 pr=-.208544 vi=.09 vh=3.9602 drv=.12 tr=.0216 th=13.94"), + ("hit", "v w=n f=51.4206 g=.22 s=.00566893424 d=.09070294785 pr=1.050624 h=.12 nz=.35 drv=.12"), + ("jump", "v w=sq f=78.2334 g=.22 s=.1097505669 d=.07346938776 pr=-.101156 du=.38 l=.72 h=.05 drv=.12"), + ("blip", "v w=sin f=78.2334 g=.22 s=.03832199546 d=.01451247166 h=.1 drv=.12") + ]; + + public const string ClassicSfxrAbstractGolfScript = + "d g=.22 drv=.12;" + + "def name=N w=n nz=.35;" + + "v w=sq f=148.7934 s=.01451247166 d=.1306122449 pu=.45 drv=.201 ad=.081844 am=1.116121255;" + + "v w=saw f=229.0554 s=.08185941043 d=.07346938776 pr=.703836 du=.31 dur=.056 h=.04 ph=.0001458 phr=-.000504;" + + "p r=.174744;v u=N f=20 s=.1907029478 d=.293877551 pu=.52 pr=.0320625 ph=-.0008712 phr=-.00035 vi=.11 vh=3.4112 drv=.2136 tr=.0264 th=13.04;" + + "p r=.11315;v w=sin f=57.5946 s=.1306122449 d=.1777777778 pr=-.208544 vi=.09 vh=3.9602 tr=.0216 th=13.94;" + + "v u=N f=51.4206 s=.00566893424 d=.09070294785 pr=1.050624 h=.12;" + + "v w=sq f=78.2334 s=.1097505669 d=.07346938776 pr=-.101156 du=.38 l=.72 h=.05;" + + "v w=sin f=78.2334 s=.03832199546 d=.01451247166 h=.1"; + + public static readonly IReadOnlyList Classic808Names = ["kick", "snare", "clap", "hat", "tom", "cowbell"]; + + public static readonly IReadOnlyList<(string Name, string Script)> Classic808PrimitiveGolfScripts = + [ + ("kick", "d w=sin g=.8 drv=.18;v f=58 s=.045 d=.42 pu=.65 pr=-3.8 min=32 l=.85"), + ("snare", "d drv=.12;def n=N w=n nz=.85 h=.45 l=.55;v w=sin f=180 g=.08 s=.02 d=.12 pr=-1.2;v u=N f=140 g=.55 s=.035 d=.2"), + ("clap", "d w=n nz=.95 h=.55 l=.42 g=.22 d=.11 drv=.1;v f=1800 s=.018 ph=.004;v f=2200 s=.022 ph=.008;v f=2600 s=.028 ph=.012"), + ("hat", "d h=.9 l=.24 drv=.05;v w=n f=9000 g=.16 s=.006 d=.055 nz=1;v w=sq f=6800 g=.045 s=.005 d=.04"), + ("tom", "d w=sin g=.55 drv=.12;v f=115 s=.055 d=.34 pu=.28 pr=-1.45 min=62 l=.78"), + ("cowbell", "d w=sq h=.18 l=.82 drv=.16;v f=540 g=.16 s=.05 d=.18 du=.43;v f=800 g=.12 s=.045 d=.16 du=.47") + ]; + + public static readonly IReadOnlyList FmBellNames = ["bell", "chime", "coin", "gong"]; + + public static readonly IReadOnlyList<(string Name, string Script)> FmBellPrimitiveGolfScripts = + [ + ("bell", "d w=sin g=.24 a=.002 s=.04 d=1.2 l=.9;def n=O fm=4.1 fmd=.55;v u=O f=440 fmi=5.8;v u=O f=880 g=.08 fmi=2.4"), + ("chime", "d w=sin g=.18 a=.001 s=.025 d=.9 h=.02;def n=O fm=3 fmd=.38;v u=O f=660 fmi=4.2;v u=O f=990 g=.07 fmi=2.1"), + ("coin", "d w=sin a=0 s=.02 d=.45 h=.04 drv=.08;v f=1200 g=.18 fm=5 fmi=3.4 fmd=.18;v f=1800 g=.09 fm=7 fmi=1.8 fmd=.12"), + ("gong", "d w=sin a=.003 s=.08 d=1.6 l=.82 drv=.1;v f=196 g=.24 fm=2.414 fmi=7.2 fmd=.9;v f=311 g=.12 fm=3.73 fmi=4.1 fmd=.7") + ]; + + public static readonly IReadOnlyList WobbleBassNames = ["talker", "growl", "yoy", "neuro"]; + + public static readonly IReadOnlyList<(string Name, string Script)> WobbleBassPrimitiveGolfScripts = + [ + ("talker", "d w=saw f=55 g=.18 s=.8 d=.25 l=.34 h=.02 drv=.3 fl=.08 fm=2 fmi=.8 fmd=.7 fs=520:90:.7,1250:170:1,2600:320:.45 fmix=.35;mod n=wob hz=4 w=tri g=.42 l=.48 fmix=.38 fmi=1.6 drv=.2 fl=.14;v;v f=110 g=.08 du=.42"), + ("growl", "d w=saw f=44 g=.2 s=.7 d=.3 l=.28 h=.01 drv=.42 fl=.18 fm=1.5 fmi=1.2 fmd=.45 nz=.05;mod n=wob hz=6 w=sin g=.32 l=.55 p=.035 drv=.28 fl=.22 nz=.08 fmi=2.2;v;v w=sq f=88 g=.09 du=.36"), + ("yoy", "d w=sq f=62 g=.16 s=.65 d=.22 l=.3 h=.03 drv=.25 fm=3 fmi=.7 fmd=.5 fs=400:80:.6,900:120:1,2100:260:.4 fmix=.28;mod n=wob hz=5 w=sq g=.5 l=.5 fmix=.45 p=.025 du=.08;v;v w=saw f=124 g=.07"), + ("neuro", "d w=saw f=49 g=.16 s=.75 d=.28 l=.25 h=.04 drv=.38 fl=.24 fm=2.7 fmi=1.5 fmd=.35 nz=.04;mod n=wob hz=7 w=hold g=.28 l=.52 p=.04 drv=.25 fl=.3 nz=.1 fmi=2.8;v;v w=tri f=147 g=.06") + ]; + + public static IEnumerable<(string Family, string Name, string Script)> PrimitiveGolfScripts() + { + foreach (var item in ClassicSfxrPrimitiveGolfScripts) yield return ("sfxr", item.Name, item.Script); + foreach (var item in Classic808PrimitiveGolfScripts) yield return ("808", item.Name, item.Script); + foreach (var item in FmBellPrimitiveGolfScripts) yield return ("fm-bell", item.Name, item.Script); + foreach (var item in WobbleBassPrimitiveGolfScripts) yield return ("wobble-bass", item.Name, item.Script); + } +} diff --git a/src/AquariumSynth.Dsl/PatchScript.cs b/src/AquariumSynth.Dsl/PatchScript.cs index dc5581a..5546157 100644 --- a/src/AquariumSynth.Dsl/PatchScript.cs +++ b/src/AquariumSynth.Dsl/PatchScript.cs @@ -88,6 +88,9 @@ public static class PatchScript case "mod": AddModBus(fields, line); break; + case "control": + AddControlLane(fields, line); + break; default: throw new PatchScriptException(line, $"unknown command `{parts[0]}`"); } @@ -126,7 +129,7 @@ public static class PatchScript ? ParseFormants(formantSpec, line) : []; - var modulators = TryGetAny(fields, ["mods"], out var mods) + var modulators = TryGetAny(fields, ["mods", "m"], out var mods) ? ParseVoiceModulators(mods, line) : []; @@ -146,7 +149,7 @@ public static class PatchScript waveform, GetFloat(fields, line, 440, "freq", "frequency", "f"), GetFloat(fields, line, 0.5f, "duty", "du"), - GetFloat(fields, line, 0, "phase")), + GetFloat(fields, line, 0, "phase", "pa")), Envelope = new Envelope( GetFloat(fields, line, 0, "attack", "a"), GetFloat(fields, line, 0.1f, "sustain", "s"), @@ -155,7 +158,7 @@ public static class PatchScript Pitch = new PitchMotion( GetFloat(fields, line, 20, "min_freq", "min"), GetFloat(fields, line, 0, "pitch_ramp", "pr"), - GetFloat(fields, line, 0, "pitch_delta", "pd"), + GetFloat(fields, line, 0, "pitch_delta", "pd", "pitch_dramp", "pdr"), GetFloat(fields, line, 0, "vibrato", "vi"), GetFloat(fields, line, 0, "vibrato_hz", "vh"), GetFloat(fields, line, 0, "vibrato_delay", "vd")), @@ -171,7 +174,7 @@ public static class PatchScript GetFloat(fields, line, 0, "phaser_ramp", "phr")), Arpeggio = arpeggio, Fm = new FrequencyModulation( - GetFloat(fields, line, 1, "fm", "fmr"), + GetFloat(fields, line, 1, "fm", "fmr", "fm_ratio"), GetFloat(fields, line, 0, "fm_index", "fmi"), GetFloat(fields, line, 0, "fm_decay", "fmd")), Color = new VoiceColor( @@ -196,6 +199,16 @@ public static class PatchScript var hz = GetFloat(fields, line, 1, "hz", "rate"); var phase = GetFloat(fields, line, 0, "phase"); + if (TryGetAny(fields, ["to", "targets"], out var routeSpec)) + { + foreach (var route in ParseRoutes(routeSpec, line)) + { + _controls.Add(new ControlLane( + $"{name}_{TargetSuffix(route.Target)}", + new Modulator(route.Target, wave, hz, route.Depth, phase))); + } + } + foreach (var (key, target) in ModTargets) { if (!TryGetAny(fields, key, out var depthText)) continue; @@ -203,6 +216,29 @@ public static class PatchScript _controls.Add(new ControlLane($"{name}_{key[0]}", new Modulator(target, wave, hz, depth, phase))); } } + + private void AddControlLane(IReadOnlyDictionary fields, int line) + { + var name = GetAny(fields, ["name", "n"], "control"); + if (!TryGetAny(fields, ["target", "t"], out var targetText)) + { + throw new PatchScriptException(line, "control lane needs target"); + } + + var wave = TryGetAny(fields, ["wave", "w"], out var waveform) + ? ParseModWaveform(waveform, line) + : ModWaveform.Sine; + + _controls.Add(new ControlLane( + name, + new Modulator( + ParseModTarget(targetText, line), + wave, + GetFloat(fields, line, 1, "hz", "rate"), + GetFloat(fields, line, 0, "depth", "d", "decay"), + GetFloat(fields, line, 0, "phase", "ph"), + GetFloat(fields, line, 0, "bias", "b")))); + } } private static readonly (string[] Keys, ModTarget Target)[] ModTargets = @@ -215,7 +251,7 @@ public static class PatchScript (["noise", "nz"], ModTarget.Noise), (["drive", "drv"], ModTarget.Drive), (["fold", "fl"], ModTarget.Fold), - (["formant_mix", "fmix"], ModTarget.FormantMix), + (["formant_mix", "fmix", "formant"], ModTarget.FormantMix), (["fm_index", "fmi"], ModTarget.FmIndex) ]; @@ -258,13 +294,23 @@ public static class PatchScript }) .ToList(); + private static IEnumerable<(ModTarget Target, float Depth)> ParseRoutes(string value, int line) => + value.Split(',', StringSplitOptions.RemoveEmptyEntries) + .Select(part => + { + var pieces = part.Split(':'); + if (pieces.Length != 2) throw new PatchScriptException(line, $"bad route `{part}`"); + return (ParseModTarget(pieces[0], line), ParseFloat(pieces[1], line)); + }); + private static string CanonicalCommand(string command) => command.ToLowerInvariant() switch { "p" or "patch" => "patch", - "d" or "defaults" => "defaults", + "d" or "default" or "defaults" => "defaults", "def" or "t" or "template" => "template", "v" or "voice" => "voice", "mod" or "wob" or "wobble" or "bus" => "mod", + "lfo" or "control" => "control", _ => command }; @@ -304,12 +350,31 @@ public static class PatchScript { foreach (var (keys, target) in ModTargets) { - if (keys.Contains(value, StringComparer.OrdinalIgnoreCase)) return target; + if (keys.Contains(value, StringComparer.OrdinalIgnoreCase) || + keys.Select(CanonicalField).Contains(CanonicalField(value), StringComparer.OrdinalIgnoreCase)) + { + return target; + } } if (value.Equals("formant", StringComparison.OrdinalIgnoreCase)) return ModTarget.FormantMix; throw new PatchScriptException(line, $"unknown mod target `{value}`"); } + private static string TargetSuffix(ModTarget target) => target switch + { + ModTarget.Gain => "gain", + ModTarget.Pitch => "pitch", + ModTarget.Duty => "duty", + ModTarget.LowPass => "lpf", + ModTarget.HighPass => "hpf", + ModTarget.Noise => "noise", + ModTarget.Drive => "drive", + ModTarget.Fold => "fold", + ModTarget.FormantMix => "formant_mix", + ModTarget.FmIndex => "fm_index", + _ => target.ToString().ToLowerInvariant() + }; + private static string Required(IReadOnlyDictionary fields, string key, int line) => fields.TryGetValue(key, out var value) ? value : throw new PatchScriptException(line, $"missing `{key}`"); diff --git a/tests/AquariumSynth.Dsl.Tests/PatchScriptTests.cs b/tests/AquariumSynth.Dsl.Tests/PatchScriptTests.cs index ebeb9d6..ca64948 100644 --- a/tests/AquariumSynth.Dsl.Tests/PatchScriptTests.cs +++ b/tests/AquariumSynth.Dsl.Tests/PatchScriptTests.cs @@ -21,6 +21,69 @@ public sealed class PatchScriptTests Assert.Equal(3, patch.Voices[0].Formants.Count); } + [Fact] + public void ParserSupportsRouteListModBus() + { + var patch = PatchScript.Parse("bus n=sway w=tri hz=2 to=g:.12,l:-.2,fmix:.35,fmi:1.4;v w=saw f=80"); + + Assert.Equal(4, patch.Controls.Count); + Assert.Contains(patch.Controls, lane => lane.Name == "sway_gain" && lane.Modulator.Target == ModTarget.Gain); + Assert.Contains(patch.Controls, lane => lane.Name == "sway_lpf" && lane.Modulator.Target == ModTarget.LowPass); + Assert.Contains(patch.Controls, lane => lane.Name == "sway_formant_mix" && lane.Modulator.Target == ModTarget.FormantMix); + Assert.Contains(patch.Controls, lane => lane.Name == "sway_fm_index" && lane.Modulator.Target == ModTarget.FmIndex); + } + + [Fact] + public void ParserSupportsExplicitControlLane() + { + var patch = PatchScript.Parse("lfo n=wob t=p w=sin hz=6 d=.04 ph=.25 b=.01;v w=saw f=80"); + + var lane = Assert.Single(patch.Controls); + Assert.Equal("wob", lane.Name); + Assert.Equal(ModTarget.Pitch, lane.Modulator.Target); + Assert.Equal(6, lane.Modulator.FrequencyHz); + Assert.Equal(.04f, lane.Modulator.Depth, 5); + Assert.Equal(.25f, lane.Modulator.Phase, 5); + Assert.Equal(.01f, lane.Modulator.Bias, 5); + } + + [Fact] + public void BuiltInExampleParsesAndExportsFaust() + { + var patch = PatchScript.Parse(BuiltInScripts.PatchScriptExample); + var export = FaustEmitter.Emit(patch); + + Assert.Equal(2, patch.Voices.Count); + Assert.Equal(5, patch.Controls.Count); + Assert.Contains("patch_mod_formant_mix", export.Source); + } + + [Fact] + public void ClassicAbstractGolfScriptParsesAndExportsFaust() + { + var patch = PatchScript.Parse(BuiltInScripts.ClassicSfxrAbstractGolfScript); + var export = FaustEmitter.Emit(patch); + + Assert.Equal(7, patch.Voices.Count); + Assert.Contains("process =", export.Source); + } + + [Fact] + public void BuiltInPrimitiveGolfScriptsParseAndExportFaust() + { + foreach (var (family, name, script) in BuiltInScripts.PrimitiveGolfScripts()) + { + var exception = Record.Exception(() => + { + var patch = PatchScript.Parse(script); + var export = FaustEmitter.Emit(patch, new FaustExportOptions($"{family}_{name}".Replace('-', '_'))); + Assert.Contains("process =", export.Source); + }); + + Assert.Null(exception); + } + } + [Fact] public void FaustEmitterProducesWobbleSource() {