Skip to content

Commit

Permalink
feat: support tiktok api
Browse files Browse the repository at this point in the history
  • Loading branch information
ElmTran committed Sep 30, 2024
1 parent 4e6c2be commit 3762c6b
Show file tree
Hide file tree
Showing 15 changed files with 601 additions and 136 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ You can download the latest version from the [release page](https://github.com/E

- [x] Selection Speech Shortcut

- [x] TikTok API

- [ ] Support Speech Synthesis Markup Language (SSML)

## License

Distributed under the MIT License. See `LICENSE` for more information.

[![License][license-shield]][license-url]


## Contact

Mengqing - [@Telegram](https://t.me/mzfbwu/) - [@Email](mailto:c897611977@gmail.com)
Expand All @@ -58,9 +61,8 @@ Mengqing - [@Telegram](https://t.me/mzfbwu/) - [@Email](mailto:c897611977@gmail.

- [Azure Cognitiveservices Speech](https://learn.microsoft.com/en-us/azure/ai-services/speech-service/get-started-text-to-speech?tabs=windows%2Cterminal&pivots=programming-language-rest#synthesize-to-a-file)


<!-- MARKDOWN LINKS & IMAGES -->
<!-- https://www.markdownguide.org/basic-syntax/#reference-style-links -->

[license-shield]: https://img.shields.io/github/license/ElmTran/praises
[license-url]: https://github.com/ElmTran/praises/blob/master/LICENSE
[license-url]: https://github.com/ElmTran/praises/blob/master/LICENSE
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "praises",
"private": true,
"version": "0.0.5",
"version": "0.0.6",
"type": "module",
"lint-staged": {
"*.{js,ts,tsx,vue}": [
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "praises"
version = "0.0.5"
version = "0.0.6"
description = "Praises is a text-to-speech tool that can help you read text easily."
authors = ["ElmTran"]
edition = "2021"
Expand Down Expand Up @@ -48,6 +48,7 @@ url = "2.5.2"
regex = "1.10.5"
strum_macros = "0.26.4"
strum = "0.26.3"
base64 = "0.22.1"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
use crate::app::HANDLE;
use crate::services::tts::{ azure, windows, msedge };
use crate::services::tts::{ azure, windows, msedge, tiktok };
use crate::config;
use crate::hotkeys;
use tauri::Manager;
Expand Down Expand Up @@ -39,6 +39,7 @@ pub async fn convert(
tts.receive().await
}
"azure" => { azure::request(text, speaker, language, style, role, rate, pitch).await }
"tiktok" => { tiktok::request(text, speaker).await }
_ => Err("Invalid service".to_string()),
}
}
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/services/tts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod windows;
pub mod azure;
pub mod msedge;
pub mod tiktok;
44 changes: 44 additions & 0 deletions src-tauri/src/services/tts/tiktok.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use reqwest::Client;
use serde_json::json;
use std::error::Error;
use base64::Engine;

static ENDPOINT: &str = "https://tiktok-tts.weilnet.workers.dev/api/generation";

pub struct TikTokTTS {
text: String,
voice: String,
}

impl TikTokTTS {
pub fn new(text: String, voice: String) -> Self {
Self { text, voice }
}

pub async fn send(&self) -> Result<Vec<u8>, Box<dyn Error>> {
let client = Client::new();
let res = client
.post(ENDPOINT)
.header("Content-Type", "application/json")
.json(
&json!({
"text": self.text,
"voice": self.voice,
})
)
.send().await?;
let res = res.error_for_status()?;
let data = res.json::<serde_json::Value>().await?;
if !data["success"].as_bool().unwrap_or(false) || !data.get("data").is_some() {
return Err(format!("TikTok API error: {:?}", data).into());
}
let audio_base64 = data["data"].as_str().ok_or("Invalid audio data")?;
let audio = base64::prelude::BASE64_STANDARD.decode(audio_base64)?;
Ok(audio)
}
}

pub async fn request(text: &str, voice: &str) -> Result<Vec<u8>, String> {
let tts = TikTokTTS::new(text.to_string(), voice.to_string());
tts.send().await.map_err(|e| e.to_string())
}
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"package": {
"productName": "praises",
"version": "0.0.5"
"version": "0.0.6"
},
"tauri": {
"allowlist": {
Expand Down
52 changes: 51 additions & 1 deletion src/locales/langs/en-US.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tts": {
"form": {
"azure": {
"language": "Language",
"speaker": "Speaker",
"style": "Style",
Expand Down Expand Up @@ -76,6 +76,56 @@
"role": "Please select role",
"template": "Please select template"
}
},
"tiktok": {
"language": "Language",
"voice": "Voice",
"options": {
"language": {
"en_us": "English (US)",
"en_uk": "English (UK)",
"en_au": "English (Australia)",
"fr": "French",
"de": "German",
"es": "Spanish",
"es_mx": "Spanish (Mexico)",
"br": "Portuguese (Brazil)",
"id": "Indonesian",
"jp": "Japanese",
"kr": "Korean",
"characters": "Characters",
"singing": "Singing"
},
"voice": {
"female": "Female",
"male1": "Male 1",
"male2": "Male 2",
"male3": "Male 3",
"male4": "Male 4",
"male": "Male",
"female1": "Female 1",
"female2": "Female 2",
"female3": "Female 3",
"ghostface": "Ghostface",
"chewbacca": "Chewbacca",
"c3po": "C-3PO",
"stitch": "Stitch",
"stormtrooper": "Stormtrooper",
"rocket": "Rocket",
"alto": "Alto",
"tenor": "Tenor",
"sunshineSoon": "Sunshine Soon",
"warmyBreeze": "Warmy Breeze",
"glorious": "Glorious",
"itGoesUp": "It Goes Up",
"chipmunk": "Chipmunk",
"dramatic": "Dramatic"
}
},
"placeholder": {
"language": "Please select language",
"voice": "Please select voice"
}
}
},
"setting": {
Expand Down
67 changes: 65 additions & 2 deletions src/locales/langs/zh-CN.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tts": {
"form": {
"azure": {
"language": "语言",
"speaker": "语音",
"style": "风格",
Expand All @@ -13,7 +13,20 @@
"language": {
"en": "英语",
"zh": "中文",
"ja": "日语"
"ja": "日语",
"en_us": "英语(美国)",
"en_uk": "英语(英国)",
"en_au": "英语(澳大利亚)",
"fr": "法语",
"de": "德语",
"es": "西班牙语",
"es_mx": "西班牙语(墨西哥)",
"br": "葡萄牙语(巴西)",
"id": "印度尼西亚语",
"jp": "日语",
"kr": "韩语",
"characters": "角色语音",
"singing": "歌唱"
},
"speaker": {
"aria": "Aria",
Expand Down Expand Up @@ -76,6 +89,56 @@
"role": "请选择角色",
"template": "请选择模板"
}
},
"tiktok": {
"language": "语言",
"voice": "语音",
"options": {
"language": {
"en_us": "英语(美国)",
"en_uk": "英语(英国)",
"en_au": "英语(澳大利亚)",
"fr": "法语",
"de": "德语",
"es": "西班牙语",
"es_mx": "西班牙语(墨西哥)",
"br": "葡萄牙语(巴西)",
"id": "印度尼西亚语",
"jp": "日语",
"kr": "韩语",
"characters": "角色语音",
"singing": "歌唱"
},
"voice": {
"female": "女性",
"male1": "男性1",
"male2": "男性2",
"male3": "男性3",
"male4": "男性4",
"male": "男性",
"female1": "女性1",
"female2": "女性2",
"female3": "女性3",
"ghostface": "鬼脸",
"chewbacca": "丘巴卡",
"c3po": "C-3PO",
"stitch": "史迪奇",
"stormtrooper": "冲锋队员",
"rocket": "火箭浣熊",
"alto": "女中音",
"tenor": "男高音",
"sunshineSoon": "阳光很快",
"warmyBreeze": "温暖微风",
"glorious": "光辉灿烂",
"itGoesUp": "音调上升",
"chipmunk": "花栗鼠",
"dramatic": "戏剧性的"
}
},
"placeholder": {
"language": "请选择语言",
"voice": "请选择语音"
}
}
},
"setting": {
Expand Down
Loading

0 comments on commit 3762c6b

Please sign in to comment.