*** title: How to use Text to Speech description: Learn how to synthesize your text using the Smallest AI API. icon: wave-square ----------------- In this tutorial, you will learn how to use the Smallest AI platform to synthesize text to speech both synchronously and asynchronously. By the end of this tutorial, you will be able to convert text into speech using our API. You can access the source code for the Python SDK on our [GitHub repository](https://github.com/smallest-inc/smallest-python-sdk). ## Requirements Before you begin, ensure you have the following: * Python (3.9 or higher) installed on your machine. * An API key from the Smallest AI platform (sign up [here](https://waves.smallest.ai)). ## Setup ### Install our SDK ```bash pip install smallestai ``` ### Set your API key as an environment variable ```bash export SMALLEST_API_KEY=YOUR_API_KEY ``` ## Synchronous Text to Speech Here is an example of how to synthesize text to speech synchronously: ' If you are using a `voice_id` corresponding to a voice clone, you should explicitly set the `model` parameter to `"lightning-large"` in the `Smallest` client or payload. ```python python from smallestai.waves import WavesClient def main(): client = WavesClient(api_key="SMALLEST_API_KEY") client.synthesize( "Hello, this is a test for sync synthesis function.", save_as="sync_synthesize.wav" ) if __name__ == "__main__": main() ``` ## Asynchronous Text to Speech Here is an example of how to synthesize text to speech asynchronously: If you are using a `voice_id` corresponding to a voice clone, you should explicitly set the `model` parameter to `"lightning-large"` in the `Smallest` client or payload. ```python python import asyncio import aiofiles from smallestai.waves import AsyncWavesClient async def main(): client = AsyncWavesClient(api_key="SMALLEST_API_KEY") async with client as tts: audio_bytes = await tts.synthesize("Hello, this is a test of the async synthesis function.") async with aiofiles.open("async_synthesize.wav", "wb") as f: await f.write(audio_bytes) # alternatively you can use the `save_as` parameter. if __name__ == "__main__": asyncio.run(main()) ``` ## Parameters * `api_key` (str): Your API key (can be set via SMALLEST\_API\_KEY environment variable) * `model` (str): TTS model to use (default: `lightning`, available: `lightning`, `lightning-large`) * `sample_rate` (int): Audio sample rate (default: 24000) * `voice_id` (str): Voice ID (default: "emily") * `speed` (float): Speech speed multiplier (default: 1.0) * `consistency` (float): Controls word repetition and skipping. Decrease it to prevent skipped words, and increase it to prevent repetition. Only supported in `lightning-large` model. (default: 0.5) * `similarity` (float): Controls the similarity between the synthesized audio and the reference audio. Increase it to make the speech more similar to the reference audio. Only supported in `lightning-large` model. (default: 0) * `enhancement` (boolean): Enhances speech quality at the cost of increased latency. Only supported in `lightning-large` model. (default: False) * `add_wav_header` (boolean): Whether to add a WAV header to the output audio. (default: Faalse) These parameters are part of the `Smallest` and `AsyncSmallest` instance. They can be set when creating the instance (as shown above). However, the `synthesize` function also accepts `kwargs`, allowing you to override any of these parameters on a per-request basis. For example, you can modify the speech speed and sample rate just for a particular synthesis request: ```python python audio_bytes = client.synthesize( "Modern problems don't always require modern solutions.", speed=1.5, # Overrides default speed sample_rate=16000 # Overrides default sample rate ) ``` ## Conclusion The Smallest AI Text-to-Speech SDK offers both synchronous and asynchronous options, catering to a variety of use cases: * **Synchronous TTS**: Ideal for applications where immediate responses are needed, such as real-time voice assistants, chatbot integrations, or interactive voice systems. It ensures that the audio is generated and available instantly for use within the same execution flow. * **Asynchronous TTS**: Designed for scenarios that involve handling multiple requests or large-scale processing. For example, if you need to convert multiple text inputs into speech concurrently, such as creating audio files for an audiobook or processing a batch of text-based announcements, asynchronous TTS allows you to execute these tasks efficiently without blocking other operations. This approach ensures scalability and optimal resource utilization, particularly in environments where time and performance are critical. By understanding these modes and tailoring their usage to specific requirements, you can build highly responsive, scalable, and efficient solutions using the Smallest AI platform. If you have any questions or suggestions, please create an issue on the [smallest-python-sdk GitHub ](https://github.com/smallest-inc/smallest-python-sdk).