From 9f035366cccabe38e2a57d6c26ece914170b32a2 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 13 May 2026 19:10:07 +0100 Subject: [PATCH] Approximate DX7 operator output levels --- src/AquariumSynth.Dsl/Dx7SysEx.cs | 83 +++++++++++++++++++ state/evidence.jsonl | 1 + state/memory.json | 11 +-- state/scratch.md | 11 +++ state/spine.yaml | 13 ++- .../AquariumSynth.Dsl.Tests/Dx7SysExTests.cs | 27 ++++++ 6 files changed, 139 insertions(+), 7 deletions(-) diff --git a/src/AquariumSynth.Dsl/Dx7SysEx.cs b/src/AquariumSynth.Dsl/Dx7SysEx.cs index 7b60ed1..c6da538 100644 --- a/src/AquariumSynth.Dsl/Dx7SysEx.cs +++ b/src/AquariumSynth.Dsl/Dx7SysEx.cs @@ -39,6 +39,13 @@ public sealed record Dx7EnvelopeApproximation( private static string F(float value) => value.ToString("0.######", System.Globalization.CultureInfo.InvariantCulture); } +public sealed record Dx7OperatorLevelApproximation( + float LinearLevel, + int ScaledOutputLevel, + int KeyScalingOffset, + int VelocityOffset, + string Notes); + public sealed record Dx7Operator( int Number, Dx7Envelope Envelope, @@ -167,6 +174,31 @@ public static class Dx7SysEx "Approximation: DX7 EG uses four rate/level segments; ADSR keeps one sustain level and a key-off release."); } + public static Dx7OperatorLevelApproximation ApproximateOperatorLevel( + Dx7Operator op, + int midiNote = 60, + int velocity = 100) + { + var scaled = ScaleOutLevel(op.OutputLevel); + var keyScaling = ScaleLevel( + midiNote, + op.BreakPoint, + op.LeftDepth, + op.RightDepth, + op.LeftCurve, + op.RightCurve); + var velocityOffset = ScaleVelocity(velocity, op.KeyVelocitySensitivity); + var internalLevel = Math.Max(0, (Math.Min(127, scaled + keyScaling) << 5) + velocityOffset); + var normalized = Math.Clamp(internalLevel / 4096f, 0, 1.5f); + + return new Dx7OperatorLevelApproximation( + normalized, + scaled, + keyScaling, + velocityOffset, + "Approximation: DX7 output level uses nonlinear scaling plus key/velocity offsets before envelope gain."); + } + public static Dx7VoiceBank ParseBank(ReadOnlySpan bytes) { var data = PackedBankData(bytes); @@ -383,6 +415,37 @@ public static class Dx7SysEx private static float Level(int value) => Math.Clamp(value, 0, 99) / 99f; + private static int ScaleOutLevel(int outLevel) + { + var value = Math.Clamp(outLevel, 0, 99); + return value >= 20 ? 28 + value : LevelLookup[value]; + } + + private static int ScaleVelocity(int velocity, int sensitivity) + { + var clampedVelocity = Math.Clamp(velocity, 0, 127); + var velValue = VelocityData[clampedVelocity >> 1] - 239; + return (((Math.Clamp(sensitivity, 0, 7) * velValue + 7) >> 3) << 4); + } + + private static int ScaleLevel(int midiNote, int breakPoint, int leftDepth, int rightDepth, int leftCurve, int rightCurve) + { + var offset = Math.Clamp(midiNote, 0, 127) - Math.Clamp(breakPoint, 0, 99) - 17; + return offset >= 0 + ? ScaleCurve((offset + 1) / 3, rightDepth, rightCurve) + : ScaleCurve(-(offset - 1) / 3, leftDepth, leftCurve); + } + + private static int ScaleCurve(int group, int depth, int curve) + { + var clampedGroup = Math.Clamp(group, 0, ExpScaleData.Length - 1); + var clampedDepth = Math.Clamp(depth, 0, 99); + var scale = curve is 0 or 3 + ? (clampedGroup * clampedDepth * 329) >> 12 + : (ExpScaleData[clampedGroup] * clampedDepth * 329) >> 15; + return curve < 2 ? -scale : scale; + } + private static float SegmentSeconds(int rate, float distance) { var normalizedRate = Math.Clamp(rate, 0, 99) / 99f; @@ -467,6 +530,26 @@ public static class Dx7SysEx private static byte SevenBit(byte value) => (byte)(value & 0x7F); + private static readonly int[] LevelLookup = + [ + 0, 1, 3, 5, 6, 8, 10, 11, 13, 14, 16, 17, 19, 20, 21, 23, 24, 25, 27, 28 + ]; + + private static readonly int[] VelocityData = + [ + 0, 70, 86, 97, 106, 114, 121, 126, 132, 138, 142, 148, 152, 156, 160, 163, + 166, 170, 173, 174, 178, 181, 184, 186, 189, 190, 194, 196, 198, 200, 202, 205, + 206, 209, 211, 213, 215, 217, 218, 220, 222, 224, 225, 227, 229, 230, 232, 233, + 235, 237, 238, 240, 241, 242, 243, 244, 246, 246, 248, 249, 250, 251, 252, 253, + 254 + ]; + + private static readonly int[] ExpScaleData = + [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 14, 16, 19, 23, 27, 33, 39, 47, 56, 66, + 80, 94, 110, 126, 142, 158, 174, 190, 206, 222, 238, 250 + ]; + private static Dx7AlgorithmRomStep ParseAlgorithmStep(int operatorNumber, string entry) { var pieces = entry.Split('/'); diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 3393d38..5fb5b50 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -24,3 +24,4 @@ {"kind":"dx7_reference_rendering","lesson":"DX7 reference rendering now uses dexed-py as optional test tooling against a public-domain SysEx fixture. Keep fixtures and renderer helpers under the test project, preserve provenance and SHA-256 beside vendored banks, and keep AquariumSynth.Dsl.nupkg free of SysEx/Python/test assets.","source":"dexed-py public-domain fixture harness slice on 2026-05-13"} {"kind":"dx7_rendered_parity","lesson":"The first thresholded rendered DX7 parity target is analog1.syx voice 13, MC-MM 5-3. Aquarium matches the rendered behavior with a short sine-like patch and explicit score/log-mel/envelope/duration/RMS/zero-crossing/centroid thresholds. Treat this as proof of the parity loop, not proof of exact DX7 operator execution; next target should audibly require operator routing.","source":"first thresholded DX7 rendered parity slice on 2026-05-13"} {"kind":"operator_feedback","lesson":"A harder DX7 algorithm-8 target exposed direct operator self-reference as non-renderable Faust. Operator feedback now emits as delayed Faust recursion, which keeps the render path alive and is closer to DX7 feedback pressure; exact DX7 feedback scaling remains unsolved.","source":"PRC SYNTH1 parity probe and delayed feedback render fix on 2026-05-13"} +{"kind":"dx7_operator_levels","lesson":"DX7 operator output level is nonlinear and includes key/velocity offsets before envelope gain. Added a reference-import helper to approximate effective Aquarium operator levels, but PRC SYNTH1 still only improved to ~0.34 with static levels and ~0.42 with envelope tweaks; hard parity now points at DX7-style operator envelope/gain evolution rather than static level mapping alone.","source":"DX7 effective operator level probe on 2026-05-13"} diff --git a/state/memory.json b/state/memory.json index 0c2b63b..bc5a67d 100644 --- a/state/memory.json +++ b/state/memory.json @@ -20,6 +20,7 @@ "reference rebuild catalog", "DX7 SysEx structural parsing", "DX7 algorithm topology metadata", + "DX7 effective operator-level approximation", "DX7 envelope-to-ADSR approximation", "standard ADSR envelope model", "note frequency/gate model", @@ -57,9 +58,9 @@ "rule": "Add DSL abstractions only when reference rebuilds prove the missing invariant. Do not consume references that cannot be backed by parity tests; parsing alone is inventory, not evidence of expressivity." }, "dx7_reference_path": { - "status": "DX7 SysEx parser extracts voice edit buffers, packed voices, packed 32-voice banks, six operators, envelopes, algorithm, feedback, oscillator sync, LFO, transpose, voice name, checksum validation, ReferencePatch features, algorithm topology from the reverse-engineered OPS algorithm ROM table, envelope-to-ADSR approximations, two Aquarium DSL rebuild attempts, first-class OperatorGraph support, public-domain SysEx fixture coverage, dexed-py reference rendering, Aquarium candidate rendering through Faust-generated C#, one thresholded rendered parity test for analog1.syx voice 13, and renderable delayed operator feedback.", - "lesson": "Algorithm 32's additive six-carrier shape maps cleanly to six Aquarium voices. Algorithm 8 justified OperatorGraph: operators, modulation edges, carriers, and feedback belong to a graph authority, not Voice fields. The first rendered parity rung is behavioral and sine-like, not exact DX7 operator execution. A harder algorithm-8 target exposed that feedback must be delayed recursion in Faust, not a direct cyclic self-reference. Readable authoring uses operator/route/carrier declarations; compact ops/edges tuples are scaffolding only.", - "next": "Choose a harder public-domain DX7 fixture voice whose rendered audio needs audible operator routing, rebuild it with delayed feedback available, and add explicit parity thresholds; exact DX7 feedback scaling and exact DX7 EG execution remain separate pressure." + "status": "DX7 SysEx parser extracts voice edit buffers, packed voices, packed 32-voice banks, six operators, envelopes, algorithm, feedback, oscillator sync, LFO, transpose, voice name, checksum validation, ReferencePatch features, algorithm topology from the reverse-engineered OPS algorithm ROM table, envelope-to-ADSR approximations, effective operator-level approximations, two Aquarium DSL rebuild attempts, first-class OperatorGraph support, public-domain SysEx fixture coverage, dexed-py reference rendering, Aquarium candidate rendering through Faust-generated C#, one thresholded rendered parity test for analog1.syx voice 13, and renderable delayed operator feedback.", + "lesson": "Algorithm 32's additive six-carrier shape maps cleanly to six Aquarium voices. Algorithm 8 justified OperatorGraph: operators, modulation edges, carriers, and feedback belong to a graph authority, not Voice fields. The first rendered parity rung is behavioral and sine-like, not exact DX7 operator execution. A harder algorithm-8 target exposed that feedback must be delayed recursion in Faust, not a direct cyclic self-reference. Static DX7 output-level approximation helps import/rebuild math but does not solve hard parity alone; PRC SYNTH1 still points at DX7-style operator envelope/gain evolution. Readable authoring uses operator/route/carrier declarations; compact ops/edges tuples are scaffolding only.", + "next": "Choose whether to add a DX7-style four-rate/four-level operator envelope surface or a reference-rebuild-only gain-envelope approximation before attempting the next hard parity threshold; exact DX7 feedback scaling remains separate pressure." }, "note_and_envelope_model": { "rule": "Notes own frequency, gate duration, and host/MIDI source. Envelopes own ADSR level shape.", @@ -74,7 +75,7 @@ }, "last_verified": { "command": "dotnet test AquariumSynthCSharp.slnx --no-restore", - "result": "49 passed", - "commit": "pending delayed operator feedback render fix" + "result": "50 passed", + "commit": "pending DX7 operator level approximation" } } diff --git a/state/scratch.md b/state/scratch.md index b3c1121..bac85f2 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -15,6 +15,17 @@ Doctrine update: Completed this slice: +- Added `Dx7OperatorLevelApproximation`, which distills DX7 operator output + level plus key/velocity scaling into a normal Aquarium `level=`-style value. + This remains DX7 reference-import knowledge, not a generic DSL feature. +- Retried `PRC SYNTH1` with effective levels and envelope tweaks. Static level + mapping alone did not rescue the hard target: quick probes moved from ~0.24 + to ~0.34, and envelope tweaks reached ~0.42. The next missing pressure is + DX7-style operator envelope/gain evolution, not just static operator levels. +- Verified with `dotnet test AquariumSynthCSharp.slnx --no-restore`: 50 passed. + +Previous slice: + - Tried the harder `analog1.syx` voice 17, `PRC SYNTH1` (algorithm 8). The initial routed Aquarium candidate scored badly (~0.23), which exposed a real missing invariant rather than a threshold-tuning problem. diff --git a/state/spine.yaml b/state/spine.yaml index b3327fb..2dbc879 100644 --- a/state/spine.yaml +++ b/state/spine.yaml @@ -1,6 +1,6 @@ project: AquariumSynthCSharp spine_version: 1 -updated: 2026-05-13T15:16:00+01:00 +updated: 2026-05-13T15:43:00+01:00 prime_directive: > Build coherent synth machinery. References exist to reveal missing structure, @@ -42,6 +42,7 @@ live_invariants: - Parameter binding uses field-site references like `lpf=@/macro/brightness`; do not add target-routing semantics unless a later map proves a new authority is needed. - Operator graph parameter binding also uses exact field-site paths under `/opgraphs/...`; do not add graph-wide macro routing. - Operator feedback must render through Faust as delayed recursion, not as a direct cyclic self-reference. + - DX7 operator output level approximation belongs in reference import/rebuild helpers, not the generic DSL model. - DX7 rate/level envelopes may lower to labeled ADSR approximations, but exact DX7 EG execution remains missing pressure. - Recompile only for graph shape changes; vary declared parameters through the hosted DSP control API. - Reference fixtures must preserve provenance, license status, source hash, and feature pressure. @@ -56,7 +57,7 @@ reference_targets: priority: 1 purpose: "Structured FM/operator graph pressure test." source: "https://github.com/asb2m10/dexed" - status: "DX7 SysEx parser, public-domain SysEx fixture, dexed-py reference rendering, first thresholded rendered parity test for analog1.syx voice 13, renderable delayed operator feedback, algorithm topology extraction, envelope-to-ADSR approximation, two Aquarium DSL rebuild attempts, first-class operator graph support, and Aquarium candidate rendering through Faust-generated C# exist." + status: "DX7 SysEx parser, public-domain SysEx fixture, dexed-py reference rendering, first thresholded rendered parity test for analog1.syx voice 13, renderable delayed operator feedback, DX7 effective operator-level approximation, algorithm topology extraction, envelope-to-ADSR approximation, two Aquarium DSL rebuild attempts, first-class operator graph support, and Aquarium candidate rendering through Faust-generated C# exist." first_slice: - "Add a harder public-domain DX7 parity target that audibly needs operator routing." zynaddsubfx: @@ -107,6 +108,14 @@ next_agent_start_here: - "dotnet test AquariumSynthCSharp.slnx --no-restore" completed_slices: + - date: 2026-05-13 + summary: > + Added `Dx7OperatorLevelApproximation`, which maps DX7 operator output + level plus key/velocity scaling into normal Aquarium operator levels for + reference rebuild work. Retried `PRC SYNTH1`; static level mapping alone + did not produce parity, and envelope tweaks only reached ~0.42 score. The + next pressure is DX7-style operator envelope/gain evolution. + verification: "dotnet test AquariumSynthCSharp.slnx --no-restore: 50 passed" - date: 2026-05-13 summary: > Probed the harder `analog1.syx` voice 17 (`PRC SYNTH1`) and found the diff --git a/tests/AquariumSynth.Dsl.Tests/Dx7SysExTests.cs b/tests/AquariumSynth.Dsl.Tests/Dx7SysExTests.cs index ca57029..c634717 100644 --- a/tests/AquariumSynth.Dsl.Tests/Dx7SysExTests.cs +++ b/tests/AquariumSynth.Dsl.Tests/Dx7SysExTests.cs @@ -172,6 +172,33 @@ public sealed class Dx7SysExTests Assert.StartsWith("env=adsr:", approximation.ToScriptSpec()); } + [Fact] + public void ApproximatesDx7OperatorLevelWithVelocityAndKeyScaling() + { + var operatorLevel = Dx7SysEx.ApproximateOperatorLevel(new Dx7Operator( + Number: 5, + Envelope: new Dx7Envelope(85, 16, 75, 61, 88, 44, 25, 0), + BreakPoint: 15, + LeftDepth: 0, + RightDepth: 62, + LeftCurve: 0, + RightCurve: 1, + RateScaling: 2, + AmplitudeModulationSensitivity: 0, + KeyVelocitySensitivity: 6, + OutputLevel: 80, + FrequencyMode: Dx7FrequencyMode.Ratio, + FrequencyCoarse: 8, + FrequencyFine: 0, + Detune: 4)); + + Assert.Equal(108, operatorLevel.ScaledOutputLevel); + Assert.Equal(-5, operatorLevel.KeyScalingOffset); + Assert.Equal(0, operatorLevel.VelocityOffset); + Assert.InRange(operatorLevel.LinearLevel, 0.80f, 0.81f); + Assert.Contains("nonlinear", operatorLevel.Notes); + } + private static byte[] InitVoice(string name, int algorithm, int feedback) { var data = new byte[Dx7SysEx.VoiceEditBufferLength];