add edge tts
This commit is contained in:
118
tts/examples.py
Normal file
118
tts/examples.py
Normal file
@ -0,0 +1,118 @@
|
||||
"""
|
||||
TTS 模块使用示例
|
||||
|
||||
演示如何使用 TTS 引擎和服务。
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from io import BytesIO
|
||||
|
||||
# 示例 1: 直接使用 Edge-TTS 引擎
|
||||
async def example_direct_engine():
|
||||
"""直接使用 EdgeTTSEngine"""
|
||||
from tts.edge_tts_engine import EdgeTTSEngine
|
||||
|
||||
engine = EdgeTTSEngine()
|
||||
print(f"Engine: {engine.get_engine_name()} v{engine.get_engine_version()}")
|
||||
|
||||
# 合成语音
|
||||
text = "你好,我是语音合成助手。"
|
||||
audio = await engine.synthesize(text, language="zh-CN")
|
||||
print(f"Audio synthesized: {audio.getbuffer().nbytes} bytes")
|
||||
|
||||
# 获取支持的声音
|
||||
voices = await engine.get_supported_voices("zh-CN")
|
||||
print(f"Supported voices: {len(voices)} found")
|
||||
for voice in voices[:3]:
|
||||
print(f" - {voice['display_name']} ({voice['name']})")
|
||||
|
||||
|
||||
# 示例 2: 使用工厂模式创建引擎
|
||||
async def example_factory():
|
||||
"""使用 TTSEngineFactory 创建引擎"""
|
||||
from tts.factory import TTSEngineFactory
|
||||
|
||||
# 创建 Edge-TTS 引擎
|
||||
engine = TTSEngineFactory.create("edge-tts")
|
||||
print(f"\nUsing {engine.get_engine_name()} engine")
|
||||
|
||||
# 合成多种语言
|
||||
texts = {
|
||||
"zh-CN": "你好,世界!",
|
||||
"en-US": "Hello, World!",
|
||||
"ja-JP": "こんにちは、世界!",
|
||||
}
|
||||
|
||||
for language, text in texts.items():
|
||||
audio = await engine.synthesize(text, language=language)
|
||||
print(f"Synthesized {language}: {audio.getbuffer().nbytes} bytes")
|
||||
|
||||
|
||||
# 示例 3: 使用高级服务
|
||||
async def example_service():
|
||||
"""使用 TTSService 高级接口"""
|
||||
from tts.service import TTSService
|
||||
|
||||
# 获取引擎信息
|
||||
info = TTSService.get_engine_info()
|
||||
print(f"\nTTS Service Info: {info}")
|
||||
|
||||
# 使用默认配置合成
|
||||
text = "使用服务默认配置合成语音。"
|
||||
audio = await TTSService.synthesize(text)
|
||||
print(f"Synthesized with defaults: {audio.getbuffer().nbytes} bytes")
|
||||
|
||||
# 使用自定义参数合成
|
||||
text = "这是一个更快的语音示例。"
|
||||
audio = await TTSService.synthesize(text, rate=1.2)
|
||||
print(f"Synthesized with rate=1.2: {audio.getbuffer().nbytes} bytes")
|
||||
|
||||
# 获取声音列表
|
||||
voices = await TTSService.get_supported_voices()
|
||||
print(f"Available voices: {len(voices)} found")
|
||||
|
||||
|
||||
# 示例 4: 保存合成的音频到文件
|
||||
async def example_save_audio():
|
||||
"""合成语音并保存到文件"""
|
||||
from tts.service import TTSService
|
||||
|
||||
text = "这是一个保存到文件的语音示例。"
|
||||
audio = await TTSService.synthesize(text)
|
||||
|
||||
# 保存为 MP3 文件
|
||||
output_file = "output_audio.mp3"
|
||||
with open(output_file, "wb") as f:
|
||||
f.write(audio.getvalue())
|
||||
print(f"\nAudio saved to {output_file}")
|
||||
|
||||
|
||||
async def main():
|
||||
"""运行所有示例"""
|
||||
print("=" * 50)
|
||||
print("TTS Module Examples")
|
||||
print("=" * 50)
|
||||
|
||||
try:
|
||||
print("\n1. Direct Engine Usage")
|
||||
print("-" * 50)
|
||||
await example_direct_engine()
|
||||
|
||||
print("\n2. Factory Pattern")
|
||||
print("-" * 50)
|
||||
await example_factory()
|
||||
|
||||
print("\n3. Service Interface")
|
||||
print("-" * 50)
|
||||
await example_service()
|
||||
|
||||
print("\n4. Save Audio to File")
|
||||
print("-" * 50)
|
||||
await example_save_audio()
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user