Tract: coarticulation fixes for the tract-weak-spots-1 listening verdict (#52)

Merged as head maintainer after Soul passes 2 and 3 (aqua-body:verdict:cut-tract-coarticulation-pr52.s2, ...fix2.s3). Unheard: every clip is a measurement, not a listening.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Aqua 2026-11-13 22:54:17 +00:00
commit ca6af1d678
19 changed files with 981 additions and 71 deletions

View file

@ -16,6 +16,8 @@ seg(t,t0,d,a,b) = a + (b - a) * clip01((t - t0) / max(0.0001, d));
bez(t,a,b,c,d) = (1.0 - t) * (1.0 - t) * (1.0 - t) * a + 3.0 * (1.0 - t) * (1.0 - t) * t * b + 3.0 * (1.0 - t) * t * t * c + t * t * t * d;
seg_bez(t,t0,d,a,b,c,e) = bez(clip01((t - t0) / max(0.0001, d)), a, b, c, e);
smooth01(x) = clip01(x) * clip01(x) * (3.0 - 2.0 * clip01(x));
smoo_from(pole, x) = loop ~ _ with { loop(prev) = select2(1 - 1', prev * pole + x * (1.0 - pole), x); };
smoo_init(x) = smoo_from(1.0 - 44.1 / ma.SR, x);
seg_smooth(t,t0,d,a,b) = a + (b - a) * smooth01((t - t0) / max(0.0001, d));
seg_exp(t,t0,d,a,b) = exp(log(max(0.00001, a)) + (log(max(0.00001, b)) - log(max(0.00001, a))) * clip01((t - t0) / max(0.0001, d)));
seg_curve(c,t,t0,d,a,b) = select2(c < 0.5, seg_exp(t,t0,d,a,b), seg(t,t0,d,a,b));

View file

@ -16,6 +16,8 @@ seg(t,t0,d,a,b) = a + (b - a) * clip01((t - t0) / max(0.0001, d));
bez(t,a,b,c,d) = (1.0 - t) * (1.0 - t) * (1.0 - t) * a + 3.0 * (1.0 - t) * (1.0 - t) * t * b + 3.0 * (1.0 - t) * t * t * c + t * t * t * d;
seg_bez(t,t0,d,a,b,c,e) = bez(clip01((t - t0) / max(0.0001, d)), a, b, c, e);
smooth01(x) = clip01(x) * clip01(x) * (3.0 - 2.0 * clip01(x));
smoo_from(pole, x) = loop ~ _ with { loop(prev) = select2(1 - 1', prev * pole + x * (1.0 - pole), x); };
smoo_init(x) = smoo_from(1.0 - 44.1 / ma.SR, x);
seg_smooth(t,t0,d,a,b) = a + (b - a) * smooth01((t - t0) / max(0.0001, d));
seg_exp(t,t0,d,a,b) = exp(log(max(0.00001, a)) + (log(max(0.00001, b)) - log(max(0.00001, a))) * clip01((t - t0) / max(0.0001, d)));
seg_curve(c,t,t0,d,a,b) = select2(c < 0.5, seg_exp(t,t0,d,a,b), seg(t,t0,d,a,b));

View file

@ -16,6 +16,8 @@ seg(t,t0,d,a,b) = a + (b - a) * clip01((t - t0) / max(0.0001, d));
bez(t,a,b,c,d) = (1.0 - t) * (1.0 - t) * (1.0 - t) * a + 3.0 * (1.0 - t) * (1.0 - t) * t * b + 3.0 * (1.0 - t) * t * t * c + t * t * t * d;
seg_bez(t,t0,d,a,b,c,e) = bez(clip01((t - t0) / max(0.0001, d)), a, b, c, e);
smooth01(x) = clip01(x) * clip01(x) * (3.0 - 2.0 * clip01(x));
smoo_from(pole, x) = loop ~ _ with { loop(prev) = select2(1 - 1', prev * pole + x * (1.0 - pole), x); };
smoo_init(x) = smoo_from(1.0 - 44.1 / ma.SR, x);
seg_smooth(t,t0,d,a,b) = a + (b - a) * smooth01((t - t0) / max(0.0001, d));
seg_exp(t,t0,d,a,b) = exp(log(max(0.00001, a)) + (log(max(0.00001, b)) - log(max(0.00001, a))) * clip01((t - t0) / max(0.0001, d)));
seg_curve(c,t,t0,d,a,b) = select2(c < 0.5, seg_exp(t,t0,d,a,b), seg(t,t0,d,a,b));

View file

@ -16,6 +16,8 @@ seg(t,t0,d,a,b) = a + (b - a) * clip01((t - t0) / max(0.0001, d));
bez(t,a,b,c,d) = (1.0 - t) * (1.0 - t) * (1.0 - t) * a + 3.0 * (1.0 - t) * (1.0 - t) * t * b + 3.0 * (1.0 - t) * t * t * c + t * t * t * d;
seg_bez(t,t0,d,a,b,c,e) = bez(clip01((t - t0) / max(0.0001, d)), a, b, c, e);
smooth01(x) = clip01(x) * clip01(x) * (3.0 - 2.0 * clip01(x));
smoo_from(pole, x) = loop ~ _ with { loop(prev) = select2(1 - 1', prev * pole + x * (1.0 - pole), x); };
smoo_init(x) = smoo_from(1.0 - 44.1 / ma.SR, x);
seg_smooth(t,t0,d,a,b) = a + (b - a) * smooth01((t - t0) / max(0.0001, d));
seg_exp(t,t0,d,a,b) = exp(log(max(0.00001, a)) + (log(max(0.00001, b)) - log(max(0.00001, a))) * clip01((t - t0) / max(0.0001, d)));
seg_curve(c,t,t0,d,a,b) = select2(c < 0.5, seg_exp(t,t0,d,a,b), seg(t,t0,d,a,b));

View file

@ -0,0 +1,104 @@
#!/usr/bin/env python3
"""What the formant numbers do not say. Measures a render for the faults Meta heard in tract-weak-spots-1 (2026-11-13) while every
formant matched its target; it does not listen, and a pass here is not a verdict (the listening page is).
ear_metrics.py FILE.wav [--f0 140] [--steady A B] [--json]
click_ratio largest pitch-period-window peak over the median window peak. A voiced word with no transient is near 1; the pre-fix
nibu (a phone boundary stepping an area) was 3.54 with its window at the /b/ closure (0.271 s); after, 1.76 (a vowel's own pulses).
step_db largest single-sample step, in dB over the median sample step of the voiced part. A cell area that jumps is one sample wide.
bandwidths LPC pole bandwidths (Hz) of the steady part, F1..F3 as (Hz, bandwidth). Natural vowels run about 40-100 Hz at F1, 50-120 at
F2, 80-200 at F3 (Hawks & Miller 1995, Fant 1972); a lossless tube rings narrower, which reads as reedy.
hnr_db harmonics-to-noise ratio of the steady part by autocorrelation at the pitch lag.
hf_db energy above 4 kHz re the total, dB.
"""
import sys, wave, json, argparse
import numpy as np
def wav(path):
with wave.open(path) as w:
return np.frombuffer(w.readframes(w.getnframes()), dtype="<i2").astype(float) / 32768.0, w.getframerate()
def click_ratio(x, sr, f0):
p = max(8, int(sr / f0))
peaks = np.array([np.abs(x[i:i + p]).max() for i in range(0, len(x) - p, p // 2)])
voiced = peaks[peaks > peaks.max() * 0.02]
if len(voiced) < 4:
return 0.0, 0.0
at = int(np.argmax(peaks)) * (p // 2) / sr
return float(peaks.max() / np.median(voiced)), at
def step_db(x):
d = np.abs(np.diff(x))
live = d[d > d.max() * 1e-3]
return float(20 * np.log10(d.max() / max(np.median(live), 1e-12))) if len(live) else 0.0
def autocorr(s, n=None):
"""Autocorrelation at lags 0..n-1 by FFT (np.correlate is quadratic)."""
n = n or len(s)
f = np.fft.rfft(s, 2 * len(s))
return np.fft.irfft(f * np.conj(f))[:n]
def lpc(s, o):
s = s * np.hamming(len(s))
r = autocorr(s, o + 1)
R = np.array([[r[abs(i - j)] for j in range(o)] for i in range(o)])
a = np.linalg.solve(R + 1e-9 * r[0] * np.eye(o), r[1:o + 1])
return np.concatenate([[1], -a])
def bandwidths(y, sr, order=14):
dec = max(1, int(round(sr / 10000)))
k = np.hanning(4 * dec + 1)
k /= k.sum()
z = np.convolve(y - y.mean(), k, "same")[::dec]
fs = sr / dec
z = np.append(z[0], z[1:] - 0.97 * z[:-1])
rt = np.roots(lpc(z, order))
rt = rt[np.imag(rt) > 0.001]
fq = np.angle(rt) * fs / (2 * np.pi)
bw = -np.log(np.abs(rt)) * fs / np.pi
# a formant is a narrow pole; broad poles (> 400 Hz) are the spectral tilt the all-pole fit spends order on
return [(int(f), int(b)) for f, b in sorted(zip(fq, bw)) if 150 < f < 4500 and b < 400][:3]
def hnr_db(y, sr, f0):
y = y - y.mean()
ac = autocorr(y)
ac /= ac[0]
lo, hi = int(sr / (f0 * 1.3)), int(sr / (f0 * 0.75))
m = float(ac[lo:hi].max())
return 10 * np.log10(max(m, 1e-6) / max(1 - m, 1e-6))
def measure(path, f0=140.0, steady=None):
x, sr = wav(path)
a, b = steady if steady else (len(x) / sr * 0.35, len(x) / sr * 0.85)
y = x[int(a * sr):int(b * sr)]
sp = np.abs(np.fft.rfft((y - y.mean()) * np.hanning(len(y))))
fr = np.fft.rfftfreq(len(y), 1 / sr)
cr, at = click_ratio(x, sr, f0)
return {
"file": path.split("/")[-1], "click_ratio": round(cr, 2), "click_at_s": round(at, 4), "step_db": round(step_db(x), 1),
"bandwidths": bandwidths(y, sr), "hnr_db": round(hnr_db(y, sr, f0), 1),
"hf_db": round(float(10 * np.log10(max((sp[fr > 4000] ** 2).sum(), 1e-20) / (sp ** 2).sum())), 1),
}
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("files", nargs="+")
ap.add_argument("--f0", type=float, default=140.0)
ap.add_argument("--steady", nargs=2, type=float)
ap.add_argument("--json", action="store_true")
a = ap.parse_args()
rows = [measure(f, a.f0, a.steady) for f in a.files]
if a.json:
print(json.dumps(rows, indent=1))
else:
for r in rows:
print(f"{r['file']:20s} click {r['click_ratio']:5.2f} at {r['click_at_s']:.3f}s step {r['step_db']:5.1f} dB hnr {r['hnr_db']:5.1f} hf {r['hf_db']:6.1f} F(Hz,bw) {r['bandwidths']}")

View file

@ -0,0 +1,39 @@
#!/usr/bin/env python3
"""A machine ear for the tract renders: what phone string a speech recogniser reads out of a clip. A PROXY for 'what was heard', never a
verdict: it is trained on natural speech and reads the vowels of this tract poorly (every isolated vowel came back as /i/), so use it
for the events it separates well (a burst read as /v/ against /b/, a nasal present or missing, a consonant that is only a click).
It reproduced three of Meta's four first listening reports on tract-weak-spots-1 before any fix: ba with burst -> 'v ɑ' (she heard
'fah then bah'), ana -> 'v ɑ m ɑ' (a basketball buzzer, no articulation), nibu -> 'i b u' (both consonants clicks, the /n/ missing).
phone_probe.py CLIP.wav...
Needs torch, transformers, scipy and the HuggingFace model facebook/wav2vec2-lv-60-espeak-cv-ft (about 1.2 GB, downloaded once into
HF_HOME). The CTC decode reads vocab.json directly, so the phonemizer library is not needed.
"""
import sys, wave, json
import numpy as np
import torch
from scipy.signal import resample_poly
from transformers import Wav2Vec2ForCTC
from huggingface_hub import hf_hub_download
MODEL = "facebook/wav2vec2-lv-60-espeak-cv-ft"
def phones(model, vocab, path):
with wave.open(path) as w:
sr = w.getframerate()
x = np.frombuffer(w.readframes(w.getnframes()), dtype="<i2").astype(np.float32) / 32768
x = resample_poly(x, 16000, sr).astype(np.float32)
x = np.concatenate([np.zeros(4000, np.float32), x, np.zeros(4000, np.float32)]) # silence either side, as speech has
x = (x - x.mean()) / (x.std() + 1e-7)
with torch.no_grad():
logits = model(torch.tensor(x)[None]).logits[0]
ids = logits.argmax(-1).tolist()
out = [vocab[i] for k, i in enumerate(ids) if (k == 0 or i != ids[k - 1]) and vocab[i] != "<pad>"]
return " ".join(out)
if __name__ == "__main__":
model = Wav2Vec2ForCTC.from_pretrained(MODEL).eval()
vocab = {v: k for k, v in json.load(open(hf_hub_download(MODEL, "vocab.json"))).items()}
for p in sys.argv[1:]:
print(f"{p.split('/')[-1]:24s} | {phones(model, vocab, p)}")

View file

@ -59,6 +59,10 @@ def stress(root):
out["nibu_i_150_250ms_db"] = db(rms(xb, sr, .15, .25), rms(xa, sr, .15, .25))
return out
def release_at(c0, c1):
"""Where a stop's articulators begin to open: StopReleaseStartSeconds in PatchScript.cs (the end less min(12 ms, 0.3 of the gesture))."""
return c1 - min(0.012, 0.3 * (c1 - c0))
def burst(root, name, c0, c1, sub="out"):
"""c0..c1: the b gesture's start and end in seconds; v1: the vowel's steady part after it."""
x, sr = load(root, sub, name); nb, _ = load(root, "outnb", name)
@ -70,11 +74,12 @@ def burst(root, name, c0, c1, sub="out"):
below = [k for k in range(int(tpk * 500), len(env)) if env[k] < pk * .1]
decay_ms = round((below[0] / 500 - tpk) * 1000, 0) if below else None
last = x[int((c1 + .06) * sr):]
return {"closure_peak": round(peak(x, sr, c0, c0 + .6 * (c1 - c0)), 4), "release_peak_full": round(peak(x, sr, c0 + .6 * (c1 - c0), c1 + .005), 4),
rel = release_at(c0, c1)
return {"closure_peak": round(peak(x, sr, c0, rel), 4), "release_peak_full": round(peak(x, sr, rel, c1 + .005), 4),
"burst_peak": round(pk, 4), "burst_peak_ms": round(tpk * 1000), "burst_to_tenth_ms": decay_ms, "vowel_peak": round(vp, 4),
"burst_re_vowel_db": db(pk, vp), "release_peak_re_vowel_db": db(peak(x, sr, c0 + .6 * (c1 - c0), c1 + .005), vp),
"burst_re_vowel_db": db(pk, vp), "release_peak_re_vowel_db": db(peak(x, sr, rel, c1 + .005), vp),
"burst_rms_in_30ms_after_peak_re_vowel_rms_db": db(rms(d, sr, tpk, tpk + .03), rms(x, sr, c1 + .06, c1 + .17)),
"burst_peak_from_120ms_after_release_to_end": round(peak(d, sr, c1 + .12, n / sr), 4), "burst_diff_samples_nonzero_before_closure_end": int(np.count_nonzero(np.abs(d[:int((c0 + .6 * (c1 - c0)) * sr)]) > 1e-9)),
"burst_peak_from_120ms_after_release_to_end": round(peak(d, sr, c1 + .12, n / sr), 4), "burst_diff_samples_nonzero_before_closure_end": int(np.count_nonzero(np.abs(d[:int(rel * sr)]) > 1e-9)),
"file_end_rms_last_100ms": round(rms(x, sr, len(x) / sr - .1, len(x) / sr), 5), "vowel_rms": round(rms(x, sr, c1 + .06, c1 + .17), 4)}
BURST_BANDS = ((0, 1000), (1000, 2000), (2000, 3500), (3500, 7000), (7000, 24000))