speaker_helper.eardrum.ear module

The ear: a phoneme recogniser that writes what it hears, not what it expects.

Module summary

A speech-to-text model is the wrong ear for this job. Whisper has a language model inside: a badly pronounced “GitHub” comes back spelled “GitHub”, because that is the likeliest word, and the loop would believe the engine got it right. A phoneme recogniser has no dictionary to fall back on: it writes the sounds.

The default ear is facebook/wav2vec2-xlsr-53-espeak-cv-ft — wav2vec2 XLSR-53 (pretrained on 53 languages) fine-tuned on Common Voice to output espeak-style IPA, which is the very alphabet the targets are written in. Greedy CTC decoding gives each phone a 20 ms frame span, which is what lets the report cut the word’s own clip out of the carrier sentence.

Where the weights come from, first match wins:

  1. SPEAKER_HELPER_PHONEME_MODEL — a directory holding the model;

  2. $SPEAKER_ENGINES_DIR/metrics/phonemes/wav2vec2-xlsr-53-espeak-cv-ft (the self-hosted bundle);

  3. ~/.cache/speaker-helper/eardrum/wav2vec2-xlsr-53-espeak-cv-ft — what speaker-helper install-voice --engine eardrum fills;

  4. the Hugging Face cache, read with local_files_only (never a download at run time).

Usage example

>>> from speaker_helper.eardrum.ear import collapse_ctc
>>> collapse_ctc([0, 5, 5, 0, 7, 7, 7, 0], blank=0)
[(5, 1, 3), (7, 4, 7)]

Author

Warith HARCHAOUI — https://linkedin.com/in/warith-harchaoui

class speaker_helper.eardrum.ear.Ear(*args, **kwargs)[source]

Bases: Protocol

Anything that turns audio into heard phones.

hear(wav_bytes)[source]

Recognise the phones in an encoded audio payload.

Parameters:

wav_bytes (bytes) – WAV (or anything soundfile reads).

Returns:

The phones, in order.

Return type:

list of HeardPhone

class speaker_helper.eardrum.ear.HeardPhone(phone, start_s, end_s)[source]

Bases: object

One phone the ear heard, with when it was heard.

Parameters:
phone

The phone (already through tokenize()).

Type:

str

start_s, end_s

Its span in the audio, in seconds.

Type:

float

end_s: float
phone: str
start_s: float
speaker_helper.eardrum.ear.PHONEME_CACHE: Path = PosixPath('~/.cache/speaker-helper/eardrum/wav2vec2-xlsr-53-espeak-cv-ft')

The local directory install-voice --engine eardrum fills.

speaker_helper.eardrum.ear.PHONEME_MODEL = 'facebook/wav2vec2-xlsr-53-espeak-cv-ft'

The recogniser’s name, on the hub and as the local directory name.

speaker_helper.eardrum.ear.PHONEME_REVISION = '2c733782da5604684829819a5eb744c193fe9398'

The exact revision the loop was measured with (pinned, so two machines installing a month apart hear with the same ear).

class speaker_helper.eardrum.ear.PhonemeEar(source=None)[source]

Bases: object

The wav2vec2 espeak-phoneme recogniser, loaded once and reused.

Parameters:

source (str or None) – Model directory or hub id; model_dir() when None.

hear(wav_bytes)[source]

Recognise the phones in wav_bytes; see Ear.

Parameters:

wav_bytes (bytes) – Encoded audio.

Returns:

The phones, with 20 ms-resolution spans.

Return type:

list of HeardPhone

speaker_helper.eardrum.ear.SAMPLE_RATE = 16000

wav2vec2 reads 16 kHz audio and emits one frame every 320 samples.

speaker_helper.eardrum.ear.check_install()[source]

0 when the ear can load, 1 otherwise (for health-style checks).

Returns:

A process exit code.

Return type:

int

speaker_helper.eardrum.ear.collapse_ctc(ids, blank)[source]

Greedy CTC collapse: merge repeats, drop blanks, keep frame spans.

Parameters:
  • ids (list of int) – The argmax token id of each frame.

  • blank (int) – The CTC blank (pad) id.

Returns:

(token_id, first_frame, end_frame) per emitted token, end exclusive.

Return type:

list of (int, int, int)

speaker_helper.eardrum.ear.model_dir()[source]

Where the phoneme recogniser’s weights are read from.

Returns:

A local directory, or the hub id (resolved from the Hugging Face cache with local_files_only).

Return type:

str