1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-17 07:49:37 +00:00

Support dynamic feature selection/use in example.

This commit is contained in:
Nolan Darilek 2019-03-25 16:34:30 +00:00
parent ac4ebea05a
commit e388934e5e
2 changed files with 44 additions and 27 deletions

View File

@ -1,30 +1,39 @@
use std::u8;
use tts::TTS;
use tts::*;
fn main() -> Result<(), std::io::Error> {
env_logger::init();
let mut tts = TTS::default()?;
tts.speak("Hello, world.", false)?;
let original_rate = tts.get_rate()?;
tts.speak(format!("Current rate: {}", original_rate), false)?;
tts.set_rate(u8::MAX)?;
tts.speak("This is very fast.", false)?;
tts.set_rate(0)?;
tts.speak("This is very slow.", false)?;
tts.set_rate(original_rate)?;
let original_pitch = tts.get_pitch()?;
tts.set_pitch(u8::MAX)?;
tts.speak("This is high-pitch.", false)?;
tts.set_pitch(0)?;
tts.speak("This is low pitch.", false)?;
tts.set_pitch(original_pitch)?;
let original_volume = tts.get_volume()?;
tts.set_volume(u8::MAX)?;
tts.speak("This is loud!", false)?;
tts.set_volume(0)?;
tts.speak("This is quiet.", false)?;
tts.set_volume(original_volume)?;
let Features { rate, .. } = tts.supported_features();
if rate {
let original_rate = tts.get_rate()?;
tts.speak(format!("Current rate: {}", original_rate), false)?;
tts.set_rate(u8::MAX)?;
tts.speak("This is very fast.", false)?;
tts.set_rate(0)?;
tts.speak("This is very slow.", false)?;
tts.set_rate(original_rate)?;
}
let Features { pitch, .. } = tts.supported_features();
if pitch {
let original_pitch = tts.get_pitch()?;
tts.set_pitch(u8::MAX)?;
tts.speak("This is high-pitch.", false)?;
tts.set_pitch(0)?;
tts.speak("This is low pitch.", false)?;
tts.set_pitch(original_pitch)?;
}
let Features { volume, .. } = tts.supported_features();
if volume {
let original_volume = tts.get_volume()?;
tts.set_volume(u8::MAX)?;
tts.speak("This is loud!", false)?;
tts.set_volume(0)?;
tts.speak("This is quiet.", false)?;
tts.set_volume(original_volume)?;
}
tts.speak("Goodbye.", false)?;
Ok(())
}

View File

@ -38,10 +38,10 @@ impl convert::From<Error> for io::Error {
}
pub struct Features {
stop: bool,
rate: bool,
pitch: bool,
volume: bool,
pub stop: bool,
pub rate: bool,
pub pitch: bool,
pub volume: bool,
}
pub trait Backend {
@ -130,7 +130,9 @@ impl TTS {
* Sets the desired speech rate.
*/
pub fn set_rate(&mut self, rate: u8) -> Result<&Self, Error> {
let Features { rate: rate_feature, .. } = self.supported_features();
let Features {
rate: rate_feature, ..
} = self.supported_features();
if rate_feature {
self.0.set_rate(rate)?;
Ok(self)
@ -155,7 +157,10 @@ impl TTS {
* Sets the desired speech pitch.
*/
pub fn set_pitch(&mut self, pitch: u8) -> Result<&Self, Error> {
let Features { pitch: pitch_feature, .. } = self.supported_features();
let Features {
pitch: pitch_feature,
..
} = self.supported_features();
if pitch_feature {
self.0.set_pitch(pitch)?;
Ok(self)
@ -180,7 +185,10 @@ impl TTS {
* Sets the desired speech volume.
*/
pub fn set_volume(&mut self, volume: u8) -> Result<&Self, Error> {
let Features { volume: volume_feature, .. } = self.supported_features();
let Features {
volume: volume_feature,
..
} = self.supported_features();
if volume_feature {
self.0.set_volume(volume)?;
Ok(self)