### Run Basic Text-to-Audio Example Source: https://github.com/anlanga/text2audio/blob/master/README.md Executes the simple example to convert a short Chinese text to audio using default settings. This is a good starting point for understanding the project's core functionality. ```bash cargo run --example simple ``` -------------------------------- ### Basic Text to Audio Conversion (Example) Source: https://github.com/anlanga/text2audio/blob/master/README.md A concise example showing how to initialize the Text2Audio converter and convert a simple English phrase to an audio file. ```rust use text2audio::Text2Audio; let converter = Text2Audio::new(&api_key); converter.convert("Hello, world!", "hello.wav").await?; ``` -------------------------------- ### Run Parallel Processing Example Source: https://github.com/anlanga/text2audio/blob/master/README.md Illustrates concurrent audio generation for improved performance, especially with long texts. This example is key for understanding how to speed up audio production. ```bash cargo run --example parallel ``` -------------------------------- ### Run Custom Voice Example Source: https://github.com/anlanga/text2audio/blob/master/README.md Shows how to customize the voice output and tune various parameters for audio generation. Use this example to explore voice options and fine-tune audio characteristics. ```bash cargo run --example custom_voice ``` -------------------------------- ### Run File Input Example Source: https://github.com/anlanga/text2audio/blob/master/README.md Converts text from a file, featuring optimized settings for long-form content. This example is relevant when dealing with substantial amounts of text stored in files. ```bash cargo run --example from_file ``` -------------------------------- ### Run AI Segmentation Example Source: https://github.com/anlanga/text2audio/blob/master/README.md Demonstrates the AI-powered semantic segmentation feature for processing long texts. This example is useful for understanding how the project handles breaking down extensive content. ```bash cargo run --example ai_splitter ``` -------------------------------- ### Builder Pattern for Configuration Source: https://github.com/anlanga/text2audio/blob/master/README.md Illustrates the use of the builder pattern for a more fluent and organized way to configure the Text2Audio converter. All customization options can be chained together. ```rust use text2audio::{Text2Audio, Model, Voice}; use std::time::Duration; let converter = Text2Audio::builder(&api_key) .model(Model::GLM4_7) .voice(Voice::Tongtong) .speed(1.2) .volume(1.5) .max_segment_length(500) .parallel(3) .thinking(true) .retry_config(3, Duration::from_millis(100)) .build(); converter.convert("优化的长文本", "narration.wav").await?; ``` -------------------------------- ### Basic Text to Audio Conversion Source: https://github.com/anlanga/text2audio/blob/master/README.md Demonstrates the fundamental usage of the Text2Audio converter. Initializes the converter with an API key and converts a simple Chinese phrase to an audio file. ```rust use text2audio::Text2Audio; #[tokio::main] async fn main() -> Result<(), Box> { let api_key = std::env::var("ZHIPU_API_KEY")?; let converter = Text2Audio::new(api_key); converter.convert("你好,世界!", "output.wav").await?; println!("Audio saved to output.wav"); Ok(()) } ``` -------------------------------- ### Custom Voice, Speed, and Volume Source: https://github.com/anlanga/text2audio/blob/master/README.md Shows how to customize the audio output by specifying voice, speed, and volume. The `with_voice`, `with_speed`, and `with_volume` methods allow for fine-tuning. ```rust use text2audio::{Text2Audio, Voice}; let converter = Text2Audio::new(&api_key) .with_voice(Voice::Xiaochen) .with_speed(1.5) // 50% faster .with_volume(2.0); // Louder converter.convert("加速版语音", "fast.wav").await?; ``` -------------------------------- ### Handle Text2Audio Conversion Errors Source: https://github.com/anlanga/text2audio/blob/master/README.md Demonstrates how to match and handle different error types returned by the `convert` method. This is crucial for robustly managing potential issues during audio conversion. ```rust use text2audio::{Text2Audio, Error}; match converter.convert(text, "output.wav").await { Ok(_) => println!("✓ Conversion successful"), Err(Error::EmptyInput) => eprintln!("✗ Error: Input text is empty"), Err(Error::TtsApi(msg)) => eprintln!("✗ TTS API Error: {}", msg), Err(Error::AiApi(msg)) => eprintln!("✗ AI API Error: {}", msg), Err(Error::Audio(msg)) => eprintln!("✗ Audio Processing Error: {}", msg), Err(Error::Io(e)) => eprintln!("✗ File I/O Error: {}", e), Err(e) => eprintln!("✗ Unexpected Error: {}", e), } ``` -------------------------------- ### Add text2audio Dependency to Cargo.toml Source: https://github.com/anlanga/text2audio/blob/master/README.md Include the text2audio crate and tokio runtime in your project's dependencies. Ensure you are using Rust 1.70 or later. ```toml [dependencies] text2audio = "0.1.0" tokio = { version = "1", features = ["full"] } ``` -------------------------------- ### Set Zhipu AI API Key Source: https://github.com/anlanga/text2audio/blob/master/README.md Set the ZHIPU_API_KEY environment variable before running the application. This is required for authentication with the Zhipu AI API. ```bash export ZHIPU_API_KEY="your_api_key_here" ``` -------------------------------- ### Parallel Processing and Retry Configuration Source: https://github.com/anlanga/text2audio/blob/master/README.md Explains how to improve performance for long texts using parallel processing and configure retry mechanisms for robustness. The `with_parallel` and `with_retry_config` methods are key here. ```rust use text2audio::{Text2Audio, Voice}; use std::time::Duration; let converter = Text2Audio::new(&api_key) .with_voice(Voice::Tongtong) .with_parallel(5) // Process up to 5 segments concurrently .with_retry_config(5, Duration::from_millis(200)); converter.convert(very_long_text, "output.wav").await?; ``` -------------------------------- ### Long Text Segmentation and Model Selection Source: https://github.com/anlanga/text2audio/blob/master/README.md Demonstrates handling long text inputs by enabling AI segmentation and selecting a specific model. The `with_model` and `with_max_segment_length` options are used for this purpose. ```rust use text2audio::{Text2Audio, Model}; let long_text = "非常长的文本..."; let converter = Text2Audio::new(&api_key) .with_model(Model::GLM4_7) // Use best model for segmentation .with_max_segment_length(300) // Shorter segments for better flow .with_thinking(true); // Enable thinking mode converter.convert(long_text, "long_audio.wav").await?; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.