Expose PT timeline comparison for primitive vocals
This commit is contained in:
parent
bd391bfb84
commit
0d05a3a12e
8 changed files with 202 additions and 3 deletions
|
|
@ -11,6 +11,12 @@ public sealed record PinkTromboneUtteranceRender(
|
|||
string FixtureId,
|
||||
IReadOnlyList<PinkTromboneControlPoint> ControlPoints);
|
||||
|
||||
public sealed record PinkTromboneReferenceTimelineSample(
|
||||
int Block,
|
||||
string Primitive,
|
||||
string Signal,
|
||||
float Value);
|
||||
|
||||
public sealed record PinkTromboneControlPoint(
|
||||
float TimeSeconds,
|
||||
PinkTromboneFixtureControls Controls,
|
||||
|
|
@ -29,6 +35,17 @@ public sealed class PinkTromboneReferenceRenderer(int sampleRate = 44100)
|
|||
return new PinkTromboneReferenceRender(samples, sampleRate, controls);
|
||||
}
|
||||
|
||||
public IReadOnlyList<PinkTromboneReferenceTimelineSample> RenderTimeline(
|
||||
PinkTromboneFixtureControls controls,
|
||||
float durationSeconds = 0.12f,
|
||||
int blockSize = 512)
|
||||
{
|
||||
var frames = Math.Max(1, (int)MathF.Round(sampleRate * Math.Max(durationSeconds, 1f / sampleRate)));
|
||||
var synth = new Synthesizer(sampleRate);
|
||||
var samples = new float[frames];
|
||||
return synth.SynthesizeTimeline(samples, _ => controls, Math.Clamp(blockSize, 1, 4096));
|
||||
}
|
||||
|
||||
public PinkTromboneUtteranceRender RenderUtterance(
|
||||
string fixtureId,
|
||||
IReadOnlyList<PinkTromboneControlPoint> controlPoints,
|
||||
|
|
@ -118,6 +135,26 @@ public sealed class PinkTromboneReferenceRenderer(int sampleRate = 44100)
|
|||
}
|
||||
}
|
||||
|
||||
public IReadOnlyList<PinkTromboneReferenceTimelineSample> SynthesizeTimeline(
|
||||
float[] buffer,
|
||||
Func<int, PinkTromboneFixtureControls> controlsAtFrame,
|
||||
int blockSize)
|
||||
{
|
||||
var p = 0;
|
||||
var block = 0;
|
||||
var timeline = new List<PinkTromboneReferenceTimelineSample>();
|
||||
while (p < buffer.Length)
|
||||
{
|
||||
var blockLength = Math.Min(Math.Min(MaxBlockLength, blockSize), buffer.Length - p);
|
||||
SynthesizeBlock(buffer, p, blockLength, controlsAtFrame(p));
|
||||
tract.Snapshot(block, timeline);
|
||||
p += blockLength;
|
||||
block++;
|
||||
}
|
||||
|
||||
return timeline;
|
||||
}
|
||||
|
||||
private void SynthesizeBlock(float[] buffer, int offset, int count, PinkTromboneFixtureControls controls)
|
||||
{
|
||||
var deltaTime = count / (float)sampleRate;
|
||||
|
|
@ -442,11 +479,61 @@ public sealed class PinkTromboneReferenceRenderer(int sampleRate = 44100)
|
|||
return lipOutput + noseOutput;
|
||||
}
|
||||
|
||||
public void Snapshot(int block, List<PinkTromboneReferenceTimelineSample> timeline)
|
||||
{
|
||||
var oralArea = Diameter.Select(diameter => diameter * diameter).DefaultIfEmpty(0).Average();
|
||||
var nasalArea = NoseDiameter.Select(diameter => diameter * diameter).DefaultIfEmpty(0).Average();
|
||||
var oralIncoming = right.Select(MathF.Abs).DefaultIfEmpty(0).Average() + left.Select(MathF.Abs).DefaultIfEmpty(0).Average();
|
||||
var oralOutgoing = junctionOutputRight.Select(MathF.Abs).DefaultIfEmpty(0).Average() + junctionOutputLeft.Take(N).Select(MathF.Abs).DefaultIfEmpty(0).Average();
|
||||
var oralEnergyIn = WaveEnergy(Diameter, right, left);
|
||||
var oralEnergyOut = WaveEnergy(Diameter, junctionOutputRight, junctionOutputLeft);
|
||||
var nasalAdmittance = Math.Clamp(NoseDiameter[0] * NoseDiameter[0], 0, 1);
|
||||
var obstruction = Diameter.Select((diameter, index) => (diameter, index)).MinBy(item => item.diameter);
|
||||
var lipFlow = right[N - 1];
|
||||
var noseFlow = noseRight[NoseLength - 1];
|
||||
|
||||
Add(timeline, block, "path:pt_oral", "area", oralArea);
|
||||
Add(timeline, block, "path:pt_oral", "incoming_wave", oralIncoming);
|
||||
Add(timeline, block, "path:pt_oral", "outgoing_wave", oralOutgoing);
|
||||
Add(timeline, block, "path:pt_oral", "energy_in", oralEnergyIn);
|
||||
Add(timeline, block, "path:pt_oral", "energy_out", oralEnergyOut);
|
||||
Add(timeline, block, "path:pt_oral", "passivity_ratio", oralEnergyIn <= 0.000001f ? 1 : oralEnergyOut / oralEnergyIn);
|
||||
Add(timeline, block, "path:pt_nasal", "area", nasalArea);
|
||||
Add(timeline, block, "branch:pt_velopharynx", "admittance", nasalAdmittance);
|
||||
Add(timeline, block, "branch:pt_velopharynx", "reflection_left", newReflectionLeft);
|
||||
Add(timeline, block, "branch:pt_velopharynx", "reflection_right", newReflectionRight);
|
||||
Add(timeline, block, "branch:pt_velopharynx", "reflection_nose", newReflectionNose);
|
||||
Add(timeline, block, "contact:pt_obstruction", "position", obstruction.index / (float)Math.Max(1, N - 1));
|
||||
Add(timeline, block, "contact:pt_obstruction", "opening", Math.Clamp(obstruction.diameter / 2.5f, 0, 1));
|
||||
Add(timeline, block, "contact:pt_obstruction", "reservoir", transients.Count);
|
||||
Add(timeline, block, "contact:pt_obstruction", "released_flow", transients.Sum(transient => transient.Strength));
|
||||
Add(timeline, block, "radiation:pt_lip", "reflection", LipReflection);
|
||||
Add(timeline, block, "radiation:pt_lip", "boundary_flow", lipFlow);
|
||||
Add(timeline, block, "radiation:pt_lip", "flow", lipFlow);
|
||||
Add(timeline, block, "radiation:pt_lip", "output", lipFlow + noseFlow);
|
||||
}
|
||||
|
||||
public void AddTransient(int position, float strength)
|
||||
{
|
||||
transients.Add(new Transient(position, Time, 0.2f, 0.3f * strength, 200));
|
||||
}
|
||||
|
||||
private static float WaveEnergy(float[] diameters, float[] rightWave, float[] leftWave)
|
||||
{
|
||||
var count = Math.Min(diameters.Length, Math.Min(rightWave.Length, leftWave.Length));
|
||||
var energy = 0f;
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var area = Math.Max(0.000001f, diameters[i] * diameters[i]);
|
||||
energy += area * (rightWave[i] * rightWave[i] + leftWave[i] * leftWave[i]);
|
||||
}
|
||||
|
||||
return energy;
|
||||
}
|
||||
|
||||
private static void Add(List<PinkTromboneReferenceTimelineSample> timeline, int block, string primitive, string signal, float value) =>
|
||||
timeline.Add(new PinkTromboneReferenceTimelineSample(block, primitive, signal, value));
|
||||
|
||||
private void CalculateMainTractReflections()
|
||||
{
|
||||
Span<float> area = stackalloc float[N];
|
||||
|
|
|
|||
|
|
@ -65,6 +65,34 @@ public static class PrimitiveReferenceReport
|
|||
return rows;
|
||||
}
|
||||
|
||||
public static IReadOnlyList<PrimitiveReferenceSample> ComparePinkTromboneTimeline(
|
||||
IReadOnlyList<ProbeTimelineSample> candidate,
|
||||
IReadOnlyList<PinkTromboneReferenceTimelineSample> reference)
|
||||
{
|
||||
var referenceByKey = reference
|
||||
.GroupBy(sample => (sample.Block, PrimitiveRole(sample.Primitive), sample.Signal))
|
||||
.ToDictionary(group => group.Key, group => group.First().Value);
|
||||
var rows = new List<PrimitiveReferenceSample>();
|
||||
|
||||
foreach (var sample in candidate)
|
||||
{
|
||||
var role = PrimitiveRole(sample.Primitive);
|
||||
if (role.Length == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!referenceByKey.TryGetValue((sample.Block, role, sample.Signal), out var expected))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Add(rows, role, sample.Signal, sample.Value, expected);
|
||||
}
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
public static string ToCsv(IReadOnlyList<PrimitiveReferenceSample> samples)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
|
|
@ -90,6 +118,33 @@ public static class PrimitiveReferenceReport
|
|||
private static void Add(List<PrimitiveReferenceSample> rows, string primitive, string signal, float candidate, float expected) =>
|
||||
rows.Add(new PrimitiveReferenceSample(PinkTromboneReference, primitive, signal, candidate, expected, candidate - expected));
|
||||
|
||||
private static string PrimitiveRole(string primitive)
|
||||
{
|
||||
if (primitive.StartsWith("path:", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return primitive.Contains("nasal", StringComparison.OrdinalIgnoreCase) || primitive.Contains("nose", StringComparison.OrdinalIgnoreCase)
|
||||
? "path:nasal"
|
||||
: "path:oral";
|
||||
}
|
||||
|
||||
if (primitive.StartsWith("branch:", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return "branch:velopharynx";
|
||||
}
|
||||
|
||||
if (primitive.StartsWith("contact:", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return "contact:obstruction";
|
||||
}
|
||||
|
||||
if (primitive.StartsWith("radiation:", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return "radiation:lip";
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private static string Escape(string value) =>
|
||||
value.Contains(',') || value.Contains('"') ? $"\"{value.Replace("\"", "\"\"", StringComparison.Ordinal)}\"" : value;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue