From b54d90d71b4ee821c1d4c24b8850ec3c5e9254b1 Mon Sep 17 00:00:00 2001 From: Aqua Date: Sat, 14 Nov 2026 00:23:00 +0000 Subject: [PATCH] Pin the Rosenberg C waveform, its zero mean and saw-matched rms, and the noise_hp corner in Hz (RosenbergPulse made public for the test) Co-Authored-By: Claude Sonnet 5.5 --- src/AquaSynth.Core/FaustExport.cs | 2 +- .../GlottalSourceSpectrumTests.cs | 90 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/AquaSynth.Core/FaustExport.cs b/src/AquaSynth.Core/FaustExport.cs index 01e0fd9..c48c1cf 100644 --- a/src/AquaSynth.Core/FaustExport.cs +++ b/src/AquaSynth.Core/FaustExport.cs @@ -770,7 +770,7 @@ public static partial class FaustEmitter // Rosenberg C flow pulse of one period (phase p of 0..1): opening 0.5 (1 - cos(pi p / 0.4)) for p < 0.4, return cos(pi/2 (p - 0.4) / 0.16) // for the next 0.16, closed after. Its mean is 0.4 / 2 + 0.16 * 2 / pi = 0.301859 and its rms about that mean is 0.372667; it is made zero-mean // and scaled by 0.57735 / 0.372667 = 1.549 to the rms of os.sawtooth (1 / sqrt 3), so a swap of the pulse leaves the SOURCE level where it was (the rendered level moves: the pulse has no strong upper harmonics for the tract to pass). - private static string RosenbergPulse(string frequency) => + public static string RosenbergPulse(string frequency) => $"(((os.lf_sawpos({frequency}) : (\\(p).(select2(p < 0.4, select2(p < 0.56, 0.0, cos(ma.PI * 0.5 * (p - 0.4) / 0.16)), 0.5 * (1.0 - cos(ma.PI * p / 0.4)))))) - 0.301859) * 1.549)"; private static (string Released, string Burst) PrimitiveContactExpression( diff --git a/tests/AquaSynth.Dsl.Tests/GlottalSourceSpectrumTests.cs b/tests/AquaSynth.Dsl.Tests/GlottalSourceSpectrumTests.cs index 4fda737..8307d61 100644 --- a/tests/AquaSynth.Dsl.Tests/GlottalSourceSpectrumTests.cs +++ b/tests/AquaSynth.Dsl.Tests/GlottalSourceSpectrumTests.cs @@ -161,4 +161,94 @@ public sealed class GlottalSourceSpectrumTests(ITestOutputHelper output) var between = Magnitude(pulse, Pitch * 1.5); Assert.True(Db(between / fundamental) < -30, $"energy between harmonics {Db(between / fundamental):F1} dB re the fundamental"); } + + // One expression compiled alone as a one-output DSP. The pulse is run at 100 Hz, which divides the rate (480 samples a period), so every sample + // sits at a known phase; the patch path scales a bare signal by a constant, so levels are read against os.sawtooth through the same path. + // Expected values are Rosenberg C written out here, independent of the emitted formula's spelling. + private float[]? RenderBare(string expression, double seconds) + { + var dsp = $"import(\"stdfaust.lib\");\nprocess = {expression};\n"; + using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: Rate)); + if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("pulse_bare", "pulse_bare", ""), dsp, (float)seconds + .1f, out var patch, out var error)) + { + if (error?.Contains("Faust toolchain not found", StringComparison.OrdinalIgnoreCase) == true || + error?.Contains("Faust DLL not found", StringComparison.OrdinalIgnoreCase) == true) + { + return null; + } + + Assert.Fail($"bare pulse compile failed: {error}"); + } + + using (patch) return patch!.Render((float)seconds); + } + + private static double RosenbergC(double p) => + p < 0.4 ? 0.5 * (1 - Math.Cos(Math.PI * p / 0.4)) : p < 0.56 ? Math.Cos(Math.PI * 0.5 * (p - 0.4) / 0.16) : 0; + + [Fact] + public void RosenbergPulseIsTheDocumentedRosenbergCZeroMeanAtTheSawsRms() + { + const int period = 480; + var pulse = RenderBare(FaustEmitter.RosenbergPulse("100.0"), 0.5); + var saw = RenderBare("os.sawtooth(100.0)", 0.5); + if (pulse is null || saw is null) return; + var cycle = pulse.Skip(period).Take(period).ToArray(); + double Rms(IEnumerable w) => Math.Sqrt(w.Sum(v => (double)v * v) / w.Count()); + // Calibrate the path's scale on the saw (rms 1/sqrt3 at the source): read every pulse sample back at source level. + var scale = Rms(saw.Skip(period).Take(period)) / (1 / Math.Sqrt(3)); + cycle = cycle.Select(v => (float)(v / scale)).ToArray(); + + // Rosenberg C has mean 0.301859 and rms 0.372667 about it; the emitted pulse is that, zero-mean, scaled to the saw's rms. + Assert.InRange(cycle.Average(), -0.003, 0.003); + var rms = Rms(cycle); + output.WriteLine($"pulse rms {rms:F5} (saw 1/sqrt3 = {1 / Math.Sqrt(3):F5}), path scale {scale:F4}, peak {cycle.Max():F4}, floor {cycle.Min():F4}"); + Assert.InRange(Db(rms / (1 / Math.Sqrt(3))), -0.05, 0.05); + + // Shape at every phase point. The phasor's alignment to the sample grid is not part of the contract, so find the shift that fits, + // then hold every point to the closed form: opening cosine, return cosine, closed plateau. A linear opening, another scale or no return phase all miss. + double Expected(int n, int shift) => (RosenbergC(((n + shift) % period) / (double)period) - 0.301859) * 1.549; + var shift = Enumerable.Range(0, period).MinBy(k => Enumerable.Range(0, period).Sum(n => Math.Pow(cycle[n] - Expected(n, k), 2))); + var worst = Enumerable.Range(0, period).Max(n => Math.Abs(cycle[n] - Expected(n, shift))); + output.WriteLine($"best shift {shift} samples, worst deviation from Rosenberg C {worst:E2}"); + Assert.True(worst < 0.01, $"worst deviation from Rosenberg C {worst:E2} at shift {shift}"); + // The regions by value: the opening peaks at p = 0.4 at (1 - mean) * 1.549, the closed floor is -mean * 1.549 and holds for 0.44 of the period (plus the tail of the return cosine inside the tolerance). + Assert.InRange(cycle.Max(), (1 - 0.301859) * 1.549 - 0.01, (1 - 0.301859) * 1.549 + 0.01); + Assert.InRange(cycle.Min(), -0.301859 * 1.549 - 0.01, -0.301859 * 1.549 + 0.01); + Assert.InRange(cycle.Count(v => Math.Abs(v - (-0.301859 * 1.549)) < 0.01), (int)(0.44 * period) - 15, (int)(0.44 * period) + 15); + } + + [Fact] + public void NoiseHighPassCornerIsTheAuthoredHertz() + { + // The source's noise alone (pressure 0 silences the voiced part), white and through fi.highpass(2, fc): band energy against the white render tells the corner. + // A 2 kHz corner leaves almost nothing under 700 Hz, passes 3-3.8 kHz whole and takes a few dB off 1.6-2.4 kHz; a 1 kHz corner passes the middle band; a 4 kHz corner cuts it. + double Band(float[] w, double lo, double hi) + { + double sum = 0; + for (var hz = lo; hz < hi; hz += 25) sum += Math.Pow(Magnitude(w, hz), 2); + return sum; + } + + double[] Shape(string extra) + { + var window = Render(.5f, extra: "pressure=0 noise=2 " + extra); + return window is null ? [] : [Band(window, 100, 700), Band(window, 1600, 2400), Band(window, 3000, 3800)]; + } + + var white = Shape(""); + var at2k = Shape("noise_hp=2000 pulse=saw"); + if (white.Length == 0 || at2k.Length == 0) return; + var at1k = Shape("noise_hp=1000"); + var at4k = Shape("noise_hp=4000"); + output.WriteLine($"band energies (100-700, 1600-2400, 3000-3800 Hz) white {string.Join(" ", white.Select(b => b.ToString("E2")))}"); + foreach (var (name, shape) in new[] { ("1k", at1k), ("2k", at2k), ("4k", at4k) }) output.WriteLine($"hp {name}: " + string.Join(" ", shape.Select(b => b.ToString("E2")))); + double Rel(double[] shape, int band) => Db(Math.Sqrt(shape[band] / white[band])); + Assert.True(Rel(at2k, 0) < -18, $"2 kHz corner leaves {Rel(at2k, 0):F1} dB at 100-700 Hz"); + Assert.InRange(Rel(at2k, 1), -6, -1.5); + Assert.True(Rel(at2k, 2) > -1.5, $"2 kHz corner leaves {Rel(at2k, 2):F1} dB at 3-3.8 kHz"); + // A 1 kHz corner passes the 1.6-2.4 kHz band nearly whole; a 4 kHz corner cuts it hard. + Assert.True(Rel(at1k, 1) > -2, $"1 kHz corner: {Rel(at1k, 1):F1} dB at 1.6-2.4 kHz"); + Assert.True(Rel(at4k, 1) < -9, $"4 kHz corner: {Rel(at4k, 1):F1} dB at 1.6-2.4 kHz"); + } }