1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| import asyncio import websockets import json import pyaudio import numpy as np import time
async def test_funasr(): """测试FunASR服务器的简单脚本"""
server_url = "ws://ip:端口号" sample_rate = 16000 chunk_duration = 0.3 chunk_size = int(sample_rate * chunk_duration)
print(f"连接到服务器: {server_url}") print(f"采样率: {sample_rate}Hz") print(f"块大小: {chunk_size} samples")
try: async with websockets.connect(server_url, ping_interval=None) as websocket: print("✅ 连接成功")
default_mode = "2pass"
is_speaking = default_mode != "offline"
config = { "mode": default_mode, "wav_name": "server", "wav_format": "pcm", "is_speaking": is_speaking, "chunk_size": [5, 10, 5], "chunk_interval": 10, "itn": True, "hotwords": '{"小翼小翼": 40, "你好小翼": 40 , "退下": 40}' }
print(f"\n使用默认模式: {default_mode}") print("\n发送的配置参数:") import json print(json.dumps(config, indent=2, ensure_ascii=False)) await websocket.send(json.dumps(config))
p = pyaudio.PyAudio()
stream = p.open( format=pyaudio.paInt16, channels=1, rate=sample_rate, input=True, frames_per_buffer=chunk_size )
print(f"\n开始录音 ({config['mode']} 模式)...") print("请说话(持续录音模式)...\n")
chunk_count = 0 silent_count = 0 speaking = False vad_threshold = 0.01 min_speech_duration = 5 max_silent_duration = 10
try: while True: audio_data = stream.read(chunk_size, exception_on_overflow=False)
audio_array = np.frombuffer(audio_data, dtype=np.int16) energy = np.mean(np.abs(audio_array)) / 32768.0
is_voice = energy > vad_threshold
bars = int(energy * 40) print(f"\r音量: [{'█' * bars}{' ' * (40 - bars)}] 能量: {energy:.4f} 语音: {'✓' if is_voice else '✗'} 块: {chunk_count}", end="")
if is_voice: await websocket.send(audio_data) chunk_count += 1 silent_count = 0 if not speaking: speaking = True print("\n🔊 检测到语音,开始发送数据") else: silent_count += 1 if speaking and silent_count > max_silent_duration: speaking = False print("\n🔇 检测到静默,结束语音段") end_msg = json.dumps({"is_speaking": False}) await websocket.send(end_msg) try: result = await asyncio.wait_for(websocket.recv(), timeout=2.0) try: data = json.loads(result) text = data.get('text', '无') if text and text.strip(): print(f"\n完整文本: {text}") else: print("\n🔍 无有效文本") except: pass except asyncio.TimeoutError: pass start_msg = json.dumps({"is_speaking": True}) await websocket.send(start_msg) chunk_count = 0 if chunk_count % 50 == 0 and chunk_count > 0: await websocket.send(audio_data[:100]) except KeyboardInterrupt: stream.stop_stream() stream.close() p.terminate() except Exception as e: print(f"\n❌ 错误: {e}") import traceback traceback.print_exc()
if __name__ == "__main__": asyncio.run(test_funasr())
|