Tts added
This commit is contained in:
+4
-12
@@ -2,8 +2,7 @@ use anyhow::{anyhow, Context, Result};
|
|||||||
use rodio::{Decoder, OutputStream, Sink};
|
use rodio::{Decoder, OutputStream, Sink};
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use std::process::{Child, Command};
|
use std::process::{Child, Command};
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, Mutex};
|
||||||
use tokio::sync::Mutex;
|
|
||||||
|
|
||||||
pub struct TtsEngine {
|
pub struct TtsEngine {
|
||||||
process: Arc<Mutex<Option<Child>>>,
|
process: Arc<Mutex<Option<Child>>>,
|
||||||
@@ -17,7 +16,6 @@ impl TtsEngine {
|
|||||||
let server_url = format!("http://127.0.0.1:{}", port);
|
let server_url = format!("http://127.0.0.1:{}", port);
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
// Check if server is already running
|
|
||||||
let already_running = client
|
let already_running = client
|
||||||
.get(&format!("{}/", server_url))
|
.get(&format!("{}/", server_url))
|
||||||
.send()
|
.send()
|
||||||
@@ -82,7 +80,6 @@ impl TtsEngine {
|
|||||||
|
|
||||||
let wav_bytes = response.bytes().await?;
|
let wav_bytes = response.bytes().await?;
|
||||||
|
|
||||||
// Play audio in a blocking thread (rodio requires sync)
|
|
||||||
let bytes = wav_bytes.to_vec();
|
let bytes = wav_bytes.to_vec();
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
if let Ok((_stream, stream_handle)) = OutputStream::try_default() {
|
if let Ok((_stream, stream_handle)) = OutputStream::try_default() {
|
||||||
@@ -98,8 +95,8 @@ impl TtsEngine {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn stop(&self) {
|
pub fn stop(&self) {
|
||||||
if let Ok(mut guard) = self.process.lock().await {
|
if let Ok(mut guard) = self.process.lock() {
|
||||||
if let Some(ref mut child) = *guard {
|
if let Some(ref mut child) = *guard {
|
||||||
let _ = child.kill();
|
let _ = child.kill();
|
||||||
let _ = child.wait();
|
let _ = child.wait();
|
||||||
@@ -118,11 +115,6 @@ impl TtsEngine {
|
|||||||
|
|
||||||
impl Drop for TtsEngine {
|
impl Drop for TtsEngine {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Ok(mut guard) = self.process.lock() {
|
self.stop();
|
||||||
if let Some(ref mut child) = *guard {
|
|
||||||
let _ = child.kill();
|
|
||||||
let _ = child.wait();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user