Start Pink Trombone DSL parity primitives

This commit is contained in:
Meta Vens 2026-05-25 19:54:26 +01:00
commit 2cb52fb929
12 changed files with 267 additions and 15 deletions

View file

@ -626,7 +626,12 @@ public static class BuiltInScripts
param name=glottal_reflection path=/pink/glottal/reflection default=0.75 min=-0.95 max=0.95 step=0.001
param name=lip_reflection path=/pink/lip/reflection default=-0.85 min=-0.98 max=0.1 step=0.001
tract_shape
name=pt_human
diameters=0.6,0.6,0.6,0.6,0.6,0.7,0.8,1.0,1.1,1.1,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.5,1.4,1.3,1.2,1.15,1.5
tract
shape=pt_human
freq=@/pink/frequency
gain=0.7
attack=0.004

View file

@ -404,6 +404,12 @@ public static class FaustEmitter
var lipOpening = parameters.Expression(OwnerField(tractPath, "lip_opening"), tract.LipOpening);
var glottalReflection = parameters.Expression(OwnerField(tractPath, "glottal_reflection"), tract.GlottalReflection);
var lipReflection = parameters.Expression(OwnerField(tractPath, "lip_reflection"), tract.LipReflection);
var areaFunction = tract.AreaFunction;
var shapeBack = F(areaFunction?.AverageDiameter(0.18f, 0.38f) ?? 1.35f);
var shapeMiddle = F(areaFunction?.AverageDiameter(0.38f, 0.68f) ?? 1.5f);
var shapeFront = F(areaFunction?.AverageDiameter(0.68f, 0.96f) ?? 1.5f);
var shapeMinimum = F(areaFunction?.MinimumDiameter ?? 0.6f);
var reflectionEnergy = F(areaFunction is null ? 0.12f : MathF.Min(1, areaFunction.ReflectionCoefficients.Sum(reflection => MathF.Abs(reflection)) / Math.Max(1, areaFunction.ReflectionCoefficients.Count)));
source.AppendLine($"{name}_tract_phase = os.phasor(1.0, {frequency});");
source.AppendLine($"{name}_tract_tongue_pos = clip01({tongueIndex} / {F(sections)});");
@ -411,10 +417,15 @@ public static class FaustEmitter
source.AppendLine($"{name}_tract_tongue_close = clip01((3.5 - {tongueDiameter}) / 3.5);");
source.AppendLine($"{name}_tract_constriction_close = clip01((1.15 - {constrictionDiameter}) / 1.15);");
source.AppendLine($"{name}_tract_lip = clip01({lipOpening} / 2.5);");
source.AppendLine($"{name}_tract_q = 2.0 + {tenseness} * 10.0 + {name}_tract_constriction_close * 8.0;");
source.AppendLine($"{name}_tract_f1 = max(90.0, 260.0 + {name}_tract_lip * 720.0 - {name}_tract_tongue_close * 260.0 + {velum} * 120.0);");
source.AppendLine($"{name}_tract_f2 = max(180.0, 820.0 + {name}_tract_tongue_pos * 1850.0 - {name}_tract_tongue_close * 640.0 - (1.0 - {name}_tract_lip) * 260.0);");
source.AppendLine($"{name}_tract_f3 = max(500.0, 1900.0 + {name}_tract_constriction_pos * 2600.0 + {name}_tract_tongue_close * 700.0);");
source.AppendLine($"{name}_tract_shape_back = {shapeBack};");
source.AppendLine($"{name}_tract_shape_middle = {shapeMiddle};");
source.AppendLine($"{name}_tract_shape_front = {shapeFront};");
source.AppendLine($"{name}_tract_shape_min = {shapeMinimum};");
source.AppendLine($"{name}_tract_reflection_energy = {reflectionEnergy};");
source.AppendLine($"{name}_tract_q = 2.0 + {tenseness} * 10.0 + {name}_tract_constriction_close * 8.0 + {name}_tract_reflection_energy * 4.0;");
source.AppendLine($"{name}_tract_f1 = max(90.0, 260.0 + {name}_tract_lip * 720.0 - {name}_tract_tongue_close * 260.0 + ({name}_tract_shape_back - 1.35) * 210.0 - (1.0 - {name}_tract_shape_min) * 120.0 + {velum} * 120.0);");
source.AppendLine($"{name}_tract_f2 = max(180.0, 820.0 + {name}_tract_tongue_pos * 1850.0 - {name}_tract_tongue_close * 640.0 + ({name}_tract_shape_middle - 1.5) * 360.0 - (1.0 - {name}_tract_lip) * 260.0);");
source.AppendLine($"{name}_tract_f3 = max(500.0, 1900.0 + {name}_tract_constriction_pos * 2600.0 + {name}_tract_tongue_close * 700.0 + ({name}_tract_shape_front - 1.5) * 520.0);");
source.AppendLine($"{name}_tract_lf = (sin(2.0 * ma.PI * {name}_tract_phase) - (0.15 + {tenseness} * 0.45) * sin(4.0 * ma.PI * {name}_tract_phase)) * {intensity} * (0.45 + 0.55 * pow(max(0.0, {tenseness}), 0.25));");
source.AppendLine($"{name}_tract_aspiration = no.noise * {intensity} * (1.0 - sqrt(max(0.0, {tenseness}))) * (0.02 + 0.16 * {name}_tract_constriction_close);");
source.AppendLine($"{name}_tract_frication = no.noise * {turbulence} * {name}_tract_constriction_close * {intensity} * (0.2 + 0.8 * {tenseness});");

View file

@ -138,6 +138,51 @@ public sealed record Formant(float FrequencyHz, float BandwidthHz, float Gain);
public sealed record FormantFrame(IReadOnlyList<Formant> Formants);
public sealed record TractAreaFunction(IReadOnlyList<float> Diameters)
{
public int Sections => Diameters.Count;
public IReadOnlyList<float> Areas => Diameters.Select(diameter => MathF.Max(0, diameter) * MathF.Max(0, diameter)).ToArray();
public IReadOnlyList<float> ReflectionCoefficients
{
get
{
if (Diameters.Count < 2) return Array.Empty<float>();
var reflections = new float[Diameters.Count - 1];
for (var i = 1; i < Diameters.Count; i++)
{
var previousArea = MathF.Max(0.000001f, Diameters[i - 1] * Diameters[i - 1]);
var area = MathF.Max(0.000001f, Diameters[i] * Diameters[i]);
reflections[i - 1] = (previousArea - area) / (previousArea + area);
}
return reflections;
}
}
public float AverageDiameter(float startFraction, float endFraction)
{
if (Diameters.Count == 0) return 0;
var start = Math.Clamp((int)MathF.Floor(startFraction * Diameters.Count), 0, Diameters.Count - 1);
var end = Math.Clamp((int)MathF.Ceiling(endFraction * Diameters.Count), start + 1, Diameters.Count);
var sum = 0f;
for (var i = start; i < end; i++)
{
sum += Diameters[i];
}
return sum / Math.Max(1, end - start);
}
public float MinimumDiameter => Diameters.Count == 0 ? 0 : Diameters.Min();
public static TractAreaFunction FromAreas(IReadOnlyList<float> areas) =>
new(areas.Select(area => MathF.Sqrt(MathF.Max(0, area))).ToArray());
}
public sealed record TractShape(string Name, TractAreaFunction AreaFunction);
public sealed record Modulator(
ModTarget Target,
ModWaveform Waveform = ModWaveform.Sine,
@ -191,7 +236,8 @@ public sealed record VocalTract(
float Turbulence = 0,
float LipOpening = 1.5f,
float GlottalReflection = 0.75f,
float LipReflection = -0.85f);
float LipReflection = -0.85f,
TractAreaFunction? AreaFunction = null);
public sealed record PatchParameter(
string Path,
@ -364,6 +410,7 @@ public sealed record SynthPatch
public IReadOnlyList<PatchLayer> Layers { get; init; } = Array.Empty<PatchLayer>();
public IReadOnlyList<HarmonicBank> HarmonicBanks { get; init; } = Array.Empty<HarmonicBank>();
public IReadOnlyList<SpectralBank> SpectralBanks { get; init; } = Array.Empty<SpectralBank>();
public IReadOnlyList<TractShape> TractShapes { get; init; } = Array.Empty<TractShape>();
public IReadOnlyList<OperatorGraph> OperatorGraphs { get; init; } = Array.Empty<OperatorGraph>();
public IReadOnlyList<ControlLane> Controls { get; init; } = Array.Empty<ControlLane>();
public IReadOnlyList<PatchParameter> Parameters { get; init; } = Array.Empty<PatchParameter>();

View file

@ -36,6 +36,8 @@ public static class PatchScript
private readonly List<PatchLayer> _layers = [];
private readonly List<HarmonicBank> _harmonicBanks = [];
private readonly List<SpectralBank> _spectralBanks = [];
private readonly List<TractShape> _tractShapes = [];
private readonly Dictionary<string, TractShape> _tractShapesByName = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, string> _defaults = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Dictionary<string, string>> _layerDefaults = new(StringComparer.OrdinalIgnoreCase);
private PendingOperatorGraph? _pendingOperatorGraph;
@ -57,6 +59,7 @@ public static class PatchScript
Layers = _layers,
HarmonicBanks = _harmonicBanks,
SpectralBanks = _spectralBanks,
TractShapes = _tractShapes,
OperatorGraphs = _operatorGraphs,
Controls = _controls,
Parameters = _parameters,
@ -108,6 +111,10 @@ public static class PatchScript
FlushPendingOperatorGraph();
AddSpectralBank(fields, line);
break;
case "tract_shape":
FlushPendingOperatorGraph();
AddTractShape(fields, line);
break;
case "voice":
FlushPendingOperatorGraph();
Voices.Add(ParseVoice(ExpandVoiceFields(fields, line), VoicePath(Voices.Count), line));
@ -265,6 +272,20 @@ public static class PatchScript
private static string SpectralPath(int spectralIndex) => $"/spectral/{spectralIndex}";
private void AddTractShape(IReadOnlyDictionary<string, string> fields, int line)
{
var name = Required(fields, "name", line);
if (_tractShapesByName.ContainsKey(name))
{
throw new PatchScriptException(line, $"duplicate tract shape `{name}`");
}
var areaFunction = ParseTractAreaFunction(fields, line);
var shape = new TractShape(name, areaFunction);
_tractShapes.Add(shape);
_tractShapesByName[name] = shape;
}
private Dictionary<string, string> ExpandVoiceFields(Dictionary<string, string> fields, int line)
{
var expanded = new Dictionary<string, string>(_defaults, StringComparer.OrdinalIgnoreCase);
@ -505,11 +526,16 @@ public static class PatchScript
}
var voice = ParseVoice(voiceFields, ownerPath, line);
var sections = GetBoundInt(fields, line, 44, OwnerField(ownerPath, "tract/sections"), "sections", "cells");
var areaFunction = ParseTractAreaFunctionReference(fields, line);
var sections = GetBoundInt(fields, line, areaFunction?.Sections ?? 44, OwnerField(ownerPath, "tract/sections"), "sections", "cells");
if (sections < 4)
{
throw new PatchScriptException(line, "tract sections must be at least 4");
}
if (areaFunction is not null && areaFunction.Sections != sections)
{
throw new PatchScriptException(line, "tract shape section count must match tract sections");
}
var noseSections = GetBoundInt(fields, line, 28, OwnerField(ownerPath, "tract/nose_sections"), "nose_sections", "nose_cells");
if (noseSections < 0)
{
@ -531,7 +557,8 @@ public static class PatchScript
GetBoundFloat(fields, line, 0, OwnerField(ownerPath, "tract/turbulence"), "turbulence", "frication"),
GetBoundFloat(fields, line, 1.5f, OwnerField(ownerPath, "tract/lip_opening"), "lip", "lip_opening", "mouth"),
GetBoundFloat(fields, line, 0.75f, OwnerField(ownerPath, "tract/glottal_reflection"), "glottal_reflection", "gr"),
GetBoundFloat(fields, line, -0.85f, OwnerField(ownerPath, "tract/lip_reflection"), "lip_reflection", "lr"))
GetBoundFloat(fields, line, -0.85f, OwnerField(ownerPath, "tract/lip_reflection"), "lip_reflection", "lr"),
areaFunction)
};
}
@ -916,6 +943,54 @@ public static class PatchScript
.ToArray();
}
private TractAreaFunction? ParseTractAreaFunctionReference(IReadOnlyDictionary<string, string> fields, int line)
{
var hasInlineShape = HasAny(fields, "diameters", "diameter", "areas", "area");
var hasNamedShape = TryGetAny(fields, ["shape", "tract_shape", "area_function"], out var shapeName);
if (hasInlineShape && hasNamedShape)
{
throw new PatchScriptException(line, "tract cannot use both a named shape and inline diameters/areas");
}
if (hasInlineShape)
{
return ParseTractAreaFunction(fields, line);
}
if (!hasNamedShape)
{
return null;
}
if (!_tractShapesByName.TryGetValue(shapeName, out var shape))
{
throw new PatchScriptException(line, $"unknown tract shape `{shapeName}`");
}
return shape.AreaFunction;
}
private static TractAreaFunction ParseTractAreaFunction(IReadOnlyDictionary<string, string> fields, int line)
{
var hasDiameters = TryGetAny(fields, ["diameters", "diameter"], out var diametersText);
var hasAreas = TryGetAny(fields, ["areas", "area"], out var areasText);
if (hasDiameters == hasAreas)
{
throw new PatchScriptException(line, "tract shape needs exactly one of diameters or areas");
}
var samples = ParseFloatList(hasDiameters ? diametersText! : areasText!, line, hasDiameters ? "diameters" : "areas");
if (samples.Count < 4)
{
throw new PatchScriptException(line, "tract shape needs at least four samples");
}
if (samples.Any(sample => sample < 0))
{
throw new PatchScriptException(line, "tract shape samples must be zero or greater");
}
return hasDiameters
? new TractAreaFunction(samples.ToArray())
: TractAreaFunction.FromAreas(samples.ToArray());
}
private static PadSpectrumProfile ParsePadSpectrumProfile(IReadOnlyDictionary<string, string> fields, int line)
{
var hasPadProfileFields =
@ -1328,6 +1403,7 @@ public static class PatchScript
"layer" or "kit" => "layer",
"harmonics" or "partials" or "drawbars" => "harmonics",
"spectrum" or "spectral" or "padsource" or "pad_source" => "spectrum",
"tractshape" or "tract_shape" or "area_function" or "tract_area" => "tract_shape",
"v" or "voice" => "voice",
"tract" or "vt" or "tractvoice" or "tract_voice" => "tract",
"opgraph" or "ops" or "operators" => "opgraph",

View file

@ -61,6 +61,8 @@ public static class PinkTromboneReference
BuiltInScripts.PinkTromboneProxy,
[
new("tract_voice_authority", "voice.Tract", "Pink Trombone pressure now lowers through a tract treatment on an ordinary AquaSynth voice."),
new("tract_area_function", "tract_shape diameters", "AquaSynth can now author reusable section diameter/area functions for tract voices."),
new("diameter_to_reflection_coefficients", "TractAreaFunction.ReflectionCoefficients", "AquaSynth derives adjacent-section reflection coefficients from squared diameters as a reusable primitive."),
new("runtime_controls", "/pink/frequency,/pink/intensity,/pink/tenseness,/pink/tongue/index,/pink/tongue/diameter,/pink/velum,/pink/turbulence", "AquaSynth can expose stable runtime parameter paths."),
new("glottal_pitch_pressure_proxy", "tract LF-style source", "AquaSynth can drive pitch/loudness-like controls without recompiling."),
new("turbulence_proxy", "position-aware frication controls", "The tract voice has constriction index/diameter and turbulence controls."),
@ -69,7 +71,7 @@ public static class PinkTromboneReference
[
new("main_tract_waveguide_cells", "44 bidirectional sections", "No AquaSynth DSL authority owns arrays of right/left traveling wave state per tract section."),
new("nose_waveguide_cells", "28 bidirectional sections", "The current DSL has no nasal branch delay-line graph or velum-controlled three-way junction."),
new("diameter_to_reflection_coefficients", "per-block area reflection update", "Formant filters do not preserve the section-area ownership or reflection math."),
new("reflection_coefficients_applied_to_waveguide", "per-block area reflection update", "AquaSynth can derive reflection coefficients, but the current Faust proxy does not apply them to bidirectional waveguide propagation."),
new("twice_rate_tract_stepping", "two tract steps per audio sample", "AquaSynth voice graphs do not express a subgraph with its own stepping rate."),
new("positioned_turbulence_injection", "noise injected into neighboring cells", "The current noise field is source/color pressure, not a positioned tract injection event."),
new("closure_release_transients", "obstruction release impulse", "AquaSynth envelopes can shape amplitude, but they do not observe obstruction state and emit release transients."),