feat: launch script && tts config

This commit is contained in:
Harold 2025-11-05 13:46:16 +08:00
parent 0d829a95e0
commit 9e36cd9791
8 changed files with 105 additions and 654 deletions

View file

@ -0,0 +1,68 @@
-- 添加 ElevenLabs 和 Cartesia TTS 供应器和模型配置
-- ==================== ElevenLabs TTS ====================
-- 添加 ElevenLabs TTS 供应器
DELETE FROM `ai_model_provider` WHERE id = 'TTS_ElevenLabs';
INSERT INTO `ai_model_provider` (`id`, `model_type`, `provider_code`, `name`, `fields`, `sort`, `creator`, `create_date`, `updater`, `update_date`) VALUES
('TTS_ElevenLabs', 'TTS', 'elevenlabs', 'ElevenLabs TTS', '[{"key":"api_key","label":"API Key","type":"string"},{"key":"voice_id","label":"Voice ID","type":"string"},{"key":"model","label":"Model","type":"string"},{"key":"output_format","label":"Output Format","type":"string"},{"key":"stability","label":"Stability","type":"number"},{"key":"similarity_boost","label":"Similarity Boost","type":"number"},{"key":"style","label":"Style","type":"number"},{"key":"use_speaker_boost","label":"Use Speaker Boost","type":"string"},{"key":"optimize_streaming_latency","label":"Optimize Streaming Latency","type":"number"},{"key":"output_dir","label":"Output Directory","type":"string"}]', 21, 1, NOW(), 1, NOW());
-- 添加 ElevenLabs TTS 模型配置
DELETE FROM `ai_model_config` WHERE id = 'TTS_ElevenLabs';
INSERT INTO `ai_model_config` VALUES (
'TTS_ElevenLabs',
'TTS',
'ElevenLabs',
'ElevenLabs TTS',
0,
1,
'{\"type\": \"elevenlabs\", \"api_key\": \"\", \"voice_id\": \"21m00Tcm4TlvDq8ikWAM\", \"model\": \"eleven_multilingual_v2\", \"output_format\": \"pcm_16000\", \"stability\": 0.5, \"similarity_boost\": 0.75, \"style\": 0.0, \"use_speaker_boost\": true, \"optimize_streaming_latency\": 3, \"output_dir\": \"tmp/\"}',
'https://elevenlabs.io/docs/introduction',
'ElevenLabs TTS 配置说明:\n1. ElevenLabs 提供高质量、情感丰富的语音合成服务\n2. 支持多语言(包括英文、中文等)和声音克隆功能\n3. 使用官方 Python SDK,延迟低、质量高\n4. API Key 获取:访问 https://elevenlabs.io/ 注册并获取\n5. Voice ID 可在 ElevenLabs 控制台中找到预设音色或创建自定义音色\n6. Model 推荐使用 eleven_multilingual_v2(多语言)或 eleven_turbo_v2(低延迟)\n7. Stability (0-1): 控制语音稳定性,越高越稳定但变化少\n8. Similarity Boost (0-1): 控制与原音色的相似度\n9. Style (0-1): 控制语音风格强度\n10. Optimize Streaming Latency (0-4): 优化流式延迟,推荐 2-3',
23,
NULL,
NULL,
NULL,
NULL
);
-- 添加 ElevenLabs 音色配置
DELETE FROM `ai_tts_voice` WHERE id IN ('TTS_ElevenLabs0001', 'TTS_ElevenLabs0002', 'TTS_ElevenLabs0003', 'TTS_ElevenLabs0004');
INSERT INTO `ai_tts_voice` VALUES ('TTS_ElevenLabs0001', 'TTS_ElevenLabs', 'Rachel - Female', '21m00Tcm4TlvDq8ikWAM', 'English', NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL);
INSERT INTO `ai_tts_voice` VALUES ('TTS_ElevenLabs0002', 'TTS_ElevenLabs', 'Drew - Male', '29vD33N1CtxCmqQRPOHJ', 'English', NULL, NULL, NULL, NULL, 2, NULL, NULL, NULL, NULL);
INSERT INTO `ai_tts_voice` VALUES ('TTS_ElevenLabs0003', 'TTS_ElevenLabs', 'Clyde - Male', '2EiwWnXFnvU5JabPnv8n', 'English', NULL, NULL, NULL, NULL, 3, NULL, NULL, NULL, NULL);
INSERT INTO `ai_tts_voice` VALUES ('TTS_ElevenLabs0004', 'TTS_ElevenLabs', 'Bella - Female', 'EXAVITQu4vr4xnSDxMaL', 'English', NULL, NULL, NULL, NULL, 4, NULL, NULL, NULL, NULL);
-- ==================== Cartesia TTS ====================
-- 添加 Cartesia TTS 供应器
DELETE FROM `ai_model_provider` WHERE id = 'TTS_Cartesia';
INSERT INTO `ai_model_provider` (`id`, `model_type`, `provider_code`, `name`, `fields`, `sort`, `creator`, `create_date`, `updater`, `update_date`) VALUES
('TTS_Cartesia', 'TTS', 'cartesia', 'Cartesia TTS', '[{"key":"api_key","label":"API Key","type":"string"},{"key":"voice_id","label":"Voice ID","type":"string"},{"key":"model","label":"Model","type":"string"},{"key":"language","label":"Language","type":"string"},{"key":"encoding","label":"Encoding","type":"string"},{"key":"sample_rate","label":"Sample Rate","type":"number"},{"key":"output_dir","label":"Output Directory","type":"string"}]', 22, 1, NOW(), 1, NOW());
-- 添加 Cartesia TTS 模型配置
DELETE FROM `ai_model_config` WHERE id = 'TTS_Cartesia';
INSERT INTO `ai_model_config` VALUES (
'TTS_Cartesia',
'TTS',
'Cartesia',
'Cartesia TTS',
0,
1,
'{\"type\": \"cartesia\", \"api_key\": \"\", \"voice_id\": \"a0e99841-438c-4a64-b679-ae501e7d6091\", \"model\": \"sonic-3\", \"language\": \"en\", \"encoding\": \"pcm_s16le\", \"sample_rate\": 24000, \"output_dir\": \"tmp/\"}',
'https://docs.cartesia.ai/',
'Cartesia TTS 配置说明:\n1. Cartesia 提供超低延迟的流式语音合成服务\n2. 使用官方 Python SDK,首包延迟极低(通常 < 200ms)\n3. 支持多种语言和声音(英语、西班牙语、法语、德语、中文等)\n4. API Key 获取:访问 https://cartesia.ai/ 注册并获取\n5. Voice ID 可在 Cartesia 控制台查看预设音色\n6. Model 选项:sonic-english(英语)、sonic-multilingual(多语言)\n7. Encoding: pcm_s16le(推荐)、pcm_mulaw 等\n8. Sample Rate: 8000/16000/24000/44100(推荐 24000)\n9. 特别适合需要实时交互的场景(如语音助手、客服)',
24,
NULL,
NULL,
NULL,
NULL
);
-- 添加 Cartesia 音色配置
DELETE FROM `ai_tts_voice` WHERE id IN ('TTS_Cartesia0001', 'TTS_Cartesia0002', 'TTS_Cartesia0003', 'TTS_Cartesia0004');
INSERT INTO `ai_tts_voice` VALUES ('TTS_Cartesia0001', 'TTS_Cartesia', 'Barbershop Man', 'a0e99841-438c-4a64-b679-ae501e7d6091', 'English', NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL);
INSERT INTO `ai_tts_voice` VALUES ('TTS_Cartesia0002', 'TTS_Cartesia', 'Kentucky Man', '41534e16-2966-4c6b-9670-111411def906', 'English', NULL, NULL, NULL, NULL, 2, NULL, NULL, NULL, NULL);
INSERT INTO `ai_tts_voice` VALUES ('TTS_Cartesia0003', 'TTS_Cartesia', 'British Lady', '79a125e8-cd45-4c13-8a67-188112f4dd22', 'English', NULL, NULL, NULL, NULL, 3, NULL, NULL, NULL, NULL);
INSERT INTO `ai_tts_voice` VALUES ('TTS_Cartesia0004', 'TTS_Cartesia', 'Friendly Reader', '69267136-1bdc-412f-ad78-0caad210fb40', 'English', NULL, NULL, NULL, NULL, 4, NULL, NULL, NULL, NULL);

View file

@ -402,3 +402,10 @@ databaseChangeLog:
- sqlFile: - sqlFile:
encoding: utf8 encoding: utf8
path: classpath:db/changelog/202510191042.sql path: classpath:db/changelog/202510191042.sql
- changeSet:
id: 202511051120
author: pany
changes:
- sqlFile:
encoding: utf8
path: classpath:db/changelog/202511051120.sql

View file

@ -649,12 +649,12 @@ TTS:
output_dir: tmp/ output_dir: tmp/
# ============== SDK 版本TTS(推荐)============== # ============== SDK 版本TTS(推荐)==============
# 使用官方SDK,代码更简洁、性能更好、维护成本更低 # 使用官方SDK,代码更简洁、性能更好、维护成本更低
CartesiaSDK: Cartesia:
# Cartesia TTS - SDK版本(推荐) # Cartesia TTS - SDK版本(推荐)
# 官方SDK实现,低延迟,支持流式合成 # 官方SDK实现,低延迟,支持流式合成
# API Key获取地址:https://play.cartesia.ai/console # API Key获取地址:https://play.cartesia.ai/console
# 或设置环境变量:export CARTESIA_API_KEY=your-api-key # 或设置环境变量:export CARTESIA_API_KEY=your-api-key
type: cartesia_sdk type: cartesia
api_key: 你的Cartesia API密钥 api_key: 你的Cartesia API密钥
# Voice ID或voice embedding # Voice ID或voice embedding
voice_id: 你的语音ID voice_id: 你的语音ID
@ -667,12 +667,12 @@ TTS:
# 采样率:8000, 16000, 22050, 24000, 44100等 # 采样率:8000, 16000, 22050, 24000, 44100等
sample_rate: 24000 sample_rate: 24000
output_dir: tmp/ output_dir: tmp/
ElevenLabsSDK: ElevenLabs:
# ElevenLabs TTS - SDK版本(推荐) # ElevenLabs TTS - SDK版本(推荐)
# 官方SDK实现,代码更简洁,维护成本更低,性能更好 # 官方SDK实现,代码更简洁,维护成本更低,性能更好
# API Key获取地址:https://elevenlabs.io/app/settings/api-keys # API Key获取地址:https://elevenlabs.io/app/settings/api-keys
# 或设置环境变量:export ELEVEN_API_KEY=your-api-key # 或设置环境变量:export ELEVEN_API_KEY=your-api-key
type: elevenlabs_sdk type: elevenlabs
api_key: 你的ElevenLabs API密钥 api_key: 你的ElevenLabs API密钥
# Voice ID获取:https://elevenlabs.io/app/voice-library # Voice ID获取:https://elevenlabs.io/app/voice-library
# 默认:21m00Tcm4TlvDq8ikWAM (Rachel) # 默认:21m00Tcm4TlvDq8ikWAM (Rachel)

View file

@ -1,626 +0,0 @@
from __future__ import annotations
"""
ElevenLabs TTS Implementation
Provides WebSocket-based streaming text-to-speech using ElevenLabs' multi-stream API.
Supports multiple concurrent synthesis streams over a single WebSocket connection.
"""
"""
TODO: change api to elevenlabs SDK using streaming API
"""
import asyncio
import base64
import dataclasses
import json
import os
import weakref
from dataclasses import dataclass
from typing import Any, Union
import aiohttp
from typing import Optional
from config.logger import setup_logging
from core.tokenize import SentenceTokenizer, WordTokenizer, blingfire
from core.utils.aio import Chan, cancel_and_wait
from core.utils.short_uuid import shortuuid
from core.utils.http_context import http_session
from core.utils.errors import (
APIConnectionError,
APITimeoutError,
APIStatusError,
)
from core.utils.types import (
APIConnectOptions,
DEFAULT_API_CONNECT_OPTIONS,
)
import core.providers.tts.base as tts
from ..emitter import AudioEmitter
from .models import (
API_BASE_URL,
DEFAULT_VOICE_ID,
TTSEncoding,
TTSModels,
Voice,
VoiceSettings,
WS_INACTIVITY_TIMEOUT,
)
logger = setup_logging()
def _sample_rate_from_format(output_format: TTSEncoding) -> int:
"""Extract sample rate from encoding format string"""
if output_format.startswith("mp3_"):
return int(output_format.split("_")[1])
elif output_format.startswith("pcm_"):
return int(output_format.split("_")[1])
elif output_format.startswith("ulaw_"):
return int(output_format.split("_")[1])
return 22050 # default
@dataclass
class _TTSOptions:
"""Internal configuration for ElevenLabs TTS"""
api_key: str
voice_id: str
voice_settings: VoiceSettings | None
model: TTSModels | str
base_url: str
encoding: TTSEncoding
sample_rate: int
word_tokenizer: WordTokenizer | SentenceTokenizer
inactivity_timeout: int
auto_mode: bool
def get_ws_url(self) -> str:
"""Get WebSocket URL for multi-stream API"""
base = self.base_url.replace("http://", "ws://").replace("https://", "wss://")
params = [
f"model_id={self.model}",
f"output_format={self.encoding}",
]
if self.auto_mode:
params.append("auto_mode=true")
params.append(f"inactivity_timeout={self.inactivity_timeout}")
url = f"{base}/text-to-speech/{self.voice_id}/multi-stream-input?"
url += "&".join(params)
return url
class TTS(tts.TTS):
"""
ElevenLabs TTS Provider
Provides high-quality, emotionally rich text-to-speech with voice cloning support.
Uses multi-stream WebSocket API for efficient concurrent synthesis.
Example:
tts = elevenlabs.TTS(
api_key="your-api-key",
voice_id="voice-id",
model="eleven_turbo_v2_5",
)
# Streaming synthesis
stream = tts.stream()
stream.push_text("Hello world!")
stream.end_input()
async for audio in stream:
# Process audio
pass
"""
def __init__(
self,
*,
api_key: str | None = None,
voice_id: str = DEFAULT_VOICE_ID,
voice_settings: VoiceSettings | None = None,
model: TTSModels | str = "eleven_multilingual_v2",
encoding: TTSEncoding = "pcm_16000",
word_tokenizer: WordTokenizer | SentenceTokenizer | None = None,
inactivity_timeout: int = WS_INACTIVITY_TIMEOUT,
auto_mode: Optional[bool] = None,
base_url: str = API_BASE_URL,
) -> None:
"""
Initialize ElevenLabs TTS.
Args:
api_key: ElevenLabs API key. If not provided, reads from ELEVEN_API_KEY env var
voice_id: Voice ID to use (default: Rachel voice)
voice_settings: Voice settings for fine-tuning quality
model: TTS model to use (default: "eleven_multilingual_v2")
encoding: Audio encoding format (default: "pcm_16000")
word_tokenizer: Word/sentence tokenizer (default: blingfire for auto_mode)
inactivity_timeout: WebSocket inactivity timeout in seconds
auto_mode: Enable auto mode for optimized latency (recommended)
base_url: API base URL (default: https://api.elevenlabs.io/v1)
"""
sample_rate = _sample_rate_from_format(encoding)
super().__init__(
capabilities=tts.TTSCapabilities(streaming=True),
sample_rate=sample_rate,
num_channels=1,
)
# Validate API key
elevenlabs_api_key = api_key or os.environ.get("ELEVEN_API_KEY")
if not elevenlabs_api_key:
raise ValueError(
"ElevenLabs API key is required. "
"Provide via api_key parameter or set ELEVEN_API_KEY environment variable"
)
# Configure tokenizer
if not word_tokenizer:
if auto_mode:
word_tokenizer = blingfire.SentenceTokenizer()
else:
from core.tokenize.basic import WordTokenizer as BasicWordTokenizer
word_tokenizer = BasicWordTokenizer(ignore_punctuation=False)
# Store options
self._opts = _TTSOptions(
api_key=elevenlabs_api_key,
voice_id=voice_id,
voice_settings=voice_settings,
model=model,
base_url=base_url,
encoding=encoding,
sample_rate=sample_rate,
word_tokenizer=word_tokenizer,
inactivity_timeout=inactivity_timeout,
auto_mode=auto_mode,
)
self._session = http_session()
# Connection management
self._current_connection: _Connection | None = None
self._connection_lock = asyncio.Lock()
# Track active streams
self._streams: weakref.WeakSet[SynthesizeStream] = weakref.WeakSet()
@property
def model(self) -> str:
"""Get the model name"""
return self._opts.model
@property
def provider(self) -> str:
"""Get the provider name"""
return "ElevenLabs"
def _ensure_session(self) -> aiohttp.ClientSession:
"""Ensure HTTP session exists"""
return http_session()
async def current_connection(self) -> _Connection:
"""Get or create the current WebSocket connection"""
async with self._connection_lock:
if (
self._current_connection
and self._current_connection.is_current
and not self._current_connection._closed
):
return self._current_connection
# Create new connection
session = self._ensure_session()
conn = _Connection(self._opts, session)
await conn.connect()
self._current_connection = conn
return conn
async def list_voices(self) -> list[Voice]:
"""List available voices from ElevenLabs API"""
session = self._ensure_session()
url = f"{self._opts.base_url}/voices"
headers = {"xi-api-key": self._opts.api_key}
async with session.get(url, headers=headers) as resp:
resp.raise_for_status()
data = await resp.json()
voices = []
for voice_data in data.get("voices", []):
voices.append(
Voice(
id=voice_data["voice_id"],
name=voice_data["name"],
category=voice_data["category"],
)
)
return voices
def stream(self, *, conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS) -> SynthesizeStream:
"""
Create a streaming synthesis session.
Returns:
SynthesizeStream for incremental text input
"""
stream = SynthesizeStream(tts=self, conn_options=conn_options)
self._streams.add(stream)
return stream
async def aclose(self) -> None:
"""Close TTS provider and cleanup resources"""
# Close all active streams
for stream in list(self._streams):
await stream.aclose()
self._streams.clear()
# Close connection
if self._current_connection:
await self._current_connection.aclose()
self._current_connection = None
class SynthesizeStream(tts.SynthesizeStream):
"""Streaming synthesis using WebSocket (multi-stream)"""
def __init__(
self, *, tts: TTS, conn_options: APIConnectOptions = DEFAULT_API_CONNECT_OPTIONS
) -> None:
super().__init__(tts=tts, conn_options=conn_options)
self._tts: TTS = tts
self._sent_tokenizer_stream = self._tts._opts.word_tokenizer.stream()
async def aclose(self) -> None:
"""Close stream and cleanup"""
await self._sent_tokenizer_stream.aclose()
await super().aclose()
async def _run(self, output_emitter: AudioEmitter) -> None:
"""
Streaming synthesis implementation using multi-stream WebSocket.
This method:
1. Connects to the shared WebSocket connection
2. Registers this stream with the connection
3. Sets up tokenizer
4. Runs two parallel tasks:
- _input_task: Reads from input_ch → tokenizer
- _sentence_task: Tokenizer → Connection (which sends to WebSocket)
5. The connection's recv loop handles audio data and routes it to this stream's emitter
"""
opts = self._tts._opts
request_id = shortuuid()
context_id = shortuuid() # Generate context_id upfront for registration
# Initialize output emitter
output_emitter.initialize(
request_id=request_id,
sample_rate=opts.sample_rate,
num_channels=1,
stream=True,
)
# Get shared connection
try:
connection = await asyncio.wait_for(
self._tts.current_connection(), timeout=10
)
except asyncio.TimeoutError as e:
raise APITimeoutError("Could not connect to ElevenLabs", timeout=10) from e
except Exception as e:
raise APIConnectionError(f"Could not connect to ElevenLabs: {e}") from e
# Create waiter for completion
waiter: asyncio.Future[None] = asyncio.get_event_loop().create_future()
# Register stream with context_id
connection.register_stream(context_id, output_emitter, waiter)
# Define tasks
async def _input_task() -> None:
"""Forward input from input_ch to tokenizer"""
try:
async for data in self._input_ch:
if isinstance(data, self._FlushSentinel):
self._sent_tokenizer_stream.flush()
continue
self._sent_tokenizer_stream.push_text(data)
finally:
self._sent_tokenizer_stream.end_input()
async def _sentence_task() -> None:
"""Send tokenized sentences to connection"""
try:
async for sentence_ev in self._sent_tokenizer_stream:
text = sentence_ev.token
formatted_text = f"{text} " # Always end with space
connection.send_content(_SynthesizeContent(context_id, formatted_text))
finally:
connection.send_content(_SynthesizeContent(context_id, "", flush=True))
connection.close_context(context_id)
# Run tasks
input_t = asyncio.create_task(_input_task(), name="elevenlabs_input")
sentence_t = asyncio.create_task(_sentence_task(), name="elevenlabs_sentence")
try:
await waiter
except asyncio.TimeoutError as e:
raise TimeoutError("ElevenLabs synthesis timed out") from e
except Exception as e:
logger.bind(tag=__name__).error(f"Synthesis error: {e}")
raise
finally:
await cancel_and_wait(input_t, sentence_t)
@dataclass
class _SynthesizeContent:
"""Content to synthesize"""
context_id: str
text: str
flush: bool = False
@dataclass
class _CloseContext:
"""Command to close a context"""
context_id: str
@dataclass
class _StreamData:
"""Data for a registered stream"""
emitter: AudioEmitter
stream: SynthesizeStream | None
waiter: asyncio.Future[None]
timeout_timer: asyncio.TimerHandle | None = None
class _Connection:
"""
Manages a single WebSocket connection with send/recv loops.
Supports multiple concurrent synthesis contexts (multi-stream).
"""
def __init__(self, opts: _TTSOptions, session: aiohttp.ClientSession) -> None:
self._opts = opts
self._session = session
self._ws: aiohttp.ClientWebSocketResponse | None = None
self._is_current = True
self._active_contexts: set[str] = set()
self._input_queue: Chan[Union[_SynthesizeContent, _CloseContext]] = Chan()
self._context_data: dict[str, _StreamData] = {}
self._send_task: asyncio.Task | None = None
self._recv_task: asyncio.Task | None = None
self._closed = False
@property
def is_current(self) -> bool:
"""Check if this connection is current"""
return self._is_current
def mark_non_current(self) -> None:
"""Mark this connection as no longer current"""
self._is_current = False
async def connect(self) -> None:
"""Establish WebSocket connection and start send/recv loops"""
if self._ws or self._closed:
return
url = self._opts.get_ws_url()
headers = {"xi-api-key": self._opts.api_key}
try:
self._ws = await asyncio.wait_for(
self._session.ws_connect(url, headers=headers),
timeout=10,
)
except asyncio.TimeoutError as e:
raise APITimeoutError(
"Connection to ElevenLabs WebSocket timed out",
timeout=10,
) from e
except aiohttp.ClientError as e:
raise APIConnectionError(
f"Failed to connect to ElevenLabs WebSocket: {e}",
url=url,
) from e
self._send_task = asyncio.create_task(self._send_loop(), name="elevenlabs_send")
self._recv_task = asyncio.create_task(self._recv_loop(), name="elevenlabs_recv")
def register_stream(
self,
context_id: str,
emitter: AudioEmitter,
done_fut: asyncio.Future[None],
) -> None:
"""Register a new synthesis stream"""
self._context_data[context_id] = _StreamData(
emitter=emitter,
stream=None, # Stream reference not needed
waiter=done_fut,
)
def send_content(self, content: _SynthesizeContent) -> None:
"""Send synthesis content to the connection"""
if self._closed or not self._ws or self._ws.closed:
raise ConnectionError("WebSocket connection is closed")
self._input_queue.send_nowait(content)
def close_context(self, context_id: str) -> None:
"""Close a specific context"""
if self._closed or not self._ws or self._ws.closed:
raise ConnectionError("WebSocket connection is closed")
self._input_queue.send_nowait(_CloseContext(context_id))
async def _send_loop(self) -> None:
"""Send loop - processes messages from input queue"""
try:
while not self._closed:
try:
msg = await self._input_queue.recv()
except Exception:
break
if not self._ws or self._ws.closed:
break
if isinstance(msg, _SynthesizeContent):
is_new_context = msg.context_id not in self._active_contexts
# Skip if not current and new context
if not self._is_current and is_new_context:
continue
# Initialize context
if is_new_context:
voice_settings_dict = None
if self._opts.voice_settings:
voice_settings_dict = _strip_nones(
dataclasses.asdict(self._opts.voice_settings)
)
init_pkt = {
"text": " ",
"context_id": msg.context_id,
}
if voice_settings_dict:
init_pkt["voice_settings"] = voice_settings_dict
await self._ws.send_json(init_pkt)
self._active_contexts.add(msg.context_id)
# Send text
pkt: dict[str, Any] = {
"text": msg.text,
"context_id": msg.context_id,
}
if msg.flush:
pkt["flush"] = True
await self._ws.send_json(pkt)
elif isinstance(msg, _CloseContext):
if msg.context_id in self._active_contexts:
close_pkt = {
"context_id": msg.context_id,
"close_context": True,
}
await self._ws.send_json(close_pkt)
except Exception as e:
logger.bind(tag=__name__).error(f"Send loop error: {e}")
finally:
if not self._closed:
await self.aclose()
async def _recv_loop(self) -> None:
"""Receive loop - processes messages from WebSocket"""
try:
while not self._closed and self._ws and not self._ws.closed:
msg = await self._ws.receive()
if msg.type in (
aiohttp.WSMsgType.CLOSED,
aiohttp.WSMsgType.CLOSE,
aiohttp.WSMsgType.CLOSING,
):
raise APIStatusError(
"ElevenLabs connection closed unexpectedly", request_id=""
)
if msg.type != aiohttp.WSMsgType.TEXT:
logger.bind(tag=__name__).warning(f"Unexpected message type: {msg.type}")
continue
data = json.loads(msg.data)
context_id = data.get("contextId")
if not context_id or context_id not in self._context_data:
continue
ctx = self._context_data[context_id]
# Check for errors
if error := data.get("error"):
logger.bind(tag=__name__).error(f"ElevenLabs error: {error}")
if not ctx.waiter.done():
ctx.waiter.set_exception(RuntimeError(f"ElevenLabs error: {error}"))
self._cleanup_context(context_id)
continue
# Start segment on first audio
if audio_b64 := data.get("audio"):
# Check if this is the first audio for this context
if context_id not in self._active_contexts:
ctx.emitter.start_segment(segment_id=context_id)
self._active_contexts.add(context_id)
audio_bytes = base64.b64decode(audio_b64)
ctx.emitter.push(audio_bytes)
# Check if final
if data.get("isFinal"):
if not ctx.waiter.done():
ctx.waiter.set_result(None)
self._cleanup_context(context_id)
if not self._is_current and not self._active_contexts:
logger.bind(tag=__name__).debug("No active contexts, shutting down")
break
except Exception as e:
logger.bind(tag=__name__).error(f"Receive loop error: {e}")
for ctx in self._context_data.values():
if not ctx.waiter.done():
ctx.waiter.set_exception(e)
self._context_data.clear()
finally:
if not self._closed:
await self.aclose()
def _cleanup_context(self, context_id: str) -> None:
"""Clean up context state"""
self._context_data.pop(context_id, None)
self._active_contexts.discard(context_id)
async def aclose(self) -> None:
"""Close the connection and clean up"""
if self._closed:
return
self._closed = True
self._input_queue.close()
# Complete all waiters
for ctx in self._context_data.values():
if not ctx.waiter.done():
ctx.waiter.set_exception(ConnectionError("Connection closed"))
self._context_data.clear()
# Close WebSocket
if self._ws:
await self._ws.close()
# Cancel tasks
if self._send_task:
await cancel_and_wait(self._send_task)
if self._recv_task:
await cancel_and_wait(self._recv_task)
self._ws = None
def _strip_nones(data: dict[str, Any]) -> dict[str, Any]:
"""Remove None values from dictionary"""
return {k: v for k, v in data.items() if v is not None}

View file

@ -627,12 +627,12 @@ TTS:
output_dir: tmp/ output_dir: tmp/
# ============== SDK 版本TTS(推荐)============== # ============== SDK 版本TTS(推荐)==============
# 使用官方SDK,代码更简洁、性能更好、维护成本更低 # 使用官方SDK,代码更简洁、性能更好、维护成本更低
CartesiaSDK: Cartesia:
# Cartesia TTS - SDK版本(推荐) # Cartesia TTS - SDK版本(推荐)
# 官方SDK实现,低延迟,支持流式合成 # 官方SDK实现,低延迟,支持流式合成
# API Key获取地址:https://play.cartesia.ai/console # API Key获取地址:https://play.cartesia.ai/console
# 或设置环境变量:export CARTESIA_API_KEY=your-api-key # 或设置环境变量:export CARTESIA_API_KEY=your-api-key
type: cartesia_sdk type: cartesia
api_key: 你的Cartesia API密钥 api_key: 你的Cartesia API密钥
# Voice ID或voice embedding # Voice ID或voice embedding
voice_id: 你的语音ID voice_id: 你的语音ID
@ -645,12 +645,12 @@ TTS:
# 采样率:8000, 16000, 22050, 24000, 44100等 # 采样率:8000, 16000, 22050, 24000, 44100等
sample_rate: 24000 sample_rate: 24000
output_dir: tmp/ output_dir: tmp/
ElevenLabsSDK: ElevenLabs:
# ElevenLabs TTS - SDK版本(推荐) # ElevenLabs TTS - SDK版本(推荐)
# 官方SDK实现,代码更简洁,维护成本更低,性能更好 # 官方SDK实现,代码更简洁,维护成本更低,性能更好
# API Key获取地址:https://elevenlabs.io/app/settings/api-keys # API Key获取地址:https://elevenlabs.io/app/settings/api-keys
# 或设置环境变量:export ELEVEN_API_KEY=your-api-key # 或设置环境变量:export ELEVEN_API_KEY=your-api-key
type: elevenlabs_sdk type: elevenlabs
api_key: 你的ElevenLabs API密钥 api_key: 你的ElevenLabs API密钥
# Voice ID获取:https://elevenlabs.io/app/voice-library # Voice ID获取:https://elevenlabs.io/app/voice-library
# 默认:21m00Tcm4TlvDq8ikWAM (Rachel) # 默认:21m00Tcm4TlvDq8ikWAM (Rachel)

View file

@ -33,7 +33,7 @@ show_usage() {
echo " Values: sg, us, local" echo " Values: sg, us, local"
echo "" echo ""
echo " -a, --action <action> Action to perform" echo " -a, --action <action> Action to perform"
echo " Values: server, all" echo " Values: server, web, all"
echo "" echo ""
echo " -h, --help Show this help message" echo " -h, --help Show this help message"
echo "" echo ""
@ -43,13 +43,14 @@ show_usage() {
echo " local - Local development (REGION=LOCAL)" echo " local - Local development (REGION=LOCAL)"
echo "" echo ""
echo "Action details:" echo "Action details:"
echo " server - Restart only xiaozhi-esp32-server" echo " server - Rebuild and restart xiaozhi-esp32-server"
echo " all - Start/restart all services (server, web, db, redis)" echo " web - Rebuild and restart xiaozhi-esp32-server-web (manager-api + manager-web)"
echo " all - Rebuild and restart server + web (does not rebuild db/redis)"
echo "" echo ""
echo "Examples:" echo "Examples:"
echo " $0 --region sg --action server" echo " $0 --region sg --action server"
echo " $0 -r us -a all" echo " $0 -r local -a web"
echo " $0 --region local --action server" echo " $0 --region local --action all"
exit 1 exit 1
} }
@ -113,13 +114,17 @@ case $ACTION in
ACTION_NAME="Server Only" ACTION_NAME="Server Only"
SERVICES="xiaozhi-esp32-server" SERVICES="xiaozhi-esp32-server"
;; ;;
web|WEB)
ACTION_NAME="Web Only (Manager API + Web)"
SERVICES="xiaozhi-esp32-server-web"
;;
all|ALL) all|ALL)
ACTION_NAME="All Services" ACTION_NAME="Server + Web"
SERVICES="" SERVICES="xiaozhi-esp32-server xiaozhi-esp32-server-web"
;; ;;
*) *)
echo -e "${RED}Error: Invalid action '${ACTION}'${NC}" echo -e "${RED}Error: Invalid action '${ACTION}'${NC}"
echo "Valid actions: server, all" echo "Valid actions: server, web, all"
exit 1 exit 1
;; ;;
esac esac
@ -141,16 +146,13 @@ echo -e "${BLUE}Current container status:${NC}"
docker ps --filter name=xiaozhi --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo "No containers running" docker ps --filter name=xiaozhi --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" 2>/dev/null || echo "No containers running"
echo "" echo ""
# Confirm action # Execute action with rebuild
if [ "$ACTION" = "server" ] || [ "$ACTION" = "SERVER" ]; then echo -e "${YELLOW}→ Rebuilding and starting: ${ACTION_NAME}${NC}"
echo -e "${YELLOW}→ Restarting xiaozhi-esp32-server...${NC}" echo -e "${CYAN} Services: ${SERVICES}${NC}"
echo "" echo ""
docker compose -f "$COMPOSE_FILE" restart xiaozhi-esp32-server
else # Use up --build -d to rebuild images and start containers
echo -e "${YELLOW}→ Starting all services...${NC}" docker compose -f "$COMPOSE_FILE" up --build -d $SERVICES
echo ""
docker compose -f "$COMPOSE_FILE" up -d
fi
echo "" echo ""
echo -e "${GREEN}✓ Operation completed${NC}" echo -e "${GREEN}✓ Operation completed${NC}"
@ -163,8 +165,8 @@ echo ""
# Show logs option # Show logs option
echo -e "${CYAN}To view logs, run:${NC}" echo -e "${CYAN}To view logs, run:${NC}"
if [ "$ACTION" = "server" ] || [ "$ACTION" = "SERVER" ]; then if [ -n "$SERVICES" ]; then
echo " docker compose -f $COMPOSE_FILE logs -f xiaozhi-esp32-server" echo " docker compose -f $COMPOSE_FILE logs -f $SERVICES"
else else
echo " docker compose -f $COMPOSE_FILE logs -f" echo " docker compose -f $COMPOSE_FILE logs -f"
fi fi