Sound works

This commit is contained in:
2026-04-19 22:56:58 +03:00
parent f0379925f1
commit 7a7ec13cb2
5 changed files with 528 additions and 1 deletions
+1
View File
@@ -1,4 +1,5 @@
pub(crate) mod words;
pub(crate) mod card_sets;
pub(crate) mod card_stats;
pub(crate) mod voice;
+39
View File
@@ -0,0 +1,39 @@
use crate::dictionary::app_data_dir;
use rodio::Decoder;
use sha2::{Digest, Sha256};
use std::fs::File;
use std::io::BufReader;
use reqwest::Client;
pub async fn get_voice(text: &str) -> BufReader<File> {
let mut path = app_data_dir();
path.push("voice");
if !path.exists() {
std::fs::create_dir(path.clone()).unwrap();
}
let hash = format!("{}.wav", hex::encode(Sha256::digest(text.as_bytes())));
path.push(hash);
if !path.exists() {
let engine_url = "http://127.0.0.1:50021";
let client = Client::new();
let query = client
.post(format!("{engine_url}/audio_query?text={text}&speaker=11"))
.send()
.await;
let query = query.unwrap().text().await.unwrap();
// Synthesis
let audio = client
.post(format!("{engine_url}/synthesis?speaker=11"))
.header("Content-Type", "application/json")
.body(query)
.send()
.await.unwrap()
.bytes()
.await.unwrap();
tokio::fs::write(&path, &audio).await.unwrap();
}
BufReader::new(File::open(path).unwrap())
}