mirror of https://github.com/ndarilek/tts-rs.git
Support dynamic feature selection/use in example.
This commit is contained in:
parent
ac4ebea05a
commit
e388934e5e
|
@ -1,30 +1,39 @@
|
||||||
use std::u8;
|
use std::u8;
|
||||||
|
|
||||||
use tts::TTS;
|
use tts::*;
|
||||||
|
|
||||||
fn main() -> Result<(), std::io::Error> {
|
fn main() -> Result<(), std::io::Error> {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let mut tts = TTS::default()?;
|
let mut tts = TTS::default()?;
|
||||||
tts.speak("Hello, world.", false)?;
|
tts.speak("Hello, world.", false)?;
|
||||||
let original_rate = tts.get_rate()?;
|
let Features { rate, .. } = tts.supported_features();
|
||||||
tts.speak(format!("Current rate: {}", original_rate), false)?;
|
if rate {
|
||||||
tts.set_rate(u8::MAX)?;
|
let original_rate = tts.get_rate()?;
|
||||||
tts.speak("This is very fast.", false)?;
|
tts.speak(format!("Current rate: {}", original_rate), false)?;
|
||||||
tts.set_rate(0)?;
|
tts.set_rate(u8::MAX)?;
|
||||||
tts.speak("This is very slow.", false)?;
|
tts.speak("This is very fast.", false)?;
|
||||||
tts.set_rate(original_rate)?;
|
tts.set_rate(0)?;
|
||||||
let original_pitch = tts.get_pitch()?;
|
tts.speak("This is very slow.", false)?;
|
||||||
tts.set_pitch(u8::MAX)?;
|
tts.set_rate(original_rate)?;
|
||||||
tts.speak("This is high-pitch.", false)?;
|
}
|
||||||
tts.set_pitch(0)?;
|
let Features { pitch, .. } = tts.supported_features();
|
||||||
tts.speak("This is low pitch.", false)?;
|
if pitch {
|
||||||
tts.set_pitch(original_pitch)?;
|
let original_pitch = tts.get_pitch()?;
|
||||||
let original_volume = tts.get_volume()?;
|
tts.set_pitch(u8::MAX)?;
|
||||||
tts.set_volume(u8::MAX)?;
|
tts.speak("This is high-pitch.", false)?;
|
||||||
tts.speak("This is loud!", false)?;
|
tts.set_pitch(0)?;
|
||||||
tts.set_volume(0)?;
|
tts.speak("This is low pitch.", false)?;
|
||||||
tts.speak("This is quiet.", false)?;
|
tts.set_pitch(original_pitch)?;
|
||||||
tts.set_volume(original_volume)?;
|
}
|
||||||
|
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)?;
|
tts.speak("Goodbye.", false)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
22
src/lib.rs
22
src/lib.rs
|
@ -38,10 +38,10 @@ impl convert::From<Error> for io::Error {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Features {
|
pub struct Features {
|
||||||
stop: bool,
|
pub stop: bool,
|
||||||
rate: bool,
|
pub rate: bool,
|
||||||
pitch: bool,
|
pub pitch: bool,
|
||||||
volume: bool,
|
pub volume: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Backend {
|
pub trait Backend {
|
||||||
|
@ -130,7 +130,9 @@ impl TTS {
|
||||||
* Sets the desired speech rate.
|
* Sets the desired speech rate.
|
||||||
*/
|
*/
|
||||||
pub fn set_rate(&mut self, rate: u8) -> Result<&Self, Error> {
|
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 {
|
if rate_feature {
|
||||||
self.0.set_rate(rate)?;
|
self.0.set_rate(rate)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
|
@ -155,7 +157,10 @@ impl TTS {
|
||||||
* Sets the desired speech pitch.
|
* Sets the desired speech pitch.
|
||||||
*/
|
*/
|
||||||
pub fn set_pitch(&mut self, pitch: u8) -> Result<&Self, Error> {
|
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 {
|
if pitch_feature {
|
||||||
self.0.set_pitch(pitch)?;
|
self.0.set_pitch(pitch)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
|
@ -180,7 +185,10 @@ impl TTS {
|
||||||
* Sets the desired speech volume.
|
* Sets the desired speech volume.
|
||||||
*/
|
*/
|
||||||
pub fn set_volume(&mut self, volume: u8) -> Result<&Self, Error> {
|
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 {
|
if volume_feature {
|
||||||
self.0.set_volume(volume)?;
|
self.0.set_volume(volume)?;
|
||||||
Ok(self)
|
Ok(self)
|
||||||
|
|
Loading…
Reference in New Issue