-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
693 lines (598 loc) · 26.3 KB
/
Copy pathserver.py
File metadata and controls
693 lines (598 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
"""MiMo TTS API 后端服务
FastAPI 实现,提供 RESTful TTS 接口。
"""
from __future__ import annotations
import asyncio
import base64
import io
import json
import logging
import tempfile
from pathlib import Path
from typing import Literal
from fastapi import FastAPI, HTTPException, Request, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, Response, StreamingResponse
from pydantic import BaseModel
from config import Settings
from director.agent import DirectorAgent
from mimo_tts.audio_utils import estimate_duration, pcm16_to_wav, read_audio_to_b64
from mimo_tts.client import MiMoTTSClient
from pipeline.batch import BatchProcessor
from pipeline.splitter import TextSplitter
from voice_lab.manager import VoiceAssetManager
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# ──────────────────────────────────────────────
# 初始化
# ──────────────────────────────────────────────
settings = Settings() # type: ignore[call-arg]
client = MiMoTTSClient(
api_key=settings.mimo_api_key,
base_url=settings.mimo_base_url,
default_model=settings.tts_model,
default_voice=settings.default_voice,
)
director = DirectorAgent(
api_key=settings.llm_api_key or settings.mimo_api_key,
base_url=settings.llm_base_url or settings.mimo_base_url,
model=settings.llm_model,
)
voice_manager = VoiceAssetManager(settings.voices_db_path)
app = FastAPI(title="MiMo TTS Studio", version="0.1.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# ──────────────────────────────────────────────
# 请求/响应模型
# ──────────────────────────────────────────────
class SynthesizeRequest(BaseModel):
text: str
director_instruction: str = ""
model: Literal["mimo-v2.5-tts", "mimo-v2.5-tts-voicedesign", "mimo-v2.5-tts-voiceclone"] = "mimo-v2.5-tts"
voice: str = "Chloe"
audio_format: Literal["wav", "mp3", "pcm16"] = "wav"
auto_director: bool = False
class VoiceDesignRequest(BaseModel):
voice_description: str
text: str = "你好,这是一个音色预览测试。"
audio_format: Literal["wav", "mp3", "pcm16"] = "wav"
class VoiceCloneRequest(BaseModel):
text: str
audio_format: Literal["wav", "mp3", "pcm16"] = "wav"
director_instruction: str = ""
class BatchRequest(BaseModel):
texts: list[str]
ids: list[str] | None = None
director_instruction: str = ""
auto_director: bool = True
model: str = "mimo-v2.5-tts"
voice: str = "Chloe"
concurrency: int = 10
class TTSResponse(BaseModel):
audio_base64: str
audio_format: str
duration_seconds: float
size_bytes: int
# ──────────────────────────────────────────────
# 端点
# ──────────────────────────────────────────────
@app.get("/")
def root():
return {"name": "MiMo TTS Studio", "version": "0.1.0", "status": "running"}
@app.get("/voices")
def list_voices():
"""列出所有可用音色"""
return {
"built_in": voice_manager.get_built_in_voices(),
"custom": [v for v in voice_manager.list_voices() if v.get("source") != "built_in"],
}
# ── OpenAI 兼容端点(Kokoro 等第三方工具需要) ──
@app.get("/v1/models")
async def get_models():
"""返回可用模型列表(OpenAI 兼容格式)"""
return {
"data": [
{"id": "mimo-v2.5-tts", "object": "model"},
{"id": "mimo-v2.5-tts-voicedesign", "object": "model"},
{"id": "mimo-v2.5-tts-voiceclone", "object": "model"},
]
}
@app.get("/v1/audio/voices")
async def get_voices():
"""返回可用音色列表(OpenAI 兼容格式,Kokoro 扩展需要)"""
all_voices = voice_manager.list_voices()
return {"voices": [v["name"] for v in all_voices]}
@app.post("/v1/audio/speech")
async def audio_speech(raw_request: Request):
"""Kokoro / Custom TTS Reader 合成端点(OpenAI TTS 兼容)
stream=false(默认): 返回完整 MP3/WAV 文件
stream=true: 返回 raw PCM16 流(audio/l16;rate=24000)
"""
# 先获取原始字节再解析 JSON,兼容非 UTF-8 编码
body = await raw_request.body()
try:
request = json.loads(body.decode("utf-8", errors="replace"))
except Exception:
# 尝试 GBK 编码(Windows 客户端常用)
try:
request = json.loads(body.decode("gbk", errors="replace"))
except Exception:
request = {}
text = request.get("input", "")
voice_input = request.get("voice", "Chloe")
clean_voice = voice_input.split("_", 1)[-1] if "_" in voice_input else voice_input
response_format = request.get("response_format", "mp3")
stream = request.get("stream", False)
# 检测自定义音色类型
voice_info = voice_manager.get_voice(clean_voice)
source = voice_info.get("source", "built_in") if voice_info else None
if stream:
def generate_pcm():
if source == "voiceclone":
clone_data = voice_manager.get_voiceclone_b64(clean_voice)
if not clone_data:
raise ValueError(f"voiceclone 音色数据缺失: {clean_voice}")
b64_data, mime = clone_data
for chunk in client.voice_clone_stream(b64_data, mime, text, ""):
yield chunk
elif source == "voicedesign":
desc = voice_info.get("description", "")
if not desc:
raise ValueError(f"voicedesign 音色描述缺失: {clean_voice}")
audio = client.voice_design(desc, text, "wav")
pcm = audio[44:]
chunk_size = 4096
for i in range(0, len(pcm), chunk_size):
yield pcm[i:i + chunk_size]
else:
for chunk in client.synthesize_stream(text, "", "mimo-v2.5-tts", clean_voice):
yield chunk
return StreamingResponse(generate_pcm(), media_type="audio/l16;rate=24000;channels=1")
else:
audio_format = "mp3" if response_format == "mp3" else "wav"
def _synthesize():
if source == "voiceclone":
clone_data = voice_manager.get_voiceclone_b64(clean_voice)
if not clone_data:
raise ValueError(f"voiceclone 音色数据缺失: {clean_voice}")
b64_data, mime = clone_data
return client.voice_clone_from_b64(b64_data, mime, text, audio_format, "")
elif source == "voicedesign":
desc = voice_info.get("description", "")
if not desc:
raise ValueError(f"voicedesign 音色描述缺失: {clean_voice}")
return client.voice_design(desc, text, audio_format)
else:
return client.synthesize(text, "", "mimo-v2.5-tts", clean_voice, audio_format)
loop = asyncio.get_event_loop()
audio_bytes = await loop.run_in_executor(None, _synthesize)
media = "audio/mpeg" if audio_format == "mp3" else "audio/wav"
return Response(content=audio_bytes, media_type=media)
@app.post("/synthesize", response_model=TTSResponse)
def synthesize(req: SynthesizeRequest):
"""语音合成 — 自动识别自定义音色并路由到对应 API"""
try:
instruction = req.director_instruction
if req.auto_director and not instruction:
instruction = director.generate_instruction(req.text)
voice_info = voice_manager.get_voice(req.voice)
source = voice_info.get("source", "built_in") if voice_info else None
if source == "voiceclone":
b64_data, mime = voice_manager.get_voiceclone_b64(req.voice)
if not b64_data:
raise ValueError(f"voiceclone 音色数据缺失: {req.voice}")
audio = client.voice_clone_from_b64(b64_data, mime, req.text, req.audio_format, instruction)
elif source == "voicedesign":
desc = voice_info.get("description", "")
if not desc:
raise ValueError(f"voicedesign 音色描述缺失: {req.voice}")
audio = client.voice_design(desc, req.text, req.audio_format)
else:
audio = client.synthesize(
text=req.text,
director_instruction=instruction,
model=req.model,
voice=req.voice,
audio_format=req.audio_format,
)
return TTSResponse(
audio_base64=base64.b64encode(audio).decode(),
audio_format=req.audio_format,
duration_seconds=estimate_duration(audio[44:]) if req.audio_format == "wav" else 0,
size_bytes=len(audio),
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/synthesize/raw")
def synthesize_raw(req: SynthesizeRequest):
"""语音合成 — 返回原始音频文件"""
try:
instruction = req.director_instruction
if req.auto_director and not instruction:
instruction = director.generate_instruction(req.text)
voice_info = voice_manager.get_voice(req.voice)
source = voice_info.get("source", "built_in") if voice_info else None
if source == "voiceclone":
b64_data, mime = voice_manager.get_voiceclone_b64(req.voice)
if not b64_data:
raise ValueError(f"voiceclone 音色数据缺失: {req.voice}")
audio = client.voice_clone_from_b64(b64_data, mime, req.text, req.audio_format, instruction)
elif source == "voicedesign":
desc = voice_info.get("description", "")
if not desc:
raise ValueError(f"voicedesign 音色描述缺失: {req.voice}")
audio = client.voice_design(desc, req.text, req.audio_format)
else:
audio = client.synthesize(
text=req.text,
director_instruction=instruction,
model=req.model,
voice=req.voice,
audio_format=req.audio_format,
)
media = {
"wav": "audio/wav",
"mp3": "audio/mpeg",
"pcm16": "audio/pcm",
}.get(req.audio_format, "audio/wav")
return Response(content=audio, media_type=media)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/voice-design")
def voice_design(req: VoiceDesignRequest):
"""音色设计"""
try:
audio = client.voice_design(req.voice_description, req.text, req.audio_format)
return TTSResponse(
audio_base64=base64.b64encode(audio).decode(),
audio_format=req.audio_format,
duration_seconds=estimate_duration(audio[44:]) if req.audio_format == "wav" else 0,
size_bytes=len(audio),
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/voice-clone")
async def voice_clone(
text: str,
audio_format: str = "wav",
director_instruction: str = "",
):
"""音色克隆 — 需要通过 multipart/form-data 上传音频文件"""
# 注意:这个简化版用 query params,完整版需要 UploadFile
raise HTTPException(status_code=501, detail="请使用 /voice-clone/upload 端点")
@app.post("/voice-clone/b64", response_model=TTSResponse)
def voice_clone_b64(sample_base64: str, sample_mime: str, text: str, audio_format: str = "wav", director_instruction: str = ""):
"""音色克隆 — Base64 输入"""
try:
audio = client.voice_clone_from_b64(sample_base64, sample_mime, text, audio_format, director_instruction)
return TTSResponse(
audio_base64=base64.b64encode(audio).decode(),
audio_format=audio_format,
duration_seconds=estimate_duration(audio[44:]) if audio_format == "wav" else 0,
size_bytes=len(audio),
)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/batch")
async def batch_synthesize(req: BatchRequest):
"""批量合成"""
try:
processor = BatchProcessor(
client=client,
max_concurrency=req.concurrency,
output_dir=settings.output_dir,
)
items = []
for i, text in enumerate(req.texts):
item_id = req.ids[i] if req.ids and i < len(req.ids) else f"item_{i}"
items.append({
"id": item_id,
"text": text,
"instruction": req.director_instruction,
})
director_agent = director if req.auto_director else None
results = await processor.process_texts(
items,
use_director=req.auto_director,
director_agent=director_agent,
voice=req.voice,
model=req.model,
)
return {"results": results}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/synthesize/stream")
def synthesize_stream(req: SynthesizeRequest):
"""语音合成 — SSE 流式返回 PCM16 chunks"""
def event_stream():
try:
instruction = req.director_instruction
if req.auto_director and not instruction:
instruction = director.generate_instruction(req.text)
voice_info = voice_manager.get_voice(req.voice)
source = voice_info.get("source", "built_in") if voice_info else None
if source == "voiceclone":
b64_data, mime = voice_manager.get_voiceclone_b64(req.voice)
if not b64_data:
raise ValueError(f"voiceclone 音色数据缺失: {req.voice}")
for chunk in client.voice_clone_stream(b64_data, mime, req.text, instruction):
yield f"data: {base64.b64encode(chunk).decode()}\n\n"
elif source == "voicedesign":
desc = voice_info.get("description", "")
if not desc:
raise ValueError(f"voicedesign 音色描述缺失: {req.voice}")
audio = client.voice_design(desc, req.text, req.audio_format)
pcm = audio[44:] if req.audio_format == "wav" else audio
chunk_size = 4096
for i in range(0, len(pcm), chunk_size):
yield f"data: {base64.b64encode(pcm[i:i+chunk_size]).decode()}\n\n"
else:
for chunk in client.synthesize_stream(req.text, instruction, req.model, req.voice):
yield f"data: {base64.b64encode(chunk).decode()}\n\n"
yield "data: [DONE]\n\n"
except Exception as e:
yield f"data: {{\"error\": \"{e}\"}}\n\n"
return StreamingResponse(event_stream(), media_type="text/event-stream")
@app.websocket("/ws/synthesize")
async def ws_synthesize(ws: WebSocket):
"""WebSocket 实时流式合成 — 边生成边发送 PCM16 chunks"""
await ws.accept()
try:
req = json.loads(await ws.receive_text())
text = req.get("text", "")
instruction = req.get("instruction", "")
model = req.get("model", "mimo-v2.5-tts")
voice = req.get("voice", "Chloe")
if not text.strip():
await ws.send_text(json.dumps({"type": "error", "message": "请输入合成文本"}))
await ws.close()
return
# 解析 voice → API 参数(同 /synthesize/stream 逻辑)
voice_info = voice_manager.get_voice(voice)
source = voice_info.get("source", "built_in") if voice_info else None
if source == "voiceclone":
clone_data = voice_manager.get_voiceclone_b64(voice)
if not clone_data:
await ws.send_text(json.dumps({"type": "error", "message": f"voiceclone 数据缺失: {voice}"}))
await ws.close()
return
b64_data, mime = clone_data
# 在线程池中收集所有 PCM chunks(避免阻塞事件循环)
loop = asyncio.get_event_loop()
all_chunks = []
def _collect_chunks():
for chunk in client.voice_clone_stream(b64_data, mime, text, instruction):
all_chunks.append(chunk)
await loop.run_in_executor(None, _collect_chunks)
elif source == "voicedesign":
desc = voice_info.get("description", "")
if not desc:
await ws.send_text(json.dumps({"type": "error", "message": f"voicedesign 描述缺失: {voice}"}))
await ws.close()
return
# voicedesign 暂不支持流式,走一次性合成
loop = asyncio.get_event_loop()
all_chunks = []
def _collect_chunks():
audio = client.voice_design(desc, text, "wav")
pcm = audio[44:] # 去掉 WAV 头
chunk_size = 4096
for i in range(0, len(pcm), chunk_size):
all_chunks.append(pcm[i:i + chunk_size])
await loop.run_in_executor(None, _collect_chunks)
else:
loop = asyncio.get_event_loop()
all_chunks = []
def _collect_chunks():
for chunk in client.synthesize_stream(text, instruction, model, voice):
all_chunks.append(chunk)
await loop.run_in_executor(None, _collect_chunks)
# 逐个发送 chunks
total_bytes = 0
for chunk in all_chunks:
await ws.send_bytes(chunk)
total_bytes += len(chunk)
duration = total_bytes / (24000 * 2) # 24kHz, 16-bit mono
await ws.send_text(json.dumps({"type": "done", "duration": round(duration, 2)}))
await ws.close()
except WebSocketDisconnect:
logger.info("WebSocket 客户端断开")
except Exception as e:
logger.error("WebSocket 错误: %s", e)
try:
await ws.send_text(json.dumps({"type": "error", "message": str(e)}))
await ws.close()
except Exception:
pass
PLAYER_HTML = """<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实时流式合成</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: #fafafa; padding: 16px; }
.box { max-width: 800px; margin: 0 auto; }
h3 { font-size: 17px; margin-bottom: 14px; }
h3 span { font-size: 12px; color: #999; font-weight: normal; }
.row { display: flex; gap: 12px; margin-bottom: 12px; align-items: flex-end; }
.col { flex: 1; }
.col label { font-size: 13px; color: #666; margin-bottom: 4px; display: block; }
textarea, select { width: 100%%; padding: 8px 12px; border: 1px solid #ddd; border-radius: 6px; font-size: 14px; }
textarea:focus, select:focus { border-color: #f97316; outline: none; }
.btn { padding: 10px 28px; background: #f97316; color: #fff; border: none; border-radius: 6px; font-size: 15px; font-weight: 600; cursor: pointer; }
.btn:hover { background: #ea580c; }
.btn:disabled { background: #ccc; cursor: not-allowed; }
.status { margin-top: 8px; padding: 8px 12px; border-radius: 6px; font-size: 13px; background: #f5f5f5; color: #666; }
.status.playing { background: #fef3c7; color: #92400e; }
.status.done { background: #d1fae5; color: #065f46; }
.status.error { background: #fee2e2; color: #991b1b; }
.wave { display: flex; align-items: center; gap: 3px; height: 24px; margin-top: 6px; }
.wave span { display: inline-block; width: 3px; background: #f97316; border-radius: 2px; animation: bounce 0.6s ease-in-out infinite; }
.wave span:nth-child(2) { animation-delay: 0.1s; }
.wave span:nth-child(3) { animation-delay: 0.2s; }
.wave span:nth-child(4) { animation-delay: 0.3s; }
.wave span:nth-child(5) { animation-delay: 0.4s; }
@keyframes bounce { 0%,100%% { height: 6px; } 50%% { height: 20px; } }
</style>
</head>
<body>
<div class="box">
<h3>实时流式合成 <span>WebSocket + Web Audio API</span></h3>
<div class="row">
<div class="col" style="flex:2">
<label>合成文本</label>
<textarea id="ws-text" rows="3" placeholder="输入要合成的文本..."></textarea>
</div>
<div class="col" style="flex:1">
<label>导演指令</label>
<textarea id="ws-instruction" rows="3" placeholder="可选:温柔、缓慢..."></textarea>
</div>
</div>
<div class="row">
<div class="col">
<label>模型</label>
<select id="ws-model">
<option value="mimo-v2.5-tts">mimo-v2.5-tts</option>
<option value="mimo-v2.5-tts-voicedesign">mimo-v2.5-tts-voicedesign</option>
<option value="mimo-v2.5-tts-voiceclone">mimo-v2.5-tts-voiceclone</option>
</select>
</div>
<div class="col">
<label>音色</label>
<select id="ws-voice"></select>
</div>
<div class="col" style="flex:0 0 auto; align-self:flex-end;">
<button class="btn" id="ws-btn" onclick="doSynth()">▶ 合成</button>
</div>
</div>
<div class="status" id="ws-status">就绪</div>
<div class="wave" id="ws-wave" style="display:none;"><span></span><span></span><span></span><span></span><span></span></div>
<audio id="ws-audio" controls style="width:100%%;margin-top:12px;"></audio>
</div>
<script>
var WS_URL = 'ws://' + location.hostname + ':18900/ws/synthesize';
var API_URL = 'http://' + location.hostname + ':18900';
var SR = 24000;
var actx = null, nextT = 0, chunks = [], playing = false, ws = null;
function getAC() {
if (!actx) actx = new (window.AudioContext || window.webkitAudioContext)({sampleRate: SR});
if (actx.state === 'suspended') actx.resume();
return actx;
}
function setStatus(t, c) {
var el = document.getElementById('ws-status');
el.textContent = t;
el.className = 'status' + (c ? ' ' + c : '');
}
function playNext() {
if (!chunks.length) { playing = false; setStatus('播放完成', 'done'); document.getElementById('ws-wave').style.display='none'; return; }
var f32 = chunks.shift();
var ctx = getAC();
var buf = ctx.createBuffer(1, f32.length, SR);
buf.getChannelData(0).set(f32);
var s = ctx.createBufferSourceNode();
s.buffer = buf; s.connect(ctx.destination);
s.start(nextT); nextT += buf.duration;
s.onended = playNext;
}
function doSynth() {
var text = document.getElementById('ws-text').value.trim();
if (!text) { alert('请输入合成文本'); return; }
var btn = document.getElementById('ws-btn');
btn.disabled = true;
document.getElementById('ws-wave').style.display = 'flex';
if (actx) { actx.close(); actx = null; }
chunks = []; playing = false;
setStatus('正在连接...', '');
ws = new WebSocket(WS_URL);
ws.binaryType = 'arraybuffer';
ws.onopen = function() {
setStatus('已连接 — 正在合成...', 'playing');
ws.send(JSON.stringify({
text: text,
instruction: document.getElementById('ws-instruction').value.trim(),
model: document.getElementById('ws-model').value,
voice: document.getElementById('ws-voice').value,
}));
};
ws.onmessage = function(e) {
if (typeof e.data === 'string') {
var m = JSON.parse(e.data);
if (m.type === 'done') {
setStatus('合成完成 — ' + m.duration + 's', 'done');
btn.disabled = false;
document.getElementById('ws-wave').style.display = 'none';
ws.close();
if (!playing && chunks.length) { playing = true; playNext(); }
} else if (m.type === 'error') {
setStatus('错误: ' + m.message, 'error');
btn.disabled = false;
document.getElementById('ws-wave').style.display = 'none';
ws.close();
}
} else {
var i16 = new Int16Array(e.data);
var f32 = new Float32Array(i16.length);
for (var i = 0; i < i16.length; i++) f32[i] = i16[i] / 32768;
chunks.push(f32);
if (!playing && chunks.length >= 1) { playing = true; playNext(); }
}
};
ws.onerror = function() {
setStatus('连接失败 — 请确认 server.py 已启动', 'error');
btn.disabled = false;
document.getElementById('ws-wave').style.display = 'none';
};
ws.onclose = function() { btn.disabled = false; };
}
// 加载音色列表
fetch(API_URL + '/voices')
.then(function(r) { return r.json(); })
.then(function(d) {
var sel = document.getElementById('ws-voice');
var g1 = document.createElement('optgroup');
g1.label = '内置音色';
d.built_in.forEach(function(n) { var o = document.createElement('option'); o.value = n; o.textContent = n; g1.appendChild(o); });
sel.appendChild(g1);
if (d.custom.length) {
var g2 = document.createElement('optgroup');
g2.label = '自定义音色';
d.custom.forEach(function(v) { var o = document.createElement('option'); o.value = v.name; o.textContent = v.name + ' (' + v.source + ')'; g2.appendChild(o); });
sel.appendChild(g2);
}
setStatus('就绪 — ' + d.built_in.length + ' 内置, ' + d.custom.length + ' 自定义音色', '');
})
.catch(function(e) { setStatus('音色加载失败: ' + e.message, 'error'); });
</script>
</body>
</html>"""
@app.get("/player", response_class=HTMLResponse)
def player_page():
"""实时流式合成播放器页面"""
return HTMLResponse(content=PLAYER_HTML)
@app.get("/director/generate")
def generate_director(text: str):
"""生成导演指令"""
try:
instruction = director.generate_instruction(text)
return {"text": text, "instruction": instruction}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
# 启动时打印可用模型和音色,方便复制到第三方工具
models = ["mimo-v2.5-tts", "mimo-v2.5-tts-voicedesign", "mimo-v2.5-tts-voiceclone"]
voices = [v["name"] for v in voice_manager.list_voices()]
print("\n" + "=" * 50)
print(" MiMo TTS Server — OpenAI 兼容端点")
print("=" * 50)
print(f" 模型: {', '.join(models)}")
print(f" 音色: {', '.join(voices)}")
print("=" * 50 + "\n")
uvicorn.run(app, host="0.0.0.0", port=18900)