### Initialize TTS Instance with Specific Backend (Rust) Source: https://context7.com/ndarilek/tts-rs/llms.txt Explicitly creates a Text-To-Speech engine instance by selecting a specific backend. This is useful on platforms that support multiple TTS engines. The example demonstrates selecting backends for Linux, Windows, Apple platforms, Android, and WebAssembly. ```rust use tts::* fn main() -> Result<(), Error> { // Create TTS with specific backend #[cfg(target_os = "linux")] let mut tts = Tts::new(Backends::SpeechDispatcher)?; #[cfg(windows)] let mut tts = Tts::new(Backends::WinRt)?; #[cfg(target_vendor = "apple")] let mut tts = Tts::new(Backends::AvFoundation)?; #[cfg(target_os = "android")] let mut tts = Tts::new(Backends::Android)?; #[cfg(target_arch = "wasm32")] let mut tts = Tts::new(Backends::Web)?; tts.speak("Using specific backend", false)?; Ok(()) } ``` -------------------------------- ### Stop Speech Synthesis with tts-rs Source: https://context7.com/ndarilek/tts-rs/llms.txt Provides an example of how to immediately halt current speech synthesis using the tts-rs library. It demonstrates speaking a long sentence, waiting, and then calling the stop function. Requires the 'stop' feature to be supported. ```rust use tts ::*; use std::{thread, time}; fn main() -> Result<(), Error> { let mut tts = Tts::default()?; let features = tts.supported_features(); if features.stop { // Start speaking a long text tts.speak("This is a very long sentence that will be interrupted.", false)?; // Wait a moment thread::sleep(time::Duration::from_millis(500)); // Stop speaking immediately tts.stop()?; println!("Speech stopped"); } else { println!("Stop feature not supported"); } Ok(()) } ``` -------------------------------- ### List and Select Voices with tts-rs Source: https://context7.com/ndarilek/tts-rs/llms.txt Demonstrates how to enumerate available voices and switch between them using the tts-rs library. It covers getting all voices, their properties, the current voice, and setting a new voice. Requires the 'voice' and potentially 'get_voice' features. ```rust use tts ::*; fn main() -> Result<(), Error> { let mut tts = Tts::default()?; let features = tts.supported_features(); if features.voice { // Get all available voices let voices = tts.voices()?; println!("Available voices: {}", voices.len()); for voice in &voices { println!("Voice ID: {}", voice.id()); println!(" Name: {}", voice.name()); println!(" Language: {}", voice.language()); if let Some(gender) = voice.gender() { println!(" Gender: {:?}", gender); } } // Get current voice if supported if features.get_voice { if let Some(current) = tts.voice()? { println!("Current voice: {}", current.name()); } } // Switch to a different voice if let Some(first_voice) = voices.first() { tts.set_voice(first_voice)?; tts.speak(format!("Now using {}", first_voice.name()), false)?; } } Ok(()) } ``` -------------------------------- ### Rust: Continuous Speech Generation Source: https://context7.com/ndarilek/tts-rs/llms.txt Shows how to generate speech continuously in a loop using the TTS-RS library. It includes platform-specific handling for macOS using `NSRunLoop` and a delay between phrases using `thread::sleep`. This example requires the `tts` and `objc2-foundation` crates. ```rust use std::{thread, time}; use tts ::*; fn main() -> Result<(), Error> { let mut tts = Tts::default()?; // Continuous speech loop for i in 1..=10 { tts.speak(format!("Speaking phrase number {}", i), false)?; // On macOS, run the NSRunLoop to process speech events #[cfg(target_os = "macos")] { let run_loop = unsafe { objc2_foundation::NSRunLoop::currentRunLoop() }; let date = unsafe { objc2_foundation::NSDate::distantFuture() }; unsafe { run_loop.runMode_beforeDate( objc2_foundation::NSDefaultRunLoopMode, &date ) }; } // Wait between phrases thread::sleep(time::Duration::from_secs(3)); } Ok(()) } ``` -------------------------------- ### Adjust Speech Volume with tts-rs Source: https://context7.com/ndarilek/tts-rs/llms.txt Shows how to adjust the loudness of synthesized speech using the tts-rs library. It includes getting current volume, volume bounds, and setting different volume levels. Requires the 'volume' feature to be supported. ```rust use tts ::*; fn main() -> Result<(), Error> { let mut tts = Tts::default()?; let features = tts.supported_features(); if features.volume { // Get current volume let original_volume = tts.get_volume()?; // Get volume bounds let min = tts.min_volume(); let max = tts.max_volume(); let normal = tts.normal_volume(); println!("Volume range: {} to {} (normal: {})", min, max, normal); // Set maximum volume tts.set_volume(max)?; tts.speak("This is very loud!", false)?; // Set minimum volume tts.set_volume(min)?; tts.speak("This is very quiet.", false)?; // Restore original volume tts.set_volume(original_volume)?; } Ok(()) } ``` -------------------------------- ### Rust: Handle TTS Errors Gracefully Source: https://context7.com/ndarilek/tts-rs/llms.txt Demonstrates how to handle various errors that can occur during TTS operations, such as unsupported features or out-of-range values. This example uses Rust's standard error handling with `match` to provide user-friendly messages for different error conditions. ```rust use tts ::*; fn main() { match Tts::default() { Ok(mut tts) => { // Try to set rate match tts.set_rate(2.0) { Ok(_) => println!("Rate set successfully"), Err(Error::UnsupportedFeature) => { println!("Rate control not supported on this platform"); } Err(Error::OutOfRange) => { println!("Rate value out of valid range"); } Err(e) => { eprintln!("Error setting rate: {:?}", e); } } // Try to speak match tts.speak("Hello", false) { Ok(_) => println!("Speaking..."), Err(Error::OperationFailed) => { eprintln!("TTS operation failed"); } Err(e) => { eprintln!("Error speaking: {:?}", e); } } } Err(Error::NoneError) => { eprintln!("No TTS backend available"); } Err(e) => { eprintln!("Failed to initialize TTS: {:?}", e); } } } ``` -------------------------------- ### Control Speech Pitch with tts-rs Source: https://context7.com/ndarilek/tts-rs/llms.txt Demonstrates how to modify the pitch (frequency) of synthesized speech using the tts-rs library. It covers getting current pitch, pitch bounds, and setting different pitch levels. Requires the 'pitch' feature to be supported. ```rust use tts ::*; fn main() -> Result<(), Error> { let mut tts = Tts::default()?; let features = tts.supported_features(); if features.pitch { // Get current pitch let original_pitch = tts.get_pitch()?; // Get pitch bounds let min = tts.min_pitch(); let max = tts.max_pitch(); let normal = tts.normal_pitch(); // Set high pitch tts.set_pitch(max)?; tts.speak("This is high-pitch voice.", false)?; // Set low pitch tts.set_pitch(min)?; tts.speak("This is low-pitch voice.", false)?; // Set normal pitch tts.set_pitch(normal)?; tts.speak("This is normal pitch.", false)?; // Restore original tts.set_pitch(original_pitch)?; } Ok(()) } ``` -------------------------------- ### Control Speech Rate (Rust) Source: https://context7.com/ndarilek/tts-rs/llms.txt Adjusts the speech synthesis rate within the platform-specific supported bounds. This includes getting the current rate, querying the minimum, maximum, and normal rates, and setting a new rate. Includes a check for rate control feature support. ```rust use tts::* fn main() -> Result<(), Error> { let mut tts = Tts::default()?; let features = tts.supported_features(); if features.rate { // Get current rate let original_rate = tts.get_rate()?; println!("Original rate: {}", original_rate); // Get rate bounds let min = tts.min_rate(); let max = tts.max_rate(); let normal = tts.normal_rate(); println!("Rate range: {} to {} (normal: {})", min, max, normal); // Set to maximum speed tts.set_rate(max)?; tts.speak("This is very fast speech.", false)?; // Set to minimum speed tts.set_rate(min)?; tts.speak("This is very slow speech.", false)?; // Restore original rate tts.set_rate(original_rate)?; } else { println!("Rate control not supported on this platform"); } Ok(()) } ``` -------------------------------- ### Initialize TTS Instance with Default Backend (Rust) Source: https://context7.com/ndarilek/tts-rs/llms.txt Creates a Text-To-Speech engine instance using the platform's default backend. Automatically selects the most suitable backend for the current environment. Checks for screen reader availability and speaks text without interrupting ongoing speech. ```rust use tts::* fn main() -> Result<(), Error> { // Create TTS instance with platform default backend let mut tts = Tts::default()?; // Check if a screen reader is available if Tts::screen_reader_available() { println!("Screen reader detected"); } // Speak text without interrupting current speech tts.speak("Hello, world.", false)?; Ok(()) } ``` -------------------------------- ### Set Up Utterance Callbacks with tts-rs Source: https://context7.com/ndarilek/tts-rs/llms.txt Illustrates how to register callbacks to track speech synthesis lifecycle events using the tts-rs library. It includes setting up callbacks for utterance begin, end, and stop events. Requires the 'utterance_callbacks' feature to be supported. ```rust use tts ::*; fn main() -> Result<(), Error> { let mut tts = Tts::default()?; let features = tts.supported_features(); if features.utterance_callbacks { // Callback when utterance begins tts.on_utterance_begin(Some(Box::new(|utterance_id| { println!("Started speaking utterance: {:?}", utterance_id); })))?; // Callback when utterance completes tts.on_utterance_end(Some(Box::new(|utterance_id| { println!("Finished speaking utterance: {:?}", utterance_id); })))?; // Callback when utterance is stopped before completion tts.on_utterance_stop(Some(Box::new(|utterance_id| { println!("Stopped utterance: {:?}", utterance_id); })))?; // Now speak something - callbacks will fire let utterance_id = tts.speak("Testing callbacks", false)?; println!("Queued utterance: {:?}", utterance_id); } Ok(()) } ``` -------------------------------- ### Check Supported TTS Features (Rust) Source: https://context7.com/ndarilek/tts-rs/llms.txt Queries the current Text-To-Speech backend to determine which features are available before using them. This includes checking support for rate control, pitch, volume, speaking status, voice selection, and utterance callbacks. ```rust use tts::* fn main() -> Result<(), Error> { let mut tts = Tts::default()?; // Get feature support information let features = tts.supported_features(); if features.rate { println!("Rate control is supported"); } if features.pitch { println!("Pitch control is supported"); } if features.volume { println!("Volume control is supported"); } if features.is_speaking { println!("Speaking status detection is supported"); let speaking = tts.is_speaking()?; println!("Currently speaking: {}", speaking); } if features.voice { println!("Voice selection is supported"); } if features.utterance_callbacks { println!("Utterance callbacks are supported"); } Ok(()) } ``` -------------------------------- ### Speak Text with Interruption Control (Rust) Source: https://context7.com/ndarilek/tts-rs/llms.txt Speaks provided text with control over whether the speech should interrupt any currently playing audio. Supports queuing speech and obtaining an utterance ID for tracking specific speech requests. ```rust use tts::* fn main() -> Result<(), Error> { let mut tts = Tts::default()?; // Speak without interrupting (queues the speech) tts.speak("This will be spoken first.", false)?; tts.speak("This will be spoken second.", false)?; // Speak with interruption (stops current speech immediately) tts.speak("This interrupts and speaks immediately!", true)?; // Get utterance ID if supported if let Some(utterance_id) = tts.speak("Track this utterance", false)? { println!("Speaking with utterance ID: {:?}", utterance_id); } Ok(()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.