From 8acf9a488bc15152be5104a711919323c1d93484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Tue, 25 Feb 2025 12:30:54 -0800 Subject: [PATCH] tts: some small HTTP-based services improvements --- src/pipecat/services/elevenlabs.py | 8 +++++--- src/pipecat/services/openai.py | 4 +++- src/pipecat/services/playht.py | 13 ++++++------- src/pipecat/services/rime.py | 6 +++--- src/pipecat/services/xtts.py | 4 +++- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/pipecat/services/elevenlabs.py b/src/pipecat/services/elevenlabs.py index 3dc42975e..5e70735f8 100644 --- a/src/pipecat/services/elevenlabs.py +++ b/src/pipecat/services/elevenlabs.py @@ -570,10 +570,12 @@ class ElevenLabsHttpTTSService(TTSService): await self.start_tts_usage_metrics(text) - yield TTSStartedFrame() + # Process the streaming response + CHUNK_SIZE = 1024 - async for chunk in response.content: - if chunk: + yield TTSStartedFrame() + async for chunk in response.content.iter_chunked(CHUNK_SIZE): + if len(chunk) > 0: await self.stop_ttfb_metrics() yield TTSAudioRawFrame(chunk, self.sample_rate, 1) except Exception as e: diff --git a/src/pipecat/services/openai.py b/src/pipecat/services/openai.py index 6ed3b4612..e49bd3a90 100644 --- a/src/pipecat/services/openai.py +++ b/src/pipecat/services/openai.py @@ -530,8 +530,10 @@ class OpenAITTSService(TTSService): await self.start_tts_usage_metrics(text) + CHUNK_SIZE = 1024 + yield TTSStartedFrame() - async for chunk in r.iter_bytes(8192): + async for chunk in r.iter_bytes(CHUNK_SIZE): if len(chunk) > 0: await self.stop_ttfb_metrics() frame = TTSAudioRawFrame(chunk, self.sample_rate, 1) diff --git a/src/pipecat/services/playht.py b/src/pipecat/services/playht.py index 56179e34c..c42a44eaa 100644 --- a/src/pipecat/services/playht.py +++ b/src/pipecat/services/playht.py @@ -383,8 +383,6 @@ class PlayHTHttpTTSService(TTSService): try: options = self._create_options() - b = bytearray() - in_header = True await self.start_ttfb_metrics() @@ -396,6 +394,8 @@ class PlayHTHttpTTSService(TTSService): yield TTSStartedFrame() + b = bytearray() + in_header = True async for chunk in playht_gen: # skip the RIFF header. if in_header: @@ -410,11 +410,10 @@ class PlayHTHttpTTSService(TTSService): fh.read(size) (data, size) = struct.unpack("<4sI", fh.read(8)) in_header = False - else: - if len(chunk): - await self.stop_ttfb_metrics() - frame = TTSAudioRawFrame(chunk, self.sample_rate, 1) - yield frame + elif len(chunk) > 0: + await self.stop_ttfb_metrics() + frame = TTSAudioRawFrame(chunk, self.sample_rate, 1) + yield frame except Exception as e: logger.error(f"{self} error generating TTS: {e}") finally: diff --git a/src/pipecat/services/rime.py b/src/pipecat/services/rime.py index 60d2d67ef..007ba958e 100644 --- a/src/pipecat/services/rime.py +++ b/src/pipecat/services/rime.py @@ -407,10 +407,10 @@ class RimeHttpTTSService(TTSService): yield TTSStartedFrame() # Process the streaming response - chunk_size = 8192 + CHUNK_SIZE = 1024 - async for chunk in response.content.iter_chunked(chunk_size): - if chunk: + async for chunk in response.content.iter_chunked(CHUNK_SIZE): + if len(chunk) > 0: await self.stop_ttfb_metrics() frame = TTSAudioRawFrame(chunk, self.sample_rate, 1) yield frame diff --git a/src/pipecat/services/xtts.py b/src/pipecat/services/xtts.py index 9d2f3dd1e..e275b50b0 100644 --- a/src/pipecat/services/xtts.py +++ b/src/pipecat/services/xtts.py @@ -150,8 +150,10 @@ class XTTSService(TTSService): yield TTSStartedFrame() + CHUNK_SIZE = 1024 + buffer = bytearray() - async for chunk in r.content.iter_chunked(1024): + async for chunk in r.content.iter_chunked(CHUNK_SIZE): if len(chunk) > 0: await self.stop_ttfb_metrics() # Append new chunk to the buffer.