Skip to content

local_voice

diapason.speech.realtime.local_voice

Fully local realtime voice: Whisper → Ollama → Kokoro, nothing leaves.

The other two providers stream the raw microphone to Google or OpenAI; this one keeps the whole loop on the machine. It is honest about what that buys and what it costs: turn-based with barge-in rather than full duplex, first spoken word ~1.5–2.5 s after the user's last syllable (measured: STT ~1 s for a 4 s French utterance, qwen3.5:9b first token 0.47 s warm, Kokoro RTF 0.28) — against ~0.5 s for Gemini Live. In exchange: no key, no account, and the factory's local-only guard can finally let voice through instead of refusing it wholesale.

Structure: send_audio only buffers and detects turn boundaries; the response pipeline (transcribe → stream tokens → speak sentence by sentence) runs as a cancellable task, because barge-in is nothing more than cancelling it. The three stages are injectable callables so the turn logic is testable without a microphone, a model server or a vocoder.

LocalVoiceSession

Bases: RealtimeVoiceSession

Turn-based local voice with barge-in, behind the realtime contract.

classify_endpoint(text)

« complete », « hesitation » ou « neutral » pour une fin de tour.

Conservateur par construction : « complete » exige une ponctuation terminale — c'est le seul verdict qui RACCOURCIT l'attente, donc le seul qui puisse couper quelqu'un. « hesitation » ne fait qu'attendre plus, l'erreur y est bon marché. Tout le reste garde le délai normal.

french_now(now=None)

The machine's local date and time, spelled out in French.

Hand-rolled rather than strftime with a locale: setlocale is process-wide state and this runs inside a server thread pool. Injected into the system prompt at every turn — a model has no clock, and "quelle heure est-il" answered with "je ne peux pas lire l'heure" was a reported failure, on a machine that obviously knows.

french_today(now=None)

La date seule, sans l'heure — stable toute la journée.

L'horloge à la minute près, recollée au prompt à chaque appel, invalidait le cache de préfixe d'Ollama dès que la minute changeait : les six mille jetons de prompt et de schémas d'outils étaient relus en entier, plusieurs secondes par tour. La voix dispose maintenant de current_time : quand l'heure compte, le modèle la LIT — c'est plus juste qu'une heure figée au début du tour, et le préfixe, lui, ne bouge plus qu'à minuit.

local_voice_readiness(timeout_s=1.5)

Return whether every local voice runtime component is available.

Keep this check cheap: model construction belongs to connect(), but a missing optional extra or system phonemizer must disable Start instead of letting the WebSocket claim readiness and fail a few seconds later.

mentions_assistant_name(text)

Vrai si un mot — ou DEUX mots adjacents recollés — ressemble au nom.

La transcription coupe le nom en deux : « Dia pasons, quelle heure… », constaté en session réelle. Un seul mot ne suffit donc pas ; les paires adjacentes se recollent avant la comparaison.

ollama_reachable(timeout_s=1.5)

True when the local model server answers. Never raises.

polish_transcript(text)

Apply the user's dictation dictionary to a voice transcript.

The dictation path earns its accuracy partly AFTER Whisper: the personal dictionary fixes the words the recognizer keeps getting wrong ("App Store", proper nouns). Voice transcripts deserve the same corrections — same user, same vocabulary, same mistakes. bump_usage=False: voice hits must not skew the dictation dictionary's learning statistics.

speakable(text)

Strip what a voice cannot say; collapse the leftover whitespace.

Returns "" when nothing pronounceable is left — an emoji-only chunk leaves its punctuation behind ("👍👍." → "."), and a vocoder handed a bare period says "point" out loud.

strip_assistant_name(text)

Retire l'appel initial — « Diapason, ouvre… » → « ouvre… ».