Skip to content

Commit

Permalink
fix: streaming TTS in non pulseaudio systems (#298)
Browse files Browse the repository at this point in the history
* fix: streaming TTS in non pulseaudio systems

* fix: streaming TTS in non pulseaudio systems

* stdin

* stdin
  • Loading branch information
JarbasAl authored Jan 30, 2025
1 parent 087da0e commit 8ec6604
Showing 1 changed file with 17 additions and 8 deletions.
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

0 comments on commit 8ec6604

Please sign in to comment.