services(tts): make sure word timestamp is reset properly

This commit is contained in:
Aleix Conchillo Flaqué
2024-11-06 18:54:12 -08:00
parent ce89bbb16e
commit beb3271168
3 changed files with 9 additions and 6 deletions

View File

@@ -391,7 +391,6 @@ class WordTTSService(TTSService):
def reset_word_timestamps(self): def reset_word_timestamps(self):
self._initial_word_timestamp = -1 self._initial_word_timestamp = -1
self._word_timestamps = []
async def add_word_timestamps(self, word_times: List[Tuple[str, float]]): async def add_word_timestamps(self, word_times: List[Tuple[str, float]]):
for word, timestamp in word_times: for word, timestamp in word_times:
@@ -426,7 +425,10 @@ class WordTTSService(TTSService):
while True: while True:
try: try:
(word, timestamp) = await self._words_queue.get() (word, timestamp) = await self._words_queue.get()
if word == "LLMFullResponseEndFrame" and timestamp == 0: if word == "Reset" and timestamp == 0:
self.reset_word_timestamps()
frame = None
elif word == "LLMFullResponseEndFrame" and timestamp == 0:
frame = LLMFullResponseEndFrame() frame = LLMFullResponseEndFrame()
frame.pts = last_pts frame.pts = last_pts
elif word == "TTSStoppedFrame" and timestamp == 0: elif word == "TTSStoppedFrame" and timestamp == 0:
@@ -435,8 +437,9 @@ class WordTTSService(TTSService):
else: else:
frame = TextFrame(word) frame = TextFrame(word)
frame.pts = self._initial_word_timestamp + timestamp frame.pts = self._initial_word_timestamp + timestamp
last_pts = frame.pts if frame:
await self.push_frame(frame) last_pts = frame.pts
await self.push_frame(frame)
self._words_queue.task_done() self._words_queue.task_done()
except asyncio.CancelledError: except asyncio.CancelledError:
break break

View File

@@ -234,7 +234,7 @@ class CartesiaTTSService(WordTTSService):
# timestamp to set send context frames. # timestamp to set send context frames.
self._context_id = None self._context_id = None
await self.add_word_timestamps( await self.add_word_timestamps(
[("TTSStoppedFrame", 0), ("LLMFullResponseEndFrame", 0)] [("TTSStoppedFrame", 0), ("LLMFullResponseEndFrame", 0), ("Reset", 0)]
) )
elif msg["type"] == "timestamps": elif msg["type"] == "timestamps":
await self.add_word_timestamps( await self.add_word_timestamps(

View File

@@ -286,7 +286,7 @@ class ElevenLabsTTSService(WordTTSService):
if isinstance(frame, (TTSStoppedFrame, StartInterruptionFrame)): if isinstance(frame, (TTSStoppedFrame, StartInterruptionFrame)):
self._started = False self._started = False
if isinstance(frame, TTSStoppedFrame): if isinstance(frame, TTSStoppedFrame):
await self.add_word_timestamps([("LLMFullResponseEndFrame", 0)]) await self.add_word_timestamps([("LLMFullResponseEndFrame", 0), ("Reset", 0)])
async def process_frame(self, frame: Frame, direction: FrameDirection): async def process_frame(self, frame: Frame, direction: FrameDirection):
await super().process_frame(frame, direction) await super().process_frame(frame, direction)