From e388934e5ef1b036e734206c5d875bb4116267e5 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Mon, 25 Mar 2019 16:34:30 +0000 Subject: [PATCH] Support dynamic feature selection/use in example. --- examples/hello_world.rs | 49 ++++++++++++++++++++++++----------------- src/lib.rs | 22 ++++++++++++------ 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 27794b9..f5886ea 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -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(()) } diff --git a/src/lib.rs b/src/lib.rs index 910eda9..1594f18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,10 +38,10 @@ impl convert::From 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)