implement tts without disk I/O

This commit is contained in:
mii443
2025-04-03 02:43:26 +09:00
parent b4de0f1ad6
commit bf4b160af7
3 changed files with 9 additions and 31 deletions

View File

@ -1,5 +1,3 @@
use std::{env, fs::File, io::Write};
use async_trait::async_trait; use async_trait::async_trait;
use regex::Regex; use regex::Regex;
use serenity::{model::prelude::Message, prelude::Context}; use serenity::{model::prelude::Message, prelude::Context};
@ -78,7 +76,7 @@ impl TTSMessage for Message {
res res
} }
async fn synthesize(&self, instance: &mut TTSInstance, ctx: &Context) -> String { async fn synthesize(&self, instance: &mut TTSInstance, ctx: &Context) -> Vec<u8> {
let text = self.parse(instance, ctx).await; let text = self.parse(instance, ctx).await;
let data_read = ctx.data.read().await; let data_read = ctx.data.read().await;
@ -129,14 +127,6 @@ impl TTSMessage for Message {
.unwrap(), .unwrap(),
}; };
let uuid = uuid::Uuid::new_v4().to_string(); audio
let path = env::current_dir().unwrap();
let file_path = path.join("audio").join(format!("{}.mp3", uuid));
let mut file = File::create(file_path.clone()).unwrap();
file.write(&audio).unwrap();
file_path.into_os_string().into_string().unwrap()
} }
} }

View File

@ -5,7 +5,6 @@ use serenity::{
}, },
prelude::Context, prelude::Context,
}; };
use songbird::input::File;
use crate::tts::message::TTSMessage; use crate::tts::message::TTSMessage;
@ -27,14 +26,13 @@ impl TTSInstance {
where where
T: TTSMessage, T: TTSMessage,
{ {
let path = message.synthesize(self, ctx).await; let audio = message.synthesize(self, ctx).await;
{ {
let manager = songbird::get(&ctx).await.unwrap(); let manager = songbird::get(&ctx).await.unwrap();
let call = manager.get(self.guild).unwrap(); let call = manager.get(self.guild).unwrap();
let mut call = call.lock().await; let mut call = call.lock().await;
let input = File::new(path); call.enqueue(audio.into()).await;
call.enqueue(input.into()).await;
} }
} }

View File

@ -1,5 +1,3 @@
use std::{env, fs::File, io::Write};
use async_trait::async_trait; use async_trait::async_trait;
use serenity::prelude::Context; use serenity::prelude::Context;
@ -21,13 +19,13 @@ pub trait TTSMessage {
/// ``` /// ```
async fn parse(&self, instance: &mut TTSInstance, ctx: &Context) -> String; async fn parse(&self, instance: &mut TTSInstance, ctx: &Context) -> String;
/// Synthesize the message and returns the path to the audio file. /// Synthesize the message and returns the audio data.
/// ///
/// Example: /// Example:
/// ```rust /// ```rust
/// let path = message.synthesize(instance, ctx).await; /// let audio = message.synthesize(instance, ctx).await;
/// ``` /// ```
async fn synthesize(&self, instance: &mut TTSInstance, ctx: &Context) -> String; async fn synthesize(&self, instance: &mut TTSInstance, ctx: &Context) -> Vec<u8>;
} }
pub struct AnnounceMessage { pub struct AnnounceMessage {
@ -44,7 +42,7 @@ impl TTSMessage for AnnounceMessage {
) )
} }
async fn synthesize(&self, instance: &mut TTSInstance, ctx: &Context) -> String { async fn synthesize(&self, instance: &mut TTSInstance, ctx: &Context) -> Vec<u8> {
let text = self.parse(instance, ctx).await; let text = self.parse(instance, ctx).await;
let data_read = ctx.data.read().await; let data_read = ctx.data.read().await;
let storage = data_read let storage = data_read
@ -74,14 +72,6 @@ impl TTSMessage for AnnounceMessage {
.await .await
.unwrap(); .unwrap();
let uuid = uuid::Uuid::new_v4().to_string(); audio
let path = env::current_dir().unwrap();
let file_path = path.join("audio").join(format!("{}.mp3", uuid));
let mut file = File::create(file_path.clone()).unwrap();
file.write(&audio).unwrap();
file_path.into_os_string().into_string().unwrap()
} }
} }