Add eSpeak gradient descent fixture
This commit is contained in:
parent
462439c0bd
commit
ce15086ca9
5 changed files with 388 additions and 4 deletions
|
|
@ -474,6 +474,22 @@ per fixture and a compact log-mel distance matrix across the workout. This is
|
|||
the first external speech judge for the vocal tract lane. It should pressure
|
||||
AquaSynth's future IPA/gesture/render path, not replace it.
|
||||
|
||||
The optional test `EspeakNgGroundTruthTrainsUtteranceEmbeddingAndSynthDriverWhenInstalled`
|
||||
turns the same idea into a gradient-descent fixture. When eSpeak is present it
|
||||
renders the tiny syllable set, extracts normalized log-mel means, trains
|
||||
`UtteranceEmbeddingNeuralEncoder` toward compressed eSpeak evidence, then trains
|
||||
`VocalTractNeuralMapper` toward tract/mod/filter/mel controls seeded by that
|
||||
evidence. It writes WAVs plus a training report under:
|
||||
|
||||
```text
|
||||
artifacts/parity/espeak-ng-gradient-descent/<timestamp>-tiny/
|
||||
*.wav
|
||||
training-report.md
|
||||
```
|
||||
|
||||
This is supervised pressure from generated speech evidence. It is not yet
|
||||
waveform-loss backprop through an AquaSynth renderer.
|
||||
|
||||
## First Work Packets
|
||||
|
||||
1. Add `docs/ipa-vocal-tract-roadmap.md` and preserve the boundary doctrine.
|
||||
|
|
@ -481,9 +497,8 @@ AquaSynth's future IPA/gesture/render path, not replace it.
|
|||
3. Add a human baseline vowel planner that emits tract-area targets, not audio.
|
||||
4. Add a tiny tract renderer prototype in AquaSynth.Faust or a test-only lab
|
||||
lane, then decide the package boundary once the prototype proves itself.
|
||||
5. Train the learned controller against eSpeak/log-mel, hand-authored tract
|
||||
targets, and PAD-style spectral-table targets once the renderer can produce
|
||||
comparable buffers.
|
||||
5. Connect the eSpeak/log-mel gradient fixture to the first audible AquaSynth
|
||||
vowel/tract renderer once the renderer can produce comparable buffers.
|
||||
6. Render five vowels and three CVC syllables through fixtures.
|
||||
7. Add the first Weksa phonology profile with explicit unsupported-feature
|
||||
diagnostics.
|
||||
|
|
|
|||
|
|
@ -112,3 +112,4 @@
|
|||
{"date":"2026-05-19","topic":"vocal-tract-learned-controller","lesson":"AquaSynth now has a small learned controller scaffold for the vocal-tract lane: phonetic features plus a semantic embedding vector feed a multilayer tanh/sigmoid MLP trained with SGD/backprop to predict tract controls plus AM/FM, LFO, filter, and mel spectral-envelope expression lanes. This is a trainable controller surface, not proof of eSpeak-level speech yet.","evidence":"Added VocalTractLearning.cs and VocalTractLearningTests.cs with feature encoding, semantic embeddings, 3-layer default network, loss-reduction tests, same-phone semantic steering, expressive modulator/filter control tests, and learned dark/bright mel-envelope steering for PAD-style spectral-table coloration."}
|
||||
{"date":"2026-05-19","topic":"vocal-tract-optimizer-hot-path","lesson":"The first learned vocal-tract optimizer should stay native and inspectable until the render-loss boundary proves it needs a tensor framework. The trainer now uses packed row-major weight buffers, owned scratch activation/delta/target buffers, unsafe pointer loops, reusable gradient/moment buffers, batched gradients, deterministic shuffling, and Adam by default instead of allocation-heavy per-example jagged-array SGD.","evidence":"Rebuilt VocalTractLearning.cs hot path around flat float arrays and unsafe loops; enabled unsafe blocks in AquaSynth.Core; VocalTractLearningTests pass against the packed-buffer Adam trainer."}
|
||||
{"date":"2026-05-19","topic":"utterance-embedding-learning-boundary","lesson":"AquaSynth should own gradient descent for Weksa speech handoff. Weksa supplies speech text embeddings, prosody/emphasis hints, and Ghostlight/Epiphany-shaped character state vectors; AquaSynth trains the tiny utterance-embedding encoder and passes that embedding into the synth-driver model.","evidence":"Extracted PackedNeuralNetwork as the shared packed-buffer Adam/SGD primitive, rewired VocalTractNeuralMapper through it, and added UtteranceEmbeddingInput/UtteranceEmbeddingNeuralEncoder plus tests for packing, training, and feeding the vocal-tract semantic embedding."}
|
||||
{"date":"2026-05-19","topic":"espeak-gradient-descent-fixture","lesson":"The first speech-learning fixture should train on generated eSpeak evidence before pretending AquaSynth has waveform-loss vocal-tract backprop. eSpeak log-mel means can supervise both utterance embedding targets and synth-driver mel/control lanes, giving the packed optimizer real generated speech pressure while the renderer is still catching up.","evidence":"Added EspeakNgGradientDescentTests. When ESPEAK_NG or espeak/espeak-ng is available, it renders a/pa/ta/ka/sa/ma, extracts log-mel means, trains UtteranceEmbeddingNeuralEncoder, trains VocalTractNeuralMapper against eSpeak-seeded control targets, asserts loss reductions, and writes artifacts/parity/espeak-ng-gradient-descent/<timestamp>-tiny/training-report.md."}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ modules:
|
|||
native_faust_runtime: src/AquaSynth.Faust/NativeFaustRuntime.cs
|
||||
dx7_reference_render_tests: tests/AquaSynth.Dsl.Tests/Dx7ReferenceParityTests.cs
|
||||
espeak_ng_reference_tests: tests/AquaSynth.Dsl.Tests/EspeakNgReferenceTests.cs
|
||||
espeak_ng_gradient_descent_tests: tests/AquaSynth.Dsl.Tests/EspeakNgGradientDescentTests.cs
|
||||
zyn_reference_reader: src/AquaSynth.Core/ZynInstrument.cs
|
||||
zyn_coverage_survey: src/AquaSynth.Core/ZynInstrument.cs
|
||||
ipa_vocal_tract_roadmap: docs/ipa-vocal-tract-roadmap.md
|
||||
|
|
@ -72,6 +73,7 @@ boundaries:
|
|||
- packed-buffer native backprop/Adam as the shared AquaSynth gradient-descent primitive
|
||||
- learned utterance embedding encoder from speech text embeddings, prosody/emphasis hints, and Ghostlight/Epiphany-shaped character state vectors
|
||||
- learned phonetic-plus-utterance-embedding synth driver with packed-buffer native backprop/Adam for tract, AM/FM, LFO, filter, and mel spectral-envelope controls
|
||||
- optional eSpeak NG generated ground-truth fixture for supervised utterance-embedding and synth-driver gradient descent
|
||||
dev_only:
|
||||
- public-domain DX7 SysEx fixtures
|
||||
- pinned GPL ZynAddSubFX source for parity tests
|
||||
|
|
|
|||
|
|
@ -86,7 +86,7 @@
|
|||
"rule": "Add DSL abstractions only when reference rebuilds prove the missing invariant. Do not consume references that cannot be backed by parity tests; parsing alone is inventory, not evidence of expressivity."
|
||||
},
|
||||
"ipa_vocal_tract_path": {
|
||||
"status": "Roadmap drafted for an IPA-to-vocal-tract lane, and the first Core contracts now defend the boundary: PhoneticIntent is the Weksa/language-owned handoff, ArticulatoryPlan is the AquaSynth realization layer, VocalTractMorphology declares anatomy capabilities, ArticulatoryConstraintReport is the host-visible veto surface, and VocalTractPlanResult is the tiny end-to-end proof artifact for host branching. IPA is an authoring input only: tokenization and feature mapping lower into language phonology profiles, timed articulatory gestures, morphology-specific tract/excitation curves, and Faust DSP. The staged support plan starts with tokenization and vowels, then core pulmonic consonants, expanded human IPA, airstream/phonation, and Weksa/alien morphologies. Optional eSpeak NG tests now provide a first external speech workout when ESPEAK_NG or espeak-ng is installed; eSpeak is intelligibility/timing pressure, not anatomy truth. AquaSynth now owns the learned utterance embedding encoder and synth driver: speech text embeddings plus prosody/emphasis hints plus Ghostlight/Epiphany-shaped character state vectors compress into utterance embeddings, and phonetic features plus that embedding feed a packed-buffer native C# backprop/Adam mapper that predicts tract, AM/FM, LFO, filter, and mel spectral-envelope controls.",
|
||||
"status": "Roadmap drafted for an IPA-to-vocal-tract lane, and the first Core contracts now defend the boundary: PhoneticIntent is the Weksa/language-owned handoff, ArticulatoryPlan is the AquaSynth realization layer, VocalTractMorphology declares anatomy capabilities, ArticulatoryConstraintReport is the host-visible veto surface, and VocalTractPlanResult is the tiny end-to-end proof artifact for host branching. IPA is an authoring input only: tokenization and feature mapping lower into language phonology profiles, timed articulatory gestures, morphology-specific tract/excitation curves, and Faust DSP. The staged support plan starts with tokenization and vowels, then core pulmonic consonants, expanded human IPA, airstream/phonation, and Weksa/alien morphologies. Optional eSpeak NG tests now provide a first external speech workout when ESPEAK_NG or espeak-ng is installed; eSpeak is intelligibility/timing pressure, not anatomy truth. AquaSynth now owns the learned utterance embedding encoder and synth driver: speech text embeddings plus prosody/emphasis hints plus Ghostlight/Epiphany-shaped character state vectors compress into utterance embeddings, and phonetic features plus that embedding feed a packed-buffer native C# backprop/Adam mapper that predicts tract, AM/FM, LFO, filter, and mel spectral-envelope controls. The first generated ground-truth training fixture renders eSpeak syllables, extracts log-mel evidence, trains the utterance encoder, then trains the synth driver toward mel/control targets seeded by that evidence.",
|
||||
"lesson": "Do not build a complete IPA parser before the tract can say basic syllables. The durable machine is tokenizer -> feature mapper -> phonology profile -> gesture planner -> morphology -> tract plan -> Faust renderer, with dependency-injected seams and mocks at each boundary. Weksa should use IPA-like phonetic input without letting human IPA semantics own alien anatomy. Anatomy has veto power: if a morphology lacks bilabial closure or another required capability, AquaSynth reports that failure instead of smearing over it.",
|
||||
"next": "Implement Stage 0/1 fixtures: IPA token spans, vowel feature bundles, human baseline vowel tract-area targets, and a tiny audible vowel render proof before expanding consonants."
|
||||
},
|
||||
|
|
|
|||
366
tests/AquaSynth.Dsl.Tests/EspeakNgGradientDescentTests.cs
Normal file
366
tests/AquaSynth.Dsl.Tests/EspeakNgGradientDescentTests.cs
Normal file
|
|
@ -0,0 +1,366 @@
|
|||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using AquaSynth.Dsl;
|
||||
|
||||
namespace AquaSynth.Dsl.Tests;
|
||||
|
||||
public sealed class EspeakNgGradientDescentTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task EspeakNgGroundTruthTrainsUtteranceEmbeddingAndSynthDriverWhenInstalled()
|
||||
{
|
||||
if (!EspeakNgReferenceRenderer.TryFind(out var renderer))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var root = RepositoryRoot();
|
||||
var artifactDir = Path.Combine(root, "artifacts", "parity", "espeak-ng-gradient-descent", RunFolderName("tiny"));
|
||||
Directory.CreateDirectory(artifactDir);
|
||||
|
||||
var fixtures = Fixtures();
|
||||
var groundTruth = new List<GroundTruthFixture>();
|
||||
foreach (var fixture in fixtures)
|
||||
{
|
||||
var wavPath = Path.Combine(artifactDir, $"{fixture.Id}.wav");
|
||||
var render = await renderer.RenderAsync(fixture.Text, fixture.Voice, wavPath);
|
||||
Assert.Equal(0, render.ExitCode);
|
||||
Assert.True(File.Exists(wavPath), $"eSpeak NG did not write {wavPath}.");
|
||||
|
||||
var samples = ReadMonoPcm16Wav(wavPath);
|
||||
var analysis = AudioAnalyzer.AnalyzeAudio(samples);
|
||||
var logMelMean = MeanBands(analysis.LogMelSpectrogram);
|
||||
groundTruth.Add(new GroundTruthFixture(fixture, logMelMean, Downsample(logMelMean, 16)));
|
||||
}
|
||||
|
||||
var utteranceEncoder = UtteranceEmbeddingNeuralEncoder.Create(
|
||||
inputSize: groundTruth[0].Fixture.UtteranceInput.ToFeatureVector().Values.Count,
|
||||
embeddingSize: 16,
|
||||
hiddenLayerSizes: [64, 48, 32],
|
||||
seed: 101);
|
||||
var utteranceExamples = groundTruth
|
||||
.Select(item => new UtteranceEmbeddingTrainingExample(
|
||||
item.Fixture.UtteranceInput.ToFeatureVector(),
|
||||
new UtteranceEmbedding(item.TargetEmbedding)))
|
||||
.ToArray();
|
||||
|
||||
var utteranceBefore = AverageUtteranceLoss(utteranceEncoder, utteranceExamples);
|
||||
var utteranceResult = utteranceEncoder.Train(utteranceExamples, new PackedNeuralTrainingOptions(Epochs: 420, LearningRate: 0.03f, Seed: 101, BatchSize: 3));
|
||||
var utteranceAfter = AverageUtteranceLoss(utteranceEncoder, utteranceExamples);
|
||||
|
||||
var vocalMapper = VocalTractNeuralMapper.Create(semanticEmbeddingSize: 16, melBandCount: 32, hiddenLayerSizes: [80, 56, 40], seed: 103);
|
||||
var vocalExamples = groundTruth
|
||||
.Select(item => new VocalTractTrainingExample(
|
||||
item.Fixture.Event,
|
||||
utteranceEncoder.Encode(item.Fixture.UtteranceInput).ToSemanticEmbedding(),
|
||||
TargetFromGroundTruth(item.Fixture.Event, item.LogMelMean)))
|
||||
.ToArray();
|
||||
|
||||
var vocalBefore = AverageVocalLoss(vocalMapper, vocalExamples);
|
||||
var vocalResult = vocalMapper.Train(vocalExamples, new VocalTractTrainingOptions(Epochs: 520, LearningRate: 0.035f, Seed: 103, BatchSize: 3));
|
||||
var vocalAfter = AverageVocalLoss(vocalMapper, vocalExamples);
|
||||
|
||||
Assert.True(utteranceAfter < utteranceBefore * 0.45f, $"utterance embedding loss did not drop enough; before={utteranceBefore}, after={utteranceAfter}");
|
||||
Assert.True(vocalAfter < vocalBefore * 0.45f, $"vocal driver loss did not drop enough; before={vocalBefore}, after={vocalAfter}");
|
||||
|
||||
var report = new List<string>
|
||||
{
|
||||
"eSpeak NG gradient descent fixture",
|
||||
$"renderer: {renderer.CommandPath}",
|
||||
"role: generated supervised ground truth for AquaSynth utterance embedding and synth-driver training",
|
||||
$"utterance_loss_before: {utteranceBefore:0.######}",
|
||||
$"utterance_loss_after: {utteranceAfter:0.######}",
|
||||
$"utterance_epochs: {utteranceResult.Steps.Count}",
|
||||
$"vocal_loss_before: {vocalBefore:0.######}",
|
||||
$"vocal_loss_after: {vocalAfter:0.######}",
|
||||
$"vocal_epochs: {vocalResult.Steps.Count}",
|
||||
"fixtures:"
|
||||
};
|
||||
foreach (var item in groundTruth)
|
||||
{
|
||||
report.Add($" {item.Fixture.Id}: text={item.Fixture.Text}, mel={FormatBands(item.LogMelMean)}");
|
||||
}
|
||||
|
||||
await File.WriteAllLinesAsync(Path.Combine(artifactDir, "training-report.md"), report);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<TrainingFixture> Fixtures() =>
|
||||
[
|
||||
Fixture("open-vowel", "a", new PhoneticFeatures(PhoneticManner.Vowel, Height: VowelHeight.Open, Backness: VowelBackness.Front), [0.90f, 0.15f, 0.20f, 0.70f, 0.40f, 0.10f, 0.25f, 0.55f]),
|
||||
Fixture("bilabial-open", "pa", new PhoneticFeatures(PhoneticManner.Stop, PhoneticPlace.Bilabial, Phonation.Voiceless), [0.80f, 0.70f, 0.22f, 0.65f, 0.35f, 0.12f, 0.40f, 0.52f]),
|
||||
Fixture("alveolar-open", "ta", new PhoneticFeatures(PhoneticManner.Stop, PhoneticPlace.Alveolar, Phonation.Voiceless), [0.74f, 0.62f, 0.35f, 0.58f, 0.42f, 0.16f, 0.45f, 0.49f]),
|
||||
Fixture("velar-open", "ka", new PhoneticFeatures(PhoneticManner.Stop, PhoneticPlace.Velar, Phonation.Voiceless), [0.68f, 0.55f, 0.48f, 0.52f, 0.46f, 0.18f, 0.52f, 0.45f]),
|
||||
Fixture("sibilant-open", "sa", new PhoneticFeatures(PhoneticManner.Fricative, PhoneticPlace.Alveolar, Phonation.Voiceless), [0.55f, 0.38f, 0.86f, 0.40f, 0.60f, 0.25f, 0.72f, 0.36f]),
|
||||
Fixture("nasal-open", "ma", new PhoneticFeatures(PhoneticManner.Nasal, PhoneticPlace.Bilabial, Phonation.Voiced, Nasalized: true), [0.82f, 0.48f, 0.28f, 0.74f, 0.38f, 0.58f, 0.30f, 0.62f])
|
||||
];
|
||||
|
||||
private static TrainingFixture Fixture(string id, string text, PhoneticFeatures features, IReadOnlyList<float> textEmbedding)
|
||||
{
|
||||
var prosody = new[] { 0.75f, text.Length / 4f, features.Manner == PhoneticManner.Fricative ? 0.95f : 0.35f, features.Manner == PhoneticManner.Nasal ? 0.80f : 0.25f };
|
||||
var characterState = new[] { 0.65f, 0.35f, 0.55f, features.Place == PhoneticPlace.Bilabial ? 0.72f : 0.28f };
|
||||
var input = new UtteranceEmbeddingInput(textEmbedding, prosody, characterState);
|
||||
var phoneticEvent = new PhoneticEvent(id, text, features, DurationSeconds: 0.20, Prosody: new PhoneticProsody(Stress: 0.75f, Intensity: 0.8f));
|
||||
return new TrainingFixture(id, text, "en", phoneticEvent, input);
|
||||
}
|
||||
|
||||
private static VocalTractControlTarget TargetFromGroundTruth(PhoneticEvent phoneticEvent, IReadOnlyList<float> logMelMean)
|
||||
{
|
||||
var isStop = phoneticEvent.Features.Manner == PhoneticManner.Stop;
|
||||
var isFricative = phoneticEvent.Features.Manner == PhoneticManner.Fricative;
|
||||
var isNasal = phoneticEvent.Features.Manner == PhoneticManner.Nasal;
|
||||
var lipAperture = phoneticEvent.Features.Place == PhoneticPlace.Bilabial ? 0.22f : 0.58f;
|
||||
return new VocalTractControlTarget(
|
||||
TongueBody: phoneticEvent.Features.Place == PhoneticPlace.Velar ? 0.72f : 0.44f,
|
||||
TongueTip: phoneticEvent.Features.Place == PhoneticPlace.Alveolar ? 0.86f : 0.34f,
|
||||
LipAperture: phoneticEvent.Features.Manner == PhoneticManner.Vowel ? 0.88f : lipAperture,
|
||||
LipRounding: 0.05f,
|
||||
Velum: isNasal ? 0.85f : 0.0f,
|
||||
GlottalTenseness: phoneticEvent.Features.Phonation == Phonation.Voiceless ? 0.20f : 0.52f,
|
||||
Turbulence: isFricative ? 0.94f : isStop ? 0.55f : 0.05f,
|
||||
Pressure: isStop ? 0.86f : isFricative ? 0.72f : 0.38f,
|
||||
AmDepth: isNasal ? 0.30f : 0.12f,
|
||||
FmDepth: isFricative ? 0.42f : 0.16f,
|
||||
LfoRate: 0.20f,
|
||||
LfoDepth: isNasal ? 0.24f : 0.10f,
|
||||
FilterCutoff: isFricative ? 0.82f : 0.55f,
|
||||
FilterResonance: isFricative ? 0.62f : 0.22f,
|
||||
MelSpectralEnvelope: logMelMean);
|
||||
}
|
||||
|
||||
private static float AverageUtteranceLoss(UtteranceEmbeddingNeuralEncoder encoder, IReadOnlyList<UtteranceEmbeddingTrainingExample> examples)
|
||||
{
|
||||
var loss = 0f;
|
||||
foreach (var example in examples)
|
||||
{
|
||||
var prediction = encoder.Encode(example.Features);
|
||||
loss += MeanSquaredError(prediction.Values, example.Target.Values);
|
||||
}
|
||||
|
||||
return loss / examples.Count;
|
||||
}
|
||||
|
||||
private static float AverageVocalLoss(VocalTractNeuralMapper mapper, IReadOnlyList<VocalTractTrainingExample> examples)
|
||||
{
|
||||
var loss = 0f;
|
||||
foreach (var example in examples)
|
||||
{
|
||||
var prediction = mapper.Predict(example.Event, example.SemanticEmbedding);
|
||||
loss += MeanSquaredError(prediction.ToVector(mapper.MelBandCount), example.Target.ToVector(mapper.MelBandCount));
|
||||
}
|
||||
|
||||
return loss / examples.Count;
|
||||
}
|
||||
|
||||
private static float MeanSquaredError(IReadOnlyList<float> actual, IReadOnlyList<float> target)
|
||||
{
|
||||
var count = Math.Min(actual.Count, target.Count);
|
||||
var loss = 0f;
|
||||
for (var index = 0; index < count; index++)
|
||||
{
|
||||
var difference = actual[index] - target[index];
|
||||
loss += difference * difference;
|
||||
}
|
||||
|
||||
return loss / Math.Max(1, count);
|
||||
}
|
||||
|
||||
private static float[] Downsample(IReadOnlyList<float> values, int count)
|
||||
{
|
||||
var output = new float[count];
|
||||
for (var index = 0; index < count; index++)
|
||||
{
|
||||
var source = (int)MathF.Round(index * (values.Count - 1) / Math.Max(1f, count - 1f));
|
||||
output[index] = values[source];
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
private static float[] MeanBands(Spectrogram spectrogram)
|
||||
{
|
||||
var means = new float[spectrogram.Bands];
|
||||
for (var frame = 0; frame < spectrogram.Frames; frame++)
|
||||
{
|
||||
for (var band = 0; band < spectrogram.Bands; band++)
|
||||
{
|
||||
means[band] += spectrogram.At(frame, band);
|
||||
}
|
||||
}
|
||||
|
||||
for (var band = 0; band < means.Length; band++)
|
||||
{
|
||||
means[band] /= Math.Max(1, spectrogram.Frames);
|
||||
}
|
||||
|
||||
return means;
|
||||
}
|
||||
|
||||
private static string RepositoryRoot()
|
||||
{
|
||||
var directory = new DirectoryInfo(AppContext.BaseDirectory);
|
||||
while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "AquaSynth.sln")))
|
||||
{
|
||||
directory = directory.Parent;
|
||||
}
|
||||
|
||||
return directory?.FullName ?? throw new InvalidOperationException("could not find repository root");
|
||||
}
|
||||
|
||||
private static string RunFolderName(string label) =>
|
||||
$"{DateTime.UtcNow:yyyyMMdd-HHmmss}-{label}";
|
||||
|
||||
private static string FormatBands(IReadOnlyList<float> bands) =>
|
||||
string.Join(',', bands.Select(value => value.ToString("0.###")));
|
||||
|
||||
private static float[] ReadMonoPcm16Wav(string path)
|
||||
{
|
||||
var bytes = File.ReadAllBytes(path);
|
||||
if (bytes.Length < 44 ||
|
||||
Encoding.ASCII.GetString(bytes, 0, 4) != "RIFF" ||
|
||||
Encoding.ASCII.GetString(bytes, 8, 4) != "WAVE")
|
||||
{
|
||||
throw new InvalidOperationException($"{path} is not a RIFF/WAVE file.");
|
||||
}
|
||||
|
||||
var offset = 12;
|
||||
ushort channels = 0;
|
||||
ushort bitsPerSample = 0;
|
||||
var dataOffset = -1;
|
||||
var dataSize = 0;
|
||||
while (offset + 8 <= bytes.Length)
|
||||
{
|
||||
var chunk = Encoding.ASCII.GetString(bytes, offset, 4);
|
||||
var size = BitConverter.ToInt32(bytes, offset + 4);
|
||||
offset += 8;
|
||||
if (chunk == "fmt ")
|
||||
{
|
||||
var format = BitConverter.ToUInt16(bytes, offset);
|
||||
channels = BitConverter.ToUInt16(bytes, offset + 2);
|
||||
bitsPerSample = BitConverter.ToUInt16(bytes, offset + 14);
|
||||
if (format != 1 || channels == 0 || bitsPerSample != 16)
|
||||
{
|
||||
throw new InvalidOperationException($"{path} must be PCM16 WAV.");
|
||||
}
|
||||
}
|
||||
else if (chunk == "data")
|
||||
{
|
||||
dataOffset = offset;
|
||||
dataSize = size;
|
||||
break;
|
||||
}
|
||||
|
||||
offset += size + (size & 1);
|
||||
}
|
||||
|
||||
if (dataOffset < 0 || channels == 0)
|
||||
{
|
||||
throw new InvalidOperationException($"{path} has no readable data chunk.");
|
||||
}
|
||||
|
||||
var frames = dataSize / (channels * 2);
|
||||
var samples = new float[frames];
|
||||
for (var frame = 0; frame < frames; frame++)
|
||||
{
|
||||
var sum = 0f;
|
||||
for (var channel = 0; channel < channels; channel++)
|
||||
{
|
||||
var index = dataOffset + (frame * channels + channel) * 2;
|
||||
sum += BitConverter.ToInt16(bytes, index) / 32768f;
|
||||
}
|
||||
|
||||
samples[frame] = sum / channels;
|
||||
}
|
||||
|
||||
return samples;
|
||||
}
|
||||
|
||||
private sealed record TrainingFixture(
|
||||
string Id,
|
||||
string Text,
|
||||
string Voice,
|
||||
PhoneticEvent Event,
|
||||
UtteranceEmbeddingInput UtteranceInput);
|
||||
|
||||
private sealed record GroundTruthFixture(
|
||||
TrainingFixture Fixture,
|
||||
float[] LogMelMean,
|
||||
float[] TargetEmbedding);
|
||||
|
||||
private sealed class EspeakNgReferenceRenderer(string commandPath)
|
||||
{
|
||||
public string CommandPath { get; } = commandPath;
|
||||
|
||||
public static bool TryFind(out EspeakNgReferenceRenderer renderer)
|
||||
{
|
||||
var configured = Environment.GetEnvironmentVariable("ESPEAK_NG");
|
||||
if (!string.IsNullOrWhiteSpace(configured) && File.Exists(configured))
|
||||
{
|
||||
renderer = new EspeakNgReferenceRenderer(configured);
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (var name in new[] { "espeak-ng", "espeak" })
|
||||
{
|
||||
if (TryFindOnPath(name, out var path))
|
||||
{
|
||||
renderer = new EspeakNgReferenceRenderer(path);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
renderer = null!;
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<(int ExitCode, string Stdout, string Stderr)> RenderAsync(string text, string voice, string wavPath)
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(wavPath)!);
|
||||
return await RunAsync(["-v", voice, "-w", wavPath, text]);
|
||||
}
|
||||
|
||||
private static bool TryFindOnPath(string name, out string path)
|
||||
{
|
||||
var paths = (Environment.GetEnvironmentVariable("PATH") ?? "")
|
||||
.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
var extensions = OperatingSystem.IsWindows()
|
||||
? (Environment.GetEnvironmentVariable("PATHEXT") ?? ".EXE;.CMD;.BAT")
|
||||
.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
: [""];
|
||||
foreach (var directory in paths)
|
||||
{
|
||||
foreach (var extension in extensions)
|
||||
{
|
||||
var candidate = Path.Combine(directory, name + extension);
|
||||
if (File.Exists(candidate))
|
||||
{
|
||||
path = candidate;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
path = "";
|
||||
return false;
|
||||
}
|
||||
|
||||
private async Task<(int ExitCode, string Stdout, string Stderr)> RunAsync(IReadOnlyList<string> arguments)
|
||||
{
|
||||
var start = new ProcessStartInfo(CommandPath)
|
||||
{
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false
|
||||
};
|
||||
foreach (var argument in arguments)
|
||||
{
|
||||
start.ArgumentList.Add(argument);
|
||||
}
|
||||
|
||||
using var process = Process.Start(start) ?? throw new InvalidOperationException($"failed to start `{CommandPath}`");
|
||||
var stdout = process.StandardOutput.ReadToEndAsync();
|
||||
var stderr = process.StandardError.ReadToEndAsync();
|
||||
await process.WaitForExitAsync();
|
||||
return (process.ExitCode, await stdout, await stderr);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue