97 lines
2.5 KiB
Python
97 lines
2.5 KiB
Python
|
|
"""
|
||
|
|
TTS 流式合成测试
|
||
|
|
"""
|
||
|
|
|
||
|
|
from pathlib import Path
|
||
|
|
from src.Module.tts.tts import StreamingTTS, synthesize, AudioChunk
|
||
|
|
|
||
|
|
|
||
|
|
def test_stream_synthesis():
|
||
|
|
"""测试流式合成 - 使用 yield 生成器"""
|
||
|
|
print("=" * 50)
|
||
|
|
print("流式语音合成测试")
|
||
|
|
print("=" * 50)
|
||
|
|
|
||
|
|
# 创建 TTS 实例
|
||
|
|
tts = StreamingTTS(
|
||
|
|
voice='Cherry',
|
||
|
|
language='Chinese',
|
||
|
|
speech_rate=1.0,
|
||
|
|
)
|
||
|
|
|
||
|
|
text = "你好,我是通义千问语音合成系统。这是一段流式合成测试。"
|
||
|
|
print(f"合成文本: {text}\n")
|
||
|
|
|
||
|
|
# 流式接收音频
|
||
|
|
total_bytes = 0
|
||
|
|
chunk_count = 0
|
||
|
|
audio_data = bytearray()
|
||
|
|
|
||
|
|
print("开始流式接收音频...")
|
||
|
|
for chunk in tts.stream(text):
|
||
|
|
if chunk.error:
|
||
|
|
print(f" 错误: {chunk.error}")
|
||
|
|
break
|
||
|
|
|
||
|
|
if chunk.data:
|
||
|
|
chunk_count += 1
|
||
|
|
total_bytes += len(chunk.data)
|
||
|
|
audio_data.extend(chunk.data)
|
||
|
|
print(
|
||
|
|
f" [Chunk {chunk_count:02d}] 收到 {len(chunk.data):5d} 字节 | 累计: {total_bytes:6d} 字节")
|
||
|
|
|
||
|
|
if chunk.is_final:
|
||
|
|
print("\n流式传输完成!")
|
||
|
|
break
|
||
|
|
|
||
|
|
# 保存为 WAV 文件
|
||
|
|
output_dir = Path(__file__).parent / 'output'
|
||
|
|
output_dir.mkdir(exist_ok=True)
|
||
|
|
wav_file = output_dir / 'stream_test.wav'
|
||
|
|
|
||
|
|
import wave
|
||
|
|
with wave.open(str(wav_file), 'wb') as wav:
|
||
|
|
wav.setnchannels(1)
|
||
|
|
wav.setsampwidth(2)
|
||
|
|
wav.setframerate(24000)
|
||
|
|
wav.writeframes(audio_data)
|
||
|
|
|
||
|
|
print(f"\n总计: {chunk_count} 个数据块, {total_bytes} 字节")
|
||
|
|
print(f"音频已保存: {wav_file}")
|
||
|
|
|
||
|
|
return chunk_count > 0
|
||
|
|
|
||
|
|
|
||
|
|
def test_convenient_function():
|
||
|
|
"""测试便捷函数"""
|
||
|
|
print("\n" + "=" * 50)
|
||
|
|
print("便捷函数测试")
|
||
|
|
print("=" * 50)
|
||
|
|
|
||
|
|
text = "这是使用便捷函数合成的语音。"
|
||
|
|
print(f"合成文本: {text}\n")
|
||
|
|
|
||
|
|
total_bytes = 0
|
||
|
|
for chunk in synthesize(text, voice='Cherry'):
|
||
|
|
if chunk.data:
|
||
|
|
total_bytes += len(chunk.data)
|
||
|
|
if chunk.is_final:
|
||
|
|
break
|
||
|
|
|
||
|
|
print(f"合成完成,总数据量: {total_bytes} 字节")
|
||
|
|
return total_bytes > 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
print("=" * 60)
|
||
|
|
print(" TTS 流式合成测试")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
success1 = test_stream_synthesis()
|
||
|
|
success2 = test_convenient_function()
|
||
|
|
|
||
|
|
print("\n" + "=" * 60)
|
||
|
|
print("测试结果:")
|
||
|
|
print(f" 流式合成: {'✓ 通过' if success1 else '✗ 失败'}")
|
||
|
|
print(f" 便捷函数: {'✓ 通过' if success2 else '✗ 失败'}")
|