From b54a96564f3f0ec7d5a5ab030bcbb4c162887a42 Mon Sep 17 00:00:00 2001 From: thewh1teagle <61390950+thewh1teagle@users.noreply.github.com> Date: Sun, 19 Jan 2025 00:34:17 +0200 Subject: [PATCH 1/2] feat: implement Send and Sync traits for TTS and AudioTag structs --- crates/sherpa-rs/src/audio_tag.rs | 16 +++++++++++++++- crates/sherpa-rs/src/diarize.rs | 13 +++++++------ crates/sherpa-rs/src/tts/kokoro.rs | 11 +++++++++++ crates/sherpa-rs/src/tts/matcha.rs | 11 +++++++++++ crates/sherpa-rs/src/tts/vits.rs | 11 +++++++++++ 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/crates/sherpa-rs/src/audio_tag.rs b/crates/sherpa-rs/src/audio_tag.rs index 5e7b4f6..a6dd120 100644 --- a/crates/sherpa-rs/src/audio_tag.rs +++ b/crates/sherpa-rs/src/audio_tag.rs @@ -46,7 +46,7 @@ impl AudioTag { let audio_tag = unsafe { sherpa_rs_sys::SherpaOnnxCreateAudioTagging(&sherpa_config) }; if audio_tag.is_null() { - bail!("Failed to create audio tagging") + bail!("Failed to create audio tagging"); } Ok(Self { audio_tag, @@ -64,6 +64,7 @@ impl AudioTag { samples.as_ptr(), samples.len() as i32, ); + let results = sherpa_rs_sys::SherpaOnnxAudioTaggingCompute( self.audio_tag, stream, @@ -75,7 +76,20 @@ impl AudioTag { let event_name = cstr_to_string((*event).name); events.push(event_name); } + + sherpa_rs_sys::SherpaOnnxDestroyOfflineStream(stream); } events } } + +unsafe impl Send for AudioTag {} +unsafe impl Sync for AudioTag {} + +impl Drop for AudioTag { + fn drop(&mut self) { + unsafe { + sherpa_rs_sys::SherpaOnnxDestroyAudioTagging(self.audio_tag); + } + } +} diff --git a/crates/sherpa-rs/src/diarize.rs b/crates/sherpa-rs/src/diarize.rs index 6b2a4c7..43336e3 100644 --- a/crates/sherpa-rs/src/diarize.rs +++ b/crates/sherpa-rs/src/diarize.rs @@ -1,6 +1,6 @@ use crate::{get_default_provider, utils::RawCStr}; use eyre::{bail, Result}; -use std::path::Path; +use std::{path::Path, ptr::null_mut}; #[derive(Debug)] pub struct Diarize { @@ -14,7 +14,7 @@ pub struct Segment { pub speaker: i32, } -type ProgressCallback = Box i32 + Send + 'static>; +type ProgressCallback = Box i32) + Send + 'static>; #[derive(Debug, Clone)] pub struct DiarizeConfig { @@ -85,7 +85,7 @@ impl Diarize { let sd = unsafe { sherpa_rs_sys::SherpaOnnxCreateOfflineSpeakerDiarization(&config) }; if sd.is_null() { - bail!("Failed to initialize offline speaker diarization") + bail!("Failed to initialize offline speaker diarization"); } Ok(Self { sd }) } @@ -103,7 +103,7 @@ impl Diarize { let callback_ptr = callback_box .as_mut() .map(|b| b.as_mut() as *mut ProgressCallback as *mut std::ffi::c_void) - .unwrap_or(std::ptr::null_mut()); + .unwrap_or(null_mut()); let result = sherpa_rs_sys::SherpaOnnxOfflineSpeakerDiarizationProcessWithCallback( self.sd, @@ -123,8 +123,9 @@ impl Diarize { sherpa_rs_sys::SherpaOnnxOfflineSpeakerDiarizationResultSortByStartTime(result); if !segments_ptr.is_null() && num_segments > 0 { - let segments_result: &[sherpa_rs_sys::SherpaOnnxOfflineSpeakerDiarizationSegment] = - std::slice::from_raw_parts(segments_ptr, num_segments as usize); + let segments_result: &[ + sherpa_rs_sys::SherpaOnnxOfflineSpeakerDiarizationSegment + ] = std::slice::from_raw_parts(segments_ptr, num_segments as usize); for segment in segments_result { // Use segment here diff --git a/crates/sherpa-rs/src/tts/kokoro.rs b/crates/sherpa-rs/src/tts/kokoro.rs index 5460537..248988e 100644 --- a/crates/sherpa-rs/src/tts/kokoro.rs +++ b/crates/sherpa-rs/src/tts/kokoro.rs @@ -63,3 +63,14 @@ impl KokoroTts { unsafe { super::create(self.tts, text, sid, speed) } } } + +unsafe impl Send for KokoroTts {} +unsafe impl Sync for KokoroTts {} + +impl Drop for KokoroTts { + fn drop(&mut self) { + unsafe { + sherpa_rs_sys::SherpaOnnxDestroyOfflineTts(self.tts); + } + } +} diff --git a/crates/sherpa-rs/src/tts/matcha.rs b/crates/sherpa-rs/src/tts/matcha.rs index 5194322..ffe9e6a 100644 --- a/crates/sherpa-rs/src/tts/matcha.rs +++ b/crates/sherpa-rs/src/tts/matcha.rs @@ -75,3 +75,14 @@ impl MatchaTts { unsafe { super::create(self.tts, text, sid, speed) } } } + +unsafe impl Send for MatchaTts {} +unsafe impl Sync for MatchaTts {} + +impl Drop for MatchaTts { + fn drop(&mut self) { + unsafe { + sherpa_rs_sys::SherpaOnnxDestroyOfflineTts(self.tts); + } + } +} diff --git a/crates/sherpa-rs/src/tts/vits.rs b/crates/sherpa-rs/src/tts/vits.rs index adf84af..4e4f74f 100644 --- a/crates/sherpa-rs/src/tts/vits.rs +++ b/crates/sherpa-rs/src/tts/vits.rs @@ -71,3 +71,14 @@ impl VitsTts { unsafe { super::create(self.tts, text, sid, speed) } } } + +unsafe impl Send for VitsTts {} +unsafe impl Sync for VitsTts {} + +impl Drop for VitsTts { + fn drop(&mut self) { + unsafe { + sherpa_rs_sys::SherpaOnnxDestroyOfflineTts(self.tts); + } + } +} From 7dc8ac58deca76cb37fc71913350926012da633c Mon Sep 17 00:00:00 2001 From: thewh1teagle <61390950+thewh1teagle@users.noreply.github.com> Date: Sun, 19 Jan 2025 00:35:30 +0200 Subject: [PATCH 2/2] bump: update sherpa-rs and sherpa-rs-sys versions to 0.6.4 --- Cargo.lock | 4 ++-- crates/sherpa-rs-sys/Cargo.toml | 2 +- crates/sherpa-rs/Cargo.toml | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f82f75..d53d1ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -731,7 +731,7 @@ dependencies = [ [[package]] name = "sherpa-rs" -version = "0.6.3" +version = "0.6.4" dependencies = [ "clap", "eyre", @@ -742,7 +742,7 @@ dependencies = [ [[package]] name = "sherpa-rs-sys" -version = "0.6.3" +version = "0.6.4" dependencies = [ "bindgen", "bzip2", diff --git a/crates/sherpa-rs-sys/Cargo.toml b/crates/sherpa-rs-sys/Cargo.toml index e245e3a..713993d 100644 --- a/crates/sherpa-rs-sys/Cargo.toml +++ b/crates/sherpa-rs-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sherpa-rs-sys" -version = "0.6.3" +version = "0.6.4" edition = "2021" authors = ["thewh1teagle"] homepage = "https://github.com/thewh1teagle/sherpa-rs" diff --git a/crates/sherpa-rs/Cargo.toml b/crates/sherpa-rs/Cargo.toml index 51c847b..a7c2418 100644 --- a/crates/sherpa-rs/Cargo.toml +++ b/crates/sherpa-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sherpa-rs" -version = "0.6.3" +version = "0.6.4" edition = "2021" authors = ["thewh1teagle"] license = "MIT" @@ -21,7 +21,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] eyre = "0.6.12" hound = { version = "3.5.1" } -sherpa-rs-sys = { path = "../sherpa-rs-sys", version = "0.6.3", default-features = false } +sherpa-rs-sys = { path = "../sherpa-rs-sys", version = "0.6.4", default-features = false } tracing = "0.1.40" [dev-dependencies]