-
Notifications
You must be signed in to change notification settings - Fork 553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
services: fix some TTS websocket service interruption handling #1272
Open
aconchillo
wants to merge
1
commit into
main
Choose a base branch
from
aleix/tts-websocket-interruptions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−52
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,6 +15,7 @@ | |||||
from pipecat.audio.utils import calculate_audio_volume, exp_smoothing | ||||||
from pipecat.frames.frames import ( | ||||||
AudioRawFrame, | ||||||
BotStartedSpeakingFrame, | ||||||
BotStoppedSpeakingFrame, | ||||||
CancelFrame, | ||||||
EndFrame, | ||||||
|
@@ -40,6 +41,7 @@ | |||||
from pipecat.metrics.metrics import MetricsData | ||||||
from pipecat.processors.aggregators.openai_llm_context import OpenAILLMContext | ||||||
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor | ||||||
from pipecat.services.websocket_service import WebsocketService | ||||||
from pipecat.transcriptions.language import Language | ||||||
from pipecat.utils.string import match_endofsentence | ||||||
from pipecat.utils.text.base_text_filter import BaseTextFilter | ||||||
|
@@ -434,6 +436,12 @@ async def _stop_frame_handler(self): | |||||
|
||||||
|
||||||
class WordTTSService(TTSService): | ||||||
"""This a base class for TTS services that support word timestamps. Word | ||||||
timestamps are useful to synchronize audio with text of the spoken | ||||||
words. This way only the spoken words are added to the conversation context. | ||||||
|
||||||
""" | ||||||
|
||||||
def __init__(self, **kwargs): | ||||||
super().__init__(**kwargs) | ||||||
self._initial_word_timestamp = -1 | ||||||
|
@@ -503,11 +511,93 @@ async def _words_task_handler(self): | |||||
self._words_queue.task_done() | ||||||
|
||||||
|
||||||
class AudioContextWordTTSService(WordTTSService): | ||||||
"""This services allow us to send multiple TTS request to the services. Each | ||||||
request could be multiple sentences long which are grouped by context. For | ||||||
this to work, the TTS service needs to support handling multiple requests at | ||||||
once (i.e. multiple simultaneous contexts). | ||||||
class WebsocketTTSService(TTSService, WebsocketService): | ||||||
"""This a base class for websocket-based TTS services.""" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same typo in other docstrings. |
||||||
|
||||||
def __init__(self, **kwargs): | ||||||
TTSService.__init__(self, **kwargs) | ||||||
WebsocketService.__init__(self) | ||||||
|
||||||
|
||||||
class InterruptibleTTSService(WebsocketTTSService): | ||||||
"""This a base class for websocket-based TTS services that don't support | ||||||
word timestamps and that don't offer a way to correlate the generated audio | ||||||
to the requested text. | ||||||
|
||||||
""" | ||||||
|
||||||
def __init__(self, **kwargs): | ||||||
super().__init__(**kwargs) | ||||||
|
||||||
# Indicates if the bot is speaking. If the bot is not speaking we don't | ||||||
# need to reconnect when the user speaks. If the bot is speaking and the | ||||||
# user interrupts we need to reconnect. | ||||||
self._bot_speaking = False | ||||||
|
||||||
async def _handle_interruption(self, frame: StartInterruptionFrame, direction: FrameDirection): | ||||||
await super()._handle_interruption(frame, direction) | ||||||
if self._bot_speaking: | ||||||
await self._disconnect() | ||||||
await self._connect() | ||||||
|
||||||
async def process_frame(self, frame: Frame, direction: FrameDirection): | ||||||
await super().process_frame(frame, direction) | ||||||
|
||||||
if isinstance(frame, BotStartedSpeakingFrame): | ||||||
self._bot_speaking = True | ||||||
elif isinstance(frame, BotStoppedSpeakingFrame): | ||||||
self._bot_speaking = False | ||||||
|
||||||
|
||||||
class WebsocketWordTTSService(WordTTSService, WebsocketService): | ||||||
"""This a base class for websocket-based TTS services that support word | ||||||
timestamps. | ||||||
|
||||||
""" | ||||||
|
||||||
def __init__(self, **kwargs): | ||||||
WordTTSService.__init__(self, **kwargs) | ||||||
WebsocketService.__init__(self) | ||||||
|
||||||
|
||||||
class InterruptibleWordTTSService(WebsocketWordTTSService): | ||||||
"""This a base class for websocket-based TTS services that support word | ||||||
timestamps but don't offer a way to correlate the generated audio to the | ||||||
requested text. | ||||||
|
||||||
""" | ||||||
|
||||||
def __init__(self, **kwargs): | ||||||
super().__init__(**kwargs) | ||||||
|
||||||
# Indicates if the bot is speaking. If the bot is not speaking we don't | ||||||
# need to reconnect when the user speaks. If the bot is speaking and the | ||||||
# user interrupts we need to reconnect. | ||||||
self._bot_speaking = False | ||||||
|
||||||
async def _handle_interruption(self, frame: StartInterruptionFrame, direction: FrameDirection): | ||||||
await super()._handle_interruption(frame, direction) | ||||||
if self._bot_speaking: | ||||||
await self._disconnect() | ||||||
await self._connect() | ||||||
|
||||||
async def process_frame(self, frame: Frame, direction: FrameDirection): | ||||||
await super().process_frame(frame, direction) | ||||||
|
||||||
if isinstance(frame, BotStartedSpeakingFrame): | ||||||
self._bot_speaking = True | ||||||
elif isinstance(frame, BotStoppedSpeakingFrame): | ||||||
self._bot_speaking = False | ||||||
|
||||||
|
||||||
class AudioContextWordTTSService(WebsocketWordTTSService): | ||||||
"""This a base class for websocket-based TTS services that support word | ||||||
timestamps and also allow correlating the generated audio with the requested | ||||||
text. | ||||||
|
||||||
Each request could be multiple sentences long which are grouped by | ||||||
context. For this to work, the TTS service needs to support handling | ||||||
multiple requests at once (i.e. multiple simultaneous contexts). | ||||||
|
||||||
The audio received from the TTS will be played in context order. That is, if | ||||||
we requested audio for a context "A" and then audio for context "B", the | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.