generated from milosgajdos/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We introduce a tts module that handles TTS tasks of rustbot. We had to revamp some thing around such as the llm having to send the chunks to both to TTS and the jet.Reader. We're using rodio for playing the speech. Signed-off-by: Milos Gajdos <[email protected]>
- Loading branch information
1 parent
a4347c6
commit 992dec6
Showing
7 changed files
with
239 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use bytes::{BufMut, Bytes, BytesMut}; | ||
use std::error::Error; | ||
use std::fmt; | ||
|
||
#[derive(Debug)] | ||
pub struct BufferFullError { | ||
pub bytes_written: usize, | ||
} | ||
|
||
impl fmt::Display for BufferFullError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "buffer is full, {} bytes written", self.bytes_written) | ||
} | ||
} | ||
|
||
impl Error for BufferFullError {} | ||
|
||
pub struct Buffer { | ||
buffer: BytesMut, | ||
max_size: usize, | ||
} | ||
|
||
impl Buffer { | ||
pub fn new(max_size: usize) -> Self { | ||
Buffer { | ||
buffer: BytesMut::with_capacity(max_size), | ||
max_size, | ||
} | ||
} | ||
|
||
pub fn write(&mut self, data: &[u8]) -> Result<usize, BufferFullError> { | ||
let available = self.max_size - self.buffer.len(); | ||
let write_len = std::cmp::min(data.len(), available); | ||
|
||
self.buffer.put_slice(&data[..write_len]); | ||
|
||
if self.buffer.len() == self.max_size { | ||
return Err(BufferFullError { | ||
bytes_written: write_len, | ||
}); | ||
} | ||
Ok(write_len) | ||
} | ||
|
||
pub fn reset(&mut self) { | ||
self.buffer.clear(); | ||
} | ||
|
||
pub fn as_bytes(&self) -> Bytes { | ||
self.buffer.clone().freeze() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use crate::{buffer, prelude::*}; | ||
use bytes::Bytes; | ||
use playht_rs::api::{self, stream::TTSStreamReq, tts::Quality}; | ||
use tokio::{self, sync::mpsc::Receiver, sync::watch}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Config { | ||
pub voice_id: Option<String>, | ||
pub quality: Option<Quality>, | ||
pub speed: Option<f32>, | ||
pub sample_rate: Option<i32>, | ||
pub buf_size: usize, | ||
} | ||
|
||
impl Default for Config { | ||
fn default() -> Self { | ||
Config { | ||
voice_id: Some(DEFAULT_VOICE_ID.to_string()), | ||
quality: Some(Quality::Low), | ||
speed: Some(1.0), | ||
sample_rate: Some(24000), | ||
buf_size: MAX_TTS_BUFFER_SIZE, | ||
} | ||
} | ||
} | ||
|
||
pub struct TTS { | ||
client: api::Client, | ||
config: Config, | ||
} | ||
|
||
impl TTS { | ||
pub fn new(c: Config) -> TTS { | ||
TTS { | ||
client: api::Client::new(), | ||
config: c, | ||
} | ||
} | ||
|
||
pub async fn stream<W>( | ||
self, | ||
w: &mut W, | ||
mut chunks: Receiver<Bytes>, | ||
mut done: watch::Receiver<bool>, | ||
) -> Result<()> | ||
where | ||
W: tokio::io::AsyncWriteExt + Unpin, | ||
{ | ||
println!("launching TTS stream"); | ||
let mut buf = buffer::Buffer::new(self.config.buf_size); | ||
let mut req = TTSStreamReq { | ||
voice: self.config.voice_id, | ||
quality: self.config.quality, | ||
speed: self.config.speed, | ||
sample_rate: self.config.sample_rate, | ||
..Default::default() | ||
}; | ||
|
||
loop { | ||
tokio::select! { | ||
_ = done.changed() => { | ||
if *done.borrow() { | ||
return Ok(()) | ||
} | ||
}, | ||
Some(chunk) = chunks.recv() => { | ||
if chunk.is_empty() { | ||
let text = String::from_utf8(buf.as_bytes().to_vec())?; | ||
req.text = Some(text); | ||
self.client.write_audio_stream(w, &req).await?; | ||
buf.reset(); | ||
continue | ||
} | ||
match buf.write(chunk.as_ref()) { | ||
Ok(_) => {}, | ||
Err(e) => { | ||
let text = String::from_utf8(buf.as_bytes().to_vec())?; | ||
req.text = Some(text); | ||
self.client.write_audio_stream(w, &req).await?; | ||
buf.reset(); | ||
let rem = chunk.len() - e.bytes_written; | ||
let chunk_slice = chunk.as_ref(); | ||
buf.write(&chunk_slice[rem..])?; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |