Skip to content

Keep transcribing when another app takes the audio device (#164) - #165

Merged
alpha5611331 merged 6 commits into
mainfrom
fix/164-capture-stall-recovery
Dec 4, 2026
Merged

alpha5611331 merged 6 commits into
mainfrom
fix/164-capture-stall-recovery

Conversation

@alpha5611331

@alpha5611331 alpha5611331 commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Fixes #164.

Problem

A live session could stop transcribing on both channels at once and never recover, while both ASR sockets stayed open. Each channel's capture graph ended at the default AudioContext destination, which tied capture to the system output device. When another app took the speakers in WASAPI exclusive mode, Chromium suspended both capture contexts together ("The AudioContext encountered an error from the audio device"). They stayed suspended after the device was released. The backend's Deepgram keepalive kept the audio-less streams open, so nothing errored, the panels froze, and the export covered only the minutes before the stall.

Changes

  1. fix(transcription) (live-transcription.service.ts)
    • Two clocks for a capture graph, each used where it is safe (see below).
    • A per-channel watchdog (1 s) that detects dead capture and recovers it, sketched below.
    • Graph construction moved into buildGraph() / teardownGraph(), shared by start() and recovery.
  2. feat(control-bar): the live signal gains an "Audio lost - recovering" state, in all nine interface languages.
  3. test: test/capture-stall.test.mjs, 33 source-level checks pinning the above. The two regressions found during review (software clock as the resting state, resume() on suspend) were each reintroduced to confirm the checks fail.
  4. Review fixes (97e7eb8, 7efbf97, 02f2b64): no indicator flicker, backoff only after attempts that re-opened something, fast retry after a refused attempt, debounced stall detection, no graph install after stop() during a rebuild, frames still queued from a replaced graph ignored, and the clock redesign below.

Why two clocks

The first version of this PR moved every capture context onto a none sink (sinkId: { type: 'none' }), clocked by a timer instead of the speakers. Review measured it against a speaker-clocked context in the same process:

none sink speakers
Normal, before a player starts 100% of real time 100%
Seconds after an ordinary player starts ~65%, for the rest of the run 100%
Speakers held exclusively ~65% suspended

~65% matches 10 ms render ticks landing on Windows' default 15.6 ms timer granularity. It happened in 4 of 6 runs. At 65% a third of the audio is dropped and the rest distorted, all session, unreported. So:

checkCapture() every 1s, per channel
├─ track ended ───────────────────────────┐
├─ no frames > 3s, or ctx left running ───┼─► lost ─► recover:
│   (2 consecutive checks)                │     5s, doubling to 60s after attempts that re-opened something;
└─ mic exact zeros > 5s ──────────────────┘     1s after an attempt the OS refused (device still held)
                                                new source via onCaptureLost (mic: setAudioInputDevice,
                                                loopback: reacquireLoopback, acquire-swap-release)
                                                + rebuild if not rendering: speakers -> software clock
on the software clock: every 3s, probe the speakers; if they render at real time, rebuild on them
ctx.onstatechange: recorded for the watchdog, never resume()d (on held speakers resume() flaps
  the context running/suspended hundreds of times a second and hides the failure)
lost clears only on evidence from the new source (frames / real signal), so no flicker
socket kept throughout

Live test, before and after

This harness runs the app's real live-transcription.service.ts, bundled with esbuild from main (c6b4fd1) and from this branch (7efbf97, re-run on 02f2b64). It uses Electron 40.9.1, electron-audio-loopback and real Windows devices, against a local WebSocket sink that stands in for the backend. Per socket per second the sink reports bytes received (whether the capture clock runs at real time: about 31,500 B/s at 16 kHz PCM16), RMS, and the dominant frequency (what is actually in the audio).

A shared-mode 440 Hz tone stands in for normal system audio. A second app takes the speakers in exclusive mode for 15 s and plays its own 1 kHz tone, standing in for Zoom playing the interviewer on a device it has taken. After release, a fresh shared-mode player plays 660 Hz.

main (before) this branch (after), 3 runs
Before the grab 440 Hz, full rate 440 Hz, full rate
Speakers taken both sockets 0 bytes until the app quits ~1.5 s gap on both channels while the watchdog moves them to the software clock; then the mic carries real audio, the loopback carries silence
During the grab 0 bytes the 1 kHz played exclusively is not captured (see limits)
Speakers released never recovers, isConnected() stays true loopback re-acquired, 660 Hz arriving ~1 s after release; both channels back on the speaker clock 1-2 s after release
After dead full rate every second

Other runs on 7efbf97:

Scenario Result
Normal session, a player starting and stopping (the condition that slowed the none sink) full rate every second, no watchdog action
Speakers 12 s, then mic 12 s (the original timeline) 0 zero-byte seconds; mic silence detected in 5 s and recovered after release
Mic held 45 s, then released refusals (NotReadableError: Device in use) logged once, retried every 1 s; mic back 1-2 s after release

Local checks: pnpm lint, tsc -b, tsc -p tsconfig.electron.json, pnpm build, pnpm test:main. All pass.

Known limits

  • While another app holds the speakers exclusively, interviewer audio is not captured at all. This is a Windows limit: loopback sees only the shared mix, which exclusive streams bypass. If Zoom takes the speakers exclusively, the interviewer is lost for as long as it holds them, plus about 1 s after release.
  • At the moment the speakers are taken, both channels lose about 1.5 s while the watchdog detects it and rebuilds.
  • During a speaker grab the mic runs on the software clock, which may be at ~65% speed and so degraded, until the speakers come back.
  • While another app holds the mic exclusively, the candidate's audio is lost for that time, plus 5 s for detection. If the first re-open "succeeds" on a still-held mic (silence again), the next attempt waits the 10 s backoff, so the mic can return up to ~10 s after release (4 s in one run, 1-2 s in another).
  • A mic muted at the OS or hardware level also reads as exact zeros, so it shows "Audio lost" and is re-opened with backoff (5, 10, 20, 40, 60 s) while it stays muted.
  • If the selected mic disappears, re-acquire falls back to the default device, while the picker still names the old one.
  • Not new here: audio during a socket reconnect, and chunks dropped while the send buffer is full, are still lost.
  • Not verified against Zoom itself or the real backend. The exclusive grab is simulated with a WASAPI exclusive stream (Python sounddevice).

Not in this PR

🤖 Generated with Claude Code

alpha5611331 and others added 3 commits December 2, 2026 12:28
…evice

Each channel's capture graph ended at the default context's destination,
which tied capture to the system output device. When another app took the
speakers in WASAPI exclusive mode, Chromium suspended both capture contexts
at once and they never came back, while both sockets stayed open and the
backend's keepalive hid the silence for the rest of the interview.

- Capture contexts now render to a `none` sink, so they are clocked
  internally and survive the output device being taken.
- A per-channel watchdog resumes a suspended context, rebuilds the graph
  when frames stop, and re-acquires the source when its track ends or the
  microphone goes digitally silent (an exclusive grab of the mic). The
  socket is kept throughout.
- Loopback is re-acquired acquire-swap-release, like the microphone path.

Refs #164

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The live signal now has a third state beside Listening and Reconnecting:
"Audio lost - recovering", shown while either channel's capture watchdog
reports its source lost. Before this, a dead capture with an open socket
looked exactly like a quiet interview.

Strings added for all nine interface languages.

Refs #164

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Source-level checks, like language-switch.test.mjs: the silent sink and its
fallback, frames counted before the socket check, resume on suspend, the
three detections (ended track, stopped frames, digital silence), mic-only
silence detection, acquire-swap-release for loopback, the watchdog cleared
on stop, and the control bar reading the stall.

Refs #164

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gitar-bot

gitar-bot Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Gitar is working

Verification

Rules No rules evaluated

Functional validation Not enabled

Set up

Auto-approval Not enabled

Set up

Gitar

alpha5611331 and others added 3 commits December 2, 2026 12:47
…effects

Review of the first pass found five problems, each fixed and pinned:

- Indicator flicker: every recovery attempt reset the timers, so a source
  that was still dead read as restored for ~5 s before failing again.
  A lost capture now clears only on evidence from the current source
  (frames, and real signal on the mic).
- Retry churn: a mic muted at the OS or hardware level is also exact
  zeros, and was re-opened every 5 s for as long as it stayed muted.
  Retries now back off exponentially to 60 s - but only after an attempt
  that actually re-opened something. A refused attempt (device held
  exclusively, NotReadableError) opened nothing and stays on the short
  interval; otherwise a minute-long grab cost up to a minute of audio
  after release (measured live: now 4 s).
- False rebuilds after a renderer freeze: frames must be missing for two
  consecutive checks.
- stop() during a rebuild: a graph torn down while its worklet loaded no
  longer installs itself over the cleared fields.
- setAudioInputDevice's doc comment had been left above the new loopback
  helpers; restored to its method. Recovery errors log name and message.

The test's loopback check was vacuous (its regex stopped at the first
`)` in `() =>`); it now inspects the constructor call and was verified
against a mutation. Also pins that the mock channel, which mutes the mic
track by design, never enables silence detection.

Refs #164

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… while it is taken

The first fix put every capture context on a `none` sink. Measured live in
the same process as a speaker-clocked context, that software clock
intermittently renders at ~65% of real time on Windows (4 of 6 runs; it
set in seconds after an ordinary player started) while the speaker clock
stays at 100%. At 65% a third of the audio is dropped and the rest
distorted, for the whole session, with nothing reporting it - worse than
the bug it replaced in the common case.

Neither clock is safe alone, so:

- Capture runs on the speakers, exactly as before #164.
- A speaker-clocked graph that stops rendering (another app took the
  speakers exclusively) is rebuilt on the software clock, so both
  channels keep capturing through the grab.
- While on the software clock, a throwaway probe context checks every 3 s
  whether the speakers render at real time again, by measuring its clock
  rather than trusting `state`; capture moves back once they do.

Also:

- No more resume() on a suspended context. On held speakers every resume
  flipped the context running/suspended hundreds of times a second, and a
  check landing on a running instant read the dead graph as healthy, so
  the fallback never happened. The handler now records the interruption
  and the watchdog counts it, which also detects a taken output device in
  ~1 s instead of waiting out the frame gap.
- A refused recovery attempt (NotReadableError: Device in use) is retried
  after 1 s instead of the 5 s base wait, and each distinct refusal is
  logged once. Measured: loopback back 1-2 s after the speakers are
  released, down from 5 s.

Live, three runs of a 15 s exclusive speaker grab: full rate on both
channels outside a ~1.5 s gap at the grab, back on the speaker clock
1-2 s after release, interviewer audio arriving ~1 s after release.
A normal session with a player starting and stopping: full rate every
second, no watchdog action.

Refs #164

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A message still queued from a graph the watchdog just replaced counted
as evidence for the new graph and was resampled at the new context's
rate. Drop it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alpha5611331
alpha5611331 merged commit 93dbb3a into main Dec 4, 2026
1 check passed
@alpha5611331
alpha5611331 deleted the fix/164-capture-stall-recovery branch December 4, 2026 00:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Live transcription silently stops on both channels when another app takes the audio device; report then covers only the first minutes

1 participant