diff --git a/docs/audio-doctrine.md b/docs/audio-doctrine.md index 4eddf64..f406862 100644 --- a/docs/audio-doctrine.md +++ b/docs/audio-doctrine.md @@ -104,8 +104,10 @@ authorities. time. - Legacy SFXR sustain maps to note gate duration, because it is how long the generated sound is held before release. -- Legacy SFXR punch maps to a non-unit sustain level when importing old SFXR - material. Keep that as compatibility mapping, not as a general envelope field. +- Legacy SFXR punch maps to a lower sustain level plus voice gain compensation + when importing old SFXR material: ADSR peak stays implicit at 1, and the + sustain level falls below that peak. Keep that as compatibility mapping, not + as a general envelope field. - Host/MIDI note mode should expose stable note frequency and note gate controls that the engine can wire to MIDI note-on/note-off behavior. diff --git a/src/AquariumSynth.Dsl/BuiltInScripts.cs b/src/AquariumSynth.Dsl/BuiltInScripts.cs index c123d49..33ffa29 100644 --- a/src/AquariumSynth.Dsl/BuiltInScripts.cs +++ b/src/AquariumSynth.Dsl/BuiltInScripts.cs @@ -65,10 +65,10 @@ public static class BuiltInScripts voice wave=square freq=148.7934 - gain=0.22 + gain=0.319 sustain=0.01451247166 decay=0.1306122449 - sustain_level=1.45 + sustain_level=0.6896552 drive=0.201 arp_delay=0.081844 arp_mult=1.116121255 @@ -93,10 +93,10 @@ public static class BuiltInScripts voice wave=noise freq=20 - gain=0.22 + gain=0.3344 sustain=0.1907029478 decay=0.293877551 - sustain_level=1.52 + sustain_level=0.6578947 pitch_ramp=0.0320625 phaser=-0.0008712 phaser_ramp=-0.00035 @@ -168,9 +168,10 @@ public static class BuiltInScripts voice wave=square freq=148.7934 + gain=0.319 sustain=0.01451247166 decay=0.1306122449 - sustain_level=1.45 + sustain_level=0.6896552 drive=0.201 arp_delay=0.081844 arp_mult=1.116121255 @@ -191,9 +192,10 @@ public static class BuiltInScripts voice use=Noise freq=20 + gain=0.3344 sustain=0.1907029478 decay=0.293877551 - sustain_level=1.52 + sustain_level=0.6578947 pitch_ramp=0.0320625 phaser=-0.0008712 phaser_ramp=-0.00035 @@ -271,10 +273,10 @@ public static class BuiltInScripts ("shield-pop", """ defaults wave=triangle - gain=0.2 + gain=0.264 sustain=0.04 decay=0.36 - sustain_level=1.32 + sustain_level=0.7575758 lpf=0.68 hpf=0.03 drive=0.12 @@ -298,10 +300,10 @@ public static class BuiltInScripts patch repeat=0.18 defaults wave=sine - gain=0.12 + gain=0.1464 sustain=0.05 decay=0.5 - sustain_level=1.22 + sustain_level=0.8196721 lpf=0.78 drive=0.06 @@ -349,12 +351,12 @@ public static class BuiltInScripts public static readonly IReadOnlyList<(string Name, string Script)> Classic808PrimitiveGolfScripts = [ ("kick", """ - defaults wave=sine gain=0.8 drive=0.18 + defaults wave=sine gain=1.32 drive=0.18 voice freq=58 sustain=0.045 decay=0.42 - sustain_level=1.65 + sustain_level=0.6060606 pitch_ramp=-3.8 min_freq=32 lpf=0.85 @@ -414,12 +416,12 @@ public static class BuiltInScripts decay=0.04 """), ("tom", """ - defaults wave=sine gain=0.55 drive=0.12 + defaults wave=sine gain=0.704 drive=0.12 voice freq=115 sustain=0.055 decay=0.34 - sustain_level=1.28 + sustain_level=0.78125 pitch_ramp=-1.45 min_freq=62 lpf=0.78 @@ -878,7 +880,7 @@ public static class BuiltInScripts gain=0.18 fm_index=0.8 - voice use=Ping freq=523.25 gain=0.12 sustain_level=1.12 + voice use=Ping freq=523.25 gain=0.1344 sustain_level=0.8928571 voice use=Ping freq=659.25 gain=0.08 sustain=0.11 decay=0.84 fm=4.2 voice use=Ping freq=783.99 gain=0.07 sustain=0.08 decay=0.72 fm_index=1.9 voice wave=sine freq=261.63 gain=0.045 sustain=0.7 decay=1.3 fm=1.5 fm_index=0.65 fm_decay=1.2 @@ -892,11 +894,11 @@ public static class BuiltInScripts defaults wave=sine - gain=0.13 + gain=0.1534 attack=0.001 sustain=0.045 decay=0.5 - sustain_level=1.18 + sustain_level=0.8474576 lpf=0.82 hpf=0.035 drive=0.1 diff --git a/src/AquariumSynth.Dsl/PatchScript.cs b/src/AquariumSynth.Dsl/PatchScript.cs index 71969da..3a30a9f 100644 --- a/src/AquariumSynth.Dsl/PatchScript.cs +++ b/src/AquariumSynth.Dsl/PatchScript.cs @@ -160,9 +160,11 @@ public static class PatchScript var frequency = GetBoundFloat(fields, line, 440, VoiceField(voiceIndex, "note/frequency"), "freq", "frequency", "f"); var gateSeconds = GetBoundFloat(fields, line, 0.1f, VoiceField(voiceIndex, "note/gate"), "gate", "hold", "duration", "sustain", "s"); var sustainLevel = GetBoundFloat(fields, line, 1, VoiceField(voiceIndex, "env/sustain_level"), "sustain_level", "sl"); + var gainScale = 1f; if (TryGetAny(fields, ["punch", "pu"], out var punch)) { - sustainLevel = 1 + ParseBoundFloat(punch, line, 0, VoiceField(voiceIndex, "env/sustain_level")); + gainScale = PunchGain(ParseBoundFloat(punch, line, 0, VoiceField(voiceIndex, "env/sustain_level"))); + sustainLevel = 1 / gainScale; } var noteSource = ParseNoteSource(GetAny(fields, ["note_source", "source"], "oneshot"), line); if (TryGetAny(fields, ["midi"], out var midi) && ParseBool(midi, line)) @@ -231,7 +233,7 @@ public static class PatchScript GetBoundFloat(fields, line, 0, VoiceField(voiceIndex, "color/formant_mix"), "formant_mix", "fmix")), Formants = formants, Modulators = modulators, - Gain = GetBoundFloat(fields, line, 0.2f, VoiceField(voiceIndex, "gain"), "gain", "g") + Gain = GetBoundFloat(fields, line, 0.2f, VoiceField(voiceIndex, "gain"), "gain", "g") * gainScale }; } @@ -816,6 +818,8 @@ public static class PatchScript ? parsed : throw new PatchScriptException(line, $"bad number `{value}`"); + private static float PunchGain(float value) => 1 + Math.Max(0, Math.Clamp(value, -1, 1)); + private static int ParseInt(string value, int line) => int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsed) ? parsed diff --git a/src/AquariumSynth.Dsl/Presets.cs b/src/AquariumSynth.Dsl/Presets.cs index 50385c7..6afe022 100644 --- a/src/AquariumSynth.Dsl/Presets.cs +++ b/src/AquariumSynth.Dsl/Presets.cs @@ -4,14 +4,14 @@ public static class Presets { public static SynthPatch AquariumPluck() { - var envelope = new Envelope(0.002f, 0, 1.38f, 0.43f); + var envelope = new Envelope(0.002f, 0, 0.7246377f, 0.43f); return new SynthPatch { Voices = [ - Simple(new Oscillator(Waveform.Sine, 440), envelope, 0.09f, 0.18f), - Simple(new Oscillator(Waveform.Triangle, 880), envelope, 0.09f, 0.05f), - Simple(new Oscillator(Waveform.Sine, 1760), envelope, 0.09f, 0.018f) + Simple(new Oscillator(Waveform.Sine, 440), envelope, 0.09f, 0.2484f), + Simple(new Oscillator(Waveform.Triangle, 880), envelope, 0.09f, 0.069f), + Simple(new Oscillator(Waveform.Sine, 1760), envelope, 0.09f, 0.02484f) ], Gain = 0.95f, SoftClip = true @@ -20,13 +20,13 @@ public static class Presets public static SynthPatch AquariumHeartbeat() { - var envelope = new Envelope(0.004f, 0, 1.62f, 0.2f); + var envelope = new Envelope(0.004f, 0, 0.617284f, 0.2f); return new SynthPatch { Voices = [ - Simple(new Oscillator(Waveform.Sine, 72), envelope, 0.08f, 0.22f), - Simple(new Oscillator(Waveform.Sine, 116), envelope, 0.08f, 0.09f) + Simple(new Oscillator(Waveform.Sine, 72), envelope, 0.08f, 0.3564f), + Simple(new Oscillator(Waveform.Sine, 116), envelope, 0.08f, 0.1458f) ], Gain = 0.9f, SoftClip = true @@ -35,7 +35,7 @@ public static class Presets public static SynthPatch AquariumVoice() { - var voice = Simple(new Oscillator(Waveform.Triangle, 220), new Envelope(0.018f, 0, 1.08f, 0.28f), 0.34f, 0.18f) with + var voice = Simple(new Oscillator(Waveform.Triangle, 220), new Envelope(0.018f, 0, 0.9259259f, 0.28f), 0.34f, 0.1944f) with { Pitch = new PitchMotion(VibratoDepth: 0.018f, VibratoHz: 5.6f), Color = new VoiceColor(0.035f, 0.18f, 0.04f, 0.12f, 4.2f, 0.68f), diff --git a/src/AquariumSynth.Dsl/SfxrParams.cs b/src/AquariumSynth.Dsl/SfxrParams.cs index 3cf24ee..cb0db7b 100644 --- a/src/AquariumSynth.Dsl/SfxrParams.cs +++ b/src/AquariumSynth.Dsl/SfxrParams.cs @@ -165,6 +165,7 @@ public sealed record SfxrParams public SynthPatch ToPatch() { var baseFrequency = SfxrFrequencyHz(BaseFreq); + var punchGain = PunchGain(EnvPunch); var voice = new Voice { Oscillator = new Oscillator(WaveType, baseFrequency, 0.5f - Math.Clamp(Duty, 0, 1) * 0.5f), @@ -172,7 +173,7 @@ public sealed record SfxrParams Envelope = new Envelope( NormalizedEnvSeconds(EnvAttack), 0, - 1 + Math.Clamp(EnvPunch, -1, 1), + 1 / punchGain, NormalizedEnvSeconds(EnvDecay)), Pitch = new PitchMotion( Math.Min(SfxrFrequencyHz(FreqLimit), baseFrequency), @@ -204,7 +205,7 @@ public sealed record SfxrParams 0, Math.Clamp(VibStrength, 0, 1) * 0.12f, 8 + Math.Clamp(VibSpeed, 0, 1) * 18), - Gain = 0.22f + Gain = 0.22f * punchGain }; return new SynthPatch @@ -223,6 +224,7 @@ public sealed record SfxrParams } private static float NormalizedEnvSeconds(float value) => Square(Math.Clamp(value, 0, 1)) * 100000 / 44100; + private static float PunchGain(float value) => 1 + Math.Max(0, Math.Clamp(value, -1, 1)); private static float Square(float value) => value * value; private static float Cube(float value) => value * value * value; } diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 1f89f24..a61fea8 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -15,4 +15,4 @@ {"kind":"dx7_reference_rebuilds","lesson":"Two DX7-style Aquarium rebuilds now make the pressure explicit: algorithm 32 maps cleanly to layered additive voices, but algorithm 8 exposes missing graph structure (operator modulation edges, summed modulators, and operator-local self-feedback). Do not patch this with more Voice fields; decide whether an operator graph model owns this invariant.","source":"DX7 rebuild catalog slice on 2026-05-13"} {"kind":"operator_graph_model","lesson":"DX7 algorithm-8 pressure justified a first-class OperatorGraph abstraction. OperatorGraph owns FM topology: operators, modulation edges, carriers, and feedback. Do not hide this authority inside Voice fields. Remaining DX7 pressure is exact feedback-register timing and rate/level envelopes.","source":"operator graph implementation slice on 2026-05-13"} {"kind":"operator_graph_authoring","lesson":"Operator graph authoring should use readable `operator`, `route`, and `carrier` declarations with `env=ad:...` or `env=adsr:...`. Compact `ops=`/`edges=` tuples are acceptable as importer/interchange scaffolding only, not the face of the DSL.","source":"user rejected number-soup operator syntax; readable operator graph syntax slice on 2026-05-13"} -{"kind":"note_envelope_model","lesson":"Aquarium's general envelope model is ADSR: attack, decay, sustain level, release. Note timing belongs to `Note` records: frequency, gate duration, and host/MIDI source. SFXR sustain maps to note gate duration; SFXR punch maps to non-unit sustain level only as compatibility import behavior.","source":"user identified SFXR sustain as note duration and Punch as non-unit sustain; note/envelope standardization slice on 2026-05-13"} +{"kind":"note_envelope_model","lesson":"Aquarium's general envelope model is ADSR: attack, decay, sustain level, release. Note timing belongs to `Note` records: frequency, gate duration, and host/MIDI source. SFXR sustain maps to note gate duration; SFXR punch maps to lower sustain level plus voice gain compensation so ADSR peak remains implicit 1 and the held level sits below the transient.","source":"user identified SFXR sustain as note duration and Punch as transient above sustain; punch mapping correction on 2026-05-13"} diff --git a/state/memory.json b/state/memory.json index 17f5516..8cf6930 100644 --- a/state/memory.json +++ b/state/memory.json @@ -56,7 +56,7 @@ }, "note_and_envelope_model": { "rule": "Notes own frequency, gate duration, and host/MIDI source. Envelopes own ADSR level shape.", - "sfxr_mapping": "SFXR sustain is note gate duration. SFXR punch maps to non-unit sustain level as compatibility import behavior, not as a general envelope field.", + "sfxr_mapping": "SFXR sustain is note gate duration. SFXR punch maps to lower ADSR sustain level plus voice gain compensation so the implicit attack peak stays at 1 and the held level sits below it.", "faust_surface": "One-shot patches use generated ADSR over patch age and note gate duration. Host/MIDI patches expose note frequency and note gate controls for the engine to wire." }, "runtime_parameters": { @@ -67,7 +67,7 @@ }, "last_verified": { "command": "dotnet test AquariumSynthCSharp.slnx --no-restore", - "result": "39 passed", - "commit": "pending note/envelope standardization commit" + "result": "40 passed", + "commit": "pending punch mapping correction commit" } } diff --git a/state/scratch.md b/state/scratch.md index 2c49003..961ec33 100644 --- a/state/scratch.md +++ b/state/scratch.md @@ -11,11 +11,12 @@ Completed this slice: gate duration, and host/MIDI source; `Envelope` now owns ADSR shape: attack, decay, sustain level, and release. - SFXR sustain duration now maps to `Note.GateSeconds`; SFXR punch maps to a - non-unit ADSR sustain level during import/legacy parsing. + lower ADSR sustain level plus voice gain compensation during import/legacy + parsing. - Added host note mode for MIDI-oriented patches through stable note frequency and note gate controls in generated Faust. - Moved built-in authoring examples off `punch=` and onto `sustain_level=`. -- Verified with `dotnet test AquariumSynthCSharp.slnx --no-restore`: 39 passed. +- Verified with `dotnet test AquariumSynthCSharp.slnx --no-restore`: 40 passed. Previous slice: diff --git a/state/spine.yaml b/state/spine.yaml index 2bb8c8d..4a41733 100644 --- a/state/spine.yaml +++ b/state/spine.yaml @@ -101,10 +101,11 @@ completed_slices: summary: > Standardized the live envelope model as ADSR and added explicit Note records for frequency, one-shot gate duration, and host/MIDI source. - SFXR sustain now maps to note gate duration; SFXR punch maps to non-unit - sustain level only as compatibility import behavior. Generated Faust can - emit one-shot ADSR envelopes or host gate/frequency controls. - verification: "dotnet test AquariumSynthCSharp.slnx --no-restore: 39 passed" + SFXR sustain now maps to note gate duration; SFXR punch maps to lower + ADSR sustain level plus voice gain compensation as compatibility import + behavior. Generated Faust can emit one-shot ADSR envelopes or host + gate/frequency controls. + verification: "dotnet test AquariumSynthCSharp.slnx --no-restore: 40 passed" - date: 2026-05-13 summary: > Added readable operator graph authoring syntax with `operator`, `route`, diff --git a/tests/AquariumSynth.Dsl.Tests/PatchScriptTests.cs b/tests/AquariumSynth.Dsl.Tests/PatchScriptTests.cs index 2d86acb..df65bcb 100644 --- a/tests/AquariumSynth.Dsl.Tests/PatchScriptTests.cs +++ b/tests/AquariumSynth.Dsl.Tests/PatchScriptTests.cs @@ -354,6 +354,16 @@ public sealed class PatchScriptTests Assert.Contains("oneshot_adsr", export.Source); } + [Fact] + public void LegacyPunchMapsToTransientPeakOverSustain() + { + var patch = PatchScript.Parse("v w=saw f=220 gain=.2 punch=.5"); + var voice = Assert.Single(patch.Voices); + + Assert.Equal(.3f, voice.Gain, 5); + Assert.Equal(2f / 3f, voice.Envelope.SustainLevel, 5); + } + [Fact] public void MidiVoiceEmitsHostNoteControls() {