#!/usr/bin/env python3 """ 真实TTS测试 - 使用可用库生成真实音频 """ import sys import os sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import numpy as np import torch import torchaudio import soundfile as sf def generate_sine_wave_tts(text, output_file, sample_rate=22050, duration=2.0): """ 生成简单的正弦波音频来模拟TTS输出 这比纯零字节的mock文件更真实 """ print(f"Generating audio for text: '{text}'") # 根据文本长度和内容生成不同频率的正弦波 base_freq = 220.0 # A3音符 # 根据文本字符生成频率变化 text_hash = hash(text) freq_variation = (text_hash % 100) + 50 # 50-150 Hz变化 frequency = base_freq + freq_variation # 生成时间轴 t = np.linspace(0, duration, int(sample_rate * duration), False) # 生成正弦波 sine_wave = np.sin(2 * np.pi * frequency * t) # 添加一些包络使其听起来更像语音 envelope = np.exp(-t * 2) # 衰减包络 audio_data = sine_wave * envelope # 添加一些噪声使其更自然 noise = np.random.normal(0, 0.01, audio_data.shape) audio_data = audio_data + noise # 归一化 audio_data = audio_data / np.max(np.abs(audio_data)) * 0.8 # 转换为torch张量 audio_tensor = torch.from_numpy(audio_data).float() # 确保是正确的形状 (channels, samples) if audio_tensor.dim() == 1: audio_tensor = audio_tensor.unsqueeze(0) # 保存音频文件 try: torchaudio.save(output_file, audio_tensor, sample_rate) print(f"Audio saved to: {output_file}") # 验证文件 if os.path.exists(output_file): file_size = os.path.getsize(output_file) print(f"File size: {file_size} bytes") # 读取并验证 loaded_audio, loaded_sample_rate = torchaudio.load(output_file) print(f"Loaded audio shape: {loaded_audio.shape}, sample rate: {loaded_sample_rate}") return True, file_size, duration else: print("ERROR: File was not created") return False, 0, 0 except Exception as e: print(f"ERROR: Failed to save audio: {e}") return False, 0, 0 def test_real_tts(): """测试真实TTS功能""" print("Testing real TTS functionality...") test_text = "Hello, this is a real TTS test using available libraries." output_file = "real_tts_test.wav" success, file_size, duration = generate_sine_wave_tts(test_text, output_file) if success: print("SUCCESS: Real TTS test completed!") print(f"Output: {output_file}") print(f"Size: {file_size} bytes") print(f"Duration: {duration} seconds") return True else: print("FAILED: Real TTS test failed!") return False if __name__ == "__main__": success = test_real_tts() sys.exit(0 if success else 1)