add voicevox support

This commit is contained in:
mii
2022-08-09 22:02:30 +09:00
parent d3c6c3195b
commit cb4e901def
6 changed files with 36 additions and 24 deletions

View File

@ -1,4 +1,4 @@
use crate::{tts::gcp_tts::gcp_tts::TTS, database::database::Database};
use crate::{tts::{gcp_tts::gcp_tts::TTS, voicevox::voicevox::VOICEVOX}, database::database::Database};
use serenity::{prelude::{TypeMapKey, RwLock}, model::id::GuildId, futures::lock::Mutex};
use crate::tts::instance::TTSInstance;
@ -15,7 +15,7 @@ impl TypeMapKey for TTSData {
pub struct TTSClientData;
impl TypeMapKey for TTSClientData {
type Value = Arc<Mutex<TTS>>;
type Value = Arc<Mutex<(TTS, VOICEVOX)>>;
}
/// Database client data

View File

@ -38,7 +38,8 @@ impl Database {
let config = UserConfig {
tts_type: Some(voice_type),
gcp_tts_voice: Some(voice_selection)
gcp_tts_voice: Some(voice_selection),
voicevox_speaker: Some(1)
};
self.connection.set(format!("discord_user:{}", user_id), serde_json::to_string(&config).unwrap())?;

View File

@ -5,5 +5,6 @@ use crate::tts::{gcp_tts::structs::voice_selection_params::VoiceSelectionParams,
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct UserConfig {
pub tts_type: Option<TTSType>,
pub gcp_tts_voice: Option<VoiceSelectionParams>
pub gcp_tts_voice: Option<VoiceSelectionParams>,
pub voicevox_speaker: Option<i64>
}

View File

@ -10,7 +10,7 @@ use crate::{
message::TTSMessage,
gcp_tts::structs::{
audio_config::AudioConfig, synthesis_input::SynthesisInput, synthesize_request::SynthesizeRequest
}
}, tts_type::{self, TTSType}
},
};
@ -27,7 +27,7 @@ impl TTSMessage for Message {
} else {
self.author.name.clone()
};
format!("<speak>{} さんの発言<break time=\"200ms\"/>{}</speak>", name, self.content)
format!("{} さんの発言<break time=\"200ms\"/>{}", name, self.content)
}
} else {
let member = self.member.clone();
@ -36,7 +36,7 @@ impl TTSMessage for Message {
} else {
self.author.name.clone()
};
format!("<speak>{} さんの発言<break time=\"200ms\"/>{}</speak>", name, self.content)
format!("{} さんの発言<break time=\"200ms\"/>{}", name, self.content)
};
instance.before_message = Some(self.clone());
@ -49,8 +49,8 @@ impl TTSMessage for Message {
println!("Parsed: {:?}", text);
let data_read = ctx.data.read().await;
let storage = data_read.get::<TTSClientData>().expect("Cannot get TTSClientStorage").clone();
let mut storage = storage.lock().await;
let storage = data_read.get::<TTSClientData>().expect("Cannot get GCP TTSClientStorage").clone();
let mut tts = storage.lock().await;
let config = {
let database = data_read.get::<DatabaseClientData>().expect("Cannot get DatabaseClientData").clone();
@ -58,10 +58,12 @@ impl TTSMessage for Message {
database.get_user_config_or_default(self.author.id.0).await.unwrap().unwrap()
};
let audio = storage.synthesize(SynthesizeRequest {
let audio = match config.tts_type.unwrap_or(TTSType::GCP) {
TTSType::GCP => {
tts.0.synthesize(SynthesizeRequest {
input: SynthesisInput {
text: None,
ssml: Some(text)
ssml: Some(format!("<speak>{}</speak>", text))
},
voice: config.gcp_tts_voice.unwrap(),
audioConfig: AudioConfig {
@ -69,7 +71,13 @@ impl TTSMessage for Message {
speakingRate: 1.2f32,
pitch: 1.0f32
}
}).await.unwrap();
}).await.unwrap()
}
TTSType::VOICEVOX => {
tts.1.synthesize(text.replace("<break time=\"200ms\"/>", ""), config.voicevox_speaker.unwrap_or(1)).await.unwrap()
}
};
let uuid = uuid::Uuid::new_v4().to_string();

View File

@ -4,7 +4,7 @@ use config::Config;
use data::{TTSData, TTSClientData, DatabaseClientData};
use database::database::Database;
use event_handler::Handler;
use tts::gcp_tts::gcp_tts::TTS;
use tts::{gcp_tts::gcp_tts::TTS, voicevox::voicevox::VOICEVOX};
use serenity::{
client::{Client, bridge::gateway::GatewayIntents},
framework::StandardFramework, prelude::RwLock, futures::lock::Mutex
@ -51,12 +51,14 @@ async fn main() {
// Create discord client
let mut client = create_client(&config.prefix, &config.token, config.application_id).await.expect("Err creating client");
// Create TTS client
// Create GCP TTS client
let tts = match TTS::new("./credentials.json".to_string()).await {
Ok(tts) => tts,
Err(err) => panic!("{}", err)
};
let voicevox = VOICEVOX::new(config.voicevox_key);
let database_client = {
let redis_client = redis::Client::open(config.redis_url).unwrap();
let con = redis_client.get_connection().unwrap();
@ -67,7 +69,7 @@ async fn main() {
{
let mut data = client.data.write().await;
data.insert::<TTSData>(Arc::new(RwLock::new(HashMap::default())));
data.insert::<TTSClientData>(Arc::new(Mutex::new(tts)));
data.insert::<TTSClientData>(Arc::new(Mutex::new((tts, voicevox))));
data.insert::<DatabaseClientData>(Arc::new(Mutex::new(database_client)));
}

View File

@ -45,7 +45,7 @@ impl TTSMessage for AnnounceMessage {
let storage = data_read.get::<TTSClientData>().expect("Cannot get TTSClientStorage").clone();
let mut storage = storage.lock().await;
let audio = storage.synthesize(SynthesizeRequest {
let audio = storage.0.synthesize(SynthesizeRequest {
input: SynthesisInput {
text: None,
ssml: Some(text)