> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trymirai.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Text to Speech

<Tip>
  In this example, we will generate audio from text.
</Tip>

<Tabs>
  <Tab title="Python">
    <Steps>
      <Step title="Create a new project">
        ```sh theme={null}
        uv init demo && cd demo
        ```
      </Step>

      <Step title="Install dependencies">
        ```sh theme={null}
        uv add uzu
        ```
      </Step>

      <Step title="Paste into main.py">
        ```python theme={null}
        import asyncio
        from pathlib import Path

        from uzu import Engine, EngineConfig


        async def main() -> None:
            engine_config = EngineConfig.create()
            engine = await Engine.create(engine_config)

            model = await engine.model("fishaudio/s1-mini")
            if model is None:
                raise RuntimeError("Model not found")
            async for update in (await engine.download(model)).iterator():
                print(f"Download progress: {update.progress}")

            text = (
                "London is the capital of United Kingdom and one of the world's most influential cities, "
                "known for its rich history, cultural diversity, and global significance in finance, politics, and the arts. "
                "Situated along the River Thames, the city blends historic landmarks like Tower of London and Buckingham Palace "
                "with modern architecture such as The Shard. London is also home to renowned institutions including the British Museum "
                "and vibrant areas like Covent Garden, offering a mix of history, entertainment, and innovation that attracts millions of visitors each year."
            )
            output_path = Path.home() / "Desktop" / "output.wav"
            session = await engine.text_to_speech(model)
            output = await session.synthesize(text)
            output.pcm_batch.save_as_wav(str(output_path))
            print(f"Output saved to: {output_path}")


        if __name__ == "__main__":
            asyncio.run(main())
        ```
      </Step>

      <Step title="Run the snippet">
        ```sh theme={null}
        uv run main.py
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab title="Swift">
    <Steps>
      <Step title="Create a new SwiftUI project">
        <img src="https://mintcdn.com/mirai-bded675a/-MIzY7hlOp3HioeZ/images/quick-start/xcode-new-project.png?fit=max&auto=format&n=-MIzY7hlOp3HioeZ&q=85&s=7ef3a6a5ada8fec6d6cfe024e598e278" alt="" width="1430" height="1020" data-path="images/quick-start/xcode-new-project.png" />
      </Step>

      <Step title="Add the SDK">
        Add this package through SPM:

        ```sh theme={null}
        https://github.com/trymirai/uzu.git
        ```
      </Step>

      <Step title="Paste the snippet">
        ```swift theme={null}
        import Foundation
        import Uzu

        public func runTextToSpeech() async throws {
            let engineConfig = EngineConfig.create()
            let engine = try await Engine.create(config: engineConfig)
            
            guard let model = try await engine.model(identifier: "fishaudio/s1-mini") else {
                return
            }
            for try await update in try await engine.download(model: model).iterator() {
                print("Download progress: \(update.progress())")
            }
            
            let text = "London is the capital of United Kingdom and one of the world’s most influential cities, known for its rich history, cultural diversity, and global significance in finance, politics, and the arts. Situated along the River Thames, the city blends historic landmarks like Tower of London and Buckingham Palace with modern architecture such as The Shard. London is also home to renowned institutions including the British Museum and vibrant areas like Covent Garden, offering a mix of history, entertainment, and innovation that attracts millions of visitors each year."
            let outputPath = FileManager.default.homeDirectoryForCurrentUser
                .appendingPathComponent("Desktop")
                .appendingPathComponent("output.wav")
            let session = try await engine.textToSpeech(model: model)
            let output = try await session.synthesize(input: text)
            try output.pcmBatch.saveAsWav(path: outputPath.path())
            print("Output saved to: \(outputPath.path())")
        }
        ```
      </Step>

      <Step title="Add the snippet call">
        ```swift theme={null}
        var body: some View {
            VStack {
                Text("On-device AI")
            }
            .onAppear() {
                Task {
                    try await runExampleName()
                }
            }
        }
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab title="TypeScript">
    <Steps>
      <Step title="Make a new project folder">
        ```sh theme={null}
        mkdir demo && cd demo
        ```
      </Step>

      <Step title="Initialize with pnpm">
        ```sh theme={null}
        pnpm init
        ```
      </Step>

      <Step title="Install dependencies">
        ```sh theme={null}
        pnpm add typescript ts-node @types/node -D
        pnpm add @trymirai/uzu
        ```
      </Step>

      <Step title="Initialize a tsconfig.json">
        ```json theme={null}
        {
            "compilerOptions": {
                "target": "es2020",
                "module": "commonjs",
                "moduleResolution": "node",
                "strict": true,
                "esModuleInterop": true,
                "outDir": "dist",
                "types": [
                    "node"
                ]
            },
            "include": [
                "*.ts"
            ]
        }
        ```
      </Step>

      <Step title="Create main.ts">
        ```ts theme={null}
        import { Engine, EngineConfig } from '@trymirai/uzu';
        import { homedir } from "os";
        import { join } from "path";

        async function main() {
            let engineConfig = EngineConfig.create();
            let engine = await Engine.create(engineConfig);

            let model = await engine.model('fishaudio/s1-mini');
            if (!model) {
                throw new Error('Model not found');
            }
            for await (const update of await engine.download(model)) {
                console.log('Download progress:', update.progress);
            }

            const text = "London is the capital of United Kingdom and one of the world’s most influential cities, known for its rich history, cultural diversity, and global significance in finance, politics, and the arts. Situated along the River Thames, the city blends historic landmarks like Tower of London and Buckingham Palace with modern architecture such as The Shard. London is also home to renowned institutions including the British Museum and vibrant areas like Covent Garden, offering a mix of history, entertainment, and innovation that attracts millions of visitors each year.";
            const outputPath = join(homedir(), "Desktop", "output.wav");
            let session = await engine.textToSpeech(model);
            let output = await session.synthesize(text);
            output.pcmBatch.saveAsWav(outputPath);
            console.log('Output saved to: ', outputPath);
        }

        main().catch((error) => {
            console.error(error);
        });
        ```
      </Step>

      <Step title="Run the snippet">
        ```sh theme={null}
        pnpm ts-node main.ts
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab title="Rust">
    <Steps>
      <Step title="Create a new project">
        ```sh theme={null}
        cargo new demo && cd demo
        ```
      </Step>

      <Step title="Install dependencies">
        ```sh theme={null}
        cargo add uzu --git https://github.com/trymirai/uzu
        cargo add tokio --features full
        ```
      </Step>

      <Step title="Paste into src/main.rs">
        ```rust theme={null}
        use uzu::{
            engine::{Engine, EngineConfig},
            session::text_to_speech::TextToSpeechSessionStreamChunk,
            types::basic::PcmBatch,
        };

        #[tokio::main]
        async fn main() -> Result<(), Box<dyn std::error::Error>> {
            let engine_config = EngineConfig::default();
            let engine = Engine::new(engine_config).await?;

            let model = engine.model("fishaudio/s1-mini".to_string()).await?.ok_or("Model not found")?;
            let downloader = engine.download(&model).await?;
            while let Some(update) = downloader.next().await {
                println!("Download progress: {}", update.progress());
            }

            let text = "London is the capital of United Kingdom and one of the world's most influential cities, \
                known for its rich history, cultural diversity, and global significance in finance, politics, and the arts. \
                Situated along the River Thames, the city blends historic landmarks like Tower of London and Buckingham Palace \
                with modern architecture such as The Shard. London is also home to renowned institutions including the British Museum \
                and vibrant areas like Covent Garden, offering a mix of history, entertainment, and innovation that attracts millions of visitors each year.";
            let output_path = dirs::home_dir().ok_or("Home not found")?.join("Desktop").join("output.wav");

            let session = engine.text_to_speech(model).await?;
            let stream = session.synthesize_stream(text.to_string()).await;
            let mut pcm_batches: Vec<PcmBatch> = Vec::new();
            while let Some(event) = stream.next().await {
                match event {
                    TextToSpeechSessionStreamChunk::Output {
                        output,
                    } => {
                        pcm_batches.push(output.pcm_batch);
                    },
                    TextToSpeechSessionStreamChunk::Error {
                        error,
                    } => {
                        println!("Error: {error}");
                    },
                }
            }

            let pcm_batch_first = pcm_batches.first().ok_or("No batches")?;
            let pcm_batch_full = PcmBatch {
                samples: pcm_batches.iter().flat_map(|batch| batch.samples.iter().copied()).collect(),
                sample_rate: pcm_batch_first.sample_rate,
                channels: pcm_batch_first.channels,
                lengths: vec![pcm_batches.iter().flat_map(|batch| batch.lengths.iter().copied()).sum()],
            };
            pcm_batch_full.save_as_wav(output_path.to_string_lossy().to_string())?;
            println!("Output saved to: {}", output_path.display());

            Ok(())
        }
        ```
      </Step>

      <Step title="Run the snippet">
        ```sh theme={null}
        cargo run --release
        ```
      </Step>
    </Steps>
  </Tab>
</Tabs>
