接收服务端识别结果 + 打印实时文本 + 打印延迟 + 写验收日志(events.jsonl)
(id, writer: MeetingWriter)
| 315 | |
| 316 | |
| 317 | async def message(id, writer: MeetingWriter): |
| 318 | """接收服务端识别结果 + 打印实时文本 + 打印延迟 + 写验收日志(events.jsonl)""" |
| 319 | import websockets |
| 320 | global websocket, voices, offline_msg_done |
| 321 | global latency_first_audio_time, latency_last_audio_time, latency_first_text_printed |
| 322 | |
| 323 | multi_mode = args.thread_num > 1 # 多路并发时,打印风格更简洁 |
| 324 | text_print = "" |
| 325 | text_print_2pass_online = "" |
| 326 | text_print_2pass_offline = "" |
| 327 | |
| 328 | if args.output_dir is not None: |
| 329 | ibest_writer = open( |
| 330 | os.path.join(args.output_dir, "text.{}".format(id)), "a", encoding="utf-8" |
| 331 | ) |
| 332 | else: |
| 333 | ibest_writer = None |
| 334 | |
| 335 | try: |
| 336 | while True: |
| 337 | meg = await websocket.recv() |
| 338 | meg = json.loads(meg) |
| 339 | |
| 340 | wav_name = meg.get("wav_name", "demo") |
| 341 | text = meg.get("text", "") |
| 342 | mode = meg.get("mode", "") |
| 343 | spk_name = meg.get("spk_name", "") |
| 344 | spk_score = meg.get("spk_score", None) |
| 345 | now_ts = time.time() |
| 346 | |
| 347 | # === 延迟统计:仅在首条 online/2pass-online 文本时计算并打印一次 === |
| 348 | latency_last_ms = None |
| 349 | latency_first_ms = None |
| 350 | if text and mode in ("online", "2pass-online"): |
| 351 | if not latency_first_text_printed.get(wav_name, False): |
| 352 | t_last = latency_last_audio_time.get(wav_name, None) |
| 353 | t_first = latency_first_audio_time.get(wav_name, None) |
| 354 | latency_last_ms = (now_ts - t_last) * 1000.0 if t_last is not None else None |
| 355 | latency_first_ms = (now_ts - t_first) * 1000.0 if t_first is not None else None |
| 356 | |
| 357 | latency_first_text_printed[wav_name] = True |
| 358 | |
| 359 | if multi_mode: |
| 360 | parts = [f"[MEETING {id}][LATENCY] wav={wav_name}, mode={mode}"] |
| 361 | if latency_last_ms is not None: |
| 362 | parts.append(f"from_last_chunk={latency_last_ms:.1f} ms") |
| 363 | if latency_first_ms is not None: |
| 364 | parts.append(f"from_first_chunk={latency_first_ms:.1f} ms") |
| 365 | print(" ".join(parts)) |
| 366 | else: |
| 367 | print( |
| 368 | f"[LATENCY] wav={wav_name}, mode={mode}, " |
| 369 | f"from_last_chunk={(latency_last_ms or 0):.1f} ms, " |
| 370 | f"from_first_chunk={(latency_first_ms or 0):.1f} ms" |
| 371 | ) |
| 372 | |
| 373 | timestamp = meg.get("timestamp", "") |
| 374 | offline_msg_done = meg.get("is_final", False) |
no test coverage detected
searching dependent graphs…