foley.weave.cli_aligners#
Forced aligners run as external programs: Montreal Forced Aligner and aeneas (#43).
Both sit behind the Aligner seam next to
WhisperXAligner, but neither is a pip extra: MFA installs
with conda, aeneas needs espeak and ffmpeg and (being AGPL) is best kept out of
foley’s own process. So each runs as a subprocess. The narration is written to a
temporary WAV, the transcript to a text file, the program aligns them, and its output
file is parsed into foley’s word_timeline. Nothing is imported, so import
foley.weave stays dol-only.
Unlike WhisperX, both align the given transcript (no speech recognition), so a
word is timed even when a recogniser would have misheard it; with no transcript they
return an empty timeline, as FakeAligner does.
Inject one where an aligner is taken:
foley.weave("narration.wav", timeline, transcript=text, aligner=MFAAligner())
foley.score(text, audio="narration.wav", aligner=AeneasAligner(python="~/venvs/aeneas/bin/python"))
Installation and the trade-offs between the aligners: misc/docs/aligners.md.
Module Attributes
MFA's (dictionary, acoustic model) per foley language code; names as in |
|
aeneas task languages (ISO 639-3) per foley language code. |
|
The tokens an aligner writes for a word it could not spell (MFA's out-of-vocabulary marker); each stands for whichever transcript word(s) sit at its place. |
|
Install hints, printed when the program is missing. |
Functions
|
|
|
aeneas's JSON sync map (one fragment per word) → |
|
MFA's |
|
Align |
Classes
|
aeneas (TTS + DTW) behind the |
|
Montreal Forced Aligner ( |
- foley.weave.cli_aligners.AENEAS_LANGUAGES: Mapping[str, str] = {'de': 'deu', 'en': 'eng', 'es': 'spa', 'fr': 'fra', 'it': 'ita', 'nl': 'nld', 'pt': 'por'}#
aeneas task languages (ISO 639-3) per foley language code.
- class foley.weave.cli_aligners.AeneasAligner(*, python='/opt/hostedtoolcache/Python/3.12.16/x64/bin/python', languages=None, timeout=None, runner=<function run>)[source]#
Bases:
objectaeneas (TTS + DTW) behind the
Alignerseam, one fragment per word.Light and language-broad (anything espeak speaks), coarser than MFA at word level, and AGPL-licensed: foley runs it as a separate program and never imports it. aeneas pins old dependencies, so it often lives in its own virtualenv; point
pythonat that interpreter.- Parameters:
python (
str) – A Python interpreter withaeneasinstalled (default: this one).languages (
Optional[Mapping[str,str]]) – foley language code → aeneas task language (defaultAENEAS_LANGUAGES).timeout (
Optional[float]) – Seconds before the run is abandoned (default: none).runner (
Callable[...,CompletedProcess]) – Runs the command (defaultsubprocess.run).
- class foley.weave.cli_aligners.MFAAligner(*, mfa='mfa', models=None, extra_args=(), timeout=None, runner=<function run>)[source]#
Bases:
objectMontreal Forced Aligner (
mfa align_one) behind theAlignerseam.Kaldi-based, MIT-licensed, and among the most accurate open aligners on read speech. It needs a pronunciation dictionary and an acoustic model per language, downloaded once (
mfa model download ...). MFA normalises the text (case, punctuation, clitics) and writes<unk>for a word missing from its dictionary (numbers too); the timeline still holds the transcript’s own words (onto_transcript()), an unknown one timed by its<unk>.- Parameters:
mfa (
str) – Themfaexecutable: a name onPATH, or a path into a conda env (no need to activate it; itsbinis put on the program’sPATH).models (
Optional[Mapping[str,tuple[str,str]]]) –{language: (dictionary, acoustic_model)}, names or paths asmfa align_onetakes them (defaultMFA_MODELS).extra_args (
Sequence[str]) – Moremfa align_oneoptions (("--beam", "100")).timeout (
Optional[float]) – Seconds before the run is abandoned (default: none).runner (
Callable[...,CompletedProcess]) – Runs the command (defaultsubprocess.run).
- foley.weave.cli_aligners.MFA_INSTALL = 'conda create -n mfa -c conda-forge montreal-forced-aligner, then mfa model download acoustic english_mfa && mfa model download dictionary english_mfa'#
Install hints, printed when the program is missing.
- foley.weave.cli_aligners.MFA_MODELS: Mapping[str, tuple[str, str]] = {'en': ('english_mfa', 'english_mfa')}#
MFA’s (dictionary, acoustic model) per foley language code; names as in
mfa model download. Extend withMFAAligner(models={...}).
- foley.weave.cli_aligners.UNKNOWN_TOKENS: frozenset[str] = frozenset({'<oov>', '<unk>', 'spn'})#
The tokens an aligner writes for a word it could not spell (MFA’s out-of-vocabulary marker); each stands for whichever transcript word(s) sit at its place.
- foley.weave.cli_aligners.onto_transcript(words, timed, *, unknown=frozenset({'<oov>', '<unk>', 'spn'}))[source]#
timed(an aligner’s tokens) mapped back onto the transcriptwords.Aligners rewrite what they are given: MFA lowercases, drops punctuation, splits clitics (
smith 's) and writes<unk>for a word it cannot spell. Anchors match the transcript’s words, so each given word takes the start of the first and the end of the last token its letters align with (a character-level diff of the normalised texts), and an unknown token stands for the word(s) at its place. A word no token covers (pure punctuation) is left out, as an unaligned word is.>>> toks = [_timed("mr", 0, 1), _timed("smith", 1, 2), _timed("'s", 2, 2.5), ... _timed("<unk>", 3, 4), _timed("cats", 4, 5)] >>> [(w["word"], w["start"], w["end"]) for w in onto_transcript( ... "Mr. Smith's 42 cats!".split(), toks)] [('Mr.', 0.0, 1.0), ("Smith's", 1.0, 2.5), ('42', 3.0, 4.0), ('cats!', 4.0, 5.0)]
- foley.weave.cli_aligners.parse_aeneas_json(text)[source]#
aeneas’s JSON sync map (one fragment per word) →
word_timeline.>>> parse_aeneas_json('{"fragments": [{"begin": "0.000", "end": "0.400", ' ... '"lines": ["the"]}, {"begin": "0.400", "end": "0.400", "lines": []}]}') [{'word': 'the', 'start': 0.0, 'end': 0.4}]
- foley.weave.cli_aligners.parse_mfa_json(text)[source]#
MFA’s
--output_format json→word_timeline(silences dropped).>>> parse_mfa_json('{"tiers": {"words": {"type": "interval", "entries": ' ... '[[0.1, 0.4, "the"], [0.4, 0.5, ""], [0.5, 0.9, "door"]]}}}') [{'word': 'the', 'start': 0.1, 'end': 0.4}, {'word': 'door', 'start': 0.5, 'end': 0.9}]
- foley.weave.cli_aligners.run_cli_aligner(audio, sample_rate, words, *, command, parse, text=<built-in method join of str object>, runner=<function run>, env=None, timeout=None, what='forced aligner')[source]#
Align
wordstoaudiowith an external program; return itsword_timeline.Writes
audio(mono, 16-bit WAV) andtext(words)to a temporary directory, runscommand(wav, txt, out), parses the file it wrote atoutand maps the program’s tokens back ontowords(onto_transcript()), so the timeline holds the words as given, whatever the program normalised. The strategy pieces (command,parse,text) are what an aligner supplies; this function owns the files and the error reporting.envis the program’s environment (default: this process’s);timeout(seconds) bounds the run.- Raises:
RuntimeError – If the program exits non-zero or writes no output (its stderr tail is in the message).
TimeoutExpired – If it runs past
timeout.
- Return type: