tts: some small HTTP-based services improvements

This commit is contained in:
Aleix Conchillo Flaqué
2025-02-25 12:30:54 -08:00
parent 96c6aeaada
commit 8acf9a488b
5 changed files with 20 additions and 15 deletions

View File

@@ -570,10 +570,12 @@ class ElevenLabsHttpTTSService(TTSService):
await self.start_tts_usage_metrics(text) await self.start_tts_usage_metrics(text)
yield TTSStartedFrame() # Process the streaming response
CHUNK_SIZE = 1024
async for chunk in response.content: yield TTSStartedFrame()
if chunk: async for chunk in response.content.iter_chunked(CHUNK_SIZE):
if len(chunk) > 0:
await self.stop_ttfb_metrics() await self.stop_ttfb_metrics()
yield TTSAudioRawFrame(chunk, self.sample_rate, 1) yield TTSAudioRawFrame(chunk, self.sample_rate, 1)
except Exception as e: except Exception as e:

View File

@@ -530,8 +530,10 @@ class OpenAITTSService(TTSService):
await self.start_tts_usage_metrics(text) await self.start_tts_usage_metrics(text)
CHUNK_SIZE = 1024
yield TTSStartedFrame() yield TTSStartedFrame()
async for chunk in r.iter_bytes(8192): async for chunk in r.iter_bytes(CHUNK_SIZE):
if len(chunk) > 0: if len(chunk) > 0:
await self.stop_ttfb_metrics() await self.stop_ttfb_metrics()
frame = TTSAudioRawFrame(chunk, self.sample_rate, 1) frame = TTSAudioRawFrame(chunk, self.sample_rate, 1)

View File

@@ -383,8 +383,6 @@ class PlayHTHttpTTSService(TTSService):
try: try:
options = self._create_options() options = self._create_options()
b = bytearray()
in_header = True
await self.start_ttfb_metrics() await self.start_ttfb_metrics()
@@ -396,6 +394,8 @@ class PlayHTHttpTTSService(TTSService):
yield TTSStartedFrame() yield TTSStartedFrame()
b = bytearray()
in_header = True
async for chunk in playht_gen: async for chunk in playht_gen:
# skip the RIFF header. # skip the RIFF header.
if in_header: if in_header:
@@ -410,11 +410,10 @@ class PlayHTHttpTTSService(TTSService):
fh.read(size) fh.read(size)
(data, size) = struct.unpack("<4sI", fh.read(8)) (data, size) = struct.unpack("<4sI", fh.read(8))
in_header = False in_header = False
else: elif len(chunk) > 0:
if len(chunk): await self.stop_ttfb_metrics()
await self.stop_ttfb_metrics() frame = TTSAudioRawFrame(chunk, self.sample_rate, 1)
frame = TTSAudioRawFrame(chunk, self.sample_rate, 1) yield frame
yield frame
except Exception as e: except Exception as e:
logger.error(f"{self} error generating TTS: {e}") logger.error(f"{self} error generating TTS: {e}")
finally: finally:

View File

@@ -407,10 +407,10 @@ class RimeHttpTTSService(TTSService):
yield TTSStartedFrame() yield TTSStartedFrame()
# Process the streaming response # Process the streaming response
chunk_size = 8192 CHUNK_SIZE = 1024
async for chunk in response.content.iter_chunked(chunk_size): async for chunk in response.content.iter_chunked(CHUNK_SIZE):
if chunk: if len(chunk) > 0:
await self.stop_ttfb_metrics() await self.stop_ttfb_metrics()
frame = TTSAudioRawFrame(chunk, self.sample_rate, 1) frame = TTSAudioRawFrame(chunk, self.sample_rate, 1)
yield frame yield frame

View File

@@ -150,8 +150,10 @@ class XTTSService(TTSService):
yield TTSStartedFrame() yield TTSStartedFrame()
CHUNK_SIZE = 1024
buffer = bytearray() 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: if len(chunk) > 0:
await self.stop_ttfb_metrics() await self.stop_ttfb_metrics()
# Append new chunk to the buffer. # Append new chunk to the buffer.