mistpe commited on
Commit
05ca874
·
verified ·
1 Parent(s): 5f2dd5b

Update main.ts

Browse files
Files changed (1) hide show
  1. main.ts +995 -10
main.ts CHANGED
@@ -1,3 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import { serve } from "https://deno.land/std/http/server.ts";
2
  import { EdgeSpeechTTS } from "https://esm.sh/@lobehub/tts@1";
3
 
@@ -118,17 +275,845 @@ async function handleSynthesisRequest(req: Request) {
118
  return synthesizeSpeech(model, voice, input);
119
  }
120
 
121
-
122
  async function handleDemoRequest(req: Request) {
123
- const groupedVoiceList = await fetchVoiceList();
124
-
125
- const html = `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>语音合成演示</title><link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;700&display=swap" rel="stylesheet"><style>:root{--primary-color:#6c8bd6;--primary-light:#a2b3e3;--primary-dark:#3d5b8f;--secondary-color:#f08080;--text-color:#333;--text-secondary:#777;--bg-color:#fff}body{font-family:'Noto Sans SC','Arial',sans-serif;color:var(--text-color);margin:0;padding:0;display:flex;justify-content:center;background-color:#fafafa;background-image:linear-gradient(135deg,#f5f7fa 0%,#c3cfe2 100%);position:relative;overflow:hidden}body::before{content:"";position:absolute;top:0;left:0;right:0;bottom:0;background:repeating-radial-gradient(circle at 50% 50%,rgba(255,255,255,0.8) 0%,rgba(255,255,255,0.8) 2%,transparent 2%,transparent 4%,rgba(255,255,255,0.8) 4%,rgba(255,255,255,0.8) 6%,transparent 6%,transparent 8%,rgba(255,255,255,0.8) 8%,rgba(255,255,255,0.8) 10%,transparent 10%),repeating-linear-gradient(45deg,#D4F4FF 0%,#D4F4FF 5%,#E6F9FF 5%,#E6F9FF 10%,#F0FAFF 10%,#F0FAFF 15%,#E6F9FF 15%,#E6F9FF 20%,#D4F4FF 20%,#D4F4FF 25%);background-blend-mode:multiply;opacity:0.8;z-index:-1;animation:glitch 15s infinite}.container{display:flex;max-width:1200px;width:100%;margin:40px;background:#fff;border-radius:12px;position:relative;background-color:rgba(255,255,255,0.8);z-index:1}@keyframes glitch{0%{background-position:0 0,0 0;filter:hue-rotate(0deg)}50%{background-position:10px 10px,-10px 10px;filter:hue-rotate(360deg)}100%{background-position:0 0,0 0;filter:hue-rotate(0deg)}}.input-area,.output-area{padding:30px;width:50%}.input-area{border-right:1px solid #E0E0E0}h1{font-size:36px;color:var(--primary-color);margin-bottom:30px}.filter-section{margin-bottom:30px}.filter-section label{display:block;font-size:16px;color:var(--text-secondary);margin-bottom:10px}.filter-section input{font-size:16px;padding:10px 15px;border:2px solid var(--primary-light);border-radius:8px;outline:none;transition:border-color .3s,box-shadow .3s;width:100%;box-sizing:border-box}.filter-section input:focus{border-color:var(--primary-color);box-shadow:0 0 0 2px var(--primary-light)}.slider-container{margin-bottom:30px}.slider-container label{display:block;font-size:16px;color:var(--text-secondary);margin-bottom:10px}.slider{-webkit-appearance:none;width:100%;height:10px;border-radius:5px;background:linear-gradient(to right,var(--secondary-color) 0%,var(--primary-color) 50%,var(--primary-light) 100%);box-shadow:inset 0 1px 2px rgba(0,0,0,0.1),0 1px rgba(255,255,255,0.1);outline:none;opacity:0.7;-webkit-transition:.2s;transition:opacity .2s;margin-bottom:10px}.slider:hover{opacity:1}.slider::-webkit-slider-thumb{-webkit-appearance:none;appearance:none;width:20px;height:20px;border-radius:50%;background:#fff;border:2px solid var(--primary-color);cursor:pointer}.slider::-moz-range-thumb{width:20px;height:20px;border-radius:50%;background:#fff;border:2px solid var(--primary-color);cursor:pointer}.slider-value{font-size:14px;color:var(--text-secondary)}.textarea-container{margin-bottom:30px}.textarea-container label{display:block;font-size:18px;margin-bottom:10px}.textarea-container textarea{width:100%;padding:10px;font-size:16px;border:2px solid var(--primary-light);border-radius:8px;outline:none;resize:vertical;transition:border-color .3s,box-shadow .3s;box-sizing:border-box;height:200px}.textarea-container textarea:focus{border-color:var(--primary-color);box-shadow:0 0 0 2px var(--primary-light)}.voice-group{margin-bottom:20px;border:2px solid var(--primary-light);border-radius:12px;overflow:hidden;cursor:move;background:#fff}.voice-header{padding:15px 20px;font-size:18px;background:var(--primary-light);color:#fff;cursor:pointer;display:flex;justify-content:space-between;align-items:center}.voice-header:hover{background:var(--primary-color)}.voice-buttons{padding:20px;display:none;gap:12px;flex-wrap:wrap}.voice-button{background:var(--secondary-color);color:#fff;border:none;padding:10px 20px;border-radius:50px;cursor:pointer;transition:filter .3s}.voice-button:hover{filter:brightness(0.9)}.chevron{transition:transform .3s}.voice-group.open .voice-buttons{display:flex}.voice-group.open .chevron{transform:rotate(180deg)}.dragging{opacity:0.5}</style></head><body><div class="container"><div class="input-area"><h1>输入文本</h1><div class="filter-section"><label for="keywords">Speaker筛选:</label><input type="text" id="keywords" value="multilingual,-TW,-CN"></div><div class="slider-container"><label for="rate">语速:</label><input type="range" min="-1" max="1" step="0.1" value="-0.1" class="slider" id="rate"><div class="slider-value" id="rateValue">-0.1</div><label for="pitch">音调:</label><input type="range" min="-1" max="1" step="0.1" value="0.1" class="slider" id="pitch"><div class="slider-value" id="pitchValue">0.1</div></div><div class="textarea-container"><label for="inputText">输入文本:</label><textarea id="inputText">Hello world</textarea></div></div><div class="output-area"><h1>选择语音</h1><div id="voices"></div></div></div><script>const voiceList = ${JSON.stringify(groupedVoiceList)};let audio=null;function filterVoices(){const keywords=document.getElementById('keywords').value.split(',').map(k=>k.trim().toLowerCase());const voicesDiv=document.getElementById('voices');voicesDiv.innerHTML='';const filteredVoices={};for(const[locale,voices]of Object.entries(voiceList)){const filtered=voices.filter(({name,friendlyName})=>keywords.some(keyword=>name.toLowerCase().includes(keyword)||friendlyName.toLowerCase().includes(keyword)));if(filtered.length>0){filteredVoices[locale]=filtered}}for(const[locale,voices]of Object.entries(filteredVoices)){const group=document.createElement('div');group.className='voice-group';group.draggable=true;const header=document.createElement('div');header.className='voice-header';header.textContent=locale.toUpperCase();const chevron=document.createElement('span');chevron.className='chevron';chevron.innerHTML='&#9660;';header.appendChild(chevron);const buttonsContainer=document.createElement('div');buttonsContainer.className='voice-buttons';voices.forEach(({model,name})=>{const button=document.createElement('button');button.className='voice-button';button.textContent=name;button.onclick=()=>synthesize(model);buttonsContainer.appendChild(button)});header.onclick=()=>{group.classList.toggle('open')};group.appendChild(header);group.appendChild(buttonsContainer);voicesDiv.appendChild(group)}addDragDropListeners()}function synthesize(model){const text=document.getElementById('inputText').value||'Hello world';const rate=document.getElementById('rate').value||'-0.1';const pitch=document.getElementById('pitch').value||'0.1';const voice=\`rate:\${rate}|pitch:\${pitch}\`;if(audio){audio.pause();audio.currentTime=0}fetch('/v1/audio/speech',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({model,input:text,voice})}).then(response=>response.blob()).then(blob=>{const audioUrl=URL.createObjectURL(blob);audio=new Audio(audioUrl);audio.play()})}function addDragDropListeners(){const voicesDiv=document.getElementById('voices');let draggedItem=null;voicesDiv.addEventListener('dragstart',e=>{draggedItem=e.target;e.target.classList.add('dragging')});voicesDiv.addEventListener('dragend',e=>{e.target.classList.remove('dragging');draggedItem=null});voicesDiv.addEventListener('dragover',e=>{e.preventDefault();const afterElement=getDragAfterElement(voicesDiv,e.clientY);if(afterElement==null){voicesDiv.appendChild(draggedItem)}else{voicesDiv.insertBefore(draggedItem,afterElement)}})}function getDragAfterElement(container,y){const draggableElements=[...container.querySelectorAll('.voice-group:not(.dragging)')];return draggableElements.reduce((closest,child)=>{const box=child.getBoundingClientRect();const offset=y-box.top-box.height/2;if(offset<0&&offset>closest.offset){return{offset:offset,element:child}}else{return closest}},{offset:Number.NEGATIVE_INFINITY}).element}filterVoices();document.getElementById('keywords').addEventListener('input',filterVoices);const rateSlider=document.getElementById('rate');const rateValue=document.getElementById('rateValue');rateSlider.oninput=function(){rateValue.innerHTML=this.value};const pitchSlider=document.getElementById('pitch');const pitchValue=document.getElementById('pitchValue');pitchSlider.oninput=function(){pitchValue.innerHTML=this.value}</script></body></html>`;
126
-
127
- return new Response(html, {
128
- headers: { "Content-Type": "text/html" },
129
- });
130
- }
131
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  serve(async (req) => {
134
  try {
 
1
+ // import { serve } from "https://deno.land/std/http/server.ts";
2
+ // import { EdgeSpeechTTS } from "https://esm.sh/@lobehub/tts@1";
3
+
4
+ // const AUTH_TOKEN = Deno.env.get("AUTH_TOKEN");
5
+ // const VOICES_URL = "https://speech.platform.bing.com/consumer/speech/synthesize/readaloud/voices/list?trustedclienttoken=6A5AA1D4EAFF4E9FB37E23D68491D6F4";
6
+
7
+ // async function fetchVoiceList() {
8
+ // const response = await fetch(VOICES_URL);
9
+ // const voices = await response.json();
10
+ // return voices.reduce((acc: Record<string, { model: string, name: string, friendlyName: string, locale: string }[]>, voice: any) => {
11
+ // const { ShortName: model, ShortName: name, FriendlyName: friendlyName, Locale: locale } = voice;
12
+ // if (!acc[locale]) acc[locale] = [];
13
+ // acc[locale].push({ model, name, friendlyName, locale });
14
+ // return acc;
15
+ // }, {});
16
+ // }
17
+
18
+ // async function synthesizeSpeech(model: string, voice: string, text: string) {
19
+ // let voiceName;
20
+ // let rate = 0;
21
+ // let pitch = 0;
22
+
23
+ // if (model.includes("tts")) {
24
+ // rate = 0.1;
25
+ // pitch = 0.2;
26
+
27
+ // switch (voice) {
28
+ // case "alloy":
29
+ // voiceName = "zh-CN-YunjianNeural";
30
+ // break;
31
+ // case "echo":
32
+ // voiceName = "zh-CN-YunyangNeural";
33
+ // break;
34
+ // case "fable":
35
+ // voiceName = "zh-CN-XiaoxiaoNeural";
36
+ // break;
37
+ // case "onyx":
38
+ // voiceName = "zh-TW-HsiaoChenNeural";
39
+ // break;
40
+ // default:
41
+ // voiceName = "zh-CN-YunxiNeural";
42
+ // break;
43
+ // }
44
+ // } else {
45
+ // voiceName = model;
46
+ // const params = Object.fromEntries(
47
+ // voice.split("|").map((p) => p.split(":") as [string, string])
48
+ // );
49
+ // rate = Number(params["rate"] || 0);
50
+ // pitch = Number(params["pitch"] || 0);
51
+ // }
52
+
53
+ // const tts = new EdgeSpeechTTS();
54
+
55
+ // const payload = {
56
+ // input: text,
57
+ // options: {
58
+ // rate: rate,
59
+ // pitch: pitch,
60
+ // voice: voiceName
61
+ // },
62
+ // };
63
+ // const response = await tts.create(payload);
64
+ // const mp3Buffer = new Uint8Array(await response.arrayBuffer());
65
+
66
+ // console.log(`Successfully synthesized speech, returning audio/mpeg response`);
67
+ // return new Response(mp3Buffer, {
68
+ // headers: { "Content-Type": "audio/mpeg" },
69
+ // });
70
+ // }
71
+
72
+ // function unauthorized(req: Request) {
73
+ // const authHeader = req.headers.get("Authorization");
74
+ // return AUTH_TOKEN && authHeader !== `Bearer ${AUTH_TOKEN}`;
75
+ // }
76
+
77
+ // function validateContentType(req: Request, expected: string) {
78
+ // const contentType = req.headers.get("Content-Type");
79
+ // if (contentType !== expected) {
80
+ // console.log(`Invalid Content-Type ${contentType}, expected ${expected}`);
81
+ // return new Response("Bad Request", { status: 400 });
82
+ // }
83
+ // }
84
+
85
+ // async function handleDebugRequest(req: Request) {
86
+ // const url = new URL(req.url);
87
+ // const voice = url.searchParams.get("voice") || "";
88
+ // const model = url.searchParams.get("model") || "";
89
+ // const text = url.searchParams.get("text") || "";
90
+
91
+ // console.log(`Debug request with model=${model}, voice=${voice}, text=${text}`);
92
+
93
+ // if (!voice || !model || !text) {
94
+ // console.log("Missing required parameters");
95
+ // return new Response("Bad Request", { status: 400 });
96
+ // }
97
+
98
+ // return synthesizeSpeech(model, voice, text);
99
+ // }
100
+
101
+ // async function handleSynthesisRequest(req: Request) {
102
+ // if (unauthorized(req)) {
103
+ // console.log("Unauthorized request");
104
+ // return new Response("Unauthorized", { status: 401 });
105
+ // }
106
+
107
+ // if (req.method !== "POST") {
108
+ // console.log(`Invalid method ${req.method}, expected POST`);
109
+ // return new Response("Method Not Allowed", { status: 405 });
110
+ // }
111
+
112
+ // const invalidContentType = validateContentType(req, "application/json");
113
+ // if (invalidContentType) return invalidContentType;
114
+
115
+ // const { model, input, voice } = await req.json();
116
+ // console.log(`Synthesis request with model=${model}, input=${input}, voice=${voice}`);
117
+
118
+ // return synthesizeSpeech(model, voice, input);
119
+ // }
120
+
121
+
122
+ // async function handleDemoRequest(req: Request) {
123
+ // const groupedVoiceList = await fetchVoiceList();
124
+
125
+ // const html = `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>语音合成演示</title><link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;700&display=swap" rel="stylesheet"><style>:root{--primary-color:#6c8bd6;--primary-light:#a2b3e3;--primary-dark:#3d5b8f;--secondary-color:#f08080;--text-color:#333;--text-secondary:#777;--bg-color:#fff}body{font-family:'Noto Sans SC','Arial',sans-serif;color:var(--text-color);margin:0;padding:0;display:flex;justify-content:center;background-color:#fafafa;background-image:linear-gradient(135deg,#f5f7fa 0%,#c3cfe2 100%);position:relative;overflow:hidden}body::before{content:"";position:absolute;top:0;left:0;right:0;bottom:0;background:repeating-radial-gradient(circle at 50% 50%,rgba(255,255,255,0.8) 0%,rgba(255,255,255,0.8) 2%,transparent 2%,transparent 4%,rgba(255,255,255,0.8) 4%,rgba(255,255,255,0.8) 6%,transparent 6%,transparent 8%,rgba(255,255,255,0.8) 8%,rgba(255,255,255,0.8) 10%,transparent 10%),repeating-linear-gradient(45deg,#D4F4FF 0%,#D4F4FF 5%,#E6F9FF 5%,#E6F9FF 10%,#F0FAFF 10%,#F0FAFF 15%,#E6F9FF 15%,#E6F9FF 20%,#D4F4FF 20%,#D4F4FF 25%);background-blend-mode:multiply;opacity:0.8;z-index:-1;animation:glitch 15s infinite}.container{display:flex;max-width:1200px;width:100%;margin:40px;background:#fff;border-radius:12px;position:relative;background-color:rgba(255,255,255,0.8);z-index:1}@keyframes glitch{0%{background-position:0 0,0 0;filter:hue-rotate(0deg)}50%{background-position:10px 10px,-10px 10px;filter:hue-rotate(360deg)}100%{background-position:0 0,0 0;filter:hue-rotate(0deg)}}.input-area,.output-area{padding:30px;width:50%}.input-area{border-right:1px solid #E0E0E0}h1{font-size:36px;color:var(--primary-color);margin-bottom:30px}.filter-section{margin-bottom:30px}.filter-section label{display:block;font-size:16px;color:var(--text-secondary);margin-bottom:10px}.filter-section input{font-size:16px;padding:10px 15px;border:2px solid var(--primary-light);border-radius:8px;outline:none;transition:border-color .3s,box-shadow .3s;width:100%;box-sizing:border-box}.filter-section input:focus{border-color:var(--primary-color);box-shadow:0 0 0 2px var(--primary-light)}.slider-container{margin-bottom:30px}.slider-container label{display:block;font-size:16px;color:var(--text-secondary);margin-bottom:10px}.slider{-webkit-appearance:none;width:100%;height:10px;border-radius:5px;background:linear-gradient(to right,var(--secondary-color) 0%,var(--primary-color) 50%,var(--primary-light) 100%);box-shadow:inset 0 1px 2px rgba(0,0,0,0.1),0 1px rgba(255,255,255,0.1);outline:none;opacity:0.7;-webkit-transition:.2s;transition:opacity .2s;margin-bottom:10px}.slider:hover{opacity:1}.slider::-webkit-slider-thumb{-webkit-appearance:none;appearance:none;width:20px;height:20px;border-radius:50%;background:#fff;border:2px solid var(--primary-color);cursor:pointer}.slider::-moz-range-thumb{width:20px;height:20px;border-radius:50%;background:#fff;border:2px solid var(--primary-color);cursor:pointer}.slider-value{font-size:14px;color:var(--text-secondary)}.textarea-container{margin-bottom:30px}.textarea-container label{display:block;font-size:18px;margin-bottom:10px}.textarea-container textarea{width:100%;padding:10px;font-size:16px;border:2px solid var(--primary-light);border-radius:8px;outline:none;resize:vertical;transition:border-color .3s,box-shadow .3s;box-sizing:border-box;height:200px}.textarea-container textarea:focus{border-color:var(--primary-color);box-shadow:0 0 0 2px var(--primary-light)}.voice-group{margin-bottom:20px;border:2px solid var(--primary-light);border-radius:12px;overflow:hidden;cursor:move;background:#fff}.voice-header{padding:15px 20px;font-size:18px;background:var(--primary-light);color:#fff;cursor:pointer;display:flex;justify-content:space-between;align-items:center}.voice-header:hover{background:var(--primary-color)}.voice-buttons{padding:20px;display:none;gap:12px;flex-wrap:wrap}.voice-button{background:var(--secondary-color);color:#fff;border:none;padding:10px 20px;border-radius:50px;cursor:pointer;transition:filter .3s}.voice-button:hover{filter:brightness(0.9)}.chevron{transition:transform .3s}.voice-group.open .voice-buttons{display:flex}.voice-group.open .chevron{transform:rotate(180deg)}.dragging{opacity:0.5}</style></head><body><div class="container"><div class="input-area"><h1>输入文本</h1><div class="filter-section"><label for="keywords">Speaker筛选:</label><input type="text" id="keywords" value="multilingual,-TW,-CN"></div><div class="slider-container"><label for="rate">语速:</label><input type="range" min="-1" max="1" step="0.1" value="-0.1" class="slider" id="rate"><div class="slider-value" id="rateValue">-0.1</div><label for="pitch">音调:</label><input type="range" min="-1" max="1" step="0.1" value="0.1" class="slider" id="pitch"><div class="slider-value" id="pitchValue">0.1</div></div><div class="textarea-container"><label for="inputText">输入文本:</label><textarea id="inputText">Hello world</textarea></div></div><div class="output-area"><h1>选择语音</h1><div id="voices"></div></div></div><script>const voiceList = ${JSON.stringify(groupedVoiceList)};let audio=null;function filterVoices(){const keywords=document.getElementById('keywords').value.split(',').map(k=>k.trim().toLowerCase());const voicesDiv=document.getElementById('voices');voicesDiv.innerHTML='';const filteredVoices={};for(const[locale,voices]of Object.entries(voiceList)){const filtered=voices.filter(({name,friendlyName})=>keywords.some(keyword=>name.toLowerCase().includes(keyword)||friendlyName.toLowerCase().includes(keyword)));if(filtered.length>0){filteredVoices[locale]=filtered}}for(const[locale,voices]of Object.entries(filteredVoices)){const group=document.createElement('div');group.className='voice-group';group.draggable=true;const header=document.createElement('div');header.className='voice-header';header.textContent=locale.toUpperCase();const chevron=document.createElement('span');chevron.className='chevron';chevron.innerHTML='&#9660;';header.appendChild(chevron);const buttonsContainer=document.createElement('div');buttonsContainer.className='voice-buttons';voices.forEach(({model,name})=>{const button=document.createElement('button');button.className='voice-button';button.textContent=name;button.onclick=()=>synthesize(model);buttonsContainer.appendChild(button)});header.onclick=()=>{group.classList.toggle('open')};group.appendChild(header);group.appendChild(buttonsContainer);voicesDiv.appendChild(group)}addDragDropListeners()}function synthesize(model){const text=document.getElementById('inputText').value||'Hello world';const rate=document.getElementById('rate').value||'-0.1';const pitch=document.getElementById('pitch').value||'0.1';const voice=\`rate:\${rate}|pitch:\${pitch}\`;if(audio){audio.pause();audio.currentTime=0}fetch('/v1/audio/speech',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({model,input:text,voice})}).then(response=>response.blob()).then(blob=>{const audioUrl=URL.createObjectURL(blob);audio=new Audio(audioUrl);audio.play()})}function addDragDropListeners(){const voicesDiv=document.getElementById('voices');let draggedItem=null;voicesDiv.addEventListener('dragstart',e=>{draggedItem=e.target;e.target.classList.add('dragging')});voicesDiv.addEventListener('dragend',e=>{e.target.classList.remove('dragging');draggedItem=null});voicesDiv.addEventListener('dragover',e=>{e.preventDefault();const afterElement=getDragAfterElement(voicesDiv,e.clientY);if(afterElement==null){voicesDiv.appendChild(draggedItem)}else{voicesDiv.insertBefore(draggedItem,afterElement)}})}function getDragAfterElement(container,y){const draggableElements=[...container.querySelectorAll('.voice-group:not(.dragging)')];return draggableElements.reduce((closest,child)=>{const box=child.getBoundingClientRect();const offset=y-box.top-box.height/2;if(offset<0&&offset>closest.offset){return{offset:offset,element:child}}else{return closest}},{offset:Number.NEGATIVE_INFINITY}).element}filterVoices();document.getElementById('keywords').addEventListener('input',filterVoices);const rateSlider=document.getElementById('rate');const rateValue=document.getElementById('rateValue');rateSlider.oninput=function(){rateValue.innerHTML=this.value};const pitchSlider=document.getElementById('pitch');const pitchValue=document.getElementById('pitchValue');pitchSlider.oninput=function(){pitchValue.innerHTML=this.value}</script></body></html>`;
126
+
127
+ // return new Response(html, {
128
+ // headers: { "Content-Type": "text/html" },
129
+ // });
130
+ // }
131
+
132
+
133
+ // serve(async (req) => {
134
+ // try {
135
+ // const url = new URL(req.url);
136
+
137
+ // if (url.pathname === "/") {
138
+ // return handleDemoRequest(req);
139
+ // }
140
+
141
+ // if (url.pathname === "/tts") {
142
+ // return handleDebugRequest(req);
143
+ // }
144
+
145
+ // if (url.pathname !== "/v1/audio/speech") {
146
+ // console.log(`Unhandled path ${url.pathname}`);
147
+ // return new Response("Not Found", { status: 404 });
148
+ // }
149
+
150
+ // return handleSynthesisRequest(req);
151
+ // } catch (err) {
152
+ // console.error(`Error processing request: ${err.message}`);
153
+ // return new Response(`Internal Server Error\n${err.message}`, {
154
+ // status: 500,
155
+ // });
156
+ // }
157
+ // });
158
  import { serve } from "https://deno.land/std/http/server.ts";
159
  import { EdgeSpeechTTS } from "https://esm.sh/@lobehub/tts@1";
160
 
 
275
  return synthesizeSpeech(model, voice, input);
276
  }
277
 
 
278
  async function handleDemoRequest(req: Request) {
279
+ const groupedVoiceList = await fetchVoiceList();
280
+
281
+ const html = `<!DOCTYPE html>
282
+ <html lang="zh-CN">
283
+ <head>
284
+ <meta charset="UTF-8">
285
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
286
+ <title>Edge TTS 语音合成演示</title>
287
+ <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
288
+ <style>
289
+ :root {
290
+ --primary-color: #1890ff;
291
+ --primary-light: #40a9ff;
292
+ --primary-dark: #096dd9;
293
+ --secondary-color: #52c41a;
294
+ --accent-color: #722ed1;
295
+ --text-color: #262626;
296
+ --text-secondary: #8c8c8c;
297
+ --bg-color: #ffffff;
298
+ --bg-secondary: #fafafa;
299
+ --border-color: #d9d9d9;
300
+ --border-radius: 8px;
301
+ --shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
302
+ --shadow-hover: 0 4px 16px rgba(0, 0, 0, 0.15);
303
+ }
304
+
305
+ * {
306
+ box-sizing: border-box;
307
+ margin: 0;
308
+ padding: 0;
309
+ }
310
+
311
+ body {
312
+ font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
313
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
314
+ color: var(--text-color);
315
+ min-height: 100vh;
316
+ line-height: 1.6;
317
+ overflow-x: hidden;
318
+ }
319
+
320
+ .container {
321
+ max-width: 1200px;
322
+ margin: 0 auto;
323
+ padding: 20px;
324
+ min-height: 100vh;
325
+ }
326
+
327
+ .header {
328
+ text-align: center;
329
+ margin-bottom: 30px;
330
+ color: white;
331
+ }
332
+
333
+ .header h1 {
334
+ font-size: clamp(1.8rem, 4vw, 3rem);
335
+ font-weight: 700;
336
+ margin-bottom: 10px;
337
+ text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
338
+ }
339
+
340
+ .header p {
341
+ font-size: clamp(1rem, 2.5vw, 1.2rem);
342
+ opacity: 0.9;
343
+ font-weight: 300;
344
+ }
345
+
346
+ .main-content {
347
+ display: grid;
348
+ grid-template-columns: 1fr;
349
+ gap: 20px;
350
+ background: var(--bg-color);
351
+ border-radius: var(--border-radius);
352
+ box-shadow: var(--shadow);
353
+ overflow: hidden;
354
+ }
355
+
356
+ @media (min-width: 768px) {
357
+ .main-content {
358
+ grid-template-columns: 1fr 1fr;
359
+ }
360
+ }
361
+
362
+ .panel {
363
+ padding: 24px;
364
+ }
365
+
366
+ .panel-title {
367
+ font-size: 1.5rem;
368
+ font-weight: 600;
369
+ color: var(--primary-color);
370
+ margin-bottom: 20px;
371
+ display: flex;
372
+ align-items: center;
373
+ gap: 8px;
374
+ }
375
+
376
+ .input-panel {
377
+ border-right: none;
378
+ }
379
+
380
+ @media (min-width: 768px) {
381
+ .input-panel {
382
+ border-right: 1px solid var(--border-color);
383
+ }
384
+ }
385
+
386
+ .form-group {
387
+ margin-bottom: 20px;
388
+ }
389
+
390
+ .form-label {
391
+ display: block;
392
+ font-weight: 500;
393
+ color: var(--text-color);
394
+ margin-bottom: 8px;
395
+ font-size: 0.9rem;
396
+ }
397
+
398
+ .form-input {
399
+ width: 100%;
400
+ padding: 12px 16px;
401
+ border: 2px solid var(--border-color);
402
+ border-radius: var(--border-radius);
403
+ font-size: 14px;
404
+ transition: all 0.3s ease;
405
+ background: var(--bg-color);
406
+ }
407
+
408
+ .form-input:focus {
409
+ outline: none;
410
+ border-color: var(--primary-color);
411
+ box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.1);
412
+ }
413
+
414
+ .form-textarea {
415
+ min-height: 120px;
416
+ resize: vertical;
417
+ font-family: inherit;
418
+ }
419
+
420
+ .slider-container {
421
+ margin-bottom: 20px;
422
+ }
423
+
424
+ .slider-wrapper {
425
+ position: relative;
426
+ margin-bottom: 8px;
427
+ }
428
+
429
+ .slider {
430
+ width: 100%;
431
+ height: 6px;
432
+ border-radius: 3px;
433
+ background: var(--border-color);
434
+ outline: none;
435
+ -webkit-appearance: none;
436
+ appearance: none;
437
+ cursor: pointer;
438
+ }
439
+
440
+ .slider::-webkit-slider-thumb {
441
+ -webkit-appearance: none;
442
+ appearance: none;
443
+ width: 20px;
444
+ height: 20px;
445
+ border-radius: 50%;
446
+ background: var(--primary-color);
447
+ cursor: pointer;
448
+ border: 2px solid white;
449
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
450
+ transition: all 0.3s ease;
451
+ }
452
+
453
+ .slider::-webkit-slider-thumb:hover {
454
+ transform: scale(1.1);
455
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
456
+ }
457
+
458
+ .slider::-moz-range-thumb {
459
+ width: 20px;
460
+ height: 20px;
461
+ border-radius: 50%;
462
+ background: var(--primary-color);
463
+ cursor: pointer;
464
+ border: 2px solid white;
465
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
466
+ }
467
+
468
+ .slider-value {
469
+ font-size: 0.85rem;
470
+ color: var(--text-secondary);
471
+ text-align: center;
472
+ background: var(--bg-secondary);
473
+ padding: 4px 8px;
474
+ border-radius: 4px;
475
+ min-width: 60px;
476
+ }
477
+
478
+ .voice-container {
479
+ max-height: 60vh;
480
+ overflow-y: auto;
481
+ border: 1px solid var(--border-color);
482
+ border-radius: var(--border-radius);
483
+ background: var(--bg-secondary);
484
+ }
485
+
486
+ .voice-container::-webkit-scrollbar {
487
+ width: 6px;
488
+ }
489
+
490
+ .voice-container::-webkit-scrollbar-track {
491
+ background: var(--bg-secondary);
492
+ }
493
+
494
+ .voice-container::-webkit-scrollbar-thumb {
495
+ background: var(--border-color);
496
+ border-radius: 3px;
497
+ }
498
+
499
+ .voice-container::-webkit-scrollbar-thumb:hover {
500
+ background: var(--text-secondary);
501
+ }
502
+
503
+ .voice-group {
504
+ border-bottom: 1px solid var(--border-color);
505
+ background: white;
506
+ transition: all 0.3s ease;
507
+ }
508
+
509
+ .voice-group:last-child {
510
+ border-bottom: none;
511
+ }
512
+
513
+ .voice-header {
514
+ padding: 16px 20px;
515
+ cursor: pointer;
516
+ display: flex;
517
+ justify-content: space-between;
518
+ align-items: center;
519
+ background: var(--bg-color);
520
+ transition: all 0.3s ease;
521
+ border-left: 4px solid transparent;
522
+ }
523
+
524
+ .voice-header:hover {
525
+ background: var(--bg-secondary);
526
+ border-left-color: var(--primary-color);
527
+ }
528
+
529
+ .voice-header.active {
530
+ background: var(--primary-color);
531
+ color: white;
532
+ border-left-color: var(--primary-dark);
533
+ }
534
+
535
+ .voice-header-title {
536
+ font-weight: 500;
537
+ font-size: 0.95rem;
538
+ }
539
+
540
+ .voice-header-count {
541
+ font-size: 0.8rem;
542
+ opacity: 0.8;
543
+ background: rgba(255, 255, 255, 0.2);
544
+ padding: 2px 8px;
545
+ border-radius: 12px;
546
+ margin-left: 8px;
547
+ }
548
+
549
+ .chevron {
550
+ transition: transform 0.3s ease;
551
+ font-size: 0.8rem;
552
+ }
553
+
554
+ .voice-group.open .chevron {
555
+ transform: rotate(180deg);
556
+ }
557
+
558
+ .voice-buttons {
559
+ padding: 16px 20px;
560
+ display: none;
561
+ gap: 8px;
562
+ flex-wrap: wrap;
563
+ background: var(--bg-secondary);
564
+ }
565
+
566
+ .voice-group.open .voice-buttons {
567
+ display: flex;
568
+ }
569
+
570
+ .voice-button {
571
+ background: white;
572
+ color: var(--text-color);
573
+ border: 1px solid var(--border-color);
574
+ padding: 8px 16px;
575
+ border-radius: 20px;
576
+ cursor: pointer;
577
+ transition: all 0.3s ease;
578
+ font-size: 0.85rem;
579
+ font-weight: 400;
580
+ white-space: nowrap;
581
+ position: relative;
582
+ overflow: hidden;
583
+ }
584
+
585
+ .voice-button:hover {
586
+ border-color: var(--primary-color);
587
+ color: var(--primary-color);
588
+ transform: translateY(-1px);
589
+ box-shadow: 0 2px 8px rgba(24, 144, 255, 0.2);
590
+ }
591
+
592
+ .voice-button:active {
593
+ transform: translateY(0);
594
+ }
595
+
596
+ .voice-button.playing {
597
+ background: var(--secondary-color);
598
+ color: white;
599
+ border-color: var(--secondary-color);
600
+ animation: pulse 1.5s infinite;
601
+ }
602
+
603
+ @keyframes pulse {
604
+ 0% { transform: scale(1); }
605
+ 50% { transform: scale(1.05); }
606
+ 100% { transform: scale(1); }
607
+ }
608
+
609
+ .chinese-voices {
610
+ background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
611
+ }
612
+
613
+ .chinese-voices .voice-header {
614
+ background: rgba(255, 255, 255, 0.9);
615
+ }
616
+
617
+ .chinese-voices .voice-header:hover {
618
+ background: rgba(255, 255, 255, 1);
619
+ }
620
+
621
+ .chinese-voices .voice-header.active {
622
+ background: var(--accent-color);
623
+ }
624
+
625
+ .loading {
626
+ display: inline-block;
627
+ width: 16px;
628
+ height: 16px;
629
+ border: 2px solid var(--border-color);
630
+ border-radius: 50%;
631
+ border-top-color: var(--primary-color);
632
+ animation: spin 1s linear infinite;
633
+ margin-right: 8px;
634
+ }
635
+
636
+ @keyframes spin {
637
+ to { transform: rotate(360deg); }
638
+ }
639
+
640
+ .status-message {
641
+ margin-top: 16px;
642
+ padding: 12px 16px;
643
+ border-radius: var(--border-radius);
644
+ font-size: 0.9rem;
645
+ text-align: center;
646
+ display: none;
647
+ }
648
+
649
+ .status-message.show {
650
+ display: block;
651
+ }
652
+
653
+ .status-message.success {
654
+ background: #f6ffed;
655
+ color: #52c41a;
656
+ border: 1px solid #b7eb8f;
657
+ }
658
+
659
+ .status-message.error {
660
+ background: #fff2f0;
661
+ color: #ff4d4f;
662
+ border: 1px solid #ffccc7;
663
+ }
664
+
665
+ .mobile-controls {
666
+ position: fixed;
667
+ bottom: 0;
668
+ left: 0;
669
+ right: 0;
670
+ background: white;
671
+ padding: 16px 20px;
672
+ box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.1);
673
+ border-top: 1px solid var(--border-color);
674
+ display: none;
675
+ z-index: 1000;
676
+ }
677
+
678
+ @media (max-width: 767px) {
679
+ .mobile-controls {
680
+ display: block;
681
+ }
682
+
683
+ .voice-container {
684
+ margin-bottom: 80px;
685
+ }
686
+ }
687
+
688
+ .current-audio-info {
689
+ font-size: 0.8rem;
690
+ color: var(--text-secondary);
691
+ margin-bottom: 8px;
692
+ }
693
+
694
+ .audio-controls {
695
+ display: flex;
696
+ gap: 12px;
697
+ align-items: center;
698
+ }
699
+
700
+ .control-button {
701
+ background: var(--primary-color);
702
+ color: white;
703
+ border: none;
704
+ padding: 10px 16px;
705
+ border-radius: var(--border-radius);
706
+ cursor: pointer;
707
+ font-size: 0.9rem;
708
+ transition: all 0.3s ease;
709
+ flex: 1;
710
+ }
711
+
712
+ .control-button:hover {
713
+ background: var(--primary-dark);
714
+ transform: translateY(-1px);
715
+ }
716
+
717
+ .control-button:disabled {
718
+ background: var(--border-color);
719
+ color: var(--text-secondary);
720
+ cursor: not-allowed;
721
+ transform: none;
722
+ }
723
+
724
+ @media (max-width: 480px) {
725
+ .container {
726
+ padding: 10px;
727
+ }
728
+
729
+ .panel {
730
+ padding: 16px;
731
+ }
732
+
733
+ .voice-button {
734
+ padding: 6px 12px;
735
+ font-size: 0.8rem;
736
+ }
737
+ }
738
+
739
+ .filter-section {
740
+ margin-bottom: 20px;
741
+ padding: 16px;
742
+ background: var(--bg-secondary);
743
+ border-radius: var(--border-radius);
744
+ border: 1px solid var(--border-color);
745
+ }
746
+
747
+ .filter-tabs {
748
+ display: flex;
749
+ gap: 8px;
750
+ margin-bottom: 12px;
751
+ flex-wrap: wrap;
752
+ }
753
+
754
+ .filter-tab {
755
+ padding: 6px 12px;
756
+ border: 1px solid var(--border-color);
757
+ border-radius: 16px;
758
+ background: white;
759
+ cursor: pointer;
760
+ font-size: 0.8rem;
761
+ transition: all 0.3s ease;
762
+ }
763
+
764
+ .filter-tab.active {
765
+ background: var(--primary-color);
766
+ color: white;
767
+ border-color: var(--primary-color);
768
+ }
769
+
770
+ .filter-tab:hover:not(.active) {
771
+ border-color: var(--primary-light);
772
+ color: var(--primary-color);
773
+ }
774
+ </style>
775
+ </head>
776
+
777
+ <body>
778
+ <div class="container">
779
+ <div class="header">
780
+ <h1>🎵 Edge TTS 语音合成</h1>
781
+ <p>支持多语言高质量语音合成,特别优化中文语音体验</p>
782
+ </div>
783
+
784
+ <div class="main-content">
785
+ <div class="panel input-panel">
786
+ <h2 class="panel-title">
787
+ 📝 输入设置
788
+ </h2>
789
+
790
+ <div class="filter-section">
791
+ <div class="filter-tabs">
792
+ <div class="filter-tab active" data-filter="chinese">中文语音</div>
793
+ <div class="filter-tab" data-filter="english">英文语音</div>
794
+ <div class="filter-tab" data-filter="multilingual">多语言</div>
795
+ <div class="filter-tab" data-filter="all">全部语音</div>
796
+ </div>
797
+ <input type="text" id="customFilter" class="form-input" placeholder="自定义筛选关键词...">
798
+ </div>
799
+
800
+ <div class="form-group">
801
+ <label class="form-label" for="rate">语速调节</label>
802
+ <div class="slider-wrapper">
803
+ <input type="range" min="-1" max="1" step="0.1" value="-0.1" class="slider" id="rate">
804
+ </div>
805
+ <div class="slider-value" id="rateValue">-0.1</div>
806
+ </div>
807
+
808
+ <div class="form-group">
809
+ <label class="form-label" for="pitch">音调调节</label>
810
+ <div class="slider-wrapper">
811
+ <input type="range" min="-1" max="1" step="0.1" value="0.1" class="slider" id="pitch">
812
+ </div>
813
+ <div class="slider-value" id="pitchValue">0.1</div>
814
+ </div>
815
+
816
+ <div class="form-group">
817
+ <label class="form-label" for="inputText">输入文本</label>
818
+ <textarea id="inputText" class="form-input form-textarea" placeholder="请输入要转换为语音的文本内容...">你好,欢迎使用Edge TTS语音合成服务!这里支持多种中文语音选择。</textarea>
819
+ </div>
820
+
821
+ <div class="status-message" id="statusMessage"></div>
822
+ </div>
823
+
824
+ <div class="panel">
825
+ <h2 class="panel-title">
826
+ 🎤 语音选择
827
+ </h2>
828
+
829
+ <div class="voice-container" id="voices"></div>
830
+ </div>
831
+ </div>
832
+
833
+ <div class="mobile-controls">
834
+ <div class="current-audio-info" id="currentAudioInfo">选择语音后开始合成</div>
835
+ <div class="audio-controls">
836
+ <button class="control-button" id="pauseBtn" disabled>暂停</button>
837
+ <button class="control-button" id="stopBtn" disabled>停止</button>
838
+ </div>
839
+ </div>
840
+ </div>
841
+
842
+ <script>
843
+ const voiceList = ${JSON.stringify(groupedVoiceList)};
844
+ let audio = null;
845
+ let currentVoiceButton = null;
846
+ let currentFilter = 'chinese';
847
+
848
+ // 中文语音配置
849
+ const chineseVoiceMapping = {
850
+ 'zh-CN': '中文 (普通话)',
851
+ 'zh-HK': '中文 (粤语)',
852
+ 'zh-TW': '中文 (台湾话)',
853
+ 'zh-CN-liaoning': '中文 (辽宁话)',
854
+ 'zh-CN-shaanxi': '中文 (陕西话)'
855
+ };
856
+
857
+ const filterPresets = {
858
+ chinese: ['zh-CN', 'zh-HK', 'zh-TW', 'zh-CN-liaoning', 'zh-CN-shaanxi'],
859
+ english: ['en-US', 'en-GB', 'en-AU', 'en-CA', 'en-IN'],
860
+ multilingual: Object.keys(voiceList).filter(locale =>
861
+ !locale.startsWith('zh-') && !locale.startsWith('en-')
862
+ ).slice(0, 10)
863
+ };
864
+
865
+ function showStatusMessage(message, type = 'success') {
866
+ const statusEl = document.getElementById('statusMessage');
867
+ statusEl.textContent = message;
868
+ statusEl.className = \`status-message show \${type}\`;
869
+ setTimeout(() => {
870
+ statusEl.classList.remove('show');
871
+ }, 3000);
872
+ }
873
+
874
+ function updateMobileControls(voiceName = '') {
875
+ const infoEl = document.getElementById('currentAudioInfo');
876
+ const pauseBtn = document.getElementById('pauseBtn');
877
+ const stopBtn = document.getElementById('stopBtn');
878
+
879
+ if (voiceName) {
880
+ infoEl.textContent = \`当前语音: \${voiceName}\`;
881
+ pauseBtn.disabled = false;
882
+ stopBtn.disabled = false;
883
+ } else {
884
+ infoEl.textContent = '选择语音后开始合成';
885
+ pauseBtn.disabled = true;
886
+ stopBtn.disabled = true;
887
+ }
888
+ }
889
+
890
+ function filterVoices(filterType = 'chinese', customKeyword = '') {
891
+ const voicesDiv = document.getElementById('voices');
892
+ voicesDiv.innerHTML = '';
893
+
894
+ let filteredVoices = {};
895
+
896
+ if (filterType === 'all') {
897
+ filteredVoices = voiceList;
898
+ } else if (filterPresets[filterType]) {
899
+ for (const locale of filterPresets[filterType]) {
900
+ if (voiceList[locale]) {
901
+ filteredVoices[locale] = voiceList[locale];
902
+ }
903
+ }
904
+ }
905
+
906
+ // 应用自定义关键词过滤
907
+ if (customKeyword.trim()) {
908
+ const keyword = customKeyword.trim().toLowerCase();
909
+ const tempFiltered = {};
910
+
911
+ for (const [locale, voices] of Object.entries(filteredVoices)) {
912
+ const matchingVoices = voices.filter(voice =>
913
+ voice.name.toLowerCase().includes(keyword) ||
914
+ voice.friendlyName.toLowerCase().includes(keyword) ||
915
+ locale.toLowerCase().includes(keyword)
916
+ );
917
+
918
+ if (matchingVoices.length > 0) {
919
+ tempFiltered[locale] = matchingVoices;
920
+ }
921
+ }
922
+
923
+ filteredVoices = tempFiltered;
924
+ }
925
+
926
+ // 渲染语音组
927
+ for (const [locale, voices] of Object.entries(filteredVoices)) {
928
+ const group = document.createElement('div');
929
+ group.className = \`voice-group \${filterType === 'chinese' ? 'chinese-voices' : ''}\`;
930
+
931
+ const header = document.createElement('div');
932
+ header.className = 'voice-header';
933
+
934
+ const displayName = chineseVoiceMapping[locale] || locale.toUpperCase();
935
+ const headerTitle = document.createElement('div');
936
+ headerTitle.innerHTML = \`
937
+ <span class="voice-header-title">\${displayName}</span>
938
+ <span class="voice-header-count">\${voices.length}个</span>
939
+ \`;
940
+
941
+ const chevron = document.createElement('span');
942
+ chevron.className = 'chevron';
943
+ chevron.innerHTML = '▼';
944
+
945
+ header.appendChild(headerTitle);
946
+ header.appendChild(chevron);
947
+
948
+ const buttonsContainer = document.createElement('div');
949
+ buttonsContainer.className = 'voice-buttons';
950
+
951
+ voices.forEach(({model, name, friendlyName}) => {
952
+ const button = document.createElement('button');
953
+ button.className = 'voice-button';
954
+
955
+ // 简化显示名称
956
+ const displayName = name.replace(/Neural$/, '').split('-').pop() || name;
957
+ button.textContent = displayName;
958
+ button.title = friendlyName;
959
+
960
+ button.onclick = () => synthesize(model, button, displayName);
961
+ buttonsContainer.appendChild(button);
962
+ });
963
+
964
+ header.onclick = () => {
965
+ group.classList.toggle('open');
966
+ header.classList.toggle('active');
967
+ };
968
+
969
+ group.appendChild(header);
970
+ group.appendChild(buttonsContainer);
971
+ voicesDiv.appendChild(group);
972
+
973
+ // 默认展开中文语音组
974
+ if (filterType === 'chinese') {
975
+ group.classList.add('open');
976
+ header.classList.add('active');
977
+ }
978
+ }
979
+ }
980
+
981
+ function synthesize(model, buttonElement, voiceName) {
982
+ const text = document.getElementById('inputText').value || '你好,欢迎使用Edge TTS语音合成服务!';
983
+ const rate = document.getElementById('rate').value || '-0.1';
984
+ const pitch = document.getElementById('pitch').value || '0.1';
985
+ const voice = \`rate:\${rate}|pitch:\${pitch}\`;
986
+
987
+ // 重置之前的按钮状态
988
+ if (currentVoiceButton) {
989
+ currentVoiceButton.classList.remove('playing');
990
+ currentVoiceButton.innerHTML = currentVoiceButton.textContent;
991
+ }
992
+
993
+ // 设置当前按钮状态
994
+ currentVoiceButton = buttonElement;
995
+ buttonElement.classList.add('playing');
996
+ buttonElement.innerHTML = '<span class="loading"></span>' + buttonElement.textContent;
997
+
998
+ // 停止之前的音频
999
+ if (audio) {
1000
+ audio.pause();
1001
+ audio.currentTime = 0;
1002
+ }
1003
+
1004
+ updateMobileControls(voiceName);
1005
+ showStatusMessage('正在合成语音,请稍候...', 'success');
1006
+
1007
+ fetch('/v1/audio/speech', {
1008
+ method: 'POST',
1009
+ headers: {'Content-Type': 'application/json'},
1010
+ body: JSON.stringify({model, input: text, voice})
1011
+ })
1012
+ .then(response => {
1013
+ if (!response.ok) {
1014
+ throw new Error('合成失败');
1015
+ }
1016
+ return response.blob();
1017
+ })
1018
+ .then(blob => {
1019
+ const audioUrl = URL.createObjectURL(blob);
1020
+ audio = new Audio(audioUrl);
1021
+
1022
+ audio.onplay = () => {
1023
+ showStatusMessage(\`正在播放: \${voiceName}\`, 'success');
1024
+ };
1025
+
1026
+ audio.onended = () => {
1027
+ buttonElement.classList.remove('playing');
1028
+ buttonElement.innerHTML = buttonElement.textContent;
1029
+ updateMobileControls();
1030
+ showStatusMessage('播放完成', 'success');
1031
+ };
1032
+
1033
+ audio.onerror = () => {
1034
+ buttonElement.classList.remove('playing');
1035
+ buttonElement.innerHTML = buttonElement.textContent;
1036
+ updateMobileControls();
1037
+ showStatusMessage('播放失败', 'error');
1038
+ };
1039
+
1040
+ audio.play();
1041
+ })
1042
+ .catch(error => {
1043
+ buttonElement.classList.remove('playing');
1044
+ buttonElement.innerHTML = buttonElement.textContent;
1045
+ updateMobileControls();
1046
+ showStatusMessage('合成失败: ' + error.message, 'error');
1047
+ });
1048
+ }
1049
+
1050
+ // 事件监听器设置
1051
+ document.addEventListener('DOMContentLoaded', function() {
1052
+ // 筛选标签页
1053
+ document.querySelectorAll('.filter-tab').forEach(tab => {
1054
+ tab.addEventListener('click', function() {
1055
+ document.querySelectorAll('.filter-tab').forEach(t => t.classList.remove('active'));
1056
+ this.classList.add('active');
1057
+ currentFilter = this.dataset.filter;
1058
+ filterVoices(currentFilter, document.getElementById('customFilter').value);
1059
+ });
1060
+ });
1061
+
1062
+ // 自定义筛选
1063
+ document.getElementById('customFilter').addEventListener('input', function() {
1064
+ filterVoices(currentFilter, this.value);
1065
+ });
1066
+
1067
+ // 滑块控制
1068
+ const rateSlider = document.getElementById('rate');
1069
+ const rateValue = document.getElementById('rateValue');
1070
+ rateSlider.oninput = function() {
1071
+ rateValue.textContent = this.value;
1072
+ };
1073
+
1074
+ const pitchSlider = document.getElementById('pitch');
1075
+ const pitchValue = document.getElementById('pitchValue');
1076
+ pitchSlider.oninput = function() {
1077
+ pitchValue.textContent = this.value;
1078
+ };
1079
+
1080
+ // 移动端控制
1081
+ document.getElementById('pauseBtn').addEventListener('click', function() {
1082
+ if (audio) {
1083
+ if (audio.paused) {
1084
+ audio.play();
1085
+ this.textContent = '暂停';
1086
+ } else {
1087
+ audio.pause();
1088
+ this.textContent = '继续';
1089
+ }
1090
+ }
1091
+ });
1092
+
1093
+ document.getElementById('stopBtn').addEventListener('click', function() {
1094
+ if (audio) {
1095
+ audio.pause();
1096
+ audio.currentTime = 0;
1097
+ if (currentVoiceButton) {
1098
+ currentVoiceButton.classList.remove('playing');
1099
+ currentVoiceButton.innerHTML = currentVoiceButton.textContent;
1100
+ }
1101
+ updateMobileControls();
1102
+ document.getElementById('pauseBtn').textContent = '暂停';
1103
+ }
1104
+ });
1105
+
1106
+ // 初始化
1107
+ filterVoices('chinese');
1108
+ });
1109
+ </script>
1110
+ </body>
1111
+ </html>`;
1112
+
1113
+ return new Response(html, {
1114
+ headers: { "Content-Type": "text/html" },
1115
+ });
1116
+ }
1117
 
1118
  serve(async (req) => {
1119
  try {