Skip to content

Commit 548e2f3

Browse files
authored
feat: implement Send and Sync traits for TTS and AudioTag structs (#77)
* feat: implement Send and Sync traits for TTS and AudioTag structs * bump: update sherpa-rs and sherpa-rs-sys versions to 0.6.4
1 parent f59f4c1 commit 548e2f3

File tree

8 files changed

+60
-12
lines changed

8 files changed

+60
-12
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sherpa-rs-sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sherpa-rs-sys"
3-
version = "0.6.3"
3+
version = "0.6.4"
44
edition = "2021"
55
authors = ["thewh1teagle"]
66
homepage = "https://github.com/thewh1teagle/sherpa-rs"

crates/sherpa-rs/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sherpa-rs"
3-
version = "0.6.3"
3+
version = "0.6.4"
44
edition = "2021"
55
authors = ["thewh1teagle"]
66
license = "MIT"
@@ -21,7 +21,7 @@ crate-type = ["cdylib", "rlib"]
2121
[dependencies]
2222
eyre = "0.6.12"
2323
hound = { version = "3.5.1" }
24-
sherpa-rs-sys = { path = "../sherpa-rs-sys", version = "0.6.3", default-features = false }
24+
sherpa-rs-sys = { path = "../sherpa-rs-sys", version = "0.6.4", default-features = false }
2525
tracing = "0.1.40"
2626

2727
[dev-dependencies]

crates/sherpa-rs/src/audio_tag.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl AudioTag {
4646
let audio_tag = unsafe { sherpa_rs_sys::SherpaOnnxCreateAudioTagging(&sherpa_config) };
4747

4848
if audio_tag.is_null() {
49-
bail!("Failed to create audio tagging")
49+
bail!("Failed to create audio tagging");
5050
}
5151
Ok(Self {
5252
audio_tag,
@@ -64,6 +64,7 @@ impl AudioTag {
6464
samples.as_ptr(),
6565
samples.len() as i32,
6666
);
67+
6768
let results = sherpa_rs_sys::SherpaOnnxAudioTaggingCompute(
6869
self.audio_tag,
6970
stream,
@@ -75,7 +76,20 @@ impl AudioTag {
7576
let event_name = cstr_to_string((*event).name);
7677
events.push(event_name);
7778
}
79+
80+
sherpa_rs_sys::SherpaOnnxDestroyOfflineStream(stream);
7881
}
7982
events
8083
}
8184
}
85+
86+
unsafe impl Send for AudioTag {}
87+
unsafe impl Sync for AudioTag {}
88+
89+
impl Drop for AudioTag {
90+
fn drop(&mut self) {
91+
unsafe {
92+
sherpa_rs_sys::SherpaOnnxDestroyAudioTagging(self.audio_tag);
93+
}
94+
}
95+
}

crates/sherpa-rs/src/diarize.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{get_default_provider, utils::RawCStr};
22
use eyre::{bail, Result};
3-
use std::path::Path;
3+
use std::{path::Path, ptr::null_mut};
44

55
#[derive(Debug)]
66
pub struct Diarize {
@@ -14,7 +14,7 @@ pub struct Segment {
1414
pub speaker: i32,
1515
}
1616

17-
type ProgressCallback = Box<dyn Fn(i32, i32) -> i32 + Send + 'static>;
17+
type ProgressCallback = Box<dyn (Fn(i32, i32) -> i32) + Send + 'static>;
1818

1919
#[derive(Debug, Clone)]
2020
pub struct DiarizeConfig {
@@ -85,7 +85,7 @@ impl Diarize {
8585
let sd = unsafe { sherpa_rs_sys::SherpaOnnxCreateOfflineSpeakerDiarization(&config) };
8686

8787
if sd.is_null() {
88-
bail!("Failed to initialize offline speaker diarization")
88+
bail!("Failed to initialize offline speaker diarization");
8989
}
9090
Ok(Self { sd })
9191
}
@@ -103,7 +103,7 @@ impl Diarize {
103103
let callback_ptr = callback_box
104104
.as_mut()
105105
.map(|b| b.as_mut() as *mut ProgressCallback as *mut std::ffi::c_void)
106-
.unwrap_or(std::ptr::null_mut());
106+
.unwrap_or(null_mut());
107107

108108
let result = sherpa_rs_sys::SherpaOnnxOfflineSpeakerDiarizationProcessWithCallback(
109109
self.sd,
@@ -123,8 +123,9 @@ impl Diarize {
123123
sherpa_rs_sys::SherpaOnnxOfflineSpeakerDiarizationResultSortByStartTime(result);
124124

125125
if !segments_ptr.is_null() && num_segments > 0 {
126-
let segments_result: &[sherpa_rs_sys::SherpaOnnxOfflineSpeakerDiarizationSegment] =
127-
std::slice::from_raw_parts(segments_ptr, num_segments as usize);
126+
let segments_result: &[
127+
sherpa_rs_sys::SherpaOnnxOfflineSpeakerDiarizationSegment
128+
] = std::slice::from_raw_parts(segments_ptr, num_segments as usize);
128129

129130
for segment in segments_result {
130131
// Use segment here

crates/sherpa-rs/src/tts/kokoro.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,14 @@ impl KokoroTts {
6363
unsafe { super::create(self.tts, text, sid, speed) }
6464
}
6565
}
66+
67+
unsafe impl Send for KokoroTts {}
68+
unsafe impl Sync for KokoroTts {}
69+
70+
impl Drop for KokoroTts {
71+
fn drop(&mut self) {
72+
unsafe {
73+
sherpa_rs_sys::SherpaOnnxDestroyOfflineTts(self.tts);
74+
}
75+
}
76+
}

crates/sherpa-rs/src/tts/matcha.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,14 @@ impl MatchaTts {
7575
unsafe { super::create(self.tts, text, sid, speed) }
7676
}
7777
}
78+
79+
unsafe impl Send for MatchaTts {}
80+
unsafe impl Sync for MatchaTts {}
81+
82+
impl Drop for MatchaTts {
83+
fn drop(&mut self) {
84+
unsafe {
85+
sherpa_rs_sys::SherpaOnnxDestroyOfflineTts(self.tts);
86+
}
87+
}
88+
}

crates/sherpa-rs/src/tts/vits.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,14 @@ impl VitsTts {
7171
unsafe { super::create(self.tts, text, sid, speed) }
7272
}
7373
}
74+
75+
unsafe impl Send for VitsTts {}
76+
unsafe impl Sync for VitsTts {}
77+
78+
impl Drop for VitsTts {
79+
fn drop(&mut self) {
80+
unsafe {
81+
sherpa_rs_sys::SherpaOnnxDestroyOfflineTts(self.tts);
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)