MCPcopy Create free account
hub / github.com/modelscope/FunASR / ws_serve

Function ws_serve

runtime/python/websocket/funasr_wss_server.py:342–534  ·  view source on GitHub ↗
(websocket, path=None)

Source from the content-addressed store, hash-verified

340
341
342async def ws_serve(websocket, path=None):
343 # websockets 新版本不会传 path,这里做兼容
344 if path is None:
345 path = getattr(websocket, "path", None)
346 frames = []
347 frames_asr = []
348 frames_asr_online = []
349 global websocket_users
350 websocket_users.add(websocket)
351
352 websocket.status_dict_asr = {} # hotword 等
353 websocket.status_dict_asr_online = {"cache": {}, "is_final": False}
354 websocket.status_dict_vad = {"cache": {}, "is_final": False}
355 websocket.status_dict_punc = {"cache": {}}
356
357 websocket.chunk_interval = 10
358 websocket.vad_pre_idx = 0
359 speech_start = False
360 speech_end_i = -1
361
362 websocket.wav_name = "microphone"
363 websocket.mode = "2pass"
364 websocket.is_speaking = True # ✅ 默认初始化,避免 AttributeError
365
366 # 保存离线片段
367 websocket.audio_fs = 16000
368 websocket.offline_seg_idx = 0
369 websocket.save_offline_segments = bool(args.save_offline_segments)
370 websocket.offline_save_dir = args.save_offline_segments_dir
371 if websocket.save_offline_segments:
372 _ensure_dir(websocket.offline_save_dir)
373 print(f"[SAVE_OFFLINE_SEG] enabled, dir={websocket.offline_save_dir}")
374
375 print("new user connected", flush=True)
376
377 try:
378 async for message in websocket:
379 # ========== 1) 先处理“文本配置消息” ==========
380 if isinstance(message, str):
381 try:
382 messagejson = json.loads(message)
383 except Exception as e:
384 print("bad json message:", e, message[:200])
385 continue
386
387 print("=============messagejson============", messagejson)
388
389 if "is_speaking" in messagejson:
390 websocket.is_speaking = bool(messagejson["is_speaking"])
391 websocket.status_dict_asr_online["is_final"] = (not websocket.is_speaking)
392
393 if "chunk_interval" in messagejson:
394 websocket.chunk_interval = _safe_int(
395 messagejson["chunk_interval"], websocket.chunk_interval
396 )
397
398 if "wav_name" in messagejson:
399 websocket.wav_name = messagejson.get("wav_name") or websocket.wav_name

Callers

nothing calls this directly

Calls 8

_ensure_dirFunction · 0.85
_safe_intFunction · 0.85
_pcm_duration_msFunction · 0.85
async_asr_onlineFunction · 0.85
async_vadFunction · 0.85
run_blockingFunction · 0.85
async_asrFunction · 0.85
ws_resetFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…