-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add respeecher tts plugin #3233
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
mitrushchienkova
wants to merge
21
commits into
livekit:main
Choose a base branch
from
respeecher:add-respeecher-TTS-plugin
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.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
143df3e
Add Respeecher TTS plugin
mitrushchienkova 3d82cc7
Format code with ruff
mitrushchienkova 7efdca6
Apply code review suggetions
mitrushchienkova c4b10a9
Add sentence tokenizetion
mitrushchienkova 05f1c53
Define sample_rate as integer instead of list of allowed values
mitrushchienkova 711de2a
Move version from model to base URL
mitrushchienkova 2cf2507
Apply review suggestions
mitrushchienkova fca99ff
Fix ruff formatting
mitrushchienkova 608c43e
Make Voice a dictionary and update timeout logic for tests
mitrushchienkova 1619a2d
Reuse websocket connection across different requests
mitrushchienkova d51bb14
Run mypy to type checks
mitrushchienkova 006bd37
Remove pcm_f32le
mitrushchienkova 34d2968
Simplify string replace
mitrushchienkova a170df6
Run ruff formatter
mitrushchienkova c5ad4c5
Reset uv.lock
mitrushchienkova 1f8db02
Fix Respeecher rebase fallout
mitrushchienkova 7c28a1a
Add Ukrainian public model and require voice_id
mitrushchienkova 263afa6
Don't close shared HTTP session in Respeecher TTS aclose
mitrushchienkova ba5e743
Address Devin review on Respeecher TTS streaming
mitrushchienkova 9da8d28
Address remaining Devin review findings
mitrushchienkova 5e13e67
Address second Devin review pass on Respeecher streaming
mitrushchienkova 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 |
|---|---|---|
| @@ -1,3 +1,30 @@ | ||
| # Text-to-speech | ||
| # Text-to-Speech Examples | ||
|
|
||
| This small example shows how you can generate real-time audio data from text. | ||
| These examples demonstrate real-time text-to-speech generation using various TTS plugins with LiveKit. | ||
|
|
||
| ## Environment Variables | ||
|
|
||
| ### Plugin API Keys | ||
| Set the API key for your chosen plugin. | ||
|
|
||
| ### LiveKit Connection | ||
| For connecting to LiveKit Cloud: | ||
| - `LIVEKIT_URL` - Your LiveKit server URL | ||
| - `LIVEKIT_API_KEY` - LiveKit API key | ||
| - `LIVEKIT_API_SECRET` - LiveKit API secret | ||
|
|
||
| ## Running Examples | ||
|
|
||
| Execute the example to connect to a LiveKit room and stream TTS audio: | ||
|
|
||
| ```bash | ||
| uv run examples/other/text-to-speech/{your_plugin}_tts.py start | ||
| ``` | ||
|
|
||
| The agent will join the room and stream synthesized speech to participants. | ||
|
|
||
| ### Running Locally | ||
|
|
||
| Running the examples with `console` mode won't play audio since the examples use `rtc.LocalAudioTrack`, which requires the LiveKit room infrastructure for audio playback. The `LocalAudioTrack` is designed to publish audio streams to LiveKit rooms where they are processed and distributed to participants. Without a room connection, the audio frames are generated but not routed to any playback device. | ||
|
|
||
| To test TTS output locally without a LiveKit room, you would need to modify the example file to save the generated audio frames to a WAV file instead of publishing them to a track. The saved WAV file can then be played using any audio player on your system. |
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import asyncio | ||
| import logging | ||
|
|
||
| from dotenv import load_dotenv | ||
|
|
||
| from livekit import rtc | ||
| from livekit.agents import AutoSubscribe, JobContext, WorkerOptions, cli | ||
| from livekit.plugins import respeecher | ||
|
|
||
| load_dotenv() | ||
|
|
||
| logger = logging.getLogger("respeecher-tts-demo") | ||
| logger.setLevel(logging.INFO) | ||
|
|
||
|
|
||
| async def entrypoint(job: JobContext): | ||
| logger.info("starting tts example agent") | ||
|
|
||
| tts = respeecher.TTS(voice_id="samantha") | ||
|
|
||
| source = rtc.AudioSource(tts.sample_rate, tts.num_channels) | ||
| track = rtc.LocalAudioTrack.create_audio_track("agent-mic", source) | ||
| options = rtc.TrackPublishOptions() | ||
| options.source = rtc.TrackSource.SOURCE_MICROPHONE | ||
|
|
||
| await job.connect(auto_subscribe=AutoSubscribe.SUBSCRIBE_NONE) | ||
| publication = await job.room.local_participant.publish_track(track, options) | ||
| await publication.wait_for_subscription() | ||
|
|
||
| async with tts.stream() as stream: | ||
|
|
||
| async def _playback_task(): | ||
| count = 0 | ||
| async for audio in stream: | ||
| count += 1 | ||
| await source.capture_frame(audio.frame) | ||
|
|
||
| task = asyncio.create_task(_playback_task()) | ||
|
|
||
| text = "Hello from Respeecher! I hope you are having a great day." | ||
|
|
||
| # split into two word chunks to simulate LLM streaming | ||
| words = text.split() | ||
| for i in range(0, len(words), 2): | ||
| chunk = " ".join(words[i : i + 2]) | ||
| if chunk: | ||
| logger.info(f'pushing chunk: "{chunk} "') | ||
| stream.push_text(chunk + " ") | ||
|
|
||
| # Mark end of input segment | ||
| stream.flush() | ||
| stream.end_input() | ||
| await asyncio.gather(task) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint)) |
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,27 @@ | ||
| # Respeecher plugin for LiveKit Agents | ||
|
|
||
| Support for [Respeecher](https://respeecher.com/)'s TTS in LiveKit Agents. | ||
|
|
||
| More information is available in the docs for the [Respeecher](https://docs.livekit.io/agents/integrations/tts/respeecher/) integration. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install livekit-plugins-respeecher | ||
| ``` | ||
|
|
||
| ## Pre-requisites | ||
|
|
||
| You'll need an API key from Respeecher. It can be set as an environment variable: `RESPEECHER_API_KEY` or passed to the `respeecher.TTS()` constructor. | ||
|
|
||
| To get the key, log in to [Respeecher Space](https://space.respeecher.com/). | ||
|
|
||
| ## Example | ||
|
|
||
| To try out the Respeecher plugin, run the example: | ||
|
|
||
| ```bash | ||
| uv run python examples/other/text-to-speech/respeecher_tts.py start | ||
| ``` | ||
|
|
||
| Check [`examples/other/text-to-speech/README.md`](../../examples/other/text-to-speech/README.md) for running details. | ||
44 changes: 44 additions & 0 deletions
44
livekit-plugins/livekit-plugins-respeecher/livekit/plugins/respeecher/__init__.py
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,44 @@ | ||
| # Copyright 2025 LiveKit, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Respeecher plugin for LiveKit Agents | ||
|
|
||
| Voice cloning and synthesis plugin for LiveKit Agents using Respeecher API. | ||
| """ | ||
|
|
||
| from .tts import TTS, ChunkedStream, SynthesizeStream | ||
| from .version import __version__ | ||
|
|
||
| __all__ = ["TTS", "ChunkedStream", "SynthesizeStream", "__version__"] | ||
|
|
||
| from livekit.agents import Plugin | ||
|
|
||
| from .log import logger | ||
|
|
||
|
|
||
| class RespeecherPlugin(Plugin): | ||
| def __init__(self) -> None: | ||
| super().__init__(__name__, __version__, __package__, logger) | ||
|
|
||
|
|
||
| Plugin.register_plugin(RespeecherPlugin()) | ||
|
|
||
| # Cleanup docs of unexported modules | ||
| _module = dir() | ||
| NOT_IN_ALL = [m for m in _module if m not in __all__] | ||
|
|
||
| __pdoc__ = {} | ||
|
|
||
| for n in NOT_IN_ALL: | ||
| __pdoc__[n] = False |
3 changes: 3 additions & 0 deletions
3
livekit-plugins/livekit-plugins-respeecher/livekit/plugins/respeecher/log.py
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,3 @@ | ||
| import logging | ||
|
|
||
| logger = logging.getLogger("livekit.plugins.respeecher") |
39 changes: 39 additions & 0 deletions
39
livekit-plugins/livekit-plugins-respeecher/livekit/plugins/respeecher/models.py
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,39 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Any, Literal | ||
|
|
||
| TTSModels = Literal[ | ||
| # Respeecher's public English model | ||
| "/public/tts/en-rt", | ||
| # Respeecher's public Ukrainian model | ||
| "/public/tts/ua-rt", | ||
| ] | ||
|
|
||
| TTSEncoding = Literal["pcm_s16le",] | ||
|
|
||
|
|
||
| """Check https://space.respeecher.com/docs/api/tts/sampling-params-guide for details""" | ||
| SamplingParams = dict[str, Any] | ||
|
|
||
|
|
||
| @dataclass | ||
| class VoiceSettings: | ||
| """Voice settings for Respeecher TTS""" | ||
|
|
||
| sampling_params: SamplingParams | None = None | ||
|
|
||
|
|
||
| class Voice(dict): | ||
| """Voice model for Respeecher - behaves like a dict with guaranteed `id` and optional `sampling_params`""" | ||
|
|
||
| def __init__(self, *args: Any, **kwargs: Any) -> None: | ||
| super().__init__(*args, **kwargs) | ||
| if "id" not in self: | ||
| raise ValueError("Voice must have an 'id' field") | ||
|
|
||
| @property | ||
| def id(self) -> str: | ||
| return str(self["id"]) | ||
|
|
||
| @property | ||
| def sampling_params(self) -> SamplingParams | None: | ||
| return self.get("sampling_params") |
Empty file.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
How can we add documentation for the new plugin?