From 282efc4a68034209c8418d86b712d8e70384e19a Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 12 May 2026 21:24:42 +0100 Subject: [PATCH] Add Aquarium synth playback tests --- Aquarium.Engine.sln | 17 +++++++ Aquarium.Engine.slnx | 3 ++ .../Audio/AquariumSynthHost.cs | 28 ++++++++++++ .../Properties/AssemblyInfo.cs | 3 ++ state/evidence.jsonl | 1 + .../Aquarium.Engine.Tests.csproj | 26 +++++++++++ .../SynthPlaybackTests.cs | 45 +++++++++++++++++++ 7 files changed, 123 insertions(+) create mode 100644 src/Aquarium.Engine/Properties/AssemblyInfo.cs create mode 100644 tests/Aquarium.Engine.Tests/Aquarium.Engine.Tests.csproj create mode 100644 tests/Aquarium.Engine.Tests/SynthPlaybackTests.cs diff --git a/Aquarium.Engine.sln b/Aquarium.Engine.sln index 6d111f2..95d49bd 100644 --- a/Aquarium.Engine.sln +++ b/Aquarium.Engine.sln @@ -29,6 +29,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameCult.Networking", "..\C EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aquarium.Sample.Minimal", "src\Aquarium.Sample.Minimal\Aquarium.Sample.Minimal.csproj", "{7564E6C2-FF01-4B5B-8C2B-F9CD796858BA}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0AB3BF05-4346-4AA6-1389-037BE0695223}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aquarium.Engine.Tests", "tests\Aquarium.Engine.Tests\Aquarium.Engine.Tests.csproj", "{37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -183,6 +187,18 @@ Global {7564E6C2-FF01-4B5B-8C2B-F9CD796858BA}.Release|x64.Build.0 = Release|Any CPU {7564E6C2-FF01-4B5B-8C2B-F9CD796858BA}.Release|x86.ActiveCfg = Release|Any CPU {7564E6C2-FF01-4B5B-8C2B-F9CD796858BA}.Release|x86.Build.0 = Release|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Debug|x64.ActiveCfg = Debug|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Debug|x64.Build.0 = Debug|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Debug|x86.ActiveCfg = Debug|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Debug|x86.Build.0 = Debug|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Release|Any CPU.Build.0 = Release|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Release|x64.ActiveCfg = Release|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Release|x64.Build.0 = Release|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Release|x86.ActiveCfg = Release|Any CPU + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -192,5 +208,6 @@ Global {51FE01D8-9EEF-46B2-8D8C-3307AC2221C1} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} {33654E0D-B31A-4062-B6AC-C1CE4C1A1F65} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} {7564E6C2-FF01-4B5B-8C2B-F9CD796858BA} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B} + {37363FC6-5AA3-49B4-A3B1-E4AB2CC986EB} = {0AB3BF05-4346-4AA6-1389-037BE0695223} EndGlobalSection EndGlobal diff --git a/Aquarium.Engine.slnx b/Aquarium.Engine.slnx index 3e24fef..6d91741 100644 --- a/Aquarium.Engine.slnx +++ b/Aquarium.Engine.slnx @@ -6,4 +6,7 @@ + + + diff --git a/src/Aquarium.Engine/Audio/AquariumSynthHost.cs b/src/Aquarium.Engine/Audio/AquariumSynthHost.cs index 93b7b22..fdb4a9b 100644 --- a/src/Aquarium.Engine/Audio/AquariumSynthHost.cs +++ b/src/Aquarium.Engine/Audio/AquariumSynthHost.cs @@ -18,6 +18,34 @@ internal sealed class AquariumSynthHost : IDisposable private string? faustLoadError; private float timeSeconds; + internal static bool TryRenderScriptWithNativeFaust(string name, string script, float gain, out float[] samples, out string? error) + { + samples = []; + if (!FaustNative.TryLoad(out var native, out var loadError)) + { + error = loadError; + return false; + } + + using (native) + { + try + { + var patch = PatchScript.Parse(script); + var export = FaustEmitter.Emit(patch, new FaustExportOptions(SafeFaustName(name))); + using var compiled = native!.Compile(SafeFaustName(name), export.Source, EstimateDuration(patch)); + samples = compiled.Render(gain); + error = null; + return true; + } + catch (Exception ex) + { + error = ex.Message; + return false; + } + } + } + public void Update(AquariumSynthDocument synth, float deltaSeconds) { timeSeconds += Math.Max(deltaSeconds, 0.0f); diff --git a/src/Aquarium.Engine/Properties/AssemblyInfo.cs b/src/Aquarium.Engine/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..71b3f58 --- /dev/null +++ b/src/Aquarium.Engine/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Aquarium.Engine.Tests")] diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 59e43ed..5dfb1f4 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -73,3 +73,4 @@ {"timestamp":"2026-05-12T20:04:11+01:00","topic":"debug-ui-mouse-input-capture","lesson":"Renderer input capture must include both keyboard ownership and mouse ownership. If Debug UI consumes wheel or mouse interaction but the renderer only reports keyboard capture, the same wheel event leaks into client camera/runtime controls after the overlay handles it.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","state/evidence.jsonl"],"source":"user dropdown scroll capture correction"} {"timestamp":"2026-05-12T20:20:42+01:00","topic":"synth-playground-control-authority","lesson":"The synth playground's source text owns recompilation through the synth host compile key and debounce; Play owns only manual trigger firing. Do not make Play force compilation or present compile status as an interactive control when it is only state feedback.","files":["src/Aquarium.Engine/Render/D3D12Renderer.cs","state/evidence.jsonl"],"source":"user synth playground UI correction"} {"timestamp":"2026-05-12T21:07:53+01:00","topic":"synth-failed-compile-state","lesson":"A failed synth compile key must be remembered separately from the ready compile key. If failed source leaves ReadyKey empty while DesiredKey still points at the same bad script, the host retries every frame and the UI appears stuck on Compiling instead of showing Failed until the source changes.","files":["src/Aquarium.Engine/Audio/AquariumSynthHost.cs","state/evidence.jsonl"],"source":"user failed compilation status correction"} +{"timestamp":"2026-05-12T21:19:20+01:00","topic":"aquarium-synth-dogfood-tests","lesson":"The deprecated Rust synth fork can remain historical evidence, but active synth confidence must live in the C# dogfood path. Aquarium needs tests that render built-in scripts through its native Faust runtime wrapper and inspect the resulting samples, not just parser/export tests in the DSL library.","files":["src/Aquarium.Engine/Audio/AquariumSynthHost.cs","tests/Aquarium.Engine.Tests/SynthPlaybackTests.cs","state/evidence.jsonl"],"source":"user deprecated Rust repo clarification"} diff --git a/tests/Aquarium.Engine.Tests/Aquarium.Engine.Tests.csproj b/tests/Aquarium.Engine.Tests/Aquarium.Engine.Tests.csproj new file mode 100644 index 0000000..0188012 --- /dev/null +++ b/tests/Aquarium.Engine.Tests/Aquarium.Engine.Tests.csproj @@ -0,0 +1,26 @@ + + + + net10.0 + enable + enable + false + + + + + + + + + + + + + + + + + + + diff --git a/tests/Aquarium.Engine.Tests/SynthPlaybackTests.cs b/tests/Aquarium.Engine.Tests/SynthPlaybackTests.cs new file mode 100644 index 0000000..80d1de6 --- /dev/null +++ b/tests/Aquarium.Engine.Tests/SynthPlaybackTests.cs @@ -0,0 +1,45 @@ +using Aquarium.Engine.Audio; +using AquariumSynth.Dsl; + +namespace Aquarium.Engine.Tests; + +public sealed class SynthPlaybackTests +{ + [Fact] + public void ClassicSfxrPresetsRenderAudibleDistinctShapesThroughAquariumFaustPath() + { + var analyzer = new AudioAnalyzer(new AudioAnalysisConfig( + FftSize: 512, + HopSize: 256, + MelBandCount: 24)); + var renders = new List<(string Name, float[] Samples, AudioAnalysis Analysis)>(); + + foreach (var (name, script) in BuiltInScripts.ClassicSfxrPrimitiveGolfScripts) + { + if (!AquariumSynthHost.TryRenderScriptWithNativeFaust($"sfxr_{name}", script, 1.0f, out var samples, out var error)) + { + Assert.Fail($"{name} failed to render through Aquarium Faust path: {error}"); + } + + var analysis = analyzer.Analyze(samples); + renders.Add((name, samples, analysis)); + Assert.True(samples.Length > 2048, $"{name} rendered too few samples: {samples.Length}"); + Assert.InRange(analysis.Features.Peak, 0.005f, 1.0f); + Assert.InRange(analysis.Features.Rms, 0.0005f, 0.8f); + Assert.InRange(analysis.Features.DurationSeconds, 0.015f, 1.4f); + Assert.True(analysis.Features.ZeroCrossingRate > 20.0f, $"{name} zero-crossing rate is suspiciously low: {analysis.Features.ZeroCrossingRate}"); + } + + Assert.Equal(BuiltInScripts.ClassicSfxrPrimitiveGolfScripts.Count, renders.Count); + for (var index = 1; index < renders.Count; index++) + { + var previous = renders[index - 1]; + var current = renders[index]; + var comparison = analyzer.Compare(previous.Samples, current.Samples); + + Assert.True( + comparison.Score < 0.98f, + $"{previous.Name} and {current.Name} rendered nearly identical envelopes; score={comparison.Score:0.000}"); + } + } +}