1
0
mirror of https://github.com/ndarilek/tts-rs.git synced 2024-11-22 15:29:38 +00:00

Remove HashMap globals for WinRT

This commit is contained in:
Bear-03 2022-07-26 16:37:45 +02:00
parent f404e180e4
commit 4cb7e609e0
No known key found for this signature in database
GPG Key ID: 3D1DC5AFDA57B32E

View File

@ -1,8 +1,8 @@
#[cfg(windows)] #[cfg(windows)]
use std::{ use std::{
collections::{HashMap, VecDeque}, collections::VecDeque,
str::FromStr, str::FromStr,
sync::Mutex, sync::{Arc, Mutex},
}; };
use lazy_static::lazy_static; use lazy_static::lazy_static;
@ -17,7 +17,9 @@ use windows::{
}, },
}; };
use crate::{Backend, BackendId, Error, Features, Gender, UtteranceId, Voice, CALLBACKS}; use crate::{
Backend, BackendId, Callbacks, Error, Features, Gender, UtteranceId, Voice, CALLBACKS,
};
impl From<windows::core::Error> for Error { impl From<windows::core::Error> for Error {
fn from(e: windows::core::Error) -> Self { fn from(e: windows::core::Error) -> Self {
@ -28,14 +30,16 @@ impl From<windows::core::Error> for Error {
#[derive(Clone)] #[derive(Clone)]
pub struct WinRt { pub struct WinRt {
id: BackendId, id: BackendId,
synth: SpeechSynthesizer, synth: Arc<SpeechSynthesizer>,
player: MediaPlayer, player: MediaPlayer,
utterances: Arc<Mutex<VecDeque<Utterance>>>,
rate: f32, rate: f32,
pitch: f32, pitch: f32,
volume: f32, volume: f32,
voice: VoiceInformation, voice: VoiceInformation,
} }
#[derive(Debug)]
struct Utterance { struct Utterance {
id: UtteranceId, id: UtteranceId,
text: String, text: String,
@ -45,99 +49,85 @@ struct Utterance {
voice: VoiceInformation, voice: VoiceInformation,
} }
impl Utterance {
fn speak(
&self,
synth: &SpeechSynthesizer,
player: &MediaPlayer,
callbacks: &mut Callbacks,
) -> Result<(), windows::core::Error> {
synth.Options()?.SetSpeakingRate(self.rate.into())?;
synth.Options()?.SetAudioPitch(self.pitch.into())?;
synth.Options()?.SetAudioVolume(self.volume.into())?;
synth.SetVoice(&self.voice)?;
let stream = synth
.SynthesizeTextToStreamAsync(&self.text.clone().into())?
.get()?;
let content_type = stream.ContentType()?;
let source = MediaSource::CreateFromStream(&stream, &content_type)?;
player.SetSource(&source)?;
player.Play()?;
if let Some(callback) = callbacks.utterance_begin.as_mut() {
callback(self.id);
}
Ok(())
}
}
lazy_static! { lazy_static! {
static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0); static ref NEXT_BACKEND_ID: Mutex<u64> = Mutex::new(0);
static ref NEXT_UTTERANCE_ID: Mutex<u64> = Mutex::new(0); static ref NEXT_UTTERANCE_ID: Mutex<u64> = Mutex::new(0);
static ref BACKEND_TO_SPEECH_SYNTHESIZER: Mutex<HashMap<BackendId, SpeechSynthesizer>> = {
let v: HashMap<BackendId, SpeechSynthesizer> = HashMap::new();
Mutex::new(v)
};
static ref BACKEND_TO_MEDIA_PLAYER: Mutex<HashMap<BackendId, MediaPlayer>> = {
let v: HashMap<BackendId, MediaPlayer> = HashMap::new();
Mutex::new(v)
};
static ref UTTERANCES: Mutex<HashMap<BackendId, VecDeque<Utterance>>> = {
let utterances: HashMap<BackendId, VecDeque<Utterance>> = HashMap::new();
Mutex::new(utterances)
};
} }
impl WinRt { impl WinRt {
pub fn new() -> std::result::Result<Self, Error> { pub fn new() -> std::result::Result<Self, Error> {
info!("Initializing WinRT backend"); info!("Initializing WinRT backend");
let synth = SpeechSynthesizer::new()?;
let player = MediaPlayer::new()?; let player = MediaPlayer::new()?;
player.SetRealTimePlayback(true)?; player.SetRealTimePlayback(true)?;
player.SetAudioCategory(MediaPlayerAudioCategory::Speech)?; player.SetAudioCategory(MediaPlayerAudioCategory::Speech)?;
let mut backend_id = NEXT_BACKEND_ID.lock().unwrap();
let bid = BackendId::WinRt(*backend_id); let bid = {
*backend_id += 1; let mut backend_id = NEXT_BACKEND_ID.lock().unwrap();
drop(backend_id); let bid = BackendId::WinRt(*backend_id);
{ *backend_id += 1;
let mut utterances = UTTERANCES.lock().unwrap();
utterances.insert(bid, VecDeque::new()); bid
} };
let mut backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap();
backend_to_media_player.insert(bid, player.clone()); let tts = Self {
drop(backend_to_media_player);
let mut backend_to_speech_synthesizer = BACKEND_TO_SPEECH_SYNTHESIZER.lock().unwrap();
backend_to_speech_synthesizer.insert(bid, synth.clone());
drop(backend_to_speech_synthesizer);
let bid_clone = bid;
player.MediaEnded(&TypedEventHandler::new(
move |sender: &Option<MediaPlayer>, _args| {
if let Some(sender) = sender {
let backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap();
let id = backend_to_media_player.iter().find(|v| v.1 == sender);
if let Some((id, _)) = id {
let mut utterances = UTTERANCES.lock().unwrap();
if let Some(utterances) = utterances.get_mut(id) {
if let Some(utterance) = utterances.pop_front() {
let mut callbacks = CALLBACKS.lock().unwrap();
let callbacks = callbacks.get_mut(id).unwrap();
if let Some(callback) = callbacks.utterance_end.as_mut() {
callback(utterance.id);
}
if let Some(utterance) = utterances.front() {
let backend_to_speech_synthesizer =
BACKEND_TO_SPEECH_SYNTHESIZER.lock().unwrap();
let id = backend_to_speech_synthesizer
.iter()
.find(|v| *v.0 == bid_clone);
if let Some((_, tts)) = id {
tts.Options()?.SetSpeakingRate(utterance.rate.into())?;
tts.Options()?.SetAudioPitch(utterance.pitch.into())?;
tts.Options()?.SetAudioVolume(utterance.volume.into())?;
tts.SetVoice(&utterance.voice)?;
let text = &utterance.text;
let stream =
tts.SynthesizeTextToStreamAsync(&text.into())?.get()?;
let content_type = stream.ContentType()?;
let source =
MediaSource::CreateFromStream(&stream, &content_type)?;
sender.SetSource(&source)?;
sender.Play()?;
if let Some(callback) = callbacks.utterance_begin.as_mut() {
callback(utterance.id);
}
}
}
}
}
}
}
Ok(())
},
))?;
Ok(Self {
id: bid, id: bid,
synth, synth: Arc::new(SpeechSynthesizer::new()?),
player, player,
utterances: Arc::new(Mutex::new(VecDeque::new())),
rate: 1., rate: 1.,
pitch: 1., pitch: 1.,
volume: 1., volume: 1.,
voice: SpeechSynthesizer::DefaultVoice()?, voice: SpeechSynthesizer::DefaultVoice()?,
}) };
let synth_clone = tts.synth.clone();
let utterances_clone = tts.utterances.clone();
tts.player.MediaEnded(&TypedEventHandler::new(
move |player: &Option<MediaPlayer>, _args| {
utterances_clone.lock().unwrap().pop_front(); // Utterance that just ended
if let Some(utterance) = utterances_clone.lock().unwrap().front() {
utterance.speak(
&synth_clone,
player.as_ref().unwrap(),
CALLBACKS.lock().unwrap().get_mut(&bid).unwrap(),
)?;
}
Ok(())
},
))?;
Ok(tts)
} }
} }
@ -167,47 +157,32 @@ impl Backend for WinRt {
if interrupt && self.is_speaking()? { if interrupt && self.is_speaking()? {
self.stop()?; self.stop()?;
} }
let utterance_id = { let utterance_id = {
let mut uid = NEXT_UTTERANCE_ID.lock().unwrap(); let mut uid = NEXT_UTTERANCE_ID.lock().unwrap();
let utterance_id = UtteranceId::WinRt(*uid); let utterance_id = UtteranceId::WinRt(*uid);
*uid += 1; *uid += 1;
utterance_id utterance_id
}; };
let mut no_utterances = false;
{ let utterance = Utterance {
let mut utterances = UTTERANCES.lock().unwrap(); id: utterance_id,
if let Some(utterances) = utterances.get_mut(&self.id) { text: text.to_string(),
no_utterances = utterances.is_empty(); rate: self.rate,
let utterance = Utterance { pitch: self.pitch,
id: utterance_id, volume: self.volume,
text: text.into(), voice: self.voice.clone(),
rate: self.rate, };
pitch: self.pitch,
volume: self.volume, if !self.is_speaking()? {
voice: self.voice.clone(), utterance.speak(
}; &self.synth,
utterances.push_back(utterance); &self.player,
} CALLBACKS.lock().unwrap().get_mut(&self.id).unwrap(),
} )?;
if no_utterances {
self.synth.Options()?.SetSpeakingRate(self.rate.into())?;
self.synth.Options()?.SetAudioPitch(self.pitch.into())?;
self.synth.Options()?.SetAudioVolume(self.volume.into())?;
self.synth.SetVoice(&self.voice)?;
let stream = self
.synth
.SynthesizeTextToStreamAsync(&text.into())?
.get()?;
let content_type = stream.ContentType()?;
let source = MediaSource::CreateFromStream(&stream, &content_type)?;
self.player.SetSource(&source)?;
self.player.Play()?;
let mut callbacks = CALLBACKS.lock().unwrap();
let callbacks = callbacks.get_mut(&self.id).unwrap();
if let Some(callback) = callbacks.utterance_begin.as_mut() {
callback(utterance_id);
}
} }
self.utterances.lock().unwrap().push_back(utterance);
Ok(Some(utterance_id)) Ok(Some(utterance_id))
} }
@ -216,19 +191,16 @@ impl Backend for WinRt {
if !self.is_speaking()? { if !self.is_speaking()? {
return Ok(()); return Ok(());
} }
let mut utterances = UTTERANCES.lock().unwrap(); let mut utterances = self.utterances.lock().unwrap();
if let Some(utterances) = utterances.get(&self.id) { let mut callbacks = CALLBACKS.lock().unwrap();
let mut callbacks = CALLBACKS.lock().unwrap(); let callbacks = callbacks.get_mut(&self.id).unwrap();
let callbacks = callbacks.get_mut(&self.id).unwrap(); if let Some(callback) = callbacks.utterance_stop.as_mut() {
if let Some(callback) = callbacks.utterance_stop.as_mut() { let utterances = utterances.iter();
for utterance in utterances { for utterance in utterances {
callback(utterance.id); callback(utterance.id);
}
} }
} }
if let Some(utterances) = utterances.get_mut(&self.id) { utterances.clear();
utterances.clear();
}
self.player.Pause()?; self.player.Pause()?;
Ok(()) Ok(())
} }
@ -246,8 +218,7 @@ impl Backend for WinRt {
} }
fn get_rate(&self) -> std::result::Result<f32, Error> { fn get_rate(&self) -> std::result::Result<f32, Error> {
let rate = self.synth.Options()?.SpeakingRate()?; Ok(self.rate)
Ok(rate as f32)
} }
fn set_rate(&mut self, rate: f32) -> std::result::Result<(), Error> { fn set_rate(&mut self, rate: f32) -> std::result::Result<(), Error> {
@ -268,8 +239,7 @@ impl Backend for WinRt {
} }
fn get_pitch(&self) -> std::result::Result<f32, Error> { fn get_pitch(&self) -> std::result::Result<f32, Error> {
let pitch = self.synth.Options()?.AudioPitch()?; Ok(self.pitch)
Ok(pitch as f32)
} }
fn set_pitch(&mut self, pitch: f32) -> std::result::Result<(), Error> { fn set_pitch(&mut self, pitch: f32) -> std::result::Result<(), Error> {
@ -290,8 +260,7 @@ impl Backend for WinRt {
} }
fn get_volume(&self) -> std::result::Result<f32, Error> { fn get_volume(&self) -> std::result::Result<f32, Error> {
let volume = self.synth.Options()?.AudioVolume()?; Ok(self.volume)
Ok(volume as f32)
} }
fn set_volume(&mut self, volume: f32) -> std::result::Result<(), Error> { fn set_volume(&mut self, volume: f32) -> std::result::Result<(), Error> {
@ -300,21 +269,17 @@ impl Backend for WinRt {
} }
fn is_speaking(&self) -> std::result::Result<bool, Error> { fn is_speaking(&self) -> std::result::Result<bool, Error> {
let utterances = UTTERANCES.lock().unwrap(); Ok(!self.utterances.lock().unwrap().is_empty())
let utterances = utterances.get(&self.id).unwrap();
Ok(!utterances.is_empty())
} }
fn voice(&self) -> Result<Option<Voice>, Error> { fn voice(&self) -> Result<Option<Voice>, Error> {
let voice = self.synth.Voice()?; Ok(Some((&self.voice).try_into()?))
let voice = voice.try_into()?;
Ok(Some(voice))
} }
fn voices(&self) -> Result<Vec<Voice>, Error> { fn voices(&self) -> Result<Vec<Voice>, Error> {
let mut rv: Vec<Voice> = vec![]; let mut rv: Vec<Voice> = vec![];
for voice in SpeechSynthesizer::AllVoices()? { for voice in SpeechSynthesizer::AllVoices()? {
rv.push(voice.try_into()?); rv.push((&voice).try_into()?);
} }
Ok(rv) Ok(rv)
} }
@ -331,19 +296,7 @@ impl Backend for WinRt {
} }
} }
impl Drop for WinRt { impl TryInto<Voice> for &VoiceInformation {
fn drop(&mut self) {
let id = self.id;
let mut backend_to_media_player = BACKEND_TO_MEDIA_PLAYER.lock().unwrap();
backend_to_media_player.remove(&id);
let mut backend_to_speech_synthesizer = BACKEND_TO_SPEECH_SYNTHESIZER.lock().unwrap();
backend_to_speech_synthesizer.remove(&id);
let mut utterances = UTTERANCES.lock().unwrap();
utterances.remove(&id);
}
}
impl TryInto<Voice> for VoiceInformation {
type Error = Error; type Error = Error;
fn try_into(self) -> Result<Voice, Self::Error> { fn try_into(self) -> Result<Voice, Self::Error> {