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 regex::Regex;
use serenity::{model::prelude::Message, prelude::Context};
@ -78,7 +76,7 @@ impl TTSMessage for Message {
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 data_read = ctx.data.read().await;
@ -129,14 +127,6 @@ impl TTSMessage for Message {
.unwrap(),
};
let uuid = uuid::Uuid::new_v4().to_string();
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()
audio
}
}

View File

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

View File

@ -1,5 +1,3 @@
use std::{env, fs::File, io::Write};
use async_trait::async_trait;
use serenity::prelude::Context;
@ -21,13 +19,13 @@ pub trait TTSMessage {
/// ```
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:
/// ```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 {
@ -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 data_read = ctx.data.read().await;
let storage = data_read
@ -74,14 +72,6 @@ impl TTSMessage for AnnounceMessage {
.await
.unwrap();
let uuid = uuid::Uuid::new_v4().to_string();
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()
audio
}
}