xiaozhi-esp32-server/main/xiaozhi-server/core/providers/tts/base.py
lapy 70771b4209 chore(westernization): translate remaining Han text across code, UI and docs
Final localization pass over everything the audit covers, driven by the
local qwen3.8:instruct endpoint (thinking disabled) with placeholder-
masked, cached, resumable batches:

- Python server: comments, docstrings, log/error messages, prompt text
- manager-api Java: javadoc, exception messages, Swagger summaries,
  westernized test fixtures (locale strings, signed-URL examples)
- manager-web / manager-mobile: component comments and misc leftovers
  (functional split regexes such as /[、;;,,]/ left byte-identical)
- digital-human: HTML/UI text, JS comments, wake-word defaults aligned
  with the server canonical English wake words
- docs: device-call guide, wakeword and search integration guides

Also widens the westernization audit exception to the whole vendored
generator bundle and retires the stale per-file exception list.
2026-10-11 17:58:15 +01:00

639 lines
26 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import re
import math
import uuid
import queue
import asyncio
import threading
import traceback
import concurrent.futures
from core.utils import p3
from datetime import datetime
from core.utils import textUtils
from typing import Callable, Any
from abc import ABC, abstractmethod
from config.logger import setup_logging
from core.utils import opus_encoder_utils
from core.utils.tts import MarkdownCleaner, convert_percentage_to_range
from core.utils.output_counter import add_device_output
from core.handle.reportHandle import enqueue_tts_report
from core.handle.sendAudioHandle import sendAudioMessage
from core.utils.util import audio_bytes_to_data_stream, audio_to_data_stream
from core.providers.tts.dto.dto import (
TTSMessageDTO,
SentenceType,
ContentType,
InterfaceType,
)
TAG = __name__
logger = setup_logging()
class TTSProviderBase(ABC):
def __init__(self, config, delete_audio_file):
self.interface_type = InterfaceType.NON_STREAM
self.conn = None
self.delete_audio_file = delete_audio_file
self.audio_file_type = "wav"
self.output_file = config.get("output_dir", "tmp/")
self.tts_timeout = float(config.get("tts_timeout", 15))
if not math.isfinite(self.tts_timeout) or self.tts_timeout <= 0:
raise ValueError("tts_timeout must be a positive finite number")
self.tts_text_queue = queue.Queue()
self.tts_audio_queue = queue.Queue()
self.tts_audio_first_sentence = True
self.before_stop_play_files = []
self.report_on_last = False
# Mapping from sentence_id to text, used by streaming TTS to retrieve correct subtitle text
self._sentence_text_map = {}
# Load replacement words for one-time regex replacement
raw_words = config.get("correct_words", [])
self.correct_words = {}
for item in raw_words:
parts = item.split("|", 1)
if len(parts) == 2:
self.correct_words[parts[0]] = parts[1]
# Build regex with longest-match priority (sort, escape, and concatenate)
if self.correct_words:
# Sort keys by length in descending order to match longer keys first and avoid partial matches
sorted_keys = sorted(self.correct_words.keys(), key=len, reverse=True)
pattern_str = "|".join(re.escape(k) for k in sorted_keys)
self._correct_words_pattern = re.compile(pattern_str)
# Build reverse replacement regex to restore original text from TTS-returned replaced text (for subtitle display)
reverse_map = {v: k for k, v in self.correct_words.items()}
sorted_reverse_keys = sorted(reverse_map.keys(), key=len, reverse=True)
reverse_pattern_str = "|".join(re.escape(k) for k in sorted_reverse_keys)
self._reverse_words_pattern = re.compile(reverse_pattern_str)
self._reverse_words_map = reverse_map
# Streaming sliding window: replacement dictionary grouped by first character for fast lookup
self._words_by_first_char = {}
for key in sorted_keys: # Use keys sorted by length descending to ensure longest match priority
first_char = key[0] if key else ""
if first_char not in self._words_by_first_char:
self._words_by_first_char[first_char] = []
self._words_by_first_char[first_char].append(key)
else:
self._correct_words_pattern = None
self._reverse_words_pattern = None
self._reverse_words_map = None
# Streaming sliding window: cached text pending match.
self._pending_prefix = ""
self.tts_text_buff = []
self.punctuations = (
".",
"?",
"?",
"!",
"!",
";",
";",
":",
)
self.first_sentence_punctuations = (
",",
"~",
",",
",",
".",
"?",
"?",
"!",
"!",
";",
";",
":",
)
self.tts_stop_request = False
self.processed_chars = 0
self.is_first_sentence = True
def generate_filename(self, extension=".wav"):
return os.path.join(
self.output_file,
f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}",
)
def handle_opus(self, opus_data: bytes):
logger.bind(tag=TAG).debug(f"Pushed {len(opus_data)} frames to the queue")
self.tts_audio_queue.put((SentenceType.MIDDLE, opus_data, None, getattr(self, 'current_sentence_id', None)))
def handle_audio_file(self, file_audio: bytes, text):
self.before_stop_play_files.append((file_audio, text))
def to_tts_stream(self, text, opus_handler: Callable[[bytes], None] = None) -> None:
# Preserve original text for display/reporting
original_text = text
text = MarkdownCleaner.clean_markdown(text)
# Use regex for a single replacement to avoid repeated traversal and partial match issues
if self._correct_words_pattern:
text = self._correct_words_pattern.sub(lambda m: self.correct_words[m.group(0)], text)
max_repeat_time = 5
if self.delete_audio_file:
# Convert files requiring deletion directly to audio data
while max_repeat_time > 0:
try:
audio_bytes = asyncio.run(self.text_to_speak(text, None))
if audio_bytes:
# Use raw text for display/reporting
self.tts_audio_queue.put((SentenceType.FIRST, None, original_text, getattr(self, 'current_sentence_id', None)))
audio_bytes_to_data_stream(
audio_bytes,
file_type=self.audio_file_type,
is_opus=True,
callback=opus_handler,
sample_rate=self.conn.sample_rate,
opus_encoder=self.opus_encoder,
)
break
else:
max_repeat_time -= 1
except Exception as e:
logger.bind(tag=TAG).warning(
f"Voice generation failed {5 - max_repeat_time + 1} times: {original_text}, error: {e}"
)
max_repeat_time -= 1
if max_repeat_time > 0:
logger.bind(tag=TAG).info(
f"Voice generation successful: {original_text}, retried {5 - max_repeat_time} times"
)
else:
logger.bind(tag=TAG).error(
f"Voice generation failed: {original_text}, please check network or service status"
)
return None
else:
tmp_file = self.generate_filename()
try:
while not os.path.exists(tmp_file) and max_repeat_time > 0:
try:
asyncio.run(self.text_to_speak(text, tmp_file))
except Exception as e:
logger.bind(tag=TAG).warning(
f"Voice generation failed {5 - max_repeat_time + 1} times: {original_text}, error: {e}"
)
# Execution failed, deleting file
if os.path.exists(tmp_file):
os.remove(tmp_file)
max_repeat_time -= 1
if max_repeat_time > 0:
logger.bind(tag=TAG).info(
f"Speech generation successful: {original_text}:{tmp_file}, retry {5 - max_repeat_time} times"
)
else:
logger.bind(tag=TAG).error(
f"Voice generation failed: {original_text}, please check network or service status"
)
self.tts_audio_queue.put((SentenceType.FIRST, None, original_text, getattr(self, 'current_sentence_id', None)))
self._process_audio_file_stream(tmp_file, callback=opus_handler)
except Exception as e:
logger.bind(tag=TAG).error(f"Failed to generate TTS file: {e}")
return None
def to_tts(self, text):
# Preserve original text for logging/display
original_text = text
text = MarkdownCleaner.clean_markdown(text)
if self._correct_words_pattern:
text = self._correct_words_pattern.sub(lambda m: self.correct_words[m.group(0)], text)
max_repeat_time = 5
if self.delete_audio_file:
# Convert files requiring deletion directly to audio data
while max_repeat_time > 0:
try:
audio_bytes = asyncio.run(self.text_to_speak(text, None))
if audio_bytes:
audio_datas = []
audio_bytes_to_data_stream(
audio_bytes,
file_type=self.audio_file_type,
is_opus=True,
callback=lambda data: audio_datas.append(data),
sample_rate=self.conn.sample_rate,
)
return audio_datas
else:
max_repeat_time -= 1
except Exception as e:
logger.bind(tag=TAG).warning(
f"Voice generation failed {5 - max_repeat_time + 1} times: {original_text}, error: {e}"
)
max_repeat_time -= 1
if max_repeat_time > 0:
logger.bind(tag=TAG).info(
f"Voice generation successful: {original_text}, retried {5 - max_repeat_time} times"
)
else:
logger.bind(tag=TAG).error(
f"Voice generation failed: {original_text}, please check network or service status"
)
return None
else:
tmp_file = self.generate_filename()
try:
while not os.path.exists(tmp_file) and max_repeat_time > 0:
try:
asyncio.run(self.text_to_speak(text, tmp_file))
except Exception as e:
logger.bind(tag=TAG).warning(
f"Voice generation failed {5 - max_repeat_time + 1} times: {original_text}, error: {e}"
)
# Execution failed, deleting file
if os.path.exists(tmp_file):
os.remove(tmp_file)
max_repeat_time -= 1
if max_repeat_time > 0:
logger.bind(tag=TAG).info(
f"Speech generation successful: {original_text}:{tmp_file}, retry {5 - max_repeat_time} times"
)
else:
logger.bind(tag=TAG).error(
f"Voice generation failed: {original_text}, please check network or service status"
)
return tmp_file
except Exception as e:
logger.bind(tag=TAG).error(f"Failed to generate TTS file: {e}")
return None
@abstractmethod
async def text_to_speak(self, text, output_file):
pass
def audio_to_pcm_data_stream(
self, audio_file_path, callback: Callable[[Any], Any] = None
):
"""Convert audio file to PCM encoding"""
return audio_to_data_stream(audio_file_path, is_opus=False, callback=callback, sample_rate=self.conn.sample_rate, opus_encoder=None)
def audio_to_opus_data_stream(
self, audio_file_path, callback: Callable[[Any], Any] = None
):
"""Convert audio file to Opus encoding"""
return audio_to_data_stream(audio_file_path, is_opus=True, callback=callback, sample_rate=self.conn.sample_rate, opus_encoder=self.opus_encoder)
def tts_one_sentence(
self,
conn,
content_type,
content_detail=None,
content_file=None,
sentence_id=None,
):
"""Send a sentence"""
if not sentence_id:
if conn.sentence_id:
sentence_id = conn.sentence_id
else:
sentence_id = str(uuid.uuid4().hex)
conn.sentence_id = sentence_id
# Segment single-sentence text
segments = re.split(r"([。!?!?;;\n])", content_detail)
for seg in segments:
self.tts_text_queue.put(
TTSMessageDTO(
sentence_id=sentence_id,
sentence_type=SentenceType.MIDDLE,
content_type=content_type,
content_detail=seg,
content_file=content_file,
)
)
async def open_audio_channels(self, conn):
self.conn = conn
# Create encoder based on conn's sample_rate; do not override if subclass has already created it (IndexTTS interface returns 24kHz - resampling pending)
if not hasattr(self, 'opus_encoder') or self.opus_encoder is None:
self.opus_encoder = opus_encoder_utils.OpusEncoderUtils(
sample_rate=conn.sample_rate, channels=1, frame_size_ms=60
)
# TTS processing thread
self.tts_priority_thread = threading.Thread(
target=self.tts_text_priority_thread, daemon=True
)
self.tts_priority_thread.start()
# Audio playback consumer thread
self.audio_play_priority_thread = threading.Thread(
target=self._audio_play_priority_thread, daemon=True
)
self.audio_play_priority_thread.start()
def store_tts_text(self, sentence_id, text):
""" stores text for specified sentence_id, used by streaming TTS to retrieve correct subtitle text
Args:
sentence_id: Session ID
text: Text to store
"""
if sentence_id and text:
self._sentence_text_map[sentence_id] = text
# Keep only the latest 5 to prevent memory leaks
if len(self._sentence_text_map) > 5:
oldest = next(iter(self._sentence_text_map))
del self._sentence_text_map[oldest]
def get_tts_text(self, sentence_id):
"""Get text corresponding to the specified sentence_id
Args:
sentence_id: Session ID
Returns:
str: Corresponding text, returns None if not found
"""
return self._sentence_text_map.get(sentence_id)
def clear_tts_text(self, sentence_id):
"""Clear text for the specified sentence_id
Args:
sentence_id: Session ID
"""
if sentence_id in self._sentence_text_map:
del self._sentence_text_map[sentence_id]
def _restore_original_text(self, text):
if not self._reverse_words_pattern or not text:
return text
return self._reverse_words_pattern.sub(
lambda m: self._reverse_words_map[m.group(0)], text
)
# Default to non-streaming processing here
# Override in subclass for streaming processing
def tts_text_priority_thread(self):
while not self.conn.stop_event.is_set():
try:
message = self.tts_text_queue.get(timeout=1)
if self.conn.client_abort:
logger.bind(tag=TAG).info("Received interrupt signal, terminating TTS text processing thread")
continue
# Filter old messages: check if sentence_id matches
if message.sentence_id != self.conn.sentence_id:
continue
if message.sentence_type == SentenceType.FIRST:
self.current_sentence_id = message.sentence_id
self.tts_stop_request = False
self.processed_chars = 0
self.tts_text_buff = []
self.is_first_sentence = True
self.tts_audio_first_sentence = True
elif ContentType.TEXT == message.content_type:
self.tts_text_buff.append(message.content_detail)
segment_text = self._get_segment_text()
if segment_text:
self.to_tts_stream(segment_text, opus_handler=self.handle_opus)
elif ContentType.FILE == message.content_type:
self._process_remaining_text_stream(opus_handler=self.handle_opus)
tts_file = message.content_file
if tts_file and os.path.exists(tts_file):
self._process_audio_file_stream(
tts_file, callback=self.handle_opus
)
if message.sentence_type == SentenceType.LAST:
self._process_remaining_text_stream(opus_handler=self.handle_opus)
self.tts_audio_queue.put(
(message.sentence_type, [], message.content_detail, message.sentence_id)
)
except queue.Empty:
continue
except Exception as e:
logger.bind(tag=TAG).error(
f"Failed to process TTS text: {str(e)}, Type: {type(e).__name__}, Stack: {traceback.format_exc()}"
)
continue
def _audio_play_priority_thread(self):
# List of text and audio to report
enqueue_text = None
enqueue_audio = []
while not self.conn.stop_event.is_set():
text = None
try:
try:
item = self.tts_audio_queue.get(timeout=0.1)
if len(item) == 4:
sentence_type, audio_datas, text, sentence_id = item
else:
sentence_type, audio_datas, text = item
sentence_id = None
except queue.Empty:
if self.conn.stop_event.is_set():
break
continue
if self.conn.client_abort:
logger.bind(tag=TAG).debug("Interrupt signal received, skipping current audio data")
enqueue_text, enqueue_audio = None, []
continue
# Report upon receiving the next text start or session end
if sentence_type is not SentenceType.MIDDLE:
if self.report_on_last:
# Accumulation mode: suitable for TTS with a single continuous audio stream (e.g., seed-tts-2.0)
# In FIRST mode, record text only; audio accumulates continuously and is reported only in LAST mode
if text:
enqueue_text = text
if sentence_type == SentenceType.LAST:
enqueue_tts_report(self.conn, enqueue_text, enqueue_audio)
enqueue_audio = []
enqueue_text = None
else:
# Non-accumulative mode: report each sentence separately
if enqueue_text is not None:
enqueue_tts_report(self.conn, enqueue_text, enqueue_audio)
enqueue_audio = []
enqueue_text = text
# Collect and report audio data
if isinstance(audio_datas, bytes):
enqueue_audio.append(audio_datas)
# Send audio
future = asyncio.run_coroutine_threadsafe(
sendAudioMessage(self.conn, sentence_type, audio_datas, text, sentence_id),
self.conn.loop,
)
future.result()
# Log output and report
if self.conn.max_output_size > 0 and text:
add_device_output(self.conn.headers.get("device-id"), len(text))
except Exception as e:
logger.bind(tag=TAG).error(f"audio_play_priority_thread: {text} {e}")
async def start_session(self, session_id):
pass
async def finish_session(self, session_id):
pass
async def close(self):
"""Resource Cleanup Method"""
self._sentence_text_map.clear()
if hasattr(self, "ws") and self.ws:
await self.ws.close()
def _get_segment_text(self):
# Merge all current text and process unsegmented parts
full_text = "".join(self.tts_text_buff)
current_text = full_text[self.processed_chars :] # Start from unprocessed position
last_punct_pos = -1
# Select different punctuation sets based on whether it is the first sentence
punctuations_to_use = (
self.first_sentence_punctuations
if self.is_first_sentence
else self.punctuations
)
for punct in punctuations_to_use:
pos = current_text.rfind(punct)
if (pos != -1 and last_punct_pos == -1) or (
pos != -1 and pos < last_punct_pos
):
last_punct_pos = pos
if last_punct_pos != -1:
segment_text_raw = current_text[: last_punct_pos + 1]
segment_text = textUtils.get_string_no_punctuation_or_emoji(
segment_text_raw
)
self.processed_chars += len(segment_text_raw) # Update processed character position
# If it is the first sentence, set the flag to False after finding the first comma
if self.is_first_sentence:
self.is_first_sentence = False
return segment_text
elif self.tts_stop_request and current_text:
segment_text = current_text
self.is_first_sentence = True # Reset Flag
return segment_text
else:
return None
def _process_audio_file_stream(
self, tts_file, callback: Callable[[Any], Any]
) -> None:
"""Process audio files and convert to the specified format
Args:
tts_file: Audio file path
callback: File processing function
"""
if tts_file.endswith(".p3"):
p3.decode_opus_from_file_stream(tts_file, callback=callback)
elif self.conn.audio_format == "pcm":
self.audio_to_pcm_data_stream(tts_file, callback=callback)
else:
self.audio_to_opus_data_stream(tts_file, callback=callback)
if (
self.delete_audio_file
and tts_file is not None
and os.path.exists(tts_file)
and tts_file.startswith(self.output_file)
):
os.remove(tts_file)
def _process_before_stop_play_files(self):
for audio_datas, text in self.before_stop_play_files:
self.tts_audio_queue.put((SentenceType.MIDDLE, audio_datas, text, getattr(self, 'current_sentence_id', None)))
self.before_stop_play_files.clear()
self.tts_audio_queue.put((SentenceType.LAST, [], None, getattr(self, 'current_sentence_id', None)))
def _process_remaining_text_stream(
self, opus_handler: Callable[[bytes], None] = None
):
"""Process remaining text and generate speech
Returns:
bool: Whether the text was successfully processed
"""
full_text = "".join(self.tts_text_buff)
remaining_text = full_text[self.processed_chars :]
if remaining_text:
segment_text = textUtils.get_string_no_punctuation_or_emoji(remaining_text)
if segment_text:
self.to_tts_stream(segment_text, opus_handler=opus_handler)
self.processed_chars += len(full_text)
return True
return False
def _apply_percentage_params(self, config):
"""Batch apply percentage parameters based on TTS_PARAM_CONFIG defined in subclass"""
for config_key, attr_name, min_val, max_val, base_val, transform in self.TTS_PARAM_CONFIG:
if config_key in config:
val = convert_percentage_to_range(config[config_key], min_val, max_val, base_val)
setattr(self, attr_name, transform(val) if transform else val)
def _match_stream_text(self, text):
"""Streaming text sliding window matching for handling cross-chunk replacement words
Args:
text: Input text fragment
Returns:
tuple: (list of confirmed text, remaining prefix to match)
"""
if not self.correct_words or not text:
return [text] if text else [], ""
result = []
pending = self._pending_prefix
i = 0
while i < len(text):
char = text[i]
# Try: pending + current character to match replacement words
test_text = pending + char
matched = False
# Iterate through potential replacement matches
candidates = self._words_by_first_char.get(pending[0], []) if pending else self._words_by_first_char.get(char, [])
for key in candidates:
if test_text == key:
# Exact match, send after replacement
result.append(self.correct_words[key])
pending = ""
matched = True
break
elif key.startswith(test_text):
# Prefix of replacement word, continue waiting
pending = test_text
matched = True
break
if matched:
i += 1
continue
# No longer match found; pending content is confirmed ready to send
if pending:
result.append(pending)
pending = ""
# Check if the current character is the start of a replacement word
if char in self._words_by_first_char:
pending = char
else:
result.append(char)
i += 1
return result, pending
def reset_stream_state(self):
"""Reset streaming state to clear residual state at session start"""
self._pending_prefix = ""