From 87c4a1bee1aa90a9136f6a31456979bb37fae64f Mon Sep 17 00:00:00 2001 From: Sharvil Nanavati Date: Tue, 27 Aug 2024 04:45:21 +0000 Subject: [PATCH] Move stop frame task creation into `TTSService.start` --- src/pipecat/services/ai_services.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/pipecat/services/ai_services.py b/src/pipecat/services/ai_services.py index 16c77ff86..d4a3c021e 100644 --- a/src/pipecat/services/ai_services.py +++ b/src/pipecat/services/ai_services.py @@ -238,13 +238,20 @@ async def process_frame(self, frame: Frame, direction: FrameDirection): else: await self.push_frame(frame, direction) + async def start(self, frame: StartFrame): + await super().start(frame) + if self._push_stop_frames: + self._stop_frame_task = self.get_event_loop().create_task(self._stop_frame_handler()) + async def stop(self, frame: EndFrame): + await super().stop(frame) if self._stop_frame_task: self._stop_frame_task.cancel() await self._stop_frame_task self._stop_frame_task = None async def cancel(self, frame: CancelFrame): + await super().cancel(frame) if self._stop_frame_task: self._stop_frame_task.cancel() await self._stop_frame_task @@ -258,9 +265,6 @@ async def push_frame(self, frame: Frame, direction: FrameDirection = FrameDirect isinstance(frame, TTSStartedFrame) or isinstance(frame, AudioRawFrame) or isinstance(frame, TTSStoppedFrame)): - if self._stop_frame_task is None: - event_loop = self.get_event_loop() - self._stop_frame_task = event_loop.create_task(self._stop_frame_handler()) await self._stop_frame_queue.put(frame) async def _stop_frame_handler(self):