services(cartesia): fix TTFB

This commit is contained in:
Aleix Conchillo Flaqué
2024-07-22 15:30:16 -07:00
parent c1170260b5
commit 087fe9a537

View File

@@ -82,7 +82,6 @@ class CartesiaTTSService(TTSService):
self._timestamped_words_buffer = []
self._receive_task = None
self._context_appending_task = None
self._waiting_for_ttfb = False
def can_generate_metrics(self) -> bool:
return True
@@ -127,7 +126,6 @@ class CartesiaTTSService(TTSService):
self._context_id = None
self._context_id_start_timestamp = None
self._timestamped_words_buffer = []
self._waiting_for_ttfb = False
await self.stop_all_metrics()
except Exception as e:
logger.exception(f"{self} error closing websocket: {e}")
@@ -148,6 +146,7 @@ class CartesiaTTSService(TTSService):
if not msg or msg["context_id"] != self._context_id:
continue
if msg["type"] == "done":
await self.stop_ttfb_metrics()
# unset _context_id but not the _context_id_start_timestamp because we are likely still
# playing out audio and need the timestamp to set send context frames
self._context_id = None
@@ -158,11 +157,9 @@ class CartesiaTTSService(TTSService):
list(zip(msg["word_timestamps"]["words"], msg["word_timestamps"]["end"]))
)
elif msg["type"] == "chunk":
await self.stop_ttfb_metrics()
if not self._context_id_start_timestamp:
self._context_id_start_timestamp = time.time()
if self._waiting_for_ttfb:
await self.stop_ttfb_metrics()
self._waiting_for_ttfb = False
frame = AudioRawFrame(
audio=base64.b64decode(msg["data"]),
sample_rate=self._output_format["sample_rate"],
@@ -198,11 +195,8 @@ class CartesiaTTSService(TTSService):
if not self._websocket:
await self._connect()
if not self._waiting_for_ttfb:
await self.start_ttfb_metrics()
self._waiting_for_ttfb = True
if not self._context_id:
await self.start_ttfb_metrics()
self._context_id = str(uuid.uuid4())
msg = {