-
Notifications
You must be signed in to change notification settings - Fork 166
feat: TTS with RealtimeModel #781
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
Open
simllll
wants to merge
7
commits into
livekit:main
Choose a base branch
from
simllll:fix-modalities
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.
+157
−48
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4361aa6
fix modalities parameter for openai
simllll a5f005b
use correct object key: output_modalities instead of modalities
simllll 48f126a
fix modalities:
simllll 1f170bf
implement missing text events
simllll a106a95
use same modalities
simllll 978613b
implement custom tts support in realtime agent-activity
simllll f962dd4
add changeset
simllll 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@livekit/agents-plugin-google': patch | ||
| '@livekit/agents-plugin-openai': patch | ||
| '@livekit/agents': patch | ||
| --- | ||
|
|
||
| Usage with separate TTS |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -1520,10 +1520,29 @@ export class AgentActivity implements RecognitionHooks { | |
| break; | ||
| } | ||
| const trNodeResult = await this.agent.transcriptionNode(msg.textStream, modelSettings); | ||
|
|
||
| // Determine if we need to tee the text stream for both text output and TTS | ||
| const needsTextOutput = !!textOutput && !!trNodeResult; | ||
| const needsTTSSynthesis = | ||
| audioOutput && | ||
| this.llm instanceof RealtimeModel && | ||
| !this.llm.capabilities.audioOutput && | ||
| this.tts; | ||
| const needsBothTextAndTTS = needsTextOutput && needsTTSSynthesis; | ||
|
|
||
| // Tee the stream if we need it for both purposes | ||
| let textStreamForOutput = trNodeResult; | ||
| let textStreamForTTS = trNodeResult; | ||
| if (needsBothTextAndTTS && trNodeResult) { | ||
| const [stream1, stream2] = trNodeResult.tee(); | ||
| textStreamForOutput = stream1; | ||
| textStreamForTTS = stream2; | ||
| } | ||
|
|
||
|
Comment on lines
+1523
to
+1541
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. Let's make sure the implementation mirrors python implementation when doing refactoring: https://github.com/livekit/agents/blob/a9bc03562f498f3666978ad008fc93b2cbbd22a9/livekit-agents/livekit/agents/voice/agent_activity.py#L2011-L2102 |
||
| let textOut: _TextOut | null = null; | ||
| if (trNodeResult) { | ||
| if (textStreamForOutput) { | ||
| const [textForwardTask, _textOut] = performTextForwarding( | ||
| trNodeResult, | ||
| textStreamForOutput, | ||
| abortController, | ||
| textOutput, | ||
| ); | ||
|
|
@@ -1532,23 +1551,49 @@ export class AgentActivity implements RecognitionHooks { | |
| } | ||
| let audioOut: _AudioOut | null = null; | ||
| if (audioOutput) { | ||
| const realtimeAudio = await this.agent.realtimeAudioOutputNode( | ||
| msg.audioStream, | ||
| modelSettings, | ||
| ); | ||
| if (realtimeAudio) { | ||
| const [forwardTask, _audioOut] = performAudioForwarding( | ||
| realtimeAudio, | ||
| audioOutput, | ||
| abortController, | ||
| // Check if realtime model has audio output capability | ||
| if (this.llm instanceof RealtimeModel && this.llm.capabilities.audioOutput) { | ||
| // Use realtime audio output | ||
| const realtimeAudio = await this.agent.realtimeAudioOutputNode( | ||
| msg.audioStream, | ||
| modelSettings, | ||
| ); | ||
| forwardTasks.push(forwardTask); | ||
| audioOut = _audioOut; | ||
| audioOut.firstFrameFut.await.finally(onFirstFrame); | ||
| if (realtimeAudio) { | ||
| const [forwardTask, _audioOut] = performAudioForwarding( | ||
| realtimeAudio, | ||
| audioOutput, | ||
| abortController, | ||
| ); | ||
| forwardTasks.push(forwardTask); | ||
| audioOut = _audioOut; | ||
| audioOut.firstFrameFut.await.finally(onFirstFrame); | ||
| } else { | ||
| this.logger.warn( | ||
| 'audio output is enabled but neither tts nor realtime audio is available', | ||
| ); | ||
| } | ||
| } else { | ||
| this.logger.warn( | ||
| 'audio output is enabled but neither tts nor realtime audio is available', | ||
| ); | ||
| // Text-only mode - synthesize audio using TTS | ||
| if (this.tts && textStreamForTTS) { | ||
| const [ttsTask, ttsStream] = performTTSInference( | ||
| (...args) => this.agent.ttsNode(...args), | ||
| textStreamForTTS, | ||
| modelSettings, | ||
| abortController, | ||
| ); | ||
| forwardTasks.push(ttsTask); | ||
|
|
||
| const [forwardTask, _audioOut] = performAudioForwarding( | ||
| ttsStream, | ||
| audioOutput, | ||
| abortController, | ||
| ); | ||
| forwardTasks.push(forwardTask); | ||
| audioOut = _audioOut; | ||
| audioOut.firstFrameFut.await.finally(onFirstFrame); | ||
| } else if (!this.tts) { | ||
| this.logger.warn('realtime model in text-only mode but no TTS is configured'); | ||
| } | ||
| } | ||
| } else if (textOut) { | ||
| textOut.firstTextFut.await.finally(onFirstFrame); | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
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.
We need to be super careful whenever doing an
teeoperation. It will lock the source stream untill both tee-ed streams have finished or been cancelled. In case of an interruption, we would have to release reader lock for both sub-streams to release resources and cannot simply cancel the trNodeResult