alignment¶
Concept: SGPA. Decisions: Alignment decisions.
Responsibility¶
Turn a transcript and its speech into one time segment per token, so audio segments can become players.
The
sgpa_segmentspartition turns segments into players. No backend and no audio rendering (that is masking); theaudioextra (librosa,soundfile) plus thetransformersextra for the default model.
Public surface¶
SpectrogramGuidedAligner(model=None, energy_weight=0.8, flux_weight=0.2, separator="|");align(transcript, audio: AudioPart, attach_audio=False) -> list[AudioSegment], which keeps the lastALIGNMENT_CACHE_SIZE(256) results keyed by tokens and the audio’s SHA-256.CtcModelprotocol (sample_rate,blank_id,vocab,log_probs(waveform));Wav2Vec2Ctc(model_id="facebook/wav2vec2-base-960h", revision=None, device="cpu")implements it. Its load report is silenced; only a missing weight other than the training-onlymasked_spec_embedlogs a warning.SgpaSegmentPartitioner(sgpa_segments, optionsSgpaOptions:model_id,revision,device,energy_weight,flux_weight,text_rolesfor word players of typed messages,transcript: send|align_only): one player per spellable word of every message with anAudioPartand a transcript; ids"{snapshot_id}:{turn}:{message}:{token}"; spans carry the word’s text range andsamples; a message with several clips aligns each to its own text part or sentence (clip_transcripts), and its spans carry the clip aspart; the default aligner comes fromdefault_aligner(model_id, revision, device, energy_weight, flux_weight), one per process and options, so runs share its model and alignments;align_onlylists audio messages inPlayerSet.hidden_text, so the model hears them without their transcript.AudioSegment(token, token_index, start_time, end_time, ctc_start_time, ctc_end_time, confidence, boundary_refined, sample_rate, start_sample, end_sample, audio=b"")withduration.decode_audio(data, sample_rate=None)(mono float32, optional resampling) andencode_wav(waveform, sample_rate)(16-bit PCM).ctc.forced_align(log_probs, targets, blank)andctc.target_spans(...): Viterbi alignment over the blank-extended target sequence.
Data flow¶
In:
AudioPartfrom domain and a transcript string or token list.Steps: decode and resample for the model;
log_probs; CTC forced alignment of the spellable characters (separator between tokens); token spans from their characters; each edge moves to the minimum ofenergy_weight× RMS (root mean square energy) +flux_weight× spectral flux within its search window when that point is quiet.UniformSegmentPartitioner(uniform_segments, optionsUniformOptions:segments, default the transcript’s word count, andframe_ms, default 20): equal-duration segments of every audio clip on whole frames (masking.partitions.snapped_edges), counted per clip. It is the matched-count control ofsgpa_segments, needs no alignment model, and lists audio messages inPlayerSet.hidden_text.Out:
AudioSegments in token order, at the source sample rate;sgpa_segmentsturns them into players for the pipeline.
Invariants¶
Adjacent segments share one boundary, which lies inside the gap between their character spans.
A token without a character in the model vocabulary gets no segment; a transcript with none raises
ValueError.Segments last at least
MIN_DURATION_S(0.05 s) and haveend_sample > start_sample.The aligner changes no global state (warnings filters, library log levels).
Known issues¶
None open.
D06 - SGPA paper not in the papers list: the method’s paper is under review, so it has no Papers section yet.
Tests¶
test_sgpa_players (also
mock_audioend to end)