Skip to content

fix(tts): avoid synthesizing and streaming TTS audio for text-only listeners (#1411) - #1412

Draft
VineetRamshetty wants to merge 2 commits into
fossasia:devfrom
VineetRamshetty:fix/1411-tts-audio-demand-gating
Draft

VineetRamshetty wants to merge 2 commits into
fossasia:devfrom
VineetRamshetty:fix/1411-tts-audio-demand-gating

Conversation

@VineetRamshetty

@VineetRamshetty VineetRamshetty commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Description

Stops VoxBento from running TTS synthesis (Deepgram Aura, ElevenLabs v4, or Supertonic ONNX) and broadcasting Stage-2 24 kHz PCM binary frames when no listener has requested TTS audio — covering both caption/overlay-only audiences (/ws/captions/, OBS /overlay/) and /listener/{event_slug} attendees who enable translated captions with AI Voice toggled off.

Related Issue

Closes #1411

Changes Made

  • WebSocket audio-subscriber tracking (portal/websockets/manager.py, portal/websockets/handlers.py):
    • Kept TTSConnectionManager._rooms as dict[str, set[WebSocket]] for backward compatibility and added self._audio_rooms with has_audio_listeners(...) and wants_audio: bool = True in add() / remove().
    • Added audio_only: bool = False to broadcast_bundle(...) so Stage-2 audio frames (and TTS-stage synthesis errors) are sent only to self._audio_rooms subscribers.
    • Updated ws_tts to parse optional ?audio=0 (wants_audio = websocket.query_params.get("audio", "1") != "0"), keeping existing /ws/tts/ clients as audio subscribers by default.
  • Dynamic TTS synthesis gating (portal/translations/worker.py):
    • Updated _build_language_tasks and recheck_audience to set synthesize_audio from tts_manager.has_audio_listeners(...) rather than audience == LISTENERS.
    • Re-checked tts_manager.has_audio_listeners(...) immediately before calling portal.tts.worker.synthesize(...) so mid-flight disconnects skip synthesis and mid-flight joins still synthesize.
    • Marked Stage-2 broadcast_bundle(...) calls with audio_only=True.
  • Listener frontend & docs (portal/static/js/listener-event.js, website/docs/api/overview.mdx, website/docs/how-it-works.mdx):
    • Passed wantsTtsAudio into startTtsWs → openTtsWs, appending ?audio=0 when text-only (preserving ?audio=0 across reconnects).
    • Deferred AudioContext / AudioScheduler initialization and gated ttsPlaybackBlocked() on ttsAudioDesired so text-only listeners do not allocate an AudioContext or see spurious "Tap to listen" prompts.
    • Documented optional ?audio=0 on /ws/tts/{room_id}/{language_code}/{booth_id}.
  • Regression tests (tests/test_tts_framing.py, tests/test_translation_pipeline.py, tests/test_fastapi_app.py, tests/test_listener_event.js):
    • Added unit, WebSocket integration, and frontend JS tests covering mixed ?audio=0 / ?audio=1 subscribers, Stage-2 isolation, caption-only skipping synthesize(), dynamic mid-flight rechecks, and lazy AudioContext + ?audio=0 reconnects.

Testing

  • node tests/test_listener_event.js — 99 passed, 0 failed

  • uv run ruff check portal/websockets/manager.py portal/websockets/handlers.py portal/translations/worker.py tests/test_tts_framing.py tests/test_translation_pipeline.py tests/test_fastapi_app.py — All checks passed

  • uv run pytest tests/test_fastapi_app.py -k "ws_tts" -v — 15 passed (177 deselected)

  • uv run pytest tests/test_tts_framing.py tests/test_translation_pipeline.py -k "not test_language_independence" -v — 27 passed (1 deselected; test_language_independence hit a pre-existing sqlite3.OperationalError: no such table: transcript_translations when run after test_tts_framing.py)

  • Tested locally

  • Existing tests pass

  • Added/updated tests (if applicable)

Screencast

Screen.Recording.2026-11-28.085843.mp4

Checklist

  • My code follows the project's coding standards
  • I have tested my changes
  • I have updated documentation where necessary
  • My changes do not introduce new warnings or errors

Additional Notes

Created with Gemini in Antigravity.

Summary by Sourcery

Gate TTS synthesis and audio delivery on active listener audio demand while retaining translated text for text-only audiences.

New Features:

  • Support text-only TTS WebSocket subscriptions via an optional audio preference while preserving translated caption delivery.

Bug Fixes:

  • Avoid TTS synthesis, audio frame broadcasting, and unnecessary browser audio initialization when no subscriber requests voice audio.
  • Recheck audio demand immediately before synthesis so audio generation reflects listeners joining or leaving during translation.

Enhancements:

  • Track audio-capable and text-only TTS subscribers separately and restrict Stage-2 audio bundles to audio subscribers.

Documentation:

  • Document the optional text-only TTS WebSocket subscription parameter and its behavior.

Tests:

  • Add coverage for audio subscriber tracking, audio-only bundle isolation, dynamic synthesis gating, WebSocket registration, and text-only frontend reconnect behavior.

@coderabbitai

coderabbitai Bot commented Nov 28, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@github-actions github-actions Bot added the ai-assisted PR created or assisted with an AI coding agent label Nov 28, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Data structures

Track text-only TTS sockets separately from audio-capable sockets so listener counts can distinguish whether synthesis is needed.

  • portal/websockets/manager.py L632-635, L653-656 (+2 small edits)

API

Expose the audio preference on TTS subscriptions and bundle delivery so clients can opt into text-only bundles and audio-only delivery can be targeted.

  • portal/websockets/handlers.py L498-500
  • portal/websockets/manager.py L628, L669-674, L690

Uses of the API

Use audio-subscriber demand to decide whether to synthesize, recheck demand before synthesis, and restrict Stage-2 audio broadcasts to audio subscribers.

  • portal/translations/worker.py L660-669 (+4 small edits)

UI

Pass the listener’s audio preference through TTS connection and reconnect logic, and avoid creating audio playback resources or prompts for text-only listeners.

  • portal/static/js/listener-event.js L250-253, L367-392 (+6 small edits)

Documentation

Document the optional text-only TTS subscription and its effect on synthesis and PCM delivery.

  • website/docs/api/overview.mdx L51
  • website/docs/how-it-works.mdx L192

Tests

Add coverage for text-only and audio subscribers, synthesis gating and rechecks, the WebSocket query option, frontend reconnect behavior, and compatible test doubles.

  • tests/test_fastapi_app.py L3211-3239
  • tests/test_listener_event.js L744-747, L917-933
  • tests/test_translation_pipeline.py L97-101, L165-169, L970-1062 (+9 small edits)
  • tests/test_tts_framing.py L85-118
  • tests/test_youtube_caption_hooks.py L209-212
  • tests/translation_load_harness.py L233-236, L249

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-assisted PR created or assisted with an AI coding agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(tts): avoid synthesizing and streaming TTS audio for text-only caption and overlay listeners

1 participant