#!/usr/bin/env python3
"""
本地 TTS 測試腳本
使用 sherpa-onnx 在本地端產生語音，無需 GPU、無需雲端
"""

import subprocess
import os

# 路徑設定
SHERPA_DIR = os.path.expanduser("~/.openclaw/tools/sherpa-onnx-tts")
TTS_BIN = f"{SHERPA_DIR}/runtime/bin/sherpa-onnx-offline-tts"
ZH_MODEL = f"{SHERPA_DIR}/models/vits-icefall-zh-aishell3"
EN_MODEL = f"{SHERPA_DIR}/models/vits-piper-en_US-lessac-high"

def tts(text, output_file="output.wav", lang="zh"):
    """產生語音檔案"""
    model_dir = ZH_MODEL if lang == "zh" else EN_MODEL
    
    cmd = [
        TTS_BIN,
        "--model-dir", model_dir,
        "-o", output_file,
        text
    ]
    
    print(f"🗣️ 正在產生語音：{text}")
    print(f"📁 模型：{lang.upper()}")
    
    result = subprocess.run(cmd, capture_output=True, text=True)
    
    if result.returncode == 0 and os.path.exists(output_file):
        size = os.path.getsize(output_file)
        print(f"✅ 完成！檔案：{output_file}（{size} bytes）")
        return output_file
    else:
        print(f"❌ 失敗：{result.stderr}")
        return None

if __name__ == "__main__":
    # 測試中文
    tts("你好，我是小綠，這是本地語音合成測試。", "test_zh.wav", "zh")
    
    # 測試英文
    tts("Hello, I am Xiao Lu. This is a local TTS test.", "test_en.wav", "en")
    
    print("\n🎵 測試完成！播放試試看：")
    print("  afplay test_zh.wav")
    print("  afplay test_en.wav")
