Skip to content

feat(timing): reconcile phoneme alignments into word timings - #9

Merged
prateekchaplot merged 2 commits into
mainfrom
arch/tts/t1.3
Nov 20, 2026
Merged

prateekchaplot merged 2 commits into
mainfrom
arch/tts/t1.3

Conversation

@prateekchaplot

Copy link
Copy Markdown
Owner

Task T1.3 from docs/ARCHITECTURE.md §14.2 (§7.5). Implements derive_word_timings(voice, result) plus the tts.timing package and 14 unit tests.

What it does

  • Tokens in the input text map one-to-one to WordTiming entries; punctuation and emoji stay attached to the text they belong to.
  • Each word is re-phonemized in isolation with the voice's own G2P and matched against the sentence's phoneme alignment run using difflib.SequenceMatcher.find_longest_match from a per-sentence cursor.
  • Words that cannot be placed honestly are emitted with aligned=false and a zero-width span after the previous word. A sentence whose aligned-token ratio is below 0.6 collapses to one unaligned entry spanning that sentence's audio.
  • Timings are accumulated in exact milliseconds across sentences, rounded half-up per word, and clamped into [previous_end, sentence_end], so they are ordered and monotonic even when ms rounding would otherwise collide.
  • aligned_word_ratio counts every token (a collapsed sentence contributes its token count as unaligned) and is -1.0 when SynthesisResult.supports_alignments is false or there are no sentences; degraded results are never phonemized.

Decisions

  • timing/ also adds an __init__.py — a package needs one; §6 lists only reconcile.py.
  • Matching drops piper's ^/$/_ and espeak's " " from both sequences. Dropping the space is required because espeak expands numbers into several words (42 → fˈɔːɹti tˈuː), and punctuation-only tokens such as — phonemize to nothing.
  • split_words is exported so callers group text with the same tokenizer. Because a collapsed sentence yields exactly one entry, T1.6 should get per-sentence entry counts by deriving on a single-sentence SynthesisResult instead of assuming one entry per token.
  • Thresholds: word aligned when the longest contiguous match covers ≥ 60% of its phonemes; sentence collapse below 60% and inclusive (3/5 stays, 2/5 collapses).

Verification

  • pytest tests/unit → 66 passed (14 new)
  • pytest tests/integration → 6 passed (real en_US-lessac-medium, TTS_DATA_DIR=.venv/tts-data)
  • ruff check src/tts tests and ruff format --check src/tts tests → clean
  • mypy --strict src/tts → clean
  • git diff --check → clean
  • Manual real-voice check: "Emoji 🙂 stays honest. Yes — indeed, the 42 dogs rest!" → ratio 0.909, monotonic; — produced the expected zero-width unaligned entry. espeak expands 🙂 into "slightly smiling face", which reconciles honestly; the unit fixture uses a synthetic zero-phoneme voice instead.

Environment note: .venv/pytest-tmp is locked on this machine (WinError 5), so runs used fresh --basetemp .venv/ptmp-* directories. Tests pass.

Suggested merge: squash merge with branch deletion (recommendation only; awaiting review).

Map each sentence's per-phoneme sample counts to word-level timings with difflib matching, honest unaligned entries, and a 60% sentence collapse threshold.
@prateekchaplot
prateekchaplot merged commit 0de115f into main Nov 20, 2026
5 checks passed
@prateekchaplot
prateekchaplot deleted the arch/tts/t1.3 branch November 20, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant