Skip to content

ptt

ptt

Push-to-talk state machine — hold to dictate, double-tap for hands-free.

Pure logic: it takes key-down / key-up events with a timestamp and emits a list of actions for a host to carry out (start capture, stop and transcribe, cancel...). No microphone, no clock of its own, no threads — which is what makes the whole dictation flow unit-testable without hardware.

Behaviour mirrors Diapason's, which is the reference product:

  • Hold the key, speak, release → transcribe what was said (normal PTT).
  • Two quick taps → hands-free: capture keeps running after release; the next tap stops it and transcribes. The second tap of the double also cancels the tiny first session so it never produces a stray transcription.
  • A tap while a normal press is somehow still held is ignored (key repeat).
  • cancel() throws away the in-flight audio without transcribing.

Classes

PushToTalk dataclass

PushToTalk(
    state: State = IDLE, _last_down_t: float | None = None
)

Feed it down(t) / up(t); act on the returned actions.

Methods:
cancel
cancel() -> List[Action]

Abort whatever is in flight (e.g. Esc, or an app-level stop).

Source code in src/diapason/desktop/ptt.py
def cancel(self) -> List[Action]:
    """Abort whatever is in flight (e.g. Esc, or an app-level stop)."""
    if self.state in (State.PTT_HELD, State.HANDS_FREE):
        self.state = State.IDLE
        return [Action.CANCEL]
    return []