Stress carries duration, amplitude and f0 (source pitch_ratio surface); plosive release burst from contact release_flow; vowels open the contact

Primary stress: duration 220/160, source flow x1.26 (+2 dB), f0 x1.06 (Sluijter & van Heuven 1996); secondary halfway (design value).
The exporter's contact term adds white noise shaped by stored pressure and 4*o*(1-o) of the opening, gain from release_flow.
Unheard.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Aqua 2026-11-08 19:25:02 +00:00
commit 563a6a8e1f
8 changed files with 203 additions and 31 deletions

View file

@ -0,0 +1,98 @@
using AquaSynth.Dsl;
using AquaSynth.Faust;
using Xunit.Abstractions;
namespace AquaSynth.Dsl.Tests;
// Renders the two cues the tract gained in this pass through the native Faust path: the source's f0 ratio (the stress pitch cue) and
// the plosive release burst. Each asserts a measured signal, not the emitted text.
[Collection("NativeFaust")]
public sealed class StressAndReleaseRenderTests(ITestOutputHelper output)
{
private const int Rate = 48000;
private static float[]? Render(string script, float seconds, Func<SynthPatch, SynthPatch>? edit = null)
{
var parsed = PatchScript.Parse(script);
var source = FaustEmitter.Emit(edit is null ? parsed : edit(parsed), new FaustExportOptions("stress_release", SampleRate: Rate)).Source;
using var compiler = new AquaSynthPatchCompiler(new AquaSynthNativeOptions(SampleRate: Rate));
if (!compiler.TryCompileSource(new AquaSynthCompileIdentity("stress_release", "stress_release", ""), source, seconds, 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($"compile failed: {error}");
}
using (patch) return patch!.Render(seconds);
}
private static double Rms(float[] x, double from, double to)
{
var a = (int)(from * Rate);
var b = Math.Min(x.Length, (int)(to * Rate));
double sum = 0;
for (var i = a; i < b; i++) sum += (double)x[i] * x[i];
return Math.Sqrt(sum / Math.Max(1, b - a));
}
private static double Peak(float[] x, double from, double to) =>
x.Skip((int)(from * Rate)).Take((int)((to - from) * Rate)).Max(Math.Abs);
// f0 by the highest autocorrelation peak between 60 and 400 Hz.
private static double F0(float[] x, double from, double to)
{
var seg = x.Skip((int)(from * Rate)).Take((int)((to - from) * Rate)).Select(v => (double)v).ToArray();
double best = 0; var bestLag = 0;
for (var lag = Rate / 400; lag <= Rate / 60; lag++)
{
double c = 0;
for (var i = 0; i + lag < seg.Length; i++) c += seg[i] * seg[i + lag];
if (c > best) { best = c; bestLag = lag; }
}
return (double)Rate / bestLag;
}
[Fact]
public void SourcePitchRatioScalesTheRenderedF0()
{
var f0 = (float ratio) =>
{
var script = IpaGestureExperiment.BuildWordScript([(new IpaGestureExperimentTarget("w_0_a", "a", "", 0.5f, PitchRatio: ratio), 1f)], 110);
var x = Render(script, 0.6f);
return x is null ? double.NaN : F0(x, 0.2, 0.5);
};
var unity = f0(1f);
var up = f0(1.5f);
output.WriteLine($"f0 at ratio 1: {unity:F1}, ratio 1.5: {up:F1}");
if (double.IsNaN(unity)) return;
Assert.InRange(unity, 108, 112);
Assert.InRange(up, 163, 167);
}
[Fact]
public void PlosiveReleaseBurstIsAboveTheClosureAndBelowTheFollowingVowel()
{
var script = IpaGestureExperiment.BuildWordScript(
[(new IpaGestureExperimentTarget("w_0_b", "b", "", 0.095f), 1f), (new IpaGestureExperimentTarget("w_1_a", "a", "", 0.18f), 1f)], 140);
var withBurst = Render(script, 0.4f);
var withoutBurst = Render(script, 0.4f, p => p with { ControlSplines = p.ControlSplines.Where(s => !s.SurfacePath.EndsWith("/release_flow", StringComparison.Ordinal)).ToArray() });
if (withBurst is null || withoutBurst is null) return;
// Everything but the burst's noise is deterministic and shared, so the difference of the two renders is the burst alone.
var burst = withBurst.Zip(withoutBurst, (x, y) => x - y).ToArray();
const double releaseFrom = 0.095 * 0.65, vowelFrom = 0.095;
var vowelPeak = Peak(withBurst, vowelFrom + 0.06, vowelFrom + 0.18);
var burstPeak = Peak(burst, releaseFrom, vowelFrom + 0.01);
var burstBeforeRelease = Peak(burst, 0, releaseFrom - 0.005);
var burstAfter = Peak(burst, vowelFrom + 0.04, 0.4);
output.WriteLine($"burst peak {burstPeak:F4} in the release, {burstBeforeRelease:F5} in the closure, {burstAfter:F5} in the vowel; vowel peak {vowelPeak:F4}");
Assert.True(burstPeak > 0.004, "the burst is a transient of audible size in the release window");
Assert.True(burstBeforeRelease < burstPeak / 20, "the closure is silent: no burst before the release");
Assert.True(burstAfter < burstPeak / 5, "the burst is over once the vowel is open");
Assert.True(burstPeak < vowelPeak / 2, "the burst stays well under the following vowel's peak (the b level fix)");
}
}

View file

@ -74,7 +74,7 @@ public sealed class WeksaPhonesTests
var word = WeksaPhonesReader.Parse(Line($"[{Ph("b", "none")},{Ph("ɪ", "primary")},{Ph("n", "none")},{Ph("b", "none")},{Ph("ʊ", "none")}]"));
var plan = WeksaWordPlan.From(word, "x");
Assert.True(plan.Renderable);
Assert.Equal([WeksaWordPlan.ConsonantSeconds, WeksaWordPlan.VowelSeconds, WeksaWordPlan.ConsonantSeconds, WeksaWordPlan.ConsonantSeconds, WeksaWordPlan.VowelSeconds],
Assert.Equal([WeksaWordPlan.ConsonantSeconds, WeksaWordPlan.VowelSeconds * WeksaWordPlan.CueFor(WeksaStress.Primary).Duration, WeksaWordPlan.ConsonantSeconds, WeksaWordPlan.ConsonantSeconds, WeksaWordPlan.VowelSeconds],
plan.Phones.Select(p => p.Target.DurationSeconds));
var patch = PatchScript.Parse(IpaGestureExperiment.BuildWordScript(plan.Phones.ToArray(), 140));
var ih = patch.ControlSplines.Where(s => s.Name.StartsWith("x_1_ɪ_vowel_", StringComparison.Ordinal)).ToDictionary(s => s.SurfacePath, s => s.Points[0].Value);
@ -85,22 +85,49 @@ public sealed class WeksaPhonesTests
Assert.Equal(0f, oo["/vocal/areas/0/area/vowel_u"]);
}
[Fact]
public void StressMovesOnlyTheIntensityOfTheMarkedPhone()
{
string Script(WeksaStress onI) => IpaGestureExperiment.BuildWordScript(
WeksaWordPlan.From(new WeksaPhones("en-US", "ˈnibu",
[new("n", WeksaStress.None), new("i", onI), new("b", WeksaStress.None), new("u", WeksaStress.None)]), "w").Phones.ToArray());
private static WeksaWordPlan Plan(WeksaStress onI) => WeksaWordPlan.From(new WeksaPhones("en-US", "ˈnibu",
[new("n", WeksaStress.None), new("i", onI), new("b", WeksaStress.None), new("u", WeksaStress.None)]), "w");
var none = Script(WeksaStress.None).Split('\n');
var primary = Script(WeksaStress.Primary).Split('\n');
Assert.Equal(none.Length, primary.Length);
var differing = Enumerable.Range(0, none.Length).Where(i => none[i] != primary[i]).ToArray();
Assert.Single(differing);
Assert.Contains("ipa=i ", primary[differing[0]]);
var strip = (string l) => Regex.Replace(l, @"intensity=\S+", "");
Assert.Equal(strip(none[differing[0]]), strip(primary[differing[0]]));
Assert.Contains("intensity=1.2", primary[differing[0]]);
[Fact]
public void StressIsLongerLouderAndHigherOnTheMarkedPhoneOnly()
{
var none = Plan(WeksaStress.None).Phones;
var primary = Plan(WeksaStress.Primary).Phones;
var secondary = Plan(WeksaStress.Secondary).Phones;
foreach (var k in new[] { 0, 2, 3 })
{
Assert.Equal(none[k].Target, primary[k].Target);
Assert.Equal(none[k].Intensity, primary[k].Intensity);
}
Assert.Equal(WeksaWordPlan.VowelSeconds, none[1].Target.DurationSeconds);
Assert.Equal(1f, none[1].Target.PitchRatio);
Assert.Equal(1f, none[1].Intensity);
Assert.Equal(WeksaWordPlan.VowelSeconds * 220f / 160f, primary[1].Target.DurationSeconds, 5);
Assert.Equal(1.06f, primary[1].Target.PitchRatio);
Assert.Equal(1.26f, primary[1].Intensity);
// Secondary sits strictly between unstressed and primary on every cue.
Assert.InRange(secondary[1].Target.DurationSeconds, none[1].Target.DurationSeconds + 1e-4f, primary[1].Target.DurationSeconds - 1e-4f);
Assert.InRange(secondary[1].Target.PitchRatio, 1.001f, 1.059f);
Assert.InRange(secondary[1].Intensity, 1.001f, 1.259f);
}
[Fact]
public void StressReachesTheScriptAsDurationF0RatioAndSourceLevelAndShiftsLaterPhones()
{
var patch = (WeksaStress s) => PatchScript.Parse(IpaGestureExperiment.BuildWordScript(Plan(s).Phones.ToArray(), 140));
var none = patch(WeksaStress.None);
var primary = patch(WeksaStress.Primary);
float First(SynthPatch p, string prefix, string surface) =>
p.ControlSplines.Single(sp => sp.Name.StartsWith(prefix, StringComparison.Ordinal) && sp.SurfacePath == surface).Points[0].Value;
// pitch_ratio surface range 0.5..2: normalized (1.06 - .5) / 1.5; unstressed holds 1 -> (1 - .5) / 1.5.
Assert.Equal((1.06f - .5f) / 1.5f, First(primary, "w_1_i_source_pitch", "/vocal/sources/0/pitch_ratio"), 4);
Assert.Equal((1f - .5f) / 1.5f, First(none, "w_1_i_source_pitch", "/vocal/sources/0/pitch_ratio"), 4);
Assert.Equal(First(none, "w_1_i_source_level", "/vocal/sources/0/flow_scale") * 1.26f, First(primary, "w_1_i_source_level", "/vocal/sources/0/flow_scale"), 4);
// The b after the stressed vowel starts later by the vowel's extra length.
float Start(SynthPatch p) => p.PhonemeGestures.Single(g => g.Ipa == "b").StartSeconds;
Assert.Equal(WeksaWordPlan.VowelSeconds * (220f / 160f - 1f), Start(primary) - Start(none), 4);
}
[Fact]