an.audio.transcripts#
Transcripts as INPUT: SRT, WebVTT, Praat TextGrid, word-timed JSON and plain text read into timed segments, and a known text matched to a transcriber’s words.
Every package in the fleet writes subtitles (an.captions, mixing.srt,
scribed’s Transcript.srt); this module is the reading direction, for a
performer’s recording that arrives with its words already written down. Three
shapes come in:
timed segments — SRT, WebVTT, a word-timed JSON, a TextGrid: each line of dialogue has a start and an end in the recording, so the take can be cut at them with no model at all;
plain text — the lines, one per line, optionally
speaker: text(thescene.mdgrammar). No times: either each line is its own audio file, or a transcriber’s timed words are matched to the text (align_words());a transcriber’s result — any object shaped like scribed’s
Transcript(segments withtext/start/end/speaker/words), read byfrom_transcript_object().
All times are seconds from the start of the recording they describe.
>>> segs = parse_transcript("1\n00:00:01,000 --> 00:00:02,500\ncarl: Hello there.\n\n"
... "2\n00:00:03,000 --> 00:00:03,800\nned: Hi.\n")
>>> [(s.speaker, s.text, s.start, s.end) for s in segs]
[('carl', 'Hello there.', 1.0, 2.5), ('ned', 'Hi.', 3.0, 3.8)]
>>> from an.audio.transcripts import align_words
>>> aligned = align_words(["Hello", "there,", "friend"],
... [("hello", 0.5, 0.9), ("there", 0.9, 1.2), ("fred", 1.3, 1.6)])
>>> aligned.words
[('Hello', 0.5, 0.9), ('there,', 0.9, 1.2), ('friend', 1.3, 1.6)]
>>> aligned.matched, aligned.total
(2, 3)
Module Attributes
The transcript formats read here, in detection order. |
|
The phone alphabets a reader can report (an#721), as the forced aligner's |
|
every label must be one of them (its stress digit dropped) for the labels to be read as ARPAbet (cutan's |
Functions
|
Which of |
|
Read |
|
The transcript |
|
SubRip cues → timed segments; a |
|
WebVTT cues → timed segments; |
|
One non-empty line per segment, untimed; |
|
A word-timed JSON → segments. |
|
A Praat TextGrid (long or short form, as a forced aligner writes it) → segments. |
|
A long-form Praat TextGrid of interval |
A transcriber's result — scribed's |
|
|
|
|
The alphabet aligner phone |
|
The words of |
|
The words of |
|
The form two words are matched by: |
|
Time the |
|
Time untimed |
|
Where to cut the recording for each timed segment: its span widened by |
|
|
Classes
|
One spoken line of a transcript: its text, and when and who, if known. |
|
|
- an.audio.transcripts.ARPABET: str = 'arpabet'#
The phone alphabets a reader can report (an#721), as the forced aligner’s
phone_alphabetnames them (an#637).
- an.audio.transcripts.ARPABET_PHONES: frozenset[str] = frozenset({'AA', 'AE', 'AH', 'AO', 'AW', 'AY', 'B', 'CH', 'D', 'DH', 'EH', 'ER', 'EY', 'F', 'G', 'HH', 'IH', 'IY', 'JH', 'K', 'L', 'M', 'N', 'NG', 'OW', 'OY', 'P', 'R', 'S', 'SH', 'T', 'TH', 'UH', 'UW', 'V', 'W', 'Y', 'Z', 'ZH'})#
every label must be one of them (its stress digit dropped) for the labels to be read as ARPAbet (cutan’s
ARPABET_TO_VISEMEkeys the same set).- Type:
The 39 ARPAbet phones of the CMU dictionary, without stress digits
- an.audio.transcripts.FORMATS: tuple[str, ...] = ('vtt', 'textgrid', 'json', 'srt', 'plain')#
The transcript formats read here, in detection order.
- class an.audio.transcripts.TranscriptSegment(text, start=None, end=None, speaker=None, words=None, phones=None, phone_alphabet=None)[source]#
Bases:
objectOne spoken line of a transcript: its text, and when and who, if known.
start/endare seconds from the recording’s start,Nonefor an untimed (plain) transcript;wordsare the segment’s own word timings when the format carries them (word JSON, TextGrid), elseNone.- phone_alphabet: str | None#
The alphabet
phonesare in ("arpabet","ipa") when the format says it (an#721): Gentle’s always ARPAbet, an MFA grid’s by its labels (phone_alphabet_of());Nonewhen unknown.
- an.audio.transcripts.cut_spans(segments, *, total, pad_s=0.05)[source]#
Where to cut the recording for each timed segment: its span widened by
pad_son each side, never past the midpoint to its neighbour or the recording’s ends (totalseconds).>>> segs = [TranscriptSegment("a", 1.0, 2.0), TranscriptSegment("b", 2.04, 3.0)] >>> cut_spans(segs, total=3.02) [(0.95, 2.02), (2.02, 3.02)]
- an.audio.transcripts.detect_format(text, *, suffix='')[source]#
Which of
FORMATStextis, by its file suffix first, then its content.>>> detect_format("WEBVTT\n\n00:01.000 --> 00:02.000\nhi") 'vtt' >>> detect_format("1\n00:00:01,000 --> 00:00:02,000\nhi"), detect_format("hi there") ('srt', 'plain') >>> detect_format('{"segments": []}'), detect_format("", suffix=".TextGrid") ('json', 'textgrid')
- Return type:
- an.audio.transcripts.from_transcript_object(obj)[source]#
A transcriber’s result — scribed’s
Transcriptor anything shaped like it (iterable segments withtext,start,end,speakerandwordshavingtext/start/end) — as segments.>>> from types import SimpleNamespace as NS >>> t = [NS(text="Hi there", start=0.2, end=0.9, speaker="A", ... words=[NS(text="Hi", start=0.2, end=0.4), NS(text="there", start=0.5, end=0.9)])] >>> from_transcript_object(t)[0].words (('Hi', 0.2, 0.4), ('there', 0.5, 0.9))
- Return type:
- an.audio.transcripts.normalize_tokens(text)[source]#
The words of
textfor matching:tokens_of()case-folded.>>> normalize_tokens("Hello, there — it's ME!") ['hello', 'there', "it's", 'me']
- an.audio.transcripts.parse_json(obj)[source]#
A word-timed JSON → segments. Accepted:
{"segments": [...]}or a list of segments, each{"text", "start", "end", "speaker", "words": [{"text"|"word", "start", "end"}, ...]}(start_ms/end_msare read too, and a word may be a[text, start, end]list); or a bare list of words, read as one segment. A word’sphonesas Gentle writes them (durations from its start) become the segment’s phones, and MFA’s JSON (tiers) is read byparse_mfa_json()(an#715). In Gentle’s JSON (transcriptandwords), a word whosecaseis notGENTLE_SAID_CASEis dropped (an#721); any other JSON keeps every word.>>> segs = parse_json({"segments": [{"speaker": "carl", "words": [ ... {"word": "Hello", "start": 0.5, "end": 0.9}, ["there", 0.9, 1.2]]}]}) >>> segs[0].text, segs[0].start, segs[0].end, segs[0].words ('Hello there', 0.5, 1.2, (('Hello', 0.5, 0.9), ('there', 0.9, 1.2)))
- Return type:
- an.audio.transcripts.parse_plain(text)[source]#
One non-empty line per segment, untimed;
speaker: textnames the speaker.>>> [s.speaker for s in parse_plain("carl: Hi.\n\nJust a line.\n")] ['carl', None]
- Return type:
- an.audio.transcripts.parse_srt(text)[source]#
SubRip cues → timed segments; a
speaker:prefix names the speaker.>>> parse_srt("1\n00:00:00,500 --> 00:00:01,000\nHi\nthere\n") [TranscriptSegment(text='Hi there', start=0.5, end=1.0, speaker=None, words=None, phones=None, phone_alphabet=None)]
- Return type:
- an.audio.transcripts.parse_textgrid(text)[source]#
A Praat TextGrid (long or short form, as a forced aligner writes it) → segments.
The words come from a tier named
words(else the first interval tier); a tier namedutterances/lines/sentencesgives one segment per non-empty interval, with the words inside it; without one, the whole grid is one segment.>>> tg = ('File type = "ooTextFile"\nObject class = "TextGrid"\nxmin = 0\nxmax = 2\n' ... 'tiers? <exists>\nsize = 1\nitem []:\n item [1]:\n class = "IntervalTier"\n' ... ' name = "words"\n xmin = 0\n xmax = 2\n intervals: size = 3\n' ... ' intervals [1]:\n xmin = 0\n xmax = 0.5\n text = ""\n' ... ' intervals [2]:\n xmin = 0.5\n xmax = 0.9\n text = "hello"\n' ... ' intervals [3]:\n xmin = 0.9\n xmax = 1.3\n text = "there"\n') >>> parse_textgrid(tg) [TranscriptSegment(text='hello there', start=0.5, end=1.3, speaker=None, words=(('hello', 0.5, 0.9), ('there', 0.9, 1.3)), phones=None, phone_alphabet=None)]
- Return type:
- an.audio.transcripts.parse_timestamp(value)[source]#
HH:MM:SS,mmm/MM:SS.mmm/ plain seconds → seconds.>>> parse_timestamp("00:01:02,500"), parse_timestamp("01:02.5"), parse_timestamp("7.25") (62.5, 62.5, 7.25)
- Return type:
- an.audio.transcripts.parse_transcript(source, *, fmt=None)[source]#
Read
source— a path to a transcript file, or the transcript text itself — into segments.fmt(one ofFORMATS) overrides detection.>>> parse_transcript("carl: Hello.\nned: Hi.")[1] TranscriptSegment(text='Hi.', start=None, end=None, speaker='ned', words=None, phones=None, phone_alphabet=None)
- Return type:
- an.audio.transcripts.parse_transcript_text(text, *, fmt=None)[source]#
The transcript
text(never a path: what a store hands back, an#493) into segments;fmt(one ofFORMATS) overrides detection.>>> parse_transcript_text("note", fmt="plain")[0].text 'note'
- Return type:
- an.audio.transcripts.parse_vtt(text)[source]#
WebVTT cues → timed segments;
<v Name>or aspeaker:prefix names the speaker.>>> parse_vtt("WEBVTT\n\n00:01.000 --> 00:02.000\n<v Carl>Hello</v>\n") [TranscriptSegment(text='Hello', start=1.0, end=2.0, speaker='Carl', words=None, phones=None, phone_alphabet=None)]
- Return type:
- an.audio.transcripts.phone_alphabet_of(labels)[source]#
The alphabet aligner phone
labelsshow, orNonewhen they show none (an#721).ARPABETwhen every label is ASCII, in one letter case throughout, and inARPABET_PHONESonce its stress digit is dropped (cutan’s rule for a clip, so its own can bephone_alphabet_of(p) or "ipa");IPAwhen any label is non-ASCII; otherwise unknown, and the lip-sync decides it per clip. Silences are not labels.>>> phone_alphabet_of(["HH", "AH0", "L"]), phone_alphabet_of(["h", "ə", "l"]) ('arpabet', 'ipa') >>> phone_alphabet_of(["AH", "T"]), phone_alphabet_of(["ai1", "ou3"]) is None ('arpabet', True)
- an.audio.transcripts.rebase_words(words, start)[source]#
words(or phones: any(label, start, end)timings) re-timed fromstart(a cut’s start): the clip-relative timings a line keeps.>>> rebase_words([("hi", 1.0, 1.4)], 0.95) (('hi', 0.05, 0.45),)
- an.audio.transcripts.segment_lines(lines, timed, *, bounds=None)[source]#
Time untimed
lines(a plain transcript) by a transcriber’stimedwords of the whole recording: the lines’ words are aligned as one text (align_words()) and handed back per line, each line spanning its first word’s start to its last word’s end, with its ownwords.>>> lines = parse_plain("carl: Hello there.\nned: Hi.") >>> timed = [("hello", 0.5, 0.9), ("there", 0.9, 1.3), ("hi", 2.0, 2.3)] >>> [(s.speaker, s.start, s.end, len(s.words)) for s in segment_lines(lines, timed)] [('carl', 0.5, 1.3, 2), ('ned', 2.0, 2.3, 1)]
- Return type:
- an.audio.transcripts.textgrid_text(tiers, *, xmax)[source]#
A long-form Praat TextGrid of interval
tiers({name: [(start, end, label), ...]}) spanning0..xmax: whatparse_textgrid()reads back, and what a forced aligner takes as a transcript of timed lines (an#716):scribed.textgrid.write_tiers(). The gaps between intervals are filled with empty ones, as Praat requires a tier to cover its span; a"in a label or a tier name is doubled; times are rounded to a microsecond, so an interval shorter than half of one is empty. Nothing is clamped: an interval outside0..xmax, empty, or overlapping another is aValueError(clamp before, where the reason is known).>>> grid = textgrid_text({"take": [(0.5, 1.2, 'say "hi"')]}, xmax=2.0) >>> _textgrid_tiers(grid) {'take': [(0.0, 0.5, ''), (0.5, 1.2, 'say "hi"'), (1.2, 2.0, '')]} >>> textgrid_text({"take": [(0.5, 1.2, "a"), (1.0, 1.5, "b")]}, xmax=2.0) Traceback (most recent call last): ... ValueError: tier 'take': interval (1.0, 1.5, 'b') overlaps the one before, which ends at 1.2
- Return type: