From f633804bd39fcb7a9204e5807528a39e8f814c3b Mon Sep 17 00:00:00 2001 From: Aqua Date: Sun, 8 Nov 2026 03:41:20 +0000 Subject: [PATCH] Vowel gestures: tract shapes found by grid search over the exported ladder EmitVowelGesture now holds tongue index, tongue and lip diameters (cm) from anchors measured by impulse response toward textbook F1/F2 (i 271/2313, u 292/876, a 703/1084, plus ae, e, o, schwa), with the constriction opened. The old targets put a at 310/2130 and i at 620/1620 through the same ladder. Tests render the authored gestures end to end and read LPC envelope peaks. Co-Authored-By: Claude Sonnet 5.5 --- src/AquaSynth.Core/PatchScript.cs | 48 ++++++-- .../VocalTractFormantTests.cs | 114 ++++++++++++++++++ 2 files changed, 155 insertions(+), 7 deletions(-) diff --git a/src/AquaSynth.Core/PatchScript.cs b/src/AquaSynth.Core/PatchScript.cs index 1b05e93..5d07533 100644 --- a/src/AquaSynth.Core/PatchScript.cs +++ b/src/AquaSynth.Core/PatchScript.cs @@ -1487,18 +1487,52 @@ public static class PatchScript } } + // Vowel tract shapes for the Kelly-Lochbaum ladder on the experiment scaffold (six rest diameters, 17 cm, 48 kHz): tongue + // section index (0..5), tongue and lip diameters in cm. Each was found by grid search over the exported ladder's impulse + // response (peak picking) toward textbook F1/F2, not by hand: measured F1/F2 in Hz in the trailing comment. + // Features are (front 0..1, openness 0..1, rounded 0/1); a descriptor selects the nearest anchor, there is no interpolation + // because the shapes are not monotone in formant space. + private static readonly (float Front, float Open, float Round, float TongueIndex, float TongueCm, float LipCm)[] VowelShapes = + [ + (1.0f, 0.0f, 0, 3.00f, 0.15f, 2.20f), // i 271/2313 + (0.0f, 0.0f, 1, 2.25f, 0.45f, 0.05f), // u 292/876 + (0.0f, 1.0f, 0, 1.50f, 0.35f, 1.00f), // a 703/1084 + (1.0f, 1.0f, 0, 0.50f, 0.30f, 1.00f), // ae 665/1755 + (1.0f, 0.5f, 0, 2.50f, 1.80f, 2.20f), // e 535/1799 + (0.0f, 0.5f, 1, 1.50f, 0.10f, 1.60f), // o 454/962 + (0.5f, 0.5f, 0, 2.00f, 1.20f, 0.60f), // schwa 497/1503 + ]; + private void EmitVowelGesture(string name, HashSet tokens, float start, float duration, float intensity) { - var frontness = tokens.Contains("front") ? 0.70f : tokens.Contains("back") ? 0.30f : 0.50f; - var openness = tokens.Contains("close") ? 0.30f : tokens.Contains("mid") ? 0.50f : tokens.Contains("open") ? 0.78f : 0.55f; - var rounding = tokens.Contains("rounded") ? 0.35f : tokens.Contains("unrounded") ? 0.72f : 0.60f; - AddAuthoredSpline($"{name}_tongue_index", "/vocal/areas/0/area/tongue_index", start, duration, (0, frontness), (1, frontness)); - AddAuthoredSpline($"{name}_tongue_diameter", "/vocal/areas/0/area/tongue_diameter", start, duration, (0, openness), (1, openness)); - AddAuthoredSpline($"{name}_lip_area", "/vocal/areas/0/area/lip_opening", start, duration, (0, rounding), (1, rounding)); - AddAuthoredSpline($"{name}_lip_radiation", "/vocal/radiation/0/aperture", start, duration, (0, Math.Clamp(rounding + 0.1f * intensity, 0, 1)), (1, Math.Clamp(rounding + 0.1f * intensity, 0, 1))); + var front = tokens.Contains("front") ? 1f : tokens.Contains("back") ? 0f : 0.5f; + var open = tokens.Contains("close") ? 0f : tokens.Contains("mid") ? 0.5f : tokens.Contains("open") ? 1f : 0.5f; + var round = tokens.Contains("rounded") && !tokens.Contains("unrounded") ? 1f : 0f; + var shape = VowelShapes + .OrderBy(candidate => MathF.Abs(candidate.Front - front) + MathF.Abs(candidate.Open - open) + MathF.Abs(candidate.Round - round)) + .First(); + HoldOnSurface($"{name}_tongue_index", "/vocal/areas/0/area/tongue_index", start, duration, shape.TongueIndex); + HoldOnSurface($"{name}_tongue_diameter", "/vocal/areas/0/area/tongue_diameter", start, duration, shape.TongueCm); + HoldOnSurface($"{name}_lip_area", "/vocal/areas/0/area/lip_opening", start, duration, shape.LipCm); + // A vowel leaves the tract unconstricted: hold the constriction at the surface maximum. + AddAuthoredSpline($"{name}_constriction_diameter", "/vocal/areas/0/area/constriction_diameter", start, duration, (0, 1), (1, 1)); + var aperture = Math.Clamp(shape.LipCm / 2.2f * (0.9f + 0.1f * intensity), 0.02f, 1); + AddAuthoredSpline($"{name}_lip_radiation", "/vocal/radiation/0/aperture", start, duration, (0, aperture), (1, aperture)); AddAuthoredSpline($"{name}_velum", "/vocal/branches/0/opening", start, duration, (0, 0.01f), (1, 0.01f)); } + // Holds a surface at a value in its own unit (cm, section index); splines carry the surface's normalized range. + private void HoldOnSurface(string name, string surfacePath, float start, float duration, float value) + { + if (!_controlSurfacesByPath.TryGetValue(surfacePath, out var surface) || surface.MaxValue <= surface.MinValue) + { + return; + } + + var normalized = Math.Clamp((value - surface.MinValue) / (surface.MaxValue - surface.MinValue), 0, 1); + AddAuthoredSpline(name, surfacePath, start, duration, (0, normalized), (1, normalized)); + } + private static string GestureDescriptor(string ipa, IReadOnlyDictionary fields) { var explicitDescriptor = GetAny(fields, ["descriptor", "desc", "features"], ""); diff --git a/tests/AquaSynth.Dsl.Tests/VocalTractFormantTests.cs b/tests/AquaSynth.Dsl.Tests/VocalTractFormantTests.cs index ec7d20a..024d22a 100644 --- a/tests/AquaSynth.Dsl.Tests/VocalTractFormantTests.cs +++ b/tests/AquaSynth.Dsl.Tests/VocalTractFormantTests.cs @@ -191,4 +191,118 @@ public sealed class VocalTractFormantTests(ITestOutputHelper output) output.WriteLine($"mouth shut: velum closed rms {Rms(shut):E2}, velum open rms {Rms(open):E2}; open peaks {string.Join(" ", Peaks(open).Select(p => p.ToString("F0")))}"); Assert.True(Rms(open) > 5 * Rms(shut), $"nasal output rms {Rms(open):E2} vs velum-closed {Rms(shut):E2}"); } + + // The authored vowel gestures (PatchScript's VowelShapes, reached through the IPA word script) rendered end to end through the + // exporter, merged splines and the ladder; the source is a bright 10 Hz saw so the tract's response is sampled densely. + private double[]? GesturePeaks(string ipa) + { + var script = IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget($"v_{ipa}", ipa, "", 1.5f), 1f)], Pitch, 0.1f) + .Replace("noise=.04", "noise=0 tilt=1", StringComparison.Ordinal) + .Replace($"freq={Pitch}", $"freq={Pitch} min_freq=5", StringComparison.Ordinal); + var window = Render(script); + if (window is null) return null; + var peaks = LpcPeaks(window); + output.WriteLine($"gesture {ipa}: peaks " + string.Join(" ", peaks.Select(p => p.ToString("F0")))); + Assert.True(peaks.Length >= 2, $"{ipa}: fewer than two spectral peaks"); + return peaks; + } + + // Formant candidates as the peaks of an LPC envelope (order 12 on the 12 kHz decimation of a 0.25 s steady stretch, pre-emphasis + // .97), the same recipe the render measurements use: the envelope sees through the harmonic lines of the voiced source. + private static double[] LpcPeaks(float[] window) + { + const int Decimate = 4, Order = 12; + var taps = Enumerable.Range(0, 4 * Decimate + 1).Select(n => 0.5 - 0.5 * Math.Cos(2 * Math.PI * (n + 1) / (4 * Decimate + 2))).ToArray(); + var norm = taps.Sum(); + var x = window.Skip(Rate / 4).Take(Rate / 4).Select(v => (double)v).ToArray(); + var mean = x.Average(); + var z = new List(); + for (var n = 2 * Decimate; n < x.Length - 2 * Decimate; n += Decimate) + { + double acc = 0; + for (var k = 0; k < taps.Length; k++) acc += taps[k] * (x[n + k - 2 * Decimate] - mean); + z.Add(acc / norm); + } + + var y = new double[z.Count]; + for (var n = 0; n < y.Length; n++) y[n] = (z[n] - (n > 0 ? .97 * z[n - 1] : 0)) * (0.54 - 0.46 * Math.Cos(2 * Math.PI * n / (y.Length - 1))); + var r = Enumerable.Range(0, Order + 1).Select(lag => Enumerable.Range(0, y.Length - lag).Sum(n => y[n] * y[n + lag])).ToArray(); + r[0] *= 1 + 1e-9; + var a = new double[Order + 1]; + a[0] = 1; + var err = r[0]; + for (var m = 1; m <= Order; m++) + { + var acc = r[m]; + for (var k = 1; k < m; k++) acc += a[k] * r[m - k]; + var kappa = -acc / err; + var next = (double[])a.Clone(); + for (var k = 1; k < m; k++) next[k] = a[k] + kappa * a[m - k]; + next[m] = kappa; + a = next; + err *= 1 - kappa * kappa; + } + + var fs = Rate / (double)Decimate; + double Envelope(double hz) + { + double re = 0, im = 0; + for (var k = 0; k <= Order; k++) { re += a[k] * Math.Cos(2 * Math.PI * hz / fs * k); im -= a[k] * Math.Sin(2 * Math.PI * hz / fs * k); } + return 1 / Math.Sqrt(re * re + im * im); + } + + var peaks = new List(); + var previous = Envelope(150); + var current = Envelope(160); + for (var hz = 170.0; hz < 4500; hz += 10) + { + var next = Envelope(hz); + if (current > previous && current >= next) peaks.Add(hz - 10); + (previous, current) = (current, next); + } + + return peaks.ToArray(); + } + + // Measured through the exported ladder on 2026-11-08 (impulse peaks i 271/2313, u 292/876, a 703/1084; the textbook values are + // i 270/2290, u 300/870, a 730/1090). The envelope of the 10 Hz render reads i 270/2380 and a 690/1060. + public static TheoryData GestureVowels => new() + { + { "i", 271, 2313 }, + { "a", 703, 1084 }, + }; + + [Theory] + [MemberData(nameof(GestureVowels))] + public void AuthoredVowelGesturesPlaceF1AndF2NearTheTextbook(string ipa, double f1, double f2) + { + var peaks = GesturePeaks(ipa); + if (peaks is null) return; + + Assert.InRange(peaks[0], f1 * .9, f1 * 1.1); + Assert.InRange(peaks[1], f2 * .9, f2 * 1.1); + } + + [Fact] + public void AuthoredRoundedBackVowelPlacesF2NearTheTextbook() + { + // u's F1 (about 290 Hz) is under the order-12 envelope's resolution; its F2 is the lowest peak it resolves. + var peaks = GesturePeaks("u"); + if (peaks is null) return; + + Assert.InRange(peaks[0], 876 * .9, 876 * 1.1); + } + + [Fact] + public void AuthoredVowelGesturesDifferInTheirFormants() + { + var i = GesturePeaks("i"); + var u = GesturePeaks("u"); + var a = GesturePeaks("a"); + if (i is null || u is null || a is null) return; + + Assert.True(a[0] > i[0] + 250, $"F1 i {i[0]} a {a[0]}"); + // F2: i 2380 > a 1060 > u 840. + Assert.True(i[1] > a[1] + 800 && a[1] > u[0] + 100, $"F2 i {i[1]} a {a[1]} u {u[0]}"); + } }