Skip to content

Commit 577698d

Browse files
committed
feat(server): add mute tts model
1 parent 8263cdb commit 577698d

File tree

7 files changed

+71
-3
lines changed

7 files changed

+71
-3
lines changed

apps/server/api/src/config/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ pub enum TtsModel {
484484
#[default]
485485
Kokoro,
486486
Voxcpm,
487+
Mute,
487488
}
488489

489490
#[derive(Clone, Debug, Deserialize, PartialEq, Default)]

apps/server/api/src/tts/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::common::ModelError;
55
use crate::config;
66
use crate::config::audio::AudioConfig;
77
use crate::config::tts::TtsConfig;
8+
use crate::tts::model::mute::TtsMute;
89
use async_trait::async_trait;
910
use futures::Stream;
1011
use model::kokoro::TtsKokoro;
@@ -23,7 +24,7 @@ pub trait Tts: Send + Sync {
2324
}
2425

2526
pub struct TtsData {
26-
pub audio: Vec<Vec<u8>>,
27+
pub audio: Option<Vec<Vec<u8>>>,
2728
pub text: String,
2829
}
2930

@@ -107,6 +108,7 @@ impl TtsFactory {
107108
)
108109
.await?,
109110
)),
111+
config::TtsModel::Mute => Ok(Box::new(TtsMute::new().await?)),
110112
}
111113
}
112114

apps/server/api/src/tts/model/kokoro/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl Tts for TtsKokoro {
8181
encode_frame_duration,
8282
);
8383
let data = TtsData {
84-
audio,
84+
audio: Some(audio),
8585
text: text.to_string(),
8686
};
8787
if let Err(e) = tx.send(Ok(data)).await {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod kokoro;
2+
pub mod mute;
23
pub mod voxcpm;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use crate::common::ModelError;
2+
use crate::tts::{Tts, TtsData, TtsError};
3+
use async_trait::async_trait;
4+
use futures::Stream;
5+
use futures::executor::block_on;
6+
use std::pin::Pin;
7+
use std::thread;
8+
use tokio::sync::mpsc::channel;
9+
use tokio_stream::StreamExt;
10+
use tokio_stream::wrappers::ReceiverStream;
11+
use tracing::{debug, error};
12+
13+
pub struct TtsMute {}
14+
15+
impl TtsMute {
16+
pub async fn new() -> Result<Self, anyhow::Error> {
17+
Ok(Self {})
18+
}
19+
}
20+
21+
#[async_trait]
22+
impl Tts for TtsMute {
23+
async fn stream(
24+
&self,
25+
mut text_stream: Pin<
26+
Box<dyn Stream<Item = core::result::Result<String, ModelError>> + Send + Sync>,
27+
>,
28+
) -> Pin<Box<dyn Stream<Item = core::result::Result<TtsData, TtsError>> + Send + Sync>> {
29+
let (tx, rx) = channel(10);
30+
thread::spawn(move || {
31+
block_on(async move {
32+
while let Some(text) = text_stream.next().await {
33+
// let instance = instance.clone();
34+
let tx = tx.clone();
35+
match &text {
36+
Ok(text) => {
37+
debug!("[TTS] receive, text = {}", text);
38+
let data = TtsData {
39+
audio: None,
40+
text: text.to_string(),
41+
};
42+
if let Err(e) = tx.send(Ok(data)).await {
43+
error!("output packet error = {}", e);
44+
break;
45+
} else {
46+
debug!("[TTS] encode and send audio success");
47+
}
48+
}
49+
Err(e) => {
50+
error!("tts text stream error = {}", e.to_string());
51+
if let Err(e) = tx.send(Err(TtsError::Text(e.to_string()))).await {
52+
error!("send error failure = {}", e);
53+
}
54+
break;
55+
}
56+
}
57+
}
58+
drop(tx);
59+
})
60+
});
61+
Box::pin(ReceiverStream::new(rx))
62+
}
63+
}

apps/server/api/src/tts/model/voxcpm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl Tts for TtsVoxCPM {
9494
encode_frame_duration,
9595
);
9696
let data = TtsData {
97-
audio,
97+
audio: Some(audio),
9898
text: text.to_string(),
9999
};
100100
if let Err(e) = tx.send(Ok(data)).await {

apps/server/api/src/ws/session/round.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ impl Round {
210210
.await?;
211211
//audio
212212
//real time send audio
213+
let audio_data = audio_data.unwrap_or_default();
213214
let data = audio_data.into_iter();
214215
speaking.store(true, Ordering::Relaxed);
215216
debug!("set speaking = true");

0 commit comments

Comments
 (0)