Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash of concurrent Win TTS synthesis #223

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `ActivateGroup` API which allows to activate groups with late activation.
- Added `DestroyGroup` API which removes the entire group from the game world.
- `DestroyUnit` API

### Fixed
- Fixed `MarkAddEvent`, `MarkChangeEvent` and `MarkRemoveEvent` position
- Fixed crash of concurrent Windows TTS synthesis ([#223](https://github.com/DCS-gRPC/rust-server/issues/223))

## [0.7.1] - 2023-01-08

Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ log = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
tokio = { version = "1.24", features = ["rt-multi-thread", "io-util", "net", "sync", "time"] }
tokio = { version = "1.24", features = ["rt-multi-thread", "io-util", "net", "sync", "time", "parking_lot"] }
tokio-stream = { version = "0.1", features = ["sync"] }
tonic = "0.8"

Expand Down
8 changes: 8 additions & 0 deletions tts/src/win.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::borrow::Cow;

use tokio::sync::Mutex;
use windows::core::HSTRING;
use windows::Media::SpeechSynthesis::SpeechSynthesizer;
use windows::Storage::Streams::DataReader;
Expand All @@ -9,10 +10,15 @@ pub struct WinConfig {
pub voice: Option<String>,
}

static MUTEX: Mutex<()> = Mutex::const_new(());

pub async fn synthesize(text: &str, config: &WinConfig) -> Result<Vec<Vec<u8>>, WinError> {
// Note, there does not seem to be a way to explicitly set 16000kHz, 16 audio bits per
// sample and mono channel.

// Prevent concurrent Windows TTS synthesis, as this might cause a crash.
let lock = MUTEX.lock().await;
rurounijones marked this conversation as resolved.
Show resolved Hide resolved

let mut voice_info = None;
if let Some(voice) = &config.voice {
let all_voices = SpeechSynthesizer::AllVoices()?;
Expand Down Expand Up @@ -91,6 +97,8 @@ pub async fn synthesize(text: &str, config: &WinConfig) -> Result<Vec<Vec<u8>>,
let mut wav = vec![0u8; size as usize];
rd.ReadBytes(wav.as_mut_slice())?;

drop(lock);

Ok(crate::wav_to_opus(wav.into()).await?)
}

Expand Down