"""Audio tool operations: TTS (edge-tts) and ASR (faster-whisper).""" import asyncio import os import re FFMPEG_BIN = "ffmpeg" MAX_TTS_CHARS = 2000 MAX_ASR_MB = 30 VOICE_FILES = { "zh_CN-huayan-medium": "晓妍(女·标准中文)", "en_US-amy-medium": "Amy(女·英语)", } _asr_model = None def _get_asr(): global _asr_model if _asr_model is None: from faster_whisper import WhisperModel _asr_model = WhisperModel("small", device="cpu", compute_type="int8") return _asr_model PIPER_DIR = os.environ.get("PIPER_DIR", "/app/piper-models") _piper_voices = {} def _load_voice(voice: str): from piper import PiperVoice if voice not in _piper_voices: model = os.path.join(PIPER_DIR, voice + ".onnx") if not os.path.exists(model): raise ValueError("音色不存在: " + voice) _piper_voices[voice] = PiperVoice.load(model) return _piper_voices[voice] def run_tts(text: str, voice: str, rate_pct: int, pitch_hz: int, out_path: str): import subprocess import tempfile import wave text = (text or "").strip() if not text: raise ValueError("请输入文字内容") if len(text) > MAX_TTS_CHARS: raise ValueError(f"文字过长(最多 {MAX_TTS_CHARS} 字)") if voice not in VOICE_FILES: voice = "zh_CN-huayan-medium" rate_pct = max(-50, min(100, int(rate_pct))) v = _load_voice(voice) with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tf: wav_path = tf.name try: with open(wav_path, "wb") as f: with wave.open(f, "wb") as wf: v.synthesize_wav(text, wf) # post-process: speed (atempo) + pitch-free, output mp3 tempo = 1 + rate_pct / 100.0 tempo = max(0.5, min(2.0, tempo)) argv = [FFMPEG_BIN, "-y", "-i", wav_path, "-filter:a", f"atempo={tempo:.4f}", "-c:a", "libmp3lame", "-b:a", "192k", out_path] env = dict(os.environ) env.pop("HTTPS_PROXY", None) r = subprocess.run(argv, capture_output=True, timeout=120, env=env) if r.returncode != 0: # fallback: plain wav->mp3 r = subprocess.run([FFMPEG_BIN, "-y", "-i", wav_path, "-c:a", "libmp3lame", "-b:a", "192k", out_path], capture_output=True, timeout=120, env=env) if r.returncode != 0: raise RuntimeError("音频转码失败") finally: try: os.remove(wav_path) except Exception: pass if not os.path.exists(out_path) or os.path.getsize(out_path) < 100: raise RuntimeError("TTS 生成失败") def run_asr(audio_path: str, language: str): model = _get_asr() language = language if language in ("zh", "en", "ja", "yue") else None segments, info = model.transcribe(audio_path, language=language, beam_size=2, vad_filter=True) out = [] texts = [] for s in segments: item = {"start": round(s.start, 1), "end": round(s.end, 1), "text": s.text.strip()} out.append(item) texts.append(s.text.strip()) return { "text": "\n".join(texts), "segments": out[:200], "language": info.language, "probability": round(info.language_probability, 2), } # voices served by local piper models VOICES = [ {"value": k, "label": v, "lang": k.split("_")[0]} for k, v in VOICE_FILES.items() ]