Add IPA trial render orchestration
This commit is contained in:
parent
62178531f9
commit
05cf7d36d6
11 changed files with 683 additions and 11 deletions
|
|
@ -699,10 +699,13 @@ public static class FaustEmitter
|
|||
var impedance = parameters.Expression(OwnerField(owner, "impedance"), port.Impedance);
|
||||
var flowScale = parameters.Expression(OwnerField(owner, "flow_scale"), port.FlowScale);
|
||||
var load = $"{voiceName}_primitive_source_{safe}_load_pressure";
|
||||
var voiced = $"{voiceName}_primitive_source_{safe}_voiced";
|
||||
var flow = $"{voiceName}_primitive_source_{safe}_flow";
|
||||
source.AppendLine($" {load} = {ProbeSignal(options, $"/debug/{voiceName}/source/{port.Name}/load_pressure", $"({impedance}) * ({pressure})", 0, 2)};");
|
||||
source.AppendLine($" {flow} = {ProbeSignal(options, $"/debug/{voiceName}/source/{port.Name}/flow", $"({flowScale}) * ma.tanh((({pressure}) * max(0.0, {opening}) * (0.5 + {tension}) + 0.08 * no.noise * {parameters.Expression(OwnerField(owner, "noise"), port.Noise)}) / max(0.05, {impedance}))", -2, 2)};");
|
||||
source.AppendLine($" {voiced} = os.osc({frequency}) * clip01({opening}) * clip01(0.25 + {tension});");
|
||||
source.AppendLine($" {flow} = {ProbeSignal(options, $"/debug/{voiceName}/source/{port.Name}/flow", $"({flowScale}) * (({voiced}) * ma.tanh((({pressure}) * (0.5 + {tension})) / max(0.05, {impedance})) + 0.08 * no.noise * {parameters.Expression(OwnerField(owner, "noise"), port.Noise)})", -2, 2)};");
|
||||
keepAlive.Add(load);
|
||||
keepAlive.Add(voiced);
|
||||
keepAlive.Add(flow);
|
||||
return flow;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -201,19 +201,19 @@ public static class IpaGestureExperiment
|
|||
var frequency = DescriptorTokens(target.Descriptor).Contains("vowel") ? 150 : 135;
|
||||
|
||||
return $$"""
|
||||
patch gain=.22
|
||||
patch gain=.45
|
||||
morphology name=oral length_cm=17 diameters=.55,.75,1.05,1.45,1.25,.8 tongue_index=3 tongue_diameter=1.4 constriction_index=4 constriction_diameter=1 lip_opening=1.2
|
||||
morphology name=nasal length_cm=12 diameters=.04,.28,.52,.72
|
||||
waveguide_path name=oral_path morphology=oral strategy=thiran order=1 max_delay=4096 loss=.998
|
||||
waveguide_path name=nasal_path morphology=nasal strategy=thiran order=1 max_delay=4096 loss=.997
|
||||
source_port name=folds path=oral_path kind=glottal position=0 pressure=.66 tension=.54 opening=.42 noise=.04 impedance=.32 flow_scale=.02
|
||||
source_port name=folds path=oral_path kind=glottal position=0 pressure=.66 tension=.54 opening=.42 noise=.04 impedance=.32 flow_scale=.12
|
||||
branch_port name=velopharynx from=oral_path from_position=.45 to=nasal_path opening=.01 coupling=1
|
||||
constriction_contact name=contact path=oral_path position=.92 opening=.5 resistance=.45 stored_pressure=.12
|
||||
radiation_load name=mouth path=oral_path kind=lip position=1 aperture=.8 reflection=-.82 impedance=.28
|
||||
probe_timeline name=flow network=voice blocks=12 block_size=64
|
||||
vocal_network name=voice paths=oral_path,nasal_path sources=folds contacts=contact branches=velopharynx radiation=mouth probe=flow
|
||||
phoneme_gesture name={{SafeDslName(target.Id)}} ipa={{target.Ipa}} descriptor={{descriptor}} start=0 dur={{Format(duration)}} intensity={{Format(intensity)}}
|
||||
vocal network=voice freq={{Format(frequency)}} gain=.2 sustain={{Format(sustain)}}
|
||||
vocal network=voice freq={{Format(frequency)}} gain=.7 sustain={{Format(sustain)}}
|
||||
""";
|
||||
}
|
||||
|
||||
|
|
|
|||
73
src/AquaSynth.Core/IpaTrialResults.cs
Normal file
73
src/AquaSynth.Core/IpaTrialResults.cs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
using GameCult.Caching;
|
||||
using GameCult.Caching.MessagePack;
|
||||
using MessagePack;
|
||||
|
||||
namespace AquaSynth.Dsl;
|
||||
|
||||
[CultDocument("aquasynth.ipa_trial_result", "aquasynth.ipa_trial_result.v1")]
|
||||
[MessagePackObject]
|
||||
public sealed record IpaTrialResult(
|
||||
[property: Key(0)]
|
||||
[property: CultName]
|
||||
string TrialId,
|
||||
[property: Key(1)] string BatchId,
|
||||
[property: Key(2)] string CreatedAtUtc,
|
||||
[property: Key(3)] string TargetSetId,
|
||||
[property: Key(4)] string[] Phonemes,
|
||||
[property: Key(5)] string ReferenceId,
|
||||
[property: Key(6)] string CandidateId,
|
||||
[property: Key(7)] string HypothesizerId,
|
||||
[property: Key(8)] string Hypothesis,
|
||||
[property: Key(9)] string CandidatePatchUri,
|
||||
[property: Key(10)] string ReferenceArtifactUri,
|
||||
[property: Key(11)] string CandidateArtifactUri,
|
||||
[property: Key(12)] string PrimitiveTimelineUri,
|
||||
[property: Key(13)] SpeechScoreMetric[] Metrics,
|
||||
[property: Key(14)] SpeechRenderArtifact[] Artifacts,
|
||||
[property: Key(15)] string EvaluatorId,
|
||||
[property: Key(16)] string EvaluationSummary,
|
||||
[property: Key(17)] string Verdict,
|
||||
[property: Key(18)] string[] KnownLies,
|
||||
[property: Key(19)] SpeechTimingReceipt[] TimingReceipts);
|
||||
|
||||
public static class IpaTrialResultCultCacheStore
|
||||
{
|
||||
public static Task UpsertResultsAsync(string filePath, IEnumerable<IpaTrialResult> results) =>
|
||||
UpsertAsync(filePath, results, result => $"ipa-trial-result:{result.TrialId}");
|
||||
|
||||
public static async Task<IReadOnlyList<IpaTrialResult>> ReadResultsAsync(string filePath)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
using var cache = await CultCacheMessagePack.OpenAsync(filePath).ConfigureAwait(false);
|
||||
return cache.GetAll<IpaTrialResult>()
|
||||
.OrderBy(result => result.TrialId, StringComparer.Ordinal)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
private static async Task UpsertAsync<T>(
|
||||
string filePath,
|
||||
IEnumerable<T> documents,
|
||||
Func<T, string> key)
|
||||
where T : class
|
||||
{
|
||||
var directory = Path.GetDirectoryName(filePath);
|
||||
if (!string.IsNullOrWhiteSpace(directory))
|
||||
{
|
||||
Directory.CreateDirectory(directory);
|
||||
}
|
||||
|
||||
using var cache = await CultCacheMessagePack.OpenAsync(
|
||||
filePath,
|
||||
new CultCacheOpenOptions { PullOnOpen = File.Exists(filePath) }).ConfigureAwait(false);
|
||||
foreach (var document in documents)
|
||||
{
|
||||
await cache.UpsertAsync(document, new CultRecordHandle<T>(new CultRecordKey(key(document)))).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await cache.FlushAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
432
src/AquaSynth.Faust/IpaTrialOrchestrator.cs
Normal file
432
src/AquaSynth.Faust/IpaTrialOrchestrator.cs
Normal file
|
|
@ -0,0 +1,432 @@
|
|||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
using AquaSynth.Dsl;
|
||||
|
||||
namespace AquaSynth.Faust;
|
||||
|
||||
public sealed record IpaTrialReferenceTarget(
|
||||
IpaGestureExperimentTarget Target,
|
||||
string ReferenceFixtureId);
|
||||
|
||||
public sealed record IpaTrialTargetSet(
|
||||
string Id,
|
||||
string Description,
|
||||
IReadOnlyList<IpaTrialReferenceTarget> Targets,
|
||||
IReadOnlyList<IpaGestureExperimentVariant> Variants)
|
||||
{
|
||||
public IReadOnlyList<IpaTrialReferenceTarget> Targets { get; init; } = Targets ?? Array.Empty<IpaTrialReferenceTarget>();
|
||||
public IReadOnlyList<IpaGestureExperimentVariant> Variants { get; init; } = Variants ?? Array.Empty<IpaGestureExperimentVariant>();
|
||||
}
|
||||
|
||||
public sealed record IpaTrialOrchestrationOptions(
|
||||
string BatchId = "ipa-seed-trials",
|
||||
string HypothesizerId = "local-hypothesis-worker",
|
||||
string EvaluatorId = "local-science-evaluator",
|
||||
int TimelineBlocks = 12,
|
||||
int SampleRate = 44100,
|
||||
string RendererToolchain = "local-faust-render",
|
||||
string ReferenceRenderer = "pink-trombone-local-fixture",
|
||||
string TrialResultStoreFileName = "ipa-trial-results.cc");
|
||||
|
||||
public sealed record IpaTrialOrchestrationResult(
|
||||
string BatchId,
|
||||
string ArtifactDirectory,
|
||||
string TrialResultStorePath,
|
||||
string SummaryPath,
|
||||
string EvaluatorReportPath,
|
||||
IReadOnlyList<IpaTrialResult> TrialResults)
|
||||
{
|
||||
public IReadOnlyList<IpaTrialResult> TrialResults { get; init; } = TrialResults ?? Array.Empty<IpaTrialResult>();
|
||||
}
|
||||
|
||||
public static class IpaTrialOrchestrator
|
||||
{
|
||||
public static IReadOnlyList<IpaTrialTargetSet> DefaultFiveSeedTrialSets { get; } =
|
||||
[
|
||||
new(
|
||||
"trial-001-vowels",
|
||||
"Open and front vowel hypotheses against static tract references.",
|
||||
[
|
||||
Target("a", "a", "voiced_open_back_unrounded_vowel", "open-vowel", .20f),
|
||||
Target("i", "i", "voiced_close_front_unrounded_vowel", "front-vowel", .20f)
|
||||
],
|
||||
[Variant("baseline", 1, 1, "vowel-seed")]),
|
||||
new(
|
||||
"trial-002-nasal",
|
||||
"Bilabial nasal velum/contact hypothesis against the ma pressure fixture.",
|
||||
[Target("m", "m", "voiced_bilabial_nasal", "bilabial-nasal-ma", .18f)],
|
||||
[Variant("nasal-base", 1, 1, "nasal-seed")]),
|
||||
new(
|
||||
"trial-003-sibilant",
|
||||
"Voiceless alveolar fricative hypothesis against the high-turbulence sibilant fixture.",
|
||||
[Target("s", "s", "voiceless_alveolar_fricative", "sibilant", .16f)],
|
||||
[Variant("hiss-base", 1.15f, .9f, "fricative-seed")]),
|
||||
new(
|
||||
"trial-004-plosive",
|
||||
"Bilabial stop closure/release hypothesis against obstruction-history pressure.",
|
||||
[Target("p", "p", "voiceless_bilabial_plosive", "closure-release", .14f)],
|
||||
[Variant("stop-base", 1.2f, .85f, "plosive-seed")]),
|
||||
new(
|
||||
"trial-005-mixed",
|
||||
"Small mixed set used to check whether the same DSL abstractions separate vowel, nasal, and fricative pressure.",
|
||||
[
|
||||
Target("mix-a", "a", "voiced_open_back_unrounded_vowel", "open-vowel", .18f),
|
||||
Target("mix-m", "m", "voiced_bilabial_nasal", "bilabial-nasal-ma", .16f),
|
||||
Target("mix-s", "s", "voiceless_alveolar_fricative", "sibilant", .14f)
|
||||
],
|
||||
[Variant("mixed-base", 1, 1, "mixed-seed")])
|
||||
];
|
||||
|
||||
public static async Task<IpaTrialOrchestrationResult> RunAsync(
|
||||
string artifactRoot,
|
||||
IReadOnlyList<IpaTrialTargetSet>? trialSets = null,
|
||||
IpaTrialOrchestrationOptions? options = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(artifactRoot);
|
||||
options ??= new IpaTrialOrchestrationOptions();
|
||||
trialSets ??= DefaultFiveSeedTrialSets;
|
||||
if (trialSets.Count == 0)
|
||||
{
|
||||
throw new ArgumentException("At least one IPA trial target set is required.", nameof(trialSets));
|
||||
}
|
||||
|
||||
var batchDirectory = Path.Combine(artifactRoot, options.BatchId);
|
||||
Directory.CreateDirectory(batchDirectory);
|
||||
var storePath = Path.Combine(batchDirectory, options.TrialResultStoreFileName);
|
||||
var referenceRenderer = new PinkTromboneReferenceRenderer();
|
||||
var analyzer = new AudioAnalyzer(new AudioAnalysisConfig(SampleRate: options.SampleRate));
|
||||
var results = new List<IpaTrialResult>();
|
||||
|
||||
foreach (var targetSet in trialSets)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var round = IpaGestureExperiment.WriteRound(
|
||||
batchDirectory,
|
||||
targetSet.Id,
|
||||
targetSet.Targets.Select(target => target.Target),
|
||||
targetSet.Variants,
|
||||
options.TimelineBlocks);
|
||||
var analysis = IpaGestureExperiment.AnalyzeRound(round);
|
||||
var targetById = targetSet.Targets.ToDictionary(target => target.Target.Id, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var candidate in round.Candidates)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var startedAt = DateTimeOffset.UtcNow;
|
||||
var startedStamp = Stopwatch.GetTimestamp();
|
||||
if (!targetById.TryGetValue(candidate.TargetId, out var target))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var candidateDir = Path.Combine(round.RoundDirectory, "audio", candidate.Id);
|
||||
Directory.CreateDirectory(candidateDir);
|
||||
var script = await File.ReadAllTextAsync(candidate.ScriptPath, cancellationToken).ConfigureAwait(false);
|
||||
var source = FaustEmitter.EmitScript(script, new FaustExportOptions(SafeName(candidate.Id))).Source;
|
||||
var dspPath = Path.Combine(candidateDir, "candidate.dsp");
|
||||
await File.WriteAllTextAsync(dspPath, source, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var referenceFixture = PinkTromboneParityFixtures.ById(target.ReferenceFixtureId);
|
||||
var reference = referenceRenderer.Render(referenceFixture.Controls, target.Target.DurationSeconds + .18f);
|
||||
var candidateRender = await FaustCompiler.RenderAsync(
|
||||
source,
|
||||
new FaustRenderOptions(reference.SampleRate, reference.Samples.Length / (float)reference.SampleRate),
|
||||
cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var referenceWav = Path.Combine(candidateDir, "reference.wav");
|
||||
var candidateWav = Path.Combine(candidateDir, "candidate.wav");
|
||||
WriteWav(referenceWav, reference.Samples, reference.SampleRate);
|
||||
var metrics = new List<SpeechScoreMetric>();
|
||||
string verdict;
|
||||
string evaluation;
|
||||
var artifacts = new List<SpeechRenderArtifact>
|
||||
{
|
||||
Artifact("candidate-script", candidate.ScriptPath),
|
||||
Artifact("candidate-dsp", dspPath),
|
||||
Artifact("primitive-timeline", candidate.TimelinePath),
|
||||
Artifact("reference-wav", referenceWav),
|
||||
Artifact("round-science-brief", analysis.ScienceBriefPath)
|
||||
};
|
||||
|
||||
if (candidateRender is null || candidateRender.Samples.Length == 0)
|
||||
{
|
||||
verdict = "render-failed";
|
||||
evaluation = candidateRender is null
|
||||
? "Faust was not available to render this candidate."
|
||||
: $"Faust render produced no samples. stderr: {candidateRender.Stderr}";
|
||||
metrics.Add(new SpeechScoreMetric("render_failed", 1, 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteWav(candidateWav, candidateRender.Samples, candidateRender.SampleRate);
|
||||
artifacts.Add(Artifact("candidate-wav", candidateWav));
|
||||
var comparison = analyzer.Compare(reference.Samples, candidateRender.Samples);
|
||||
metrics.AddRange(Metrics(candidate, round, comparison));
|
||||
verdict = Verdict(comparison);
|
||||
evaluation = EvaluationSentence(target.Target.Ipa, target.ReferenceFixtureId, comparison, verdict);
|
||||
await WriteComparisonReportAsync(
|
||||
Path.Combine(candidateDir, "comparison.txt"),
|
||||
target,
|
||||
candidate,
|
||||
comparison,
|
||||
verdict,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
artifacts.Add(Artifact("comparison-report", Path.Combine(candidateDir, "comparison.txt")));
|
||||
}
|
||||
|
||||
var latency = Stopwatch.GetElapsedTime(startedStamp).TotalMilliseconds;
|
||||
results.Add(new IpaTrialResult(
|
||||
$"{targetSet.Id}:{candidate.Id}",
|
||||
options.BatchId,
|
||||
startedAt.ToString("O", CultureInfo.InvariantCulture),
|
||||
targetSet.Id,
|
||||
targetSet.Targets.Select(item => item.Target.Ipa).Distinct(StringComparer.Ordinal).ToArray(),
|
||||
target.ReferenceFixtureId,
|
||||
candidate.Id,
|
||||
options.HypothesizerId,
|
||||
Hypothesis(targetSet, target, candidate),
|
||||
candidate.ScriptPath,
|
||||
referenceWav,
|
||||
File.Exists(candidateWav) ? candidateWav : "",
|
||||
candidate.TimelinePath,
|
||||
metrics.ToArray(),
|
||||
artifacts.ToArray(),
|
||||
options.EvaluatorId,
|
||||
evaluation,
|
||||
verdict,
|
||||
KnownLies(options, target.ReferenceFixtureId),
|
||||
[
|
||||
new SpeechTimingReceipt(
|
||||
"ipa-trial-render-score",
|
||||
startedAt.ToString("O", CultureInfo.InvariantCulture),
|
||||
latency,
|
||||
0,
|
||||
Confidence(metrics),
|
||||
"Local worker generated the candidate patch, rendered Faust audio, compared log-mel/articulation evidence, and wrote CultCache trial result data.")
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
await IpaTrialResultCultCacheStore.UpsertResultsAsync(storePath, results).ConfigureAwait(false);
|
||||
var summaryPath = Path.Combine(batchDirectory, "summary.csv");
|
||||
await File.WriteAllTextAsync(summaryPath, SummaryCsv(results), cancellationToken).ConfigureAwait(false);
|
||||
var evaluatorReport = Path.Combine(batchDirectory, "evaluator-report.md");
|
||||
await File.WriteAllTextAsync(evaluatorReport, EvaluatorReport(options, results), cancellationToken).ConfigureAwait(false);
|
||||
await IpaTrialResultCultCacheStore.UpsertResultsAsync(storePath, results).ConfigureAwait(false);
|
||||
|
||||
return new IpaTrialOrchestrationResult(
|
||||
options.BatchId,
|
||||
batchDirectory,
|
||||
storePath,
|
||||
summaryPath,
|
||||
evaluatorReport,
|
||||
results);
|
||||
}
|
||||
|
||||
private static IpaTrialReferenceTarget Target(string id, string ipa, string descriptor, string fixture, float duration) =>
|
||||
new(new IpaGestureExperimentTarget(id, ipa, descriptor, duration), fixture);
|
||||
|
||||
private static IpaGestureExperimentVariant Variant(string id, float intensity, float duration, string tag) =>
|
||||
new(id, intensity, duration, [tag]);
|
||||
|
||||
private static IReadOnlyList<SpeechScoreMetric> Metrics(
|
||||
IpaGestureExperimentCandidate candidate,
|
||||
IpaGestureExperimentResult round,
|
||||
AudioComparison comparison)
|
||||
{
|
||||
var gesture = round.Metrics.FirstOrDefault(metric =>
|
||||
metric.CandidateId.Equals(candidate.Id, StringComparison.OrdinalIgnoreCase) &&
|
||||
metric.Metric.Equals("gesture_score", StringComparison.OrdinalIgnoreCase))?.Value ?? 0;
|
||||
return
|
||||
[
|
||||
new("gesture_score", gesture, .25f),
|
||||
new("log_mel_cosine", comparison.LogMelCosineSimilarity, .25f),
|
||||
new("log_mel_distance", comparison.LogMelDistance, .15f),
|
||||
new("audio_score", comparison.Score, .15f),
|
||||
new("articulation_score", comparison.Articulation.ArticulationScore, .10f),
|
||||
new("rms_ratio", comparison.RmsRatio, .05f),
|
||||
new("speech_band_ratio", comparison.Articulation.SpeechBandRatio, .05f)
|
||||
];
|
||||
}
|
||||
|
||||
private static string Verdict(AudioComparison comparison)
|
||||
{
|
||||
if (comparison.LogMelCosineSimilarity >= .70f && comparison.Articulation.ArticulationScore >= .45f)
|
||||
{
|
||||
return "promising";
|
||||
}
|
||||
|
||||
if (comparison.LogMelCosineSimilarity >= .35f)
|
||||
{
|
||||
return "pressure";
|
||||
}
|
||||
|
||||
return "weak";
|
||||
}
|
||||
|
||||
private static string EvaluationSentence(string ipa, string referenceId, AudioComparison comparison, string verdict) =>
|
||||
$"IPA `{ipa}` against `{referenceId}` is `{verdict}`: logMelCosine={comparison.LogMelCosineSimilarity:0.0000}, logMelDistance={comparison.LogMelDistance:0.0000}, articulation={comparison.Articulation.ArticulationScore:0.0000}, rmsRatio={comparison.RmsRatio:0.0000}.";
|
||||
|
||||
private static string Hypothesis(IpaTrialTargetSet set, IpaTrialReferenceTarget target, IpaGestureExperimentCandidate candidate) =>
|
||||
$"{set.Description} Candidate `{candidate.Id}` tests whether descriptor `{target.Target.Descriptor}` drives the public anatomical control surfaces enough to approach `{target.ReferenceFixtureId}` spectrogram pressure.";
|
||||
|
||||
private static string[] KnownLies(IpaTrialOrchestrationOptions options, string referenceId) =>
|
||||
[
|
||||
$"Ground truth is `{referenceId}` from {options.ReferenceRenderer}, not an open IPA recording dataset.",
|
||||
"Log-mel evidence is normalized shape pressure and must be read with RMS/articulation witnesses.",
|
||||
"The first seed trials render one candidate per hypothesis set; they explore the orchestration surface before optimizer scale-out."
|
||||
];
|
||||
|
||||
private static float Confidence(IReadOnlyList<SpeechScoreMetric> metrics)
|
||||
{
|
||||
var cosine = metrics.FirstOrDefault(metric => metric.Name == "log_mel_cosine")?.Value ?? 0;
|
||||
var gesture = metrics.FirstOrDefault(metric => metric.Name == "gesture_score")?.Value ?? 0;
|
||||
return Math.Clamp((cosine + gesture) * .5f, 0, 1);
|
||||
}
|
||||
|
||||
private static SpeechRenderArtifact Artifact(string kind, string path) =>
|
||||
new(kind, path, File.Exists(path) ? Sha256(path) : "");
|
||||
|
||||
private static string Sha256(string path)
|
||||
{
|
||||
using var stream = File.OpenRead(path);
|
||||
var hash = SHA256.HashData(stream);
|
||||
return "sha256:" + Convert.ToHexString(hash).ToLowerInvariant();
|
||||
}
|
||||
|
||||
private static async Task WriteComparisonReportAsync(
|
||||
string path,
|
||||
IpaTrialReferenceTarget target,
|
||||
IpaGestureExperimentCandidate candidate,
|
||||
AudioComparison comparison,
|
||||
string verdict,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var text =
|
||||
$"candidate={candidate.Id}{Environment.NewLine}" +
|
||||
$"ipa={target.Target.Ipa}{Environment.NewLine}" +
|
||||
$"descriptor={target.Target.Descriptor}{Environment.NewLine}" +
|
||||
$"reference={target.ReferenceFixtureId}{Environment.NewLine}" +
|
||||
$"verdict={verdict}{Environment.NewLine}" +
|
||||
$"logMelCosine={comparison.LogMelCosineSimilarity:0.######}{Environment.NewLine}" +
|
||||
$"logMelDistance={comparison.LogMelDistance:0.######}{Environment.NewLine}" +
|
||||
$"score={comparison.Score:0.######}{Environment.NewLine}" +
|
||||
$"rmsRatio={comparison.RmsRatio:0.######}{Environment.NewLine}" +
|
||||
$"centroidRatio={comparison.CentroidRatio:0.######}{Environment.NewLine}" +
|
||||
$"articulation={comparison.Articulation.ArticulationScore:0.######}{Environment.NewLine}" +
|
||||
$"speechBandRatio={comparison.Articulation.SpeechBandRatio:0.######}{Environment.NewLine}";
|
||||
await File.WriteAllTextAsync(path, text, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private static string SummaryCsv(IReadOnlyList<IpaTrialResult> results)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine("trial_id,target_set_id,candidate_id,reference_id,verdict,gesture_score,log_mel_cosine,log_mel_distance,audio_score,articulation_score,rms_ratio");
|
||||
foreach (var result in results)
|
||||
{
|
||||
builder.Append(Escape(result.TrialId));
|
||||
builder.Append(',');
|
||||
builder.Append(Escape(result.TargetSetId));
|
||||
builder.Append(',');
|
||||
builder.Append(Escape(result.CandidateId));
|
||||
builder.Append(',');
|
||||
builder.Append(Escape(result.ReferenceId));
|
||||
builder.Append(',');
|
||||
builder.Append(Escape(result.Verdict));
|
||||
builder.Append(',');
|
||||
builder.Append(Metric(result, "gesture_score"));
|
||||
builder.Append(',');
|
||||
builder.Append(Metric(result, "log_mel_cosine"));
|
||||
builder.Append(',');
|
||||
builder.Append(Metric(result, "log_mel_distance"));
|
||||
builder.Append(',');
|
||||
builder.Append(Metric(result, "audio_score"));
|
||||
builder.Append(',');
|
||||
builder.Append(Metric(result, "articulation_score"));
|
||||
builder.Append(',');
|
||||
builder.AppendLine(Metric(result, "rms_ratio"));
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
private static string EvaluatorReport(IpaTrialOrchestrationOptions options, IReadOnlyList<IpaTrialResult> results)
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine($"# IPA Trial Evaluator Report: {options.BatchId}");
|
||||
builder.AppendLine();
|
||||
builder.AppendLine("Authority: this evaluator reads generated candidate patches, primitive timelines, rendered audio comparisons, and CultCache trial records. It does not mutate optimizer checkpoints.");
|
||||
builder.AppendLine();
|
||||
foreach (var group in results.GroupBy(result => result.TargetSetId).OrderBy(group => group.Key, StringComparer.Ordinal))
|
||||
{
|
||||
builder.AppendLine($"## {group.Key}");
|
||||
foreach (var result in group.OrderBy(result => result.TrialId, StringComparer.Ordinal))
|
||||
{
|
||||
builder.Append("- ");
|
||||
builder.Append(result.CandidateId);
|
||||
builder.Append(": ");
|
||||
builder.Append(result.EvaluationSummary);
|
||||
builder.Append(" Known contamination: ");
|
||||
builder.AppendLine(string.Join(" / ", result.KnownLies));
|
||||
}
|
||||
builder.AppendLine();
|
||||
}
|
||||
|
||||
builder.AppendLine("## Next Hypothesis Pressure");
|
||||
foreach (var result in results
|
||||
.OrderBy(result => float.Parse(Metric(result, "log_mel_cosine"), CultureInfo.InvariantCulture))
|
||||
.ThenBy(result => result.TrialId, StringComparer.Ordinal)
|
||||
.Take(5))
|
||||
{
|
||||
builder.Append("- ");
|
||||
builder.Append(result.TrialId);
|
||||
builder.Append(": inspect ");
|
||||
builder.Append(result.PrimitiveTimelineUri);
|
||||
builder.Append(" and ");
|
||||
builder.Append(result.CandidatePatchUri);
|
||||
builder.AppendLine(" before adding more synthesis dressing.");
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
private static string Metric(IpaTrialResult result, string name) =>
|
||||
(result.Metrics.FirstOrDefault(metric => metric.Name == name)?.Value ?? 0)
|
||||
.ToString("0.######", CultureInfo.InvariantCulture);
|
||||
|
||||
private static string SafeName(string value) =>
|
||||
new(value.Select(character => char.IsLetterOrDigit(character) ? character : '_').ToArray());
|
||||
|
||||
private static string Escape(string value) =>
|
||||
value.Contains(',') || value.Contains('"') || value.Contains('\n') || value.Contains('\r')
|
||||
? $"\"{value.Replace("\"", "\"\"", StringComparison.Ordinal)}\""
|
||||
: value;
|
||||
|
||||
private static void WriteWav(string path, IReadOnlyList<float> samples, int sampleRate)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
using var stream = File.Create(path);
|
||||
using var writer = new BinaryWriter(stream, Encoding.ASCII);
|
||||
var dataLength = samples.Count * sizeof(short);
|
||||
writer.Write(Encoding.ASCII.GetBytes("RIFF"));
|
||||
writer.Write(36 + dataLength);
|
||||
writer.Write(Encoding.ASCII.GetBytes("WAVEfmt "));
|
||||
writer.Write(16);
|
||||
writer.Write((short)1);
|
||||
writer.Write((short)1);
|
||||
writer.Write(sampleRate);
|
||||
writer.Write(sampleRate * sizeof(short));
|
||||
writer.Write((short)sizeof(short));
|
||||
writer.Write((short)16);
|
||||
writer.Write(Encoding.ASCII.GetBytes("data"));
|
||||
writer.Write(dataLength);
|
||||
foreach (var sample in samples)
|
||||
{
|
||||
writer.Write((short)Math.Clamp(sample * short.MaxValue, short.MinValue, short.MaxValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue