feat(extras): add pause()/resume() to MicrophoneStream#219
Open
ccampbell-aai wants to merge 1 commit into
Open
feat(extras): add pause()/resume() to MicrophoneStream#219ccampbell-aai wants to merge 1 commit into
ccampbell-aai wants to merge 1 commit into
Conversation
Lets voice-agent builders mute the mic while the agent speaks (breaking the TTS->mic->transcribe feedback loop) without tearing down the session. While paused the device is still drained but silence is yielded, keeping the streaming session alive. Since pause/resume invites controlling the mic from another thread, also make close() thread-safe: teardown now waits (via a lock) for any in-flight device read to finish — closing the PortAudio stream mid-read deadlocks the reading thread — and is idempotent. Document that AsyncStreamingClient users should wrap the blocking reads via asyncio.to_thread rather than passing the iterator directly. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
aurpsis-aai
approved these changes
Jul 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #101.
Adds
pause(),resume(), and apausedproperty toaai.extras.MicrophoneStream, so voice-agent builders can mute mic input mid-session — e.g. while the agent's TTS is playing — without tearing down the streaming session.Design: silence-forwarding. While paused, the device is still read (so PyAudio's input buffer keeps draining) but silence of equal length is yielded in place of the captured audio. This keeps the session alive (no 4031 idle timeout) and avoids buffer overflows — the two failure modes of simply skipping reads or stopping iteration. Pause takes effect within one chunk (~100ms).
Thread-safe
close(). Since pause/resume invites controlling the mic from another thread,close()is hardened: a lock now serializes device reads against teardown, soclose()waits for any in-flight read to finish instead of closing the PortAudio stream mid-read()— which deadlocks the reading thread (verified live on macOS/CoreAudio).close()is also idempotent now (was a double-free before).Asyncio guidance. New class docstring documents that
AsyncStreamingClientusers should feed the mic throughasyncio.to_threadrather than passing the iterator directly (blocking reads stall the event loop — same class of problem as the existingstream_filegotcha).pause()/resume()are safe to call from the event loop in that pattern.Testing
pytest tests/unit/test_extras.py— 6 passed. New tests:close()during an in-flight read: no deadlock, teardown runs exactly once after the read completes, secondclose()is a no-op (fails pre-fix)ruff==0.3.5format + check clean,mypy==1.5.1clean🤖 Generated with Claude Code