Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: streaming TTS in non pulseaudio systems #298

Merged
merged 4 commits into from
Jan 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions ovos_plugin_manager/templates/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import sys
import subprocess
import shutil
from os.path import isfile, join
from pathlib import Path
from queue import Queue
Expand Down Expand Up @@ -1119,7 +1120,19 @@ class StreamingTTSCallbacks:
def __init__(self, bus, play_args=None, tts_config=None):
self.bus = bus
self.config = tts_config or {}
self.play_args = play_args or ["paplay"]
if not play_args:
# Check for the best available player depending on the system's audio server.
# anything that accepts audio via stdin should work
# TODO - pw-play only outputs high pitched noise, investigate and add it here for pipewire systems
player = shutil.which("ffplay") or shutil.which("paplay") or shutil.which("aplay")
if not player:
raise RuntimeError("No audio player found (please install 'ffmpeg', 'pulseaudio-utils' or 'alsa-utils').")
self.play_args = [player]
if player.endswith("ffplay"):
self.play_args += ["-autoexit", "-nodisp"]
self.play_args += ["-"]
else:
self.play_args = play_args
self._process = None

def stream_start(self, message=None):
Expand All @@ -1129,9 +1142,7 @@ def stream_start(self, message=None):
- "recognizer_loop:audio_output_start"
"""
LOG.info(f"TTS stream start: {self.__class__.__name__}")
message = message or \
dig_for_message() or \
Message("speak")
message = message or dig_for_message() or Message("speak")

# we don't use the regular PlaybackThread here, we need to handle recognizer_loop:audio_output_start
if not self.config.get("pulse_duck", False):
Expand All @@ -1144,7 +1155,7 @@ def stream_start(self, message=None):
self._process = subprocess.Popen(self.play_args, stdin=subprocess.PIPE)

def stream_chunk(self, chunk):
"""play streamed chunk of audio"""
"""Play streamed chunk of audio"""
LOG.debug(f"TTS stream chunk: {self.__class__.__name__} - {len(chunk)} bytes")
if self._process:
self._process.stdin.write(chunk)
Expand All @@ -1158,9 +1169,7 @@ def stream_stop(self, listen=False, message=None):
- 'mycroft.mic.listen'
"""
LOG.info(f"TTS stream stop: {self.__class__.__name__}")
message = message or \
dig_for_message() or \
Message("speak")
message = message or dig_for_message() or Message("speak")

if self._process:
self._process.stdin.close()
Expand Down
Loading