Ai_GirlFriend/test_wav_asr.py

144 lines
4.4 KiB
Python
Raw Normal View History

2026-03-05 13:34:40 +08:00
#!/usr/bin/env python3
"""
测试 WAV 格式 ASR
"""
import sys
import os
sys.path.append('.')
import requests
import base64
import wave
import struct
import math
import logging
# 设置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def create_wav_file():
"""创建一个标准的 WAV 文件"""
sample_rate = 16000
duration = 3 # 3 秒
# 生成音频样本
samples = []
for i in range(sample_rate * duration):
t = i / sample_rate
# 生成复合波形,模拟语音
f0 = 200 + 100 * math.sin(2 * math.pi * 0.5 * t) # 变化的基频
sample = 0
for harmonic in range(1, 5):
amplitude = 1.0 / harmonic
sample += amplitude * math.sin(2 * math.pi * f0 * harmonic * t)
# 添加包络
envelope = 0.5 * (1 + math.sin(2 * math.pi * 2 * t))
final_sample = sample * envelope * 0.3
# 转换为 16-bit 整数
sample_int = int(16000 * final_sample)
sample_int = max(-32767, min(32767, sample_int))
samples.append(sample_int)
# 写入 WAV 文件
wav_file = "test_audio.wav"
with wave.open(wav_file, 'wb') as wav:
wav.setnchannels(1) # 单声道
wav.setsampwidth(2) # 16-bit
wav.setframerate(sample_rate) # 16kHz
# 写入样本数据
for sample in samples:
wav.writeframes(struct.pack('<h', sample))
logger.info(f"创建 WAV 文件: {wav_file}, 时长: {duration}")
return wav_file
def test_wav_asr():
"""测试 WAV ASR"""
# 创建 WAV 文件
wav_file = create_wav_file()
try:
# 读取 WAV 文件
with open(wav_file, 'rb') as f:
wav_data = f.read()
logger.info(f"WAV 文件大小: {len(wav_data)} 字节")
# 转换为 base64
wav_base64 = base64.b64encode(wav_data).decode('utf-8')
logger.info(f"Base64 编码长度: {len(wav_base64)}")
# 准备请求数据
request_data = {
'audio_data': wav_base64,
'format': 'wav'
}
# 发送请求到后端
url = "http://192.168.1.141:30102/voice/call/asr"
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer test_token'
}
logger.info(f"发送 ASR 请求到: {url}")
response = requests.post(url, json=request_data, headers=headers, timeout=60)
logger.info(f"响应状态码: {response.status_code}")
logger.info(f"响应内容: {response.text}")
if response.status_code == 200:
result = response.json()
logger.info(f"✅ ASR 请求成功")
logger.info(f"识别结果: {result}")
if 'data' in result and 'text' in result['data']:
text = result['data']['text']
logger.info(f"🎯 识别文本: {text}")
# 检查结果
if "未识别到语音内容" in text or "音频中未检测到有效语音" in text:
logger.info("✅ WAV 格式正确,但没有检测到语音(这是预期的,因为是纯音调)")
return True
elif "音频格式解码失败" in text or "DECODE_ERROR" in text:
logger.error("❌ WAV 格式有问题")
return False
else:
logger.info("✅ ASR 处理成功")
return True
else:
logger.warning("响应格式不符合预期")
return False
else:
logger.error(f"❌ ASR 请求失败: {response.status_code}")
return False
except Exception as e:
logger.error(f"❌ 测试失败: {e}")
import traceback
logger.error(f"错误堆栈: {traceback.format_exc()}")
return False
finally:
# 清理文件
try:
if os.path.exists(wav_file):
os.remove(wav_file)
except:
pass
if __name__ == "__main__":
logger.info("开始测试 WAV ASR...")
success = test_wav_asr()
if success:
logger.info("🎉 WAV ASR 测试成功!")
logger.info("现在可以在前端测试录音功能了")
else:
logger.error("💥 WAV ASR 测试失败")
sys.exit(1)