Update tts worker

This commit is contained in:
2024-04-05 20:41:55 +11:00
parent 2918033612
commit 75ed3065e3
+44 -5
View File
@@ -1,19 +1,54 @@
import { Router } from 'itty-router'
/**
* demo
* https://home-tts.junv.workers.dev/tts?text=%E4%BD%A0%E7%9C%9F%E7%9A%84%E6%98%AF%E5%BE%88%E8%AE%A8%E5%8E%8C%E5%91%A2&haha=hahaJunv&voice=zh-CN-XiaoxiaoNeural&voiceStyle=angry
*
* to publish
* wrangler publish tts-bot.ts --compatibility-date 2024-04-05
*
*
*/
const router = Router()
const botToken = BOT_TOKEN;
const chatId = CHAT_ID;
const ttsKey = TTS_KEY
const format = "audio-16khz-128kbitrate-mono-mp3";
// const defaultVoice = 'zh-CN-XiaoxiaoNeural'; //female
const defaultVoice = 'zh-CN-YunfengNeural'
const defaultVoice = 'zh-CN-XiaoxiaoNeural'
// male
// zh-CN-YunfengNeural
// zh-CN-YunyangNeural
async function sendToAzure(text: string, voice: string, format: string) {
// 'zh-CN-XiaoxiaoNeural'; //female
const defaultVoiceStyle = "cheerful"
// "assistant",
// "chat",
// "customerservice",
// "newscast",
// "affectionate",
// "angry",
// "calm",
// "cheerful",
// "disgruntled",
// "fearful",
// "gentle",
// "lyrical",
// "sad",
// "serious",
// "poetry-reading",
// "friendly",
// "chat-casual",
// "whisper",
// "sorry"
async function sendToAzure(text: string, voice: string, voiceStyle:string, format: string) {
const endpoint = `https://australiaeast.tts.speech.microsoft.com/cognitiveservices/v1`
const body = `<speak version="1.0" xml:lang="${voice}"><voice xml:lang="${voice}" xml:gender="Female" name="${voice}">${text}</voice></speak>`
const body = `<speak version="1.0" xml:lang="${voice}"><voice xml:lang="${voice}" xml:gender="Female" name="${voice}" style="${voiceStyle}" >${text}</voice></speak>`
const response = await fetch(`${endpoint}`, {
method: 'POST',
@@ -66,7 +101,7 @@ async function handleBotRequest(request) {
var audioData;
try {
// Generate the audio file from the input text
audioData = await sendToAzure(text, defaultVoice, format);
audioData = await sendToAzure(text, defaultVoice, defaultVoiceStyle, format);
} catch (err) {
await new Response('Error: something went wrong', { status: 500 });
}
@@ -89,6 +124,7 @@ async function handleRequest(request: Request): Promise<Response> {
return new Response('Error: No valid params provided', { status: 400 });
}
var voice = params.voice
var voiceStyle = params.voiceStyle
const haha = params.haha
const text = params.text
@@ -98,8 +134,11 @@ async function handleRequest(request: Request): Promise<Response> {
if (voice == null) {
voice = defaultVoice
}
if (voiceStyle == null) {
voiceStyle = defaultVoiceStyle
}
const audioData = await sendToAzure(text, voice, format)
const audioData = await sendToAzure(text, voice, voiceStyle, format)
return new Response(audioData, {
headers: {