livekit speechify integration code example for me
# ask-ai
c
livekit speechify integration code example for me
t
Thanks for reaching out, Hai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Here is a complete example for integrating Speechify TTS with LiveKit: 1. First, install the required package:
pip install "livekit-agents[speechify]~=1.2"
2. Next, create a
.env
file in your project root and add your Speechify API key:
SPEECHIFY_API_KEY=your_api_key_here
3. Then, use the following Python code for the integration:
Copy code
import asyncio
import logging
from dotenv import load_dotenv
from livekit import rtc
from livekit.agents import AutoSubscribe, JobContext, WorkerOptions, cli
from livekit.agents.tts import TTSCapabilities
from livekit.plugins import speechify

load_dotenv()

logger = logging.getLogger("speechify-tts-demo")
logger.setLevel(<http://logging.INFO|logging.INFO>)

async def entrypoint(ctx: JobContext):
    try:
        await ctx.connect(auto_subscribe=AutoSubscribe.SUBSCRIBE_NONE)
        participant = await ctx.wait_for_participant()
        <http://logger.info|logger.info>("Connected to LiveKit room and participant joined")

        tts = speechify.TTS(voice_id="jack", model="simba-english", language="en", encoding="mp3_24000", loudness_normalization=True, text_normalization=True)
        assert isinstance(tts.capabilities, TTSCapabilities)
        <http://logger.info|logger.info>(f"TTS initialized with sample rate: {tts.sample_rate}")

        source = rtc.AudioSource(tts.sample_rate, tts.num_channels)
        track = rtc.LocalAudioTrack.create_audio_track("tts-audio", source)
        options = rtc.TrackPublishOptions()
        options.source = rtc.TrackSource.SOURCE_MICROPHONE

        publication = await ctx.room.local_participant.publish_track(track, options)
        await publication.wait_for_subscription()
        <http://logger.info|logger.info>("Audio track published successfully")

        text = "Hello! I'm using Speechify TTS with LiveKit Agents to convert this text into natural-sounding speech in real time."
        async for output in tts.synthesize(text):
            await source.capture_frame(output.frame)

        <http://logger.info|logger.info>("TTS synthesis completed successfully")

    except Exception as e:
        logger.error("An error occurred in the TTS agent: %s", str(e), exc_info=True)
        raise
    finally:
        if 'tts' in locals():
            await tts.aclose()

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
If you'd like, you can also explore listing available voices programmatically or using SSML for more advanced speech control. Please feel free to reach out if you have any questions or need a hand with anything else—I’m here to help! Sources: Speechify TTS integration guide | LiveKit Docs | livekit-plugins/livekit-plugins-speechify/pyproject.toml
c
is there anyway i can set ssml?
t
Absolutely! You can definitely use SSML (Speech Synthesis Markup Language) with Speechify TTS to have more control over pronunciation, pitch, speed, and other speech features. Here’s a simple example to get you started:
Copy code
ssml_text = ''

async for output in tts.synthesize(ssml_text):
    await source.capture_frame(output.frame)
Just swap out the
ssml_text
content with your own SSML markup. If you have any questions or want me to walk you through it, I’m here to help!
c
tts = speechify.TTS( model="simba-english", _voice_id_="oliver", ), what is the raw api call for this so i can try postman?
t
Hi there! I’m happy to guide you through making the raw API call for Speechify TTS. You’ll be sending a POST request to this endpoint:
Copy code
<https://api.sws.speechify.com/v1/audio/stream>
Here’s how you can structure your request: • HTTP Method: POST • Headers:
Authorization: Bearer <your_api_key>
Accept: audio/<format>
(for example,
audio/ogg
if you’re using
ogg_24000
) ◦
x-caller: livekit
Request Body (JSON):
Copy code
{
  "input": "text to synthesize",
  "voice_id": "specified_voice_id",
  "language": "language_code_if_provided",
  "model": "simba-english",
  "audio_format": "ogg",
  "options": {
    "loudness_normalization": true_or_false_if_set,
    "text_normalization": true_or_false_if_set
  }
}
Just be sure to swap out
<your_api_key>
with your actual Speechify API key, and feel free to tweak the body parameters to fit your needs. If you have any questions or want me to walk you through any part of this, I’m here to help! Sources: examples/other/text-to-speech/rime_tts.py | Speechify TTS integration guide | LiveKit Docs