1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! This examples shows how to iterate over the available TTS voices.
use std::io::{self, Write};
use sapi_lite::tts::{installed_voices, SyncSynthesizer, Voice};
fn choose_voice() -> Option<Voice> {
// Collect all the install voices, without any filters or sorting preferences.
let mut voices: Vec<Voice> = installed_voices(None, None).unwrap().collect();
// Display the list to the user.
println!("Available voices:");
for (idx, voice) in voices.iter().enumerate() {
println!("{}) {}", idx + 1, voice.name().unwrap().to_string_lossy());
}
// Prompt the user to select a voice from the list.
print!("Choose a voice: ");
io::stdout().flush().unwrap();
// Get the user's selection.
let mut line = String::new();
io::stdin().read_line(&mut line).unwrap();
// If the selection is valid, return the selected voice.
let selected_idx = line.trim_end().parse::<usize>().ok()?;
if (selected_idx > 0) && (selected_idx <= voices.len()) {
Some(voices.swap_remove(selected_idx - 1))
} else {
None
}
}
fn main() {
// Initialize SAPI.
sapi_lite::initialize().unwrap();
// Have the user choose a voice from the list of available voices.
let voice = match choose_voice() {
Some(voice) => voice,
None => return,
};
// Create a speech synthesizer.
let synth = SyncSynthesizer::new().unwrap();
// Configure the synthesizer to use the selected voice.
synth.set_voice(&voice).unwrap();
// Speak a phrase in the selected voice.
synth
.speak(
"The pellet with the poison's in the flagon with the dragon.",
None,
)
.unwrap();
// We don't need SAPI anymore. Clean up and free the resources.
sapi_lite::finalize();
}