5. 回答简洁,直接给代码+关键注释\r
6. 给出结论的时候,列出的每一个改动,后面都要附带影响描述\r
7. try 之类的安全检测要尽量简化,只有必要的部分才加。\r
+8. 所有windows下执行的环境都是gbk编码,python如果想证实显示中文,要注意编码格式。\r
# 无按键确认音:不按 key_prompt;仅按键/PTT 开录时播 start.mp3(自动续聊不播)
# PTT idle 短按:notify 话术 PTT_HINT;长按开录:start.mp3
-RECORD_START_TONE_FILE = "U:/static/start.mp3" # 按键/PTT 开录提示音(非 server.started 自动开麦)
+RECORD_START_TONE_FILE = "U:/static/start.pcm" # 按键/PTT 开录提示音(PCM 走 play_pcm 复用)
RECORD_START_TONE_GAP_MS = 180
PTT_MIN_RECORD_MS = 400
# 11. 调试开关(量产可全 False)
# =============================================================================
-LED_DEBUG = True
-LED_STATE_DEBUG = True
-KEY_LED_DEBUG = True
+LED_DEBUG = False
+LED_STATE_DEBUG = False
+KEY_LED_DEBUG = False
HW_STATUS_DEBUG = True
\ No newline at end of file
# 无按键确认音:不按 key_prompt;仅按键/PTT 开录时播 start.mp3(自动续聊不播)
# PTT idle 短按:notify 话术 PTT_HINT;长按开录:start.mp3
-RECORD_START_TONE_FILE = "U:/static/start.mp3" # 按键/PTT 开录提示音(非 server.started 自动开麦)
+RECORD_START_TONE_FILE = "U:/static/start.pcm" # 按键/PTT 开录提示音(PCM 走 play_pcm 复用)
RECORD_START_TONE_GAP_MS = 180
PTT_MIN_RECORD_MS = 400
# 11. 调试开关(量产可全 False)
# =============================================================================
-LED_DEBUG = True
-LED_STATE_DEBUG = True
-KEY_LED_DEBUG = True
+LED_DEBUG = False
+LED_STATE_DEBUG = False
+KEY_LED_DEBUG = False
HW_STATUS_DEBUG = True
\ No newline at end of file
--- /dev/null
+# simple MP3 play test
+
+import audio, utime, uos
+
+MP3 = '/usr/static/start.mp3'
+
+aud = audio.Audio(0)
+
+with open(MP3, 'rb') as f:
+ mp3 = f.read()
+print('file: ' + MP3 + ' size=' + str(len(mp3)) + 'B')
+
+print('')
+print('--- test 1: playStream(3) once ---')
+t0 = utime.ticks_ms()
+ret = aud.playStream(3, mp3)
+t1 = utime.ticks_ms()
+print('playStream ret=' + str(ret) + ' took ' + str(utime.ticks_diff(t1, t0)) + 'ms')
+print('waiting 500ms for playback...')
+utime.sleep_ms(500)
+print('done. Did you hear it?')
+
+print('')
+print('--- test 2: stopPlayStream latency ---')
+t0 = utime.ticks_ms()
+ret = aud.playStream(3, mp3)
+print('playStream ret=' + str(ret))
+utime.sleep_ms(10) # let it start
+t1 = utime.ticks_ms()
+ret2 = aud.stopPlayStream()
+t2 = utime.ticks_ms()
+print('stopPlayStream ret=' + str(ret2) + ' took ' + str(utime.ticks_diff(t2, t1)) + 'ms')
+
+print('')
+print('--- test 3: loop 5x with 300ms gap ---')
+for i in range(5):
+ t0 = utime.ticks_ms()
+ ret = aud.playStream(3, mp3)
+ t1 = utime.ticks_ms()
+ print('#' + str(i+1) + ' play ret=' + str(ret) + ' took ' + str(utime.ticks_diff(t1, t0)) + 'ms')
+ utime.sleep_ms(300)
+
+aud.stopPlayStream()
+print('done.')
--- /dev/null
+# playback interrupt test v12 - hybrid PCM keep-open
+# PCM<->PCM: keep handle, stream_stop + stream_write_start only
+# PCM->MP3: MUST close WRITE handle (C层占设备,stream_halt不够)
+# MP3->PCM: reopen handle + retry (like VC_Play_Start)
+
+import audio, utime, gc, math, uos
+
+SR = 16000
+MP3 = '/usr/static/start.mp3'
+THINK = '/usr/static/think.mp3'
+
+_cb = []
+_busy = 0
+_G_PCM = None
+_pcm_open_cnt = 0
+_pcm_close_cnt = 0
+_pcm_reset_cnt = 0
+
+def _on_cb(e):
+ global _busy
+ t = utime.ticks_ms()
+ if e == 0:
+ _cb.append((t, 'START'))
+ _busy = 1
+ elif e == 7:
+ _cb.append((t, 'FINISH'))
+ _busy = 0
+ else:
+ _cb.append((t, 'EVT' + str(e)))
+
+def _us(v):
+ if v >= 1000000:
+ return str(round(v / 10000) / 100.0) + 's'
+ if v >= 1000:
+ return str(round(v / 10) / 100.0) + 'ms'
+ return str(v) + 'us'
+
+def _tc(l, f, *a):
+ gc.collect()
+ t0 = utime.ticks_us()
+ r = f(*a)
+ d = utime.ticks_diff(utime.ticks_us(), t0)
+ print('[TIME] ' + l + ' -> ' + _us(d) + ' ret=' + str(r))
+ return r, d
+
+def _clr():
+ global _cb, _busy
+ _cb.clear()
+ _busy = 0
+
+def _wait_on_cb(timeout_ms=4000, L=''):
+ t0 = utime.ticks_ms()
+ while not _busy:
+ if utime.ticks_diff(utime.ticks_ms(), t0) > timeout_ms:
+ print('[WAIT] ' + L + ' cb TIMEOUT ' + str(timeout_ms) + 'ms')
+ return False
+ utime.sleep_ms(10)
+ dt1 = utime.ticks_diff(utime.ticks_ms(), t0)
+ print('[WAIT] ' + L + ' START in ' + str(dt1) + 'ms')
+ while _busy:
+ if utime.ticks_diff(utime.ticks_ms(), t0) > timeout_ms + 5000:
+ print('[WAIT] ' + L + ' FINISH TIMEOUT')
+ return False
+ utime.sleep_ms(10)
+ dt2 = utime.ticks_diff(utime.ticks_ms(), t0)
+ print('[WAIT] ' + L + ' FINISH in ' + str(dt2) + 'ms')
+ return True
+
+def _wait_start(L='', timeout_ms=800):
+ t0 = utime.ticks_ms()
+ while not _busy:
+ if utime.ticks_diff(utime.ticks_ms(), t0) > timeout_ms:
+ print('[WAIT] ' + L + ' START TIMEOUT')
+ return False
+ utime.sleep_ms(10)
+ print('[WAIT] ' + L + ' START in ' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+ return True
+
+def _wait_ms(ms, L='sleep'):
+ print('[WAIT] ' + L + ' ' + str(ms) + 'ms')
+ utime.sleep_ms(ms)
+
+def _aud_release():
+ try:
+ aud.stopPlayStream()
+ except Exception:
+ pass
+ try:
+ aud.stopAll()
+ except Exception:
+ pass
+
+def _pcm_release_for_mp3():
+ """PCM->MP3:必须 close WRITE handle,否则 aud.play 无回调。"""
+ global _G_PCM, _pcm_close_cnt
+ t0 = utime.ticks_us()
+ if _G_PCM is not None:
+ try:
+ _G_PCM.stream_stop()
+ except Exception:
+ pass
+ try:
+ _G_PCM.close()
+ except Exception:
+ pass
+ _pcm_close_cnt += 1
+ print('[PCM-KEEP] close for mp3 #' + str(_pcm_close_cnt))
+ _G_PCM = None
+ _aud_release()
+ utime.sleep_ms(100)
+ print('[PCM-KEEP] mp3 prep ' + _us(utime.ticks_diff(utime.ticks_us(), t0)))
+
+def _mp3_play(label, n=1, gap_ms=400):
+ for i in range(n):
+ if _G_PCM is not None:
+ _pcm_release_for_mp3()
+ _clr()
+ r, u = _tc(label + ' #' + str(i + 1), aud.play, 1, 0, MP3)
+ _wait_on_cb(3000, label + ' #' + str(i + 1))
+ if i + 1 < n:
+ utime.sleep_ms(gap_ms)
+ return r
+
+def _mp3_once(label, path=None):
+ if path is None:
+ path = MP3
+ _pcm_release_for_mp3()
+ _clr()
+ r, u = _tc(label, aud.play, 1, 0, path)
+ _wait_on_cb(5000, label)
+ _aud_release()
+ utime.sleep_ms(80)
+ return r
+
+
+# === PCM session ===
+
+def _pcm(buf_ms=200, freq=660):
+ n = SR * buf_ms // 1000
+ buf = bytearray(n * 2)
+ for i in range(n):
+ v = int(8000 * math.sin(2 * math.pi * freq * i / SR))
+ buf[i * 2] = v & 0xFF
+ buf[i * 2 + 1] = (v >> 8) & 0xFF
+ return buf
+
+def _pcm_handle_open():
+ global _G_PCM, _pcm_open_cnt
+ if _G_PCM is not None:
+ return _G_PCM
+ last_err = None
+ for i in range(3):
+ try:
+ if i > 0:
+ utime.sleep_ms(80 + i * 60)
+ _G_PCM = audio.Audio.PCM(0, 1, SR, 1, 1, 2)
+ try:
+ _G_PCM.stream_stop()
+ except Exception:
+ pass
+ ret = _G_PCM.stream_write_start()
+ _pcm_open_cnt += 1
+ print('[PCM-KEEP] handle open #' + str(_pcm_open_cnt) + ' ret=' + str(ret) + ' try=' + str(i))
+ return _G_PCM
+ except Exception as e:
+ last_err = e
+ print('[PCM-KEEP] open fail try=' + str(i) + ' err=' + str(e))
+ try:
+ if _G_PCM is not None:
+ _G_PCM.close()
+ except Exception:
+ pass
+ _G_PCM = None
+ raise last_err
+
+def _pcm_stream_reset(after_mp3=0):
+ global _G_PCM, _pcm_reset_cnt
+ if after_mp3:
+ _aud_release()
+ utime.sleep_ms(80)
+ p = _pcm_handle_open()
+ try:
+ p.stream_pause(0)
+ except Exception:
+ pass
+ try:
+ p.stream_stop()
+ except Exception:
+ pass
+ ret = p.stream_write_start()
+ _pcm_reset_cnt += 1
+ print('[PCM-KEEP] stream reset #' + str(_pcm_reset_cnt) + ' ret=' + str(ret))
+ return p
+
+def _pcm_play(buf, label='', after_mp3=0, flush=0):
+ if flush or after_mp3 or _G_PCM is None:
+ p = _pcm_stream_reset(after_mp3=after_mp3)
+ else:
+ p = _G_PCM
+ if not isinstance(buf, bytearray):
+ buf = bytearray(buf)
+ ret = p.stream_write(buf, len(buf))
+ print('[PCM] stream_write ' + label + ' len=' + str(len(buf)) + ' ret=' + str(ret))
+ return ret
+
+def _pcm_session_close():
+ global _G_PCM
+ if _G_PCM is None:
+ return
+ try:
+ _G_PCM.stream_stop()
+ except Exception:
+ pass
+ try:
+ _G_PCM.close()
+ except Exception:
+ pass
+ print('[PCM-KEEP] session close')
+ _G_PCM = None
+
+
+# === init ===
+print('[INIT] v12 hybrid keep-open')
+aud = audio.Audio(0)
+aud.setCallback(_on_cb)
+aud.setVolume(11)
+
+try:
+ print('[INIT] mp3=' + MP3 + ' (' + str(uos.stat(MP3)[6]) + 'B)')
+except Exception:
+ print('[INIT] mp3 NOT FOUND')
+try:
+ print('[INIT] think=' + THINK + ' (' + str(uos.stat(THINK)[6]) + 'B)')
+except Exception:
+ THINK = MP3
+
+
+# === S0-S3: MP3 only ===
+print('')
+print('=' * 50)
+print('[S0] think.mp3 once')
+_clr()
+t0 = utime.ticks_ms()
+_, _ = _tc('S0', aud.play, 1, 0, THINK)
+_wait_on_cb(5000, 'S0')
+print('[S0] total=' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+
+print('')
+print('=' * 50)
+print('[S1] start.mp3 x5')
+_clr()
+t0 = utime.ticks_ms()
+_mp3_play('S1', 5, 400)
+print('[S1] total=' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+
+print('')
+print('=' * 50)
+print('[S2] MP3 interrupt on START')
+_clr()
+t0 = utime.ticks_ms()
+_, _ = _tc('S2#1', aud.play, 1, 0, MP3)
+_wait_start('S2#1', 800)
+_clr()
+tk = utime.ticks_ms()
+_, ss = _tc('S2 stop', aud.stopPlayStream)
+utime.sleep_ms(20)
+_, sp = _tc('S2#2', aud.play, 1, 0, MP3)
+_wait_on_cb(3000, 'S2#2')
+print('[S2] lapse=' + str(utime.ticks_diff(utime.ticks_ms(), tk)) + 'ms')
+
+print('')
+print('=' * 50)
+print('[S3] MP3 x3')
+_clr()
+t0 = utime.ticks_ms()
+_mp3_play('S3', 3, 200)
+print('[S3] total=' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+
+
+# === S4+: hybrid ===
+print('')
+print('=' * 50)
+print('[S4] PCM 550Hz -> MP3 (close handle before mp3)')
+_clr()
+t0 = utime.ticks_ms()
+_pcm_play(_pcm(300, 550), 'S4 550Hz', flush=1)
+_wait_ms(450, 'S4 pcm')
+_mp3_once('S4 mp3')
+print('[S4] total=' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+
+print('')
+print('=' * 50)
+print('[S5] MP3 -> PCM 880Hz (reopen after mp3)')
+_clr()
+t0 = utime.ticks_ms()
+_clr()
+_, _ = _tc('S5 mp3', aud.play, 1, 0, MP3)
+_wait_start('S5 mp3', 500)
+_aud_release()
+utime.sleep_ms(80)
+_pcm_play(_pcm(300, 880), 'S5 880Hz', after_mp3=1)
+_wait_ms(450, 'S5 pcm')
+print('[S5] total=' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+
+print('')
+print('=' * 50)
+print('[S6] MP3 x3 (close pcm handle before each)')
+_clr()
+t0 = utime.ticks_ms()
+_mp3_play('S6', 3, 150)
+print('[S6] total=' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+
+print('')
+print('=' * 50)
+print('[S7] PCM-A -> PCM-B (keep handle, reset only)')
+_clr()
+t0 = utime.ticks_ms()
+_pcm_play(_pcm(600, 440), 'S7 A 440Hz', flush=1)
+_wait_ms(200, 'S7 A partial')
+_pcm_play(_pcm(400, 1200), 'S7 B 1200Hz', flush=1)
+_wait_ms(500, 'S7 B play')
+print('[S7] total=' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+
+print('')
+print('=' * 50)
+print('[S8] MP3 -> PCM 660Hz')
+_clr()
+t0 = utime.ticks_ms()
+_pcm_release_for_mp3()
+_clr()
+_, _ = _tc('S8 mp3', aud.play, 1, 0, MP3)
+_wait_start('S8 mp3', 500)
+_pcm_play(_pcm(400, 660), 'S8 660Hz', after_mp3=1)
+_wait_ms(500, 'S8 pcm')
+print('[S8] total=' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+
+print('')
+print('=' * 50)
+print('[S9] PCM -> MP3 (close then mp3)')
+_clr()
+t0 = utime.ticks_ms()
+_pcm_play(_pcm(600, 500), 'S9 PCM 500Hz', flush=1)
+_wait_ms(200, 'S9 partial')
+_mp3_once('S9 mp3')
+print('[S9] total=' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+
+print('')
+print('=' * 50)
+print('[S10] PCM storm (reset only)')
+_clr()
+t0 = utime.ticks_ms()
+for idx, (freq, ms, tag) in enumerate(((440, 300, 'A1'), (1200, 250, 'B'), (880, 300, 'A2'))):
+ _pcm_play(_pcm(ms, freq), 'S10 ' + tag + ' ' + str(freq), flush=1)
+ if idx < 2:
+ _wait_ms(120, 'S10 ' + tag + ' partial')
+ else:
+ _wait_ms(ms + 100, 'S10 ' + tag + ' finish')
+print('[S10] total=' + str(utime.ticks_diff(utime.ticks_ms(), t0)) + 'ms')
+
+
+# === cleanup ===
+_pcm_session_close()
+try:
+ aud.stopAll()
+except Exception:
+ pass
+
+print('')
+print('=' * 50)
+print('=== DONE v12 hybrid ===')
+print('PCM open=' + str(_pcm_open_cnt) + ' close=' + str(_pcm_close_cnt) + ' reset=' + str(_pcm_reset_cnt))
+print('PCM<->PCM: keep handle | PCM<->MP3: close/reopen required by driver')
--- /dev/null
+# -*- coding: utf-8 -*-\r
+"""\r
+录音缓存回收 + 暂停恢复 + 耗时测试\r
+\r
+测试目标:\r
+ 1. 测量 stream_start / stream_stop / stop 耗时(us 级)\r
+ 2. 验证 stream_stop → stream_start 能否恢复录音(无 pause/resume 接口)\r
+ 3. 验证排干后 PCM 完整性(文件可播放)\r
+\r
+录音硬件链路:\r
+ DMA → stream_ring_buf → stream_read() → 文件\r
+\r
+停录时序:\r
+ stream_stop() → 循环 stream_read 排干 → stop()\r
+\r
+输出文件:\r
+ /usr/test_phase1_2_3.pcm (全部三个阶段连续PCM,可播放)\r
+\r
+用法:\r
+ 在设备 REPL 粘贴运行,或导入后手动调用测试函数\r
+"""\r
+\r
+import audio\r
+import utime\r
+import uos\r
+import _thread\r
+import gc\r
+\r
+\r
+SAMPLE_RATE = 16000\r
+AUDIO_BUF_SIZE = 20 * 1024\r
+BYTES_PER_SEC = SAMPLE_RATE * 2 # 16bit mono = 32000 bytes/s\r
+RESERVE_KB = 16 # 留给其他文件的缓冲\r
+\r
+# --- 自动选择存储路径 & 根据可用空间计算录音时长 ---\r
+def _fs_free(path):\r
+ """返回 path 所在文件系统的剩余字节。失败返回 -1。"""\r
+ try:\r
+ st = uos.statvfs(path)\r
+ return st[0] * st[3] # f_bsize * f_bavail\r
+ except Exception:\r
+ return -1\r
+\r
+PCM_PATH = "/usr/test_drain.pcm"\r
+free_bytes = _fs_free("/usr/")\r
+if free_bytes > 0:\r
+ usable = free_bytes - RESERVE_KB * 1024\r
+ if usable > 0:\r
+ max_sec = int(usable // BYTES_PER_SEC)\r
+ print("[TEST] /usr/ 可用 {}KB,扣除 {}KB 缓冲后可用 {}KB ≈ {}s PCM".format(\r
+ free_bytes // 1024, RESERVE_KB, usable // 1024, max_sec))\r
+ else:\r
+ max_sec = 0\r
+else:\r
+ # 尝试 SD 卡\r
+ free_sd = _fs_free("/sd/")\r
+ if free_sd > 0:\r
+ PCM_PATH = "/sd/test_drain.pcm"\r
+ usable = free_sd - RESERVE_KB * 1024\r
+ max_sec = int(usable // BYTES_PER_SEC)\r
+ print("[TEST] SD卡 可用 {}KB,可用 {}s PCM".format(free_sd // 1024, max_sec))\r
+ else:\r
+ max_sec = 0\r
+\r
+if max_sec < 5:\r
+ print("[TEST] ❌ 可用空间不足(需至少 5s ≈ 160KB),请清理 /usr/ 或插入 SD 卡")\r
+ # 不 raise,让下面的测试自己处理\r
+ SEC1, SEC2, SEC3 = 0, 0, 0\r
+else:\r
+ # 按比例分配:Phase1 占 50%,Phase2/3 各 25%,每段最少 2s\r
+ sec = min(max_sec, 12)\r
+ SEC1 = max(2, min(sec * 5 // 10, 5))\r
+ SEC2 = max(2, min(sec * 3 // 10, 3))\r
+ SEC3 = max(2, min(sec * 2 // 10, 3))\r
+ print("[TEST] 总可用 {}s PCM → 分段: Phase1={}s Phase2={}s Phase3={}s".format(sec, SEC1, SEC2, SEC3))\r
+\r
+# ========== 计时工具 ==========\r
+def _us_str(us_val):\r
+ if us_val >= 1000000:\r
+ return "{:.2f}s".format(us_val / 1000000.0)\r
+ elif us_val >= 1000:\r
+ return "{:.2f}ms".format(us_val / 1000.0)\r
+ return "{}us".format(us_val)\r
+\r
+def _timed_call(label, fn, *args):\r
+ gc.collect()\r
+ t0 = utime.ticks_us()\r
+ ret = fn(*args)\r
+ t1 = utime.ticks_us()\r
+ us = utime.ticks_diff(t1, t0)\r
+ print("[TIME] {} → {} ret={}".format(label, _us_str(us), ret))\r
+ return ret, us\r
+\r
+# ========== 核心测试 ==========\r
+def test_pause_resume_and_timing():\r
+ """三种场景:\r
+ Phase1: 录音 5s → stream_stop (排干) → 检查数据量\r
+ Phase2: 再 stream_start (不停设备) → 录音 3s → stream_stop (排干)\r
+ Phase3: 再 stream_start → 录音 3s → stream_stop (排干) → stop\r
+ 全程写同一个 PCM 文件,最后验证可播放。\r
+ """\r
+ rd_buf = bytearray(AUDIO_BUF_SIZE)\r
+ total_bytes = 0\r
+ frame_cnt = 0\r
+\r
+ # ---- 打开文件 ----\r
+ try:\r
+ uos.remove(PCM_PATH)\r
+ except Exception:\r
+ pass\r
+ fd = open(PCM_PATH, "wb")\r
+ print("[TEST] 输出文件: {}".format(PCM_PATH))\r
+\r
+ # ---- 安全写入辅助 ----\r
+ def _fwrite(data):\r
+ try:\r
+ fd.write(data)\r
+ except OSError as e:\r
+ import uerrno\r
+ if getattr(uerrno, "ENOSPC", None) and hasattr(e, "args") and e.args[0] == getattr(uerrno, "ENOSPC", 28):\r
+ print("[TEST] ❌ 磁盘空间不足,停止写入 (已写 {}字节)".format(total_bytes))\r
+ return False\r
+ raise\r
+ return True\r
+\r
+ # ---- 创建录音对象 + 对象创建耗时 ----\r
+ gc.collect()\r
+ t0 = utime.ticks_us()\r
+ rec = audio.Record()\r
+ t1 = utime.ticks_us()\r
+ print("[TIME] audio.Record() → {} (底层驱动初始化)".format(\r
+ _us_str(utime.ticks_diff(t1, t0))))\r
+\r
+ # ---- Phase 1: 首次录音 ----\r
+ print("\n[TEST] ========== Phase1: 首次录音 ==========")\r
+ _, us = _timed_call("Phase1 stream_start", rec.stream_start,\r
+ audio.Record.WAV, SAMPLE_RATE, 0)\r
+ t_start = utime.ticks_ms()\r
+\r
+ phase1_sec = SEC1\r
+ print("[TEST] 录音 {}s ...".format(phase1_sec))\r
+ while True:\r
+ elapsed = utime.ticks_diff(utime.ticks_ms(), t_start)\r
+ if elapsed >= phase1_sec * 1000:\r
+ break\r
+ rd_len = rec.stream_read(rd_buf, AUDIO_BUF_SIZE)\r
+ if rd_len > 0:\r
+ if not _fwrite(bytes(rd_buf[:rd_len])):\r
+ break\r
+ total_bytes += rd_len\r
+ frame_cnt += 1\r
+ utime.sleep_ms(5)\r
+\r
+ phase1_bytes_before_stop = total_bytes\r
+ p1_dur = utime.ticks_diff(utime.ticks_ms(), t_start)\r
+ print("[TEST] Phase1 完成: 已录 {}帧 / {}字节 / {:.2f}s".format(\r
+ frame_cnt, total_bytes, p1_dur / 1000.0))\r
+\r
+ # Phase1 停止 + 排干\r
+ _t_stop1 = utime.ticks_us()\r
+ rec.stream_stop()\r
+ print("[TIME] Phase1 stream_stop → {}".format(\r
+ _us_str(utime.ticks_diff(utime.ticks_us(), _t_stop1))))\r
+\r
+ drain1 = 0\r
+ for i in range(10):\r
+ rd_len = rec.stream_read(rd_buf, AUDIO_BUF_SIZE)\r
+ if rd_len > 0:\r
+ if not _fwrite(bytes(rd_buf[:rd_len])):\r
+ break\r
+ total_bytes += rd_len\r
+ drain1 += rd_len\r
+ else:\r
+ break\r
+ print("[TEST] Phase1 排干: {}字节 ({}轮)".format(drain1, i + 1 if rd_len > 0 else i))\r
+\r
+ # ---- Phase 2: 暂停后恢复 (stream_start 直接再调) ----\r
+ print("\n[TEST] ========== Phase2: 暂停后恢复 ==========")\r
+ ret2, us2 = _timed_call("Phase2 stream_start", rec.stream_start,\r
+ audio.Record.WAV, SAMPLE_RATE, 0)\r
+ if ret2 is not None and ret2 < 0:\r
+ print("[TEST] ❌ Phase2 stream_start 返回 {} → 不支持 stop 后直接 restart".format(ret2))\r
+ rec.stream_stop()\r
+ rec.stop()\r
+ fd.close()\r
+ return\r
+\r
+ t2_start = utime.ticks_ms()\r
+ phase2_sec = SEC2\r
+ print("[TEST] 录音 {}s ...".format(phase2_sec))\r
+ p2_bytes_before = total_bytes\r
+ while True:\r
+ elapsed = utime.ticks_diff(utime.ticks_ms(), t2_start)\r
+ if elapsed >= phase2_sec * 1000:\r
+ break\r
+ rd_len = rec.stream_read(rd_buf, AUDIO_BUF_SIZE)\r
+ if rd_len > 0:\r
+ if not _fwrite(bytes(rd_buf[:rd_len])):\r
+ break\r
+ total_bytes += rd_len\r
+ utime.sleep_ms(5)\r
+\r
+ p2_bytes = total_bytes - p2_bytes_before\r
+ print("[TEST] Phase2 完成: {}字节 / {:.2f}s".format(\r
+ p2_bytes, utime.ticks_diff(utime.ticks_ms(), t2_start) / 1000.0))\r
+\r
+ # Phase2 停止 + 排干\r
+ rec.stream_stop()\r
+ drain2 = 0\r
+ for i in range(10):\r
+ rd_len = rec.stream_read(rd_buf, AUDIO_BUF_SIZE)\r
+ if rd_len > 0:\r
+ if not _fwrite(bytes(rd_buf[:rd_len])):\r
+ break\r
+ total_bytes += rd_len\r
+ drain2 += rd_len\r
+ else:\r
+ break\r
+ print("[TEST] Phase2 排干: {}字节".format(drain2))\r
+\r
+ # ---- Phase 3: 再次恢复 ----\r
+ print("\n[TEST] ========== Phase3: 再次恢复 ==========")\r
+ ret3, us3 = _timed_call("Phase3 stream_start", rec.stream_start,\r
+ audio.Record.WAV, SAMPLE_RATE, 0)\r
+ if ret3 is not None and ret3 < 0:\r
+ print("[TEST] ❌ Phase3 stream_start 返回 {} → 第二次 restart 失败".format(ret3))\r
+ rec.stream_stop()\r
+ rec.stop()\r
+ fd.close()\r
+ return\r
+\r
+ t3_start = utime.ticks_ms()\r
+ phase3_sec = SEC3\r
+ print("[TEST] 录音 {}s ...".format(phase3_sec))\r
+ p3_bytes_before = total_bytes\r
+ while True:\r
+ elapsed = utime.ticks_diff(utime.ticks_ms(), t3_start)\r
+ if elapsed >= phase3_sec * 1000:\r
+ break\r
+ rd_len = rec.stream_read(rd_buf, AUDIO_BUF_SIZE)\r
+ if rd_len > 0:\r
+ if not _fwrite(bytes(rd_buf[:rd_len])):\r
+ break\r
+ total_bytes += rd_len\r
+ utime.sleep_ms(5)\r
+\r
+ p3_bytes = total_bytes - p3_bytes_before\r
+ print("[TEST] Phase3 完成: {}字节".format(p3_bytes))\r
+\r
+ # Phase3 停止 + 排干\r
+ rec.stream_stop()\r
+ drain3 = 0\r
+ for i in range(10):\r
+ rd_len = rec.stream_read(rd_buf, AUDIO_BUF_SIZE)\r
+ if rd_len > 0:\r
+ if not _fwrite(bytes(rd_buf[:rd_len])):\r
+ break\r
+ total_bytes += rd_len\r
+ drain3 += rd_len\r
+ else:\r
+ break\r
+ print("[TEST] Phase3 排干: {}字节".format(drain3))\r
+\r
+ # ---- 最终关闭 ----\r
+ _, us_stop_total = _timed_call("Final stop()", rec.stop)\r
+ fd.close()\r
+\r
+ # ---- 汇总 ----\r
+ total_sec = total_bytes / (SAMPLE_RATE * 2) # 16bit mono\r
+ print("\n[TEST] ========== 测试结果汇总 ==========")\r
+ print("[TEST] Phase1: 录音+排干 {}字节 排干 {}字节".format(\r
+ phase1_bytes_before_stop - phase1_bytes_before_stop + total_bytes - (phase1_bytes_before_stop + p2_bytes + p3_bytes),\r
+ drain1))\r
+ print("[TEST] Phase2: 录音+排干 {}字节 排干 {}字节".format(p2_bytes, drain2))\r
+ print("[TEST] Phase3: 录音+排干 {}字节 排干 {}字节".format(p3_bytes, drain3))\r
+ print("[TEST] 总 PCM: {} 字节 ({:.2f}KB) → {:.3f}s".format(\r
+ total_bytes, total_bytes / 1024.0, total_sec))\r
+ print("[TEST] 文件: {}".format(PCM_PATH))\r
+\r
+ # 检查 Phase 之间是否有 gap(预期在 stop→start 间有短暂静音)\r
+ expected = (phase1_sec + phase2_sec + phase3_sec) * SAMPLE_RATE * 2\r
+ gap = expected - total_bytes\r
+ print("[TEST] 预期总量: {}字节 实际: {}字节 差额: {}字节 ({:.3f}s)".format(\r
+ expected, total_bytes, gap, gap / (SAMPLE_RATE * 2.0)))\r
+ if gap > SAMPLE_RATE * 2 * 2: # 超过 2s 的空缺\r
+ print("[TEST] ⚠️ 中间有超过 2s 的音频空缺,可能是 stop→start 重建耗时")\r
+ elif gap > SAMPLE_RATE * 2 * 0.2:\r
+ print("[TEST] ℹ️ 中间有少量空缺 ({:.0f}ms),属 stop→start 正常开销".format(\r
+ gap / (SAMPLE_RATE * 2) * 1000))\r
+ else:\r
+ print("[TEST] ✅ 三段接近连续,stop→start 开销极小")\r
+\r
+ print("[TEST] ==== 播放验证 ====")\r
+ print("[TEST] 用 Audacity: File→Import→Raw Data, Signed 16-bit PCM, "\r
+ "1 channel, {}Hz".format(SAMPLE_RATE))\r
+\r
+\r
+def test_start_stop_timing_only():\r
+ """纯耗时测试:反复 start→stop→stop(复位) 测得启动/停止/关闭耗时"""\r
+ print("\n[TEST] ========== 纯耗时测试 ==========")\r
+ rd_buf = bytearray(AUDIO_BUF_SIZE)\r
+\r
+ gc.collect()\r
+ rec = audio.Record()\r
+\r
+ # ---- Round 1: stream_start 耗时 ----\r
+ print("\n[TEST] --- Round 1: 首次 stream_start ---")\r
+ _, us_start1 = _timed_call(" stream_start", rec.stream_start,\r
+ audio.Record.WAV, SAMPLE_RATE, 0)\r
+\r
+ # 读几帧确认录音正常\r
+ utime.sleep_ms(200)\r
+ for _ in range(5):\r
+ rec.stream_read(rd_buf, AUDIO_BUF_SIZE)\r
+\r
+ _, us_stop1 = _timed_call(" stream_stop", rec.stream_stop)\r
+\r
+ # 排干\r
+ for _ in range(5):\r
+ if rec.stream_read(rd_buf, AUDIO_BUF_SIZE) <= 0:\r
+ break\r
+\r
+ print("[TEST] Round1 总计: start {} + stop {} = {}".format(\r
+ _us_str(us_start1), _us_str(us_stop1),\r
+ _us_str(us_start1 + us_stop1)))\r
+\r
+ # ---- Round 2: 第二次 start (不调 stop) ----\r
+ print("\n[TEST] --- Round 2: 第二次 stream_start (不调 stop()) ---")\r
+ ret2, us_start2 = _timed_call(" stream_start", rec.stream_start,\r
+ audio.Record.WAV, SAMPLE_RATE, 0)\r
+ utime.sleep_ms(200)\r
+ for _ in range(5):\r
+ rec.stream_read(rd_buf, AUDIO_BUF_SIZE)\r
+\r
+ _, us_stop2 = _timed_call(" stream_stop", rec.stream_stop)\r
+ for _ in range(5):\r
+ if rec.stream_read(rd_buf, AUDIO_BUF_SIZE) <= 0:\r
+ break\r
+ print("[TEST] Round2 总计: start {} + stop {} = {}".format(\r
+ _us_str(us_start2), _us_str(us_stop2),\r
+ _us_str(us_start2 + us_stop2)))\r
+\r
+ # ---- Round 3: 第三次 (stop 之后再 start) ----\r
+ print("\n[TEST] --- Round 3: stop() 后再 stream_start ---")\r
+ _, us_fstop = _timed_call(" stop()", rec.stop)\r
+ ret3, us_start3 = _timed_call(" stream_start", rec.stream_start,\r
+ audio.Record.WAV, SAMPLE_RATE, 0)\r
+ utime.sleep_ms(200)\r
+ for _ in range(5):\r
+ rec.stream_read(rd_buf, AUDIO_BUF_SIZE)\r
+\r
+ _, us_stop3 = _timed_call(" stream_stop", rec.stream_stop)\r
+ for _ in range(5):\r
+ if rec.stream_read(rd_buf, AUDIO_BUF_SIZE) <= 0:\r
+ break\r
+\r
+ _, us_fstop2 = _timed_call(" stop()", rec.stop)\r
+ print("[TEST] Round3 总计: start {} + stop {} + stop(两次) {} = {}".format(\r
+ _us_str(us_start3), _us_str(us_stop3),\r
+ _us_str(us_fstop + us_fstop2),\r
+ _us_str(us_start3 + us_stop3 + us_fstop + us_fstop2)))\r
+\r
+ # ---- 汇总 ----\r
+ print("\n[TEST] ========== 耗时汇总 ==========")\r
+ print("[TEST] stream_start 首次: {} 二次(不stop): {} stop后: {}".format(\r
+ _us_str(us_start1), _us_str(us_start2), _us_str(us_start3)))\r
+ print("[TEST] stream_stop {} {} {}".format(\r
+ _us_str(us_stop1), _us_str(us_stop2), _us_str(us_stop3)))\r
+ print("[TEST] stop() {} {}".format(\r
+ _us_str(us_fstop), _us_str(us_fstop2)))\r
+\r
+\r
+def run_all():\r
+ """完整测试流程:先测耗时(破坏性小),再测 pause/resume 可行性"""\r
+ print("[TEST] ==== 录音耗时与暂停恢复测试 ====")\r
+ print("[TEST] 左栏 [PHASE] 标识测试阶段/耗时")\r
+ print("[TEST] 右栏 [TIME] 标识 API 耗时(us/ms)")\r
+ print("[TEST]")\r
+\r
+ test_start_stop_timing_only()\r
+ test_pause_resume_and_timing()\r
+\r
+ print("\n[TEST] ==== 全部测试完成 ====")\r
+\r
+\r
+# 直接在 REPL 粘贴运行,去掉 __name__ 判断(MicroPython REPL 里不命中)\r
+try:\r
+ run_all()\r
+except KeyboardInterrupt:\r
+ print("[TEST] 被用户中断")\r
+except Exception as e:\r
+ print("[TEST] 异常:", e)\r
--- /dev/null
+# -*- coding: utf-8 -*-\r
+"""\r
+PC 端验证脚本:分析设备录音排干测试产出的 PCM 文件。\r
+\r
+用法:\r
+ python record_verify_pc.py test_drain.pcm [16384]\r
+ ↑ ↑\r
+ PCM文件 可选: 音频块大小(字节)\r
+\r
+功能:\r
+ 1. 将 RAW PCM (S16LE, 16kHz, mono) 转为可播放 WAV\r
+ 2. 检测音频能量分布,判断是否存在尾部静音/截断/异常\r
+ 3. 输出 RMS 能量曲线图(每 block 的均方根值)\r
+ 4. 判断"停止前已有数据"和"停止后排干数据"的衔接是否正常\r
+\r
+运行环境需求:\r
+ pip install numpy matplotlib\r
+ (可选) pip install sounddevice # 如需直接播放\r
+"""\r
+\r
+import sys\r
+import struct\r
+import wave\r
+import os\r
+from pathlib import Path\r
+\r
+\r
+def pcm_to_wav(pcm_path, sample_rate=16000, channels=1, sampwidth=2):\r
+ """将 RAW PCM 转为 WAV 文件,返回 WAV 路径和帧数"""\r
+ wav_path = str(Path(pcm_path).with_suffix(".wav"))\r
+ with open(pcm_path, "rb") as f_pcm:\r
+ raw = f_pcm.read()\r
+ with wave.open(wav_path, "w") as f_wav:\r
+ f_wav.setnchannels(channels)\r
+ f_wav.setsampwidth(sampwidth)\r
+ f_wav.setframerate(sample_rate)\r
+ f_wav.writeframes(raw)\r
+ return wav_path, len(raw) // (channels * sampwidth)\r
+\r
+\r
+def analyze_energy(pcm_path, block_size=16384, sample_rate=16000):\r
+ """分析 PCM 能量分布,返回 RMS 数组"""\r
+ import numpy as np\r
+ with open(pcm_path, "rb") as f:\r
+ raw = f.read()\r
+ samples = struct.unpack(f"<{len(raw) // 2}h", raw)\r
+ arr = np.array(samples, dtype=np.float64)\r
+ total_frames = len(arr)\r
+ n_blocks = (total_frames + block_size - 1) // block_size\r
+\r
+ rms = np.zeros(n_blocks)\r
+ for i in range(n_blocks):\r
+ start = i * block_size\r
+ end = min(start + block_size, total_frames)\r
+ block = arr[start:end]\r
+ rms[i] = np.sqrt(np.mean(block.astype(np.float64) ** 2))\r
+\r
+ return arr, rms, total_frames, sample_rate\r
+\r
+\r
+def print_energy_summary(rms, total_frames, sample_rate, block_size):\r
+ """打印能量摘要"""\r
+ import numpy as np\r
+ dur_s = total_frames / sample_rate\r
+ mean_rms = float(np.mean(rms))\r
+ max_rms = float(np.max(rms))\r
+ # 找到大幅低于均值的块(可能是静音/截断)\r
+ quiet_threshold = mean_rms * 0.1\r
+ quiet_blocks = np.where(rms < quiet_threshold)[0]\r
+\r
+ print(f"\n{'='*60}")\r
+ print(f"📊 音频能量分析")\r
+ print(f"{'='*60}")\r
+ print(f" 总帧数: {total_frames:,} ({dur_s:.2f}s)")\r
+ print(f" 块大小: {block_size} samples ({block_size / sample_rate * 1000:.0f}ms)")\r
+ print(f" 块数量: {len(rms)}")\r
+ print(f" RMS 均值: {mean_rms:.1f}")\r
+ print(f" RMS 峰值: {max_rms:.1f}")\r
+ print(f" 安静阈值 (10%均值): {quiet_threshold:.1f}")\r
+ print(f" 安静块数: {len(quiet_blocks)}/{len(rms)} "\r
+ f"({len(quiet_blocks) / max(len(rms), 1) * 100:.1f}%)")\r
+\r
+ # 分析尾部:最后 10% 是否异常安静\r
+ tail_start = max(0, len(rms) - max(1, len(rms) // 10))\r
+ tail_rms = rms[tail_start:]\r
+ tail_mean = float(np.mean(tail_rms)) if len(tail_rms) > 0 else 0\r
+ tail_quiet = sum(1 for x in tail_rms if x < quiet_threshold)\r
+ print(f"\n 🔍 尾部分析 (最后 {len(tail_rms)} 块, 约 {len(tail_rms) * block_size / sample_rate:.1f}s):")\r
+ print(f" 尾部 RMS 均值: {tail_mean:.1f}")\r
+ print(f" 尾部安静块: {tail_quiet}/{len(tail_rms)}")\r
+\r
+ # 安静块分布\r
+ if len(quiet_blocks) > 0:\r
+ tail_ratio = tail_quiet / max(len(tail_rms), 1)\r
+ if tail_ratio > 0.8:\r
+ print(f"\n ⚠️ 尾部异常安静! 可能存在截断或数据丢失")\r
+ elif tail_quiet > 0 and tail_quiet <= max(1, len(tail_rms) // 4):\r
+ print(f"\n ℹ️ 尾部有少量安静块,可能是正常静音或排干间隙")\r
+ else:\r
+ print(f"\n ℹ️ 安静块分布均匀或集中在中间,非尾部特有")\r
+\r
+ # 检查开头是否安静(正常录音开头应该有声音)\r
+ head_blocks = rms[:min(5, len(rms))]\r
+ head_mean = float(np.mean(head_blocks)) if len(head_blocks) > 0 else 0\r
+ print(f"\n 🔍 开头分析 (前 {len(head_blocks)} 块):")\r
+ print(f" 开头 RMS 均值: {head_mean:.1f}")\r
+ if head_mean < quiet_threshold * 2 and mean_rms > quiet_threshold * 2:\r
+ print(f" 开头存在静音段(可能是按键按下前的延迟)")\r
+\r
+\r
+def plot_energy(rms, total_frames, sample_rate, block_size, pcm_path):\r
+ """绘制 RMS 能量曲线"""\r
+ try:\r
+ import matplotlib.pyplot as plt\r
+ import numpy as np\r
+ except ImportError:\r
+ print("\n ⚠️ matplotlib 未安装,跳过图表输出")\r
+ return\r
+\r
+ dur_s = total_frames / sample_rate\r
+ time_axis = np.linspace(0, dur_s, len(rms))\r
+\r
+ fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8))\r
+ fig.suptitle(f"PCM 音频分析 — {Path(pcm_path).name}", fontsize=14)\r
+\r
+ # 上:RMS 能量曲线\r
+ ax1.plot(time_axis, rms, "b-", alpha=0.7, linewidth=0.8)\r
+ ax1.axhline(y=np.mean(rms), color="r", linestyle="--", alpha=0.5, label=f"均值 {np.mean(rms):.0f}")\r
+ ax1.axhline(y=np.mean(rms) * 0.1, color="orange", linestyle=":", alpha=0.5, label="安静阈值")\r
+ ax1.set_xlabel("时间 (s)")\r
+ ax1.set_ylabel("RMS 能量")\r
+ ax1.set_title(f"RMS 能量曲线 (块大小: {block_size} samples, {len(rms)} 块)")\r
+ ax1.legend(loc="upper right")\r
+ ax1.grid(True, alpha=0.3)\r
+\r
+ # 标记"停止点"(如果能识别,这里假设在 70%~90% 的位置有一个特征)\r
+ # 找能量陡降超过 50% 的位置,标记为可能的停止点\r
+ for i in range(1, len(rms) - 1):\r
+ if rms[i] > np.mean(rms) * 0.3 and rms[i + 1] < rms[i] * 0.3:\r
+ ax1.axvline(x=time_axis[i], color="green", linestyle="--", alpha=0.5, label="可能停止点")\r
+ break\r
+\r
+ # 下:直方图(RMS 分布)\r
+ ax2.hist(rms, bins=50, color="steelblue", edgecolor="white", alpha=0.8)\r
+ ax2.axvline(x=np.mean(rms), color="r", linestyle="--", alpha=0.5, label=f"均值 {np.mean(rms):.0f}")\r
+ ax2.set_xlabel("RMS 值")\r
+ ax2.set_ylabel("频次")\r
+ ax2.set_title("RMS 分布直方图")\r
+ ax2.legend(loc="upper right")\r
+ ax2.grid(True, alpha=0.3)\r
+\r
+ plt.tight_layout()\r
+ png_path = str(Path(pcm_path).with_suffix(".png"))\r
+ plt.savefig(png_path, dpi=100)\r
+ print(f"\n 📈 能量图已保存: {png_path}")\r
+ print(f" 💡 查看方式: 双击 {png_path}")\r
+ # plt.show() # 如需弹出窗口,取消注释\r
+\r
+\r
+def main():\r
+ if len(sys.argv) < 2:\r
+ print("用法: python record_verify_pc.py <pcm文件> [block_size]")\r
+ print("示例: python record_verify_pc.py test_drain.pcm 16384")\r
+ sys.exit(1)\r
+\r
+ pcm_path = sys.argv[1]\r
+ block_size = int(sys.argv[2]) if len(sys.argv) > 2 else 16384\r
+\r
+ if not os.path.exists(pcm_path):\r
+ print(f"❌ 文件不存在: {pcm_path}")\r
+ sys.exit(1)\r
+\r
+ file_size = os.path.getsize(pcm_path)\r
+ print(f"📁 文件: {pcm_path}")\r
+ print(f" 大小: {file_size:,} 字节 ({file_size / 1024:.1f} KB)")\r
+\r
+ # 1. 转 WAV\r
+ wav_path, frames = pcm_to_wav(pcm_path, 16000, 1, 2)\r
+ dur_s = frames / 16000\r
+ print(f" 转换: {wav_path}")\r
+ print(f" 时长: {dur_s:.2f}s ({frames:,} 帧)")\r
+\r
+ if frames == 0:\r
+ print(f"❌ PCM 文件为空,请检查录音是否成功")\r
+ sys.exit(1)\r
+\r
+ # 2. 能量分析\r
+ arr, rms, total_frames, sr = analyze_energy(pcm_path, block_size, 16000)\r
+ print_energy_summary(rms, total_frames, sr, block_size)\r
+\r
+ # 3. 绘图\r
+ plot_energy(rms, total_frames, sr, block_size, pcm_path)\r
+\r
+ # 4. 最终结论\r
+ print(f"\n{'='*60}")\r
+ print(f"✅ 验证完成。可用以下方式播放 WAV:")\r
+ print(f" Windows: 双击 {wav_path}")\r
+ print(f" Python: import sounddevice as sd; sd.play(arr, {sr})")\r
+\r
+\r
+if __name__ == "__main__":\r
+ main()\r
--- /dev/null
+\r
+import sys\r
+target = r'd:\workspace\diaozhui\ 2.sdk\project\aPIN_1_5\sdk\services\microPython\ports\quectel\core est\playback_interrupt_test.py'\r
+with open(target, 'w', encoding='utf-8') as f:\r
+ f.write(sys.stdin.read())\r
+print('done')\r
from vc_hw import set_ipv4_datacall, _BatteryCfg
from vc_uart import AI_UART
-audio_buf = ['chaobingfatis', 'chenggong', 'didianguanji', 'IMEIhao', 'jianquancuowu', 'key_prompt', 'liuliangtishi', 'Low_power_warning', 'R_CHARGE_FULL', 'R_CHARGE_IN', 'R_CHARGE_OUT', 'R_READ_TIME_0', 'R_READ_TIME_1', 'R_READ_TIME_2', 'R_READ_TIME_3', 'R_READ_TIME_4', 'R_READ_TIME_5', 'R_READ_TIME_6', 'R_READ_TIME_7', 'R_READ_TIME_8', 'R_READ_TIME_9', 'R_READ_TIME_A', 'R_READ_TIME_B', 'R_READ_TIME_C', 'R_READ_TIME_D', 'R_READ_TIME_E', 'R_READ_TIME_F', 'R_READ_TIME_G', 'R_READ_TIME_H', 'R_READ_TIME_I', 'R_READ_TIME_J', 'R_READ_TIME_K', 'R_READ_TIME_L', 'R_READ_TIME_M', 'R_READ_TIME_N', 'R_READ_TIME_O', 'R_READ_TIME_P', 'R_READ_TIME_Q', 'R_READ_TIME_R', 'R_READ_TIME_S', 'R_READ_TIME_T', 'R_READ_TIME_U', 'R_READ_TIME_V', 'R_READ_TIME_W', 'R_READ_TIME_X', 'R_READ_TIME_Y', 'R_READ_TIME_Z', 'send_tone', 'shouhou', 'xinhaoruo1', 'zhengzaiguanji']
+audio_buf = ['chaobingfatis', 'chenggong', 'didianguanji', 'IMEIhao', 'jianquancuowu', 'liuliangtishi', 'Low_power_warning', 'R_CHARGE_FULL', 'R_CHARGE_IN', 'R_CHARGE_OUT', 'R_READ_TIME_0', 'R_READ_TIME_1', 'R_READ_TIME_2', 'R_READ_TIME_3', 'R_READ_TIME_4', 'R_READ_TIME_5', 'R_READ_TIME_6', 'R_READ_TIME_7', 'R_READ_TIME_8', 'R_READ_TIME_9', 'R_READ_TIME_A', 'R_READ_TIME_B', 'R_READ_TIME_C', 'R_READ_TIME_D', 'R_READ_TIME_E', 'R_READ_TIME_F', 'R_READ_TIME_G', 'R_READ_TIME_H', 'R_READ_TIME_I', 'R_READ_TIME_J', 'R_READ_TIME_K', 'R_READ_TIME_L', 'R_READ_TIME_M', 'R_READ_TIME_N', 'R_READ_TIME_O', 'R_READ_TIME_P', 'R_READ_TIME_Q', 'R_READ_TIME_R', 'R_READ_TIME_S', 'R_READ_TIME_T', 'R_READ_TIME_U', 'R_READ_TIME_V', 'R_READ_TIME_W', 'R_READ_TIME_X', 'R_READ_TIME_Y', 'R_READ_TIME_Z', 'send_tone', 'xinhaoruo1', 'zhengzaiguanji']
class AI_dev(Abstract):
# AI
def __init__(self):
audio_buf.index("R_READ_TIME_{}".format(iccid[8])),audio_buf.index("R_READ_TIME_{}".format(iccid[9])),audio_buf.index("R_READ_TIME_{}".format(iccid[10])),audio_buf.index("R_READ_TIME_{}".format(iccid[11])),\
audio_buf.index("R_READ_TIME_{}".format(iccid[12])),audio_buf.index("R_READ_TIME_{}".format(iccid[13])),audio_buf.index("R_READ_TIME_{}".format(iccid[14])),audio_buf.index("R_READ_TIME_{}".format(iccid[15])),\
audio_buf.index("R_READ_TIME_{}".format(iccid[16])),audio_buf.index("R_READ_TIME_{}".format(iccid[17])),audio_buf.index("R_READ_TIME_{}".format(iccid[18])),audio_buf.index("R_READ_TIME_{}".format(iccid[19])))
- str_data += '{}.mp3+'.format(audio_buf.index("shouhou"))#可联系售后解决,联系方式见售后客服电话是400-886-8777或到微信公众号柒牌SEVENBRAND自行充值:
self.aud.montage_play('U:/static/audio.bin',1,1,str_data)
#开定时器不让系统休眠
self.play_imei_timer_count=1
VC_WS_IO_LOG = 0
# 1=打印出站 JSON 摘要(start 音色/TTS 参数、synthesis 文本等);0=仅必要的状态一行
VC_PROTO_TX_LOG = 0
-# 长对话 L1(VC_IDLE_AUTO_CLOSE_SEC):仅 tts.* / started / TTS 音频下行重置 close_timerout。
+# 长对话 L1(VC_IDLE_AUTO_CLOSE_SEC):play_end/空窗 arm;开说/tts.started clear;录/想/播期间不计。
# 保活不计入 L1:V2.5 文本 ping/pong、JSON heartbeat;RFC6455 OP_PING/OP_PONG(uwebsocket 内处理)。
# V2.5:默认不发标准 PING,发文本 "ping"(VC_WS_PING_INTERVAL_SEC>0);须响应入站标准 PING→PONG。
# V3:无自定义心跳;默认不发标准 PING;须响应入站标准 PING→PONG。
return False
return True
- def _vc_reset_long_idle_timer(self, tag=""):
- """é\95¿å¯¹è¯\9d L1ï¼\9aç\94¨æ\88·å¼\80麦/æ\9c\89æ\95\88轮次活å\8a¨å\88\99æ¸\85é\9b¶æ\97 TTS è¶\85æ\97¶è®¡æ\95°。"""
+ def _vc_clear_l1_silence_timer(self, tag=""):
+ """é\95¿å¯¹è¯\9d L1ï¼\9aç\94¨æ\88·å¼\80麦/æ\96° TTS/æ\9c\89æ\95\88交äº\92 â\86\92 ä½\9cåº\9f交äº\92空çª\97计æ\97¶。"""
try:
if getattr(self, "VC_mode", -1) != 0:
return
+ if getattr(self, "_vc_l1_silence_since_ms", 0):
+ try:
+ print("[VC][idle_L1] clear reason={}".format(tag or "-"))
+ except Exception:
+ pass
+ self._vc_l1_silence_since_ms = 0
self.close_timerout = 0
except Exception:
pass
+ def _vc_reset_long_idle_timer(self, tag=""):
+ """兼容旧调用:等同清除 L1 交互空窗计时。"""
+ self._vc_clear_l1_silence_timer(tag)
+
+ def _vc_arm_l1_silence_timer(self, tag=""):
+ """长对话 L1:从「AI 反馈结束 / 等人说话」起计交互空窗,满 VC_IDLE_AUTO_CLOSE_SEC 则 Close。"""
+ try:
+ if not self._vc_is_long_dialogue_mode():
+ return
+ if getattr(self, "Close_flag", 0):
+ return
+ if getattr(self, "_vc_session_started", 0) != 1:
+ return
+ if getattr(self, "VC_satrt_flag", 0) != 1 and getattr(
+ self, "AI_client_VC_isconnect", 0
+ ) != 1:
+ return
+ self._vc_l1_silence_since_ms = utime.ticks_ms()
+ self.close_timerout = 0
+ try:
+ print("[VC][idle_L1] arm reason={}".format(tag or "-"))
+ except Exception:
+ pass
+ except Exception:
+ pass
+
+ def _vc_l1_silence_blocked(self):
+ """录音/思考/播 TTS/本地短音期间不计空窗、不 Close。"""
+ try:
+ if getattr(self, "VC_record_state_flag", 0) != 0:
+ return True
+ if getattr(self, "VC_Play_flag", 0) == 1:
+ return True
+ if hasattr(self, "_vc_local_play_busy") and self._vc_local_play_busy():
+ return True
+ t = getattr(self.fsm, "turn", VCTurnState.Idle)
+ if t in (
+ VCTurnState.Thinking,
+ VCTurnState.TtsReceiving,
+ VCTurnState.Playing,
+ VCTurnState.AwaitPlayedAck,
+ ):
+ return True
+ if getattr(self, "_vc_idle_close_in_progress", 0) == 1:
+ return True
+ except Exception:
+ return True
+ return False
+
+ def _vc_l1_silence_tick(self):
+ """长对话 L1:交互空窗超时 → Close(与 25s turn_cap 独立;开说/新 TTS 会 clear)。"""
+ try:
+ if not self._vc_is_long_dialogue_mode():
+ return
+ since = getattr(self, "_vc_l1_silence_since_ms", 0)
+ if not since:
+ return
+ if self._vc_l1_silence_blocked():
+ return
+ idle_sec = cfg_int("VC_IDLE_AUTO_CLOSE_SEC", 20)
+ if idle_sec < 5:
+ idle_sec = 5
+ try:
+ elapsed_ms = utime.ticks_diff(utime.ticks_ms(), since)
+ except Exception:
+ elapsed_ms = 0
+ self.close_timerout = max(1, elapsed_ms // 1000)
+ if elapsed_ms < idle_sec * 1000:
+ return
+ try:
+ print("[VC][idle_L1] 交互空窗超时: elapsed={}s thr={}s VC_mode={} VC_satrt={} ws_conn={} {}".format(
+ self.close_timerout, idle_sec, self.VC_mode,
+ getattr(self, "VC_satrt_flag", -1), getattr(self, "AI_client_VC_isconnect", -1),
+ self._vc_fsm_log_snip()))
+ except Exception:
+ pass
+ try:
+ print("[VC][idle_L1] 处理=Close(播完/空窗无交互,需再次按键进入)")
+ except Exception:
+ pass
+ print("[VC][long_idle] branch=l1_silence_timeout -> Close")
+ self._vc_l1_silence_since_ms = 0
+ try:
+ self._vc_idle_l1_close = 1
+ except Exception:
+ pass
+ EventMesh.publish("publish_AI_btnInfo", {"func": self.Close})
+ except Exception:
+ pass
+
def _vc_long_dialogue_defer_vad_stop(self):
# LONG_DIALOGUE_ASR_MERGE_EN=0:关闭句末续说拼接窗口,本地 VAD 判句末即停麦提交,
# 降低等待时延(与“连续读/续说”互斥,按场景择一);默认 1 保持原拼接行为。
if getattr(self, "AI_audio_queue", None) is not None:
if self.AI_audio_queue.empty() != True:
self.AI_audio_queue.clear()
- self._ptt_hard_mute_local_playback()
- if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None:
- self.VC_Play_End()
+ self._vc_downlink_stop_playback("canceled/continue_speech")
self.fsm.await_played = False
self._need_audio_played_ack = 0
self.fsm.on_server_canceled()
if getattr(self, "AI_audio_queue", None) is not None:
if self.AI_audio_queue.empty() != True:
self.AI_audio_queue.clear()
- self._ptt_hard_mute_local_playback()
- if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None:
- self.VC_Play_End()
+ self._vc_downlink_stop_playback("canceled/stale")
self.fsm.await_played = False
self._need_audio_played_ack = 0
self.fsm.on_server_canceled()
except Exception:
pass
try:
- self._ptt_hard_mute_local_playback()
- except Exception:
- pass
- try:
- if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None:
- self.VC_Play_End()
+ self._vc_downlink_stop_playback(reason)
except Exception:
pass
try:
self._uplink_tx_fail_cnt = 0
self._uplink_tx_fail_bytes = 0
self._uplink_tx_fail_log_ms = 0
+ self._vc_drop_reset()
+ self._vc_rec_hw_start_ms = 0
+ self._vc_drop_reset()
# 状态机骨架(后续逐步把旧逻辑迁移进来)
self.fsm = VCStateMachine(self)
usys.print_exception(e)
self.audio_play_state = 1 #音频播放完状态
self.close_timerout = 0
+ self._vc_l1_silence_since_ms = 0
self._idle_deep_sec = 0
self._vc_idle_deep_armed_suppress = 0
self._vc_idle_close_in_progress = 0
pass
if _bg_active:
EventMesh.publish("system_sleep_rest")
- # --- Level 1:长对话轮次间隙 idle_sec 内无 TTS 下行 → Close(JSON ping/pong 不重置、不暂停累计)
- # close_timerout:仅 tts.* / started / TTS 音频重置;听/想/播/录期 _vc_long_idle_timer_armed=False
- idle_sec = cfg_int("VC_IDLE_AUTO_CLOSE_SEC", 20)
- if idle_sec < 5:
- idle_sec = 5
- if self._vc_long_idle_timer_armed():
- self.close_timerout += 1
- else:
- self.close_timerout = 0
- if self.VC_mode == 0 and idle_sec > 0 and self.close_timerout > idle_sec:
- try:
- if getattr(self, "VC_record_state_flag", 0) != 0:
- self.close_timerout = 0
- return
- except Exception:
- pass
- try:
- print("[VC][idle_L1] 长对话无TTS反馈超时: elapsed={}s thr={}s VC_mode={} VC_satrt={} ws_conn={} {}".format(
- self.close_timerout, idle_sec, self.VC_mode,
- getattr(self, "VC_satrt_flag", -1), getattr(self, "AI_client_VC_isconnect", -1),
- self._vc_fsm_log_snip()))
- except Exception:
- pass
- try:
- print("[VC][idle_L1] 处理=Close(无TTS反馈进idle,需再次按键进入)")
- except Exception:
- pass
- print("[VC][long_idle] branch=no_tts_timeout -> Close")
- try:
- self._vc_idle_l1_close = 1
- except Exception:
- pass
- EventMesh.publish("publish_AI_btnInfo", {"func": self.Close})
+ # --- Level 1:长对话交互空窗(play_end 等 arm;开说/新 TTS clear;录/想/播期间不计)
+ try:
+ self._vc_l1_silence_tick()
+ except Exception:
+ pass
# --- Level 2:长对话+单对话,业务 idle 累计 VC_IDLE_DEEP_SLEEP_SEC → 断开(若仍连)+ 深睡事件
deep_sec = cfg_int("VC_IDLE_DEEP_SLEEP_SEC", 60)
#self.aud.aud_tone_play(16,200)#播放按键音
self.LocalRespondingEnded_timerout = 0
self.close_timerout = 0
+ self._vc_l1_silence_since_ms = 0
self._idle_deep_sec = 0
self._vc_idle_deep_armed_suppress = 0
self.AI_ASR_timer.stop()
self._1S_timer.stop()
self.close_timerout = 0
+ self._vc_l1_silence_since_ms = 0
self._idle_deep_sec = 0
self.factory_timer.stop()
self.AI_TTS_str = ""
return
if ev == "tts.started":
self._vc_tts_rx_counters_reset()
+ try:
+ self._vc_clear_l1_silence_timer("tts.started")
+ except Exception:
+ pass
if self._vc_is_long_dialogue_mode():
if hasattr(self, "_vc_ld_on_tts_started"):
self._vc_ld_on_tts_started()
break
self.AI_client_VC_recv_lock = 1
if self._vc_rx_resets_long_idle_timer(recv_data):
- self.close_timerout = 0
+ if hasattr(self, "_vc_clear_l1_silence_timer"):
+ self._vc_clear_l1_silence_timer("rx_tts")
+ else:
+ self.close_timerout = 0
if recv_data == "websocket close":
# 服务器关闭连接
_ws_close_clean = self._vc_ws_close_is_clean()
self.LocalRespondingEnded_timerout = 0
elif jsload_data["event"] == "asr.result" :#asr结果
try:
- self.AI_ASR_str = jsload_data["payload"]["content"]
+ raw = jsload_data["payload"]["content"]
except Exception:
try:
- self.AI_ASR_str = jsload_data.get("payload", {}).get("text", "") or ""
+ raw = jsload_data.get("payload", {}).get("text", "") or ""
except Exception:
- self.AI_ASR_str = ""
+ raw = ""
+ try:
+ if hasattr(self, "_vc_merge_asr_stream_text"):
+ self.AI_ASR_str = self._vc_merge_asr_stream_text(raw)
+ else:
+ self.AI_ASR_str = raw
+ except Exception:
+ self.AI_ASR_str = raw
print("[VC][ASR] {}".format(self.AI_ASR_str))
try:
if (self.AI_ASR_str or "").strip():
stream_on = EventMesh.publish("get_vc_tts_playback_active") == 1
except Exception:
stream_on = False
- pcm_busy = getattr(self, "play_pcm", None) is not None
playing = getattr(self, "VC_Play_flag", 0) == 1
try:
thinking = getattr(self.fsm, "turn", VCTurnState.Idle) == VCTurnState.Thinking
vad_cont = getattr(self, "_vc_vad_had_active_after_silence", 0) == 1
if (
(not stream_on)
- and (not pcm_busy)
and (not playing)
and (not thinking)
and (not vad_cont)
+ and getattr(self, "VC_record_state_flag", 0) < 2
):
- self._vc_local_play('U:/static/think.mp3', 1, 1, 650)
+ self._vc_local_play('U:/static/think.pcm', 1, 1, 650)
self._asr_completed_tone_done = 1
elif vad_cont:
try:
print("[VC][ASR] skip think tone (vad pause merged)")
except Exception:
pass
+ else:
+ try:
+ print("[VC][ASR] skip think tone stream={} play={} think={} vad={}".format(
+ int(stream_on), int(playing), int(thinking), int(vad_cont)))
+ except Exception:
+ pass
except Exception:
pass
print("self.commands_mode:{}".format(self.commands_mode))
def _vc_user_record_entry_long(self, reason=""):
"""
长对话开麦:
- - 按键/打断续聊:先 start.mp3,滴声结束后再上行+蓝慢闪
+ - 按键/打断续聊:先 start.pcm,滴声结束后再开录+上行(避免 play_pcm 与 record 并行丢字)
- 自动续聊(server.started/play_end 等):不播 start,直接上行
"""
try:
self._vc_skip_local_play_stop_record = 1
except Exception:
pass
+ should_tone = False
+ try:
+ should_tone = self._vc_should_play_record_start_tone(reason)
+ except Exception:
+ should_tone = False
+ if should_tone:
+ played = False
+ try:
+ played = self._vc_play_record_start_tone(via=reason)
+ except Exception:
+ played = False
+ if played:
+ try:
+ # 滴声已在 _vc_local_play_pcm 内同步播完+delay,此处仅留短 guard 再开麦
+ guard = cfg_int("RECORD_START_TONE_GAP_MS", 180)
+ total_ms = int(guard) // 4 + 40
+ if total_ms < 80:
+ total_ms = 80
+ if total_ms > 160:
+ total_ms = 160
+ self._vc_user_record_tone_timer.stop()
+ self._vc_user_record_tone_timer.start(total_ms, 0, self._vc_user_record_tone_timer_cb)
+ print("[VC][user_record] entry_long key_tone reason={} post_tone_ms={}".format(
+ reason, total_ms))
+ except Exception as e:
+ usys.print_exception(e)
+ try:
+ self._vc_user_record_uplink_open(reason)
+ except Exception:
+ pass
+ return
try:
if getattr(self, "VC_record_state_flag", 0) == 0:
self.VC_Speech_Start()
pass
except Exception as e:
usys.print_exception(e)
- hold_ms = cfg_int("RECORD_START_TONE_GAP_MS", 180)
- played = False
try:
- if self._vc_should_play_record_start_tone(reason):
- played = self._vc_play_record_start_tone(via=reason)
+ self._vc_user_record_uplink_open(reason)
except Exception:
- played = False
- if not played:
- try:
- self._vc_user_record_uplink_open(reason)
- except Exception:
- pass
- return
- try:
- total_ms = int(hold_ms) + 40
- if total_ms < 200:
- total_ms = 200
- self._vc_user_record_tone_timer.stop()
- self._vc_user_record_tone_timer.start(total_ms, 0, self._vc_user_record_tone_timer_cb)
- print("[VC][user_record] entry_long key_tone reason={} tone_ms={}".format(reason, total_ms))
- except Exception as e:
- usys.print_exception(e)
- try:
- self._vc_user_record_uplink_open(reason)
- except Exception:
- pass
+ pass
def _vc_user_record_entry_ptt(self, reason=""):
"""
except Exception:
pass
try:
- self.VC_Play_End()
+ if self._vc_pcm_session_keep_open() and self._vc_is_long_dialogue_mode():
+ self.VC_Play_SoftEnd()
+ else:
+ self.VC_Play_End()
except Exception:
pass
try:
self._fsm_apply_actions(post)
except Exception:
pass
+ try:
+ if (
+ self._vc_is_long_dialogue_mode()
+ and getattr(self, "Close_flag", 0) == 0
+ and getattr(self, "VC_record_state_flag", 0) == 0
+ ):
+ self._vc_arm_l1_silence_timer("play_end")
+ except Exception:
+ pass
self._vc_finish_play_end_close_if_needed()
def _vc_fire_local_play_end(self, tag=""):
"""TTS 本地播完:入编排队列,避免在播放线程直接改 FSM。"""
self._vc_enqueue_btn(self._vc_orchestrator_play_end_cb, tag)
- def _vc_local_play(self, path, play_mode=1, loop=1, hold_ms=650):
- """本地 MP3 短音(think/start 等):与上行录音并行,不停录;仅打断服务端 TTS 流。"""
+ def _vc_is_local_pcm_path(self, path):
+ try:
+ return str(path).lower().endswith(".pcm")
+ except Exception:
+ return False
+
+ def _vc_static_open_paths(self, path):
+ """aud.play 用 U:/static;open() 需 /usr/static。"""
+ paths = []
+ try:
+ p = str(path).strip()
+ except Exception:
+ return [path]
+ if p.startswith("U:/"):
+ paths.append("/usr/" + p[3:])
+ paths.append(p)
+ elif p.startswith("u:/"):
+ paths.append("/usr/" + p[3:])
+ paths.append(p)
+ elif p:
+ paths.append(p)
+ # 去重
+ out = []
+ for x in paths:
+ if x not in out:
+ out.append(x)
+ return out
+
+ def _vc_open_static_file(self, path):
+ last_err = None
+ for p in self._vc_static_open_paths(path):
+ try:
+ return open(p, "rb"), p
+ except OSError as e:
+ last_err = e
+ print("[VC] static_open fail path={} err={}".format(p, e))
+ if last_err:
+ raise last_err
+ raise OSError(19, "ENODEV")
+
+ def _vc_local_play_mp3_file(self, path, play_mode=1, loop=1, hold_ms=650):
+ """本地 MP3 短音(aud.play);montage/回退用。"""
try:
from vc_media_arb import MediaArbiter
MediaArbiter.before_local(self)
except Exception:
pass
usys.print_exception(e)
- def _vc_tts_playback_active(self):
- """仅服务端 TTS/PCM 流在播(VC_Play_Start);aud.play 本地短音不算。"""
+
+ def _vc_pcm_session_keep_open(self):
+ """长对话会话内保留 play_pcm WRITE handle(对话结束/Close 前不 close)。"""
try:
- if getattr(self, "VC_Play_flag", 0) == 1:
- return True
+ if getattr(self, "VC_satrt_flag", 0) != 1:
+ return False
+ if getattr(self, "Close_flag", 0) == 1:
+ return False
+ if int(getattr(self, "_vc_ms_phase", 0)) != 0:
+ return False
+ if getattr(self, "VC_play_TYPE", None) != VC_play_TYPE_PCM:
+ return False
+ if int(getattr(self, "VC_play_samplerate", 0)) != 16000:
+ return False
+ return True
+ except Exception:
+ return False
+
+ def _vc_downlink_stop_playback(self, reason="", force_hard=0):
+ """停下行/TTS:长对话优先软停保留 handle;montage/Close/PTT 等走硬关。"""
+ if not force_hard and self._vc_pcm_session_keep_open() and self._vc_is_long_dialogue_mode():
+ self._vc_soft_stop_tts_pcm(reason or "downlink_stop")
+ return
+ try:
+ self._ptt_hard_mute_local_playback()
+ except Exception:
+ pass
+ try:
+ if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None:
+ self.VC_Play_End()
+ except Exception:
+ pass
+
+ def _vc_soft_stop_tts_pcm(self, reason=""):
+ """停 TTS 流、清队列,保留 play_pcm handle(供本地 PCM 短音复用)。"""
+ try:
+ self.VC_Play_flag = 0
except Exception:
pass
try:
if getattr(self, "play_pcm", None) is not None:
- return True
+ self.play_pcm.stream_stop()
except Exception:
pass
- return False
+ try:
+ if getattr(self, "AI_audio_queue", None) is not None:
+ if self.AI_audio_queue.empty() != True:
+ self.AI_audio_queue.clear()
+ except Exception:
+ pass
+ try:
+ keep = 1 if getattr(self, "play_pcm", None) is not None else 0
+ print("[VC] soft_stop_tts_pcm reason={} keep_pcm={}".format(reason or "-", keep))
+ except Exception:
+ pass
+
+ def _vc_play_pcm_stream_begin(self, after_file=0):
+ """打开或复用 play_pcm 写流;after_file=1 时先 stopAll(MP3 切 PCM 遗留)。"""
+ if after_file:
+ try:
+ self.aud.stopPlayStream()
+ self.aud.stopAll()
+ except Exception:
+ pass
+ utime.sleep_ms(80)
+ last_err = None
+ for i in range(3):
+ try:
+ reuse = getattr(self, "play_pcm", None) is not None
+ if self.play_pcm is None:
+ if i > 0:
+ utime.sleep_ms(80 + i * 60)
+ self.play_pcm = audio.Audio.PCM(0, 1, self.VC_play_samplerate, 1, 1, 2)
+ try:
+ self.play_pcm.stream_pause(0)
+ except Exception:
+ pass
+ try:
+ self.play_pcm.stream_stop()
+ except Exception:
+ pass
+ ret = self.play_pcm.stream_write_start()
+ tag = "reuse" if reuse else "open"
+ print("[VC] local_pcm stream_write_start {} ret={} try={}".format(tag, ret, i))
+ return ret
+ except Exception as e:
+ last_err = e
+ try:
+ if self.play_pcm is not None:
+ self.play_pcm.close()
+ except Exception:
+ pass
+ self.play_pcm = None
+ if last_err:
+ raise last_err
+
+ def _vc_wait_local_pcm_drain(self, max_ms=400):
+ """等本地短音播完,避免紧接着 tts.started 冲掉 ring。"""
+ if not getattr(self, "_vc_local_play_active", 0):
+ return
+ t0 = utime.ticks_ms()
+ while utime.ticks_diff(utime.ticks_ms(), t0) < max_ms:
+ try:
+ if self.play_pcm is not None and self.play_pcm.stream_is_empty() == 1:
+ break
+ except Exception:
+ break
+ utime.sleep_ms(10)
+
+ def _vc_local_play_pcm(self, path, hold_ms=650):
+ """本地 PCM 短音:走 play_pcm 写流;打断 TTS 时 soft-stop,不 close handle。"""
+ try:
+ from vc_media_arb import MediaArbiter
+ if MediaArbiter.llm_active(self):
+ self._vc_soft_stop_tts_pcm("local_pcm")
+ elif getattr(self, "VC_record_state_flag", 0) != 0:
+ try:
+ self.aud.stopPlayStream()
+ except Exception:
+ pass
+ except Exception:
+ pass
+ try:
+ hold = int(hold_ms)
+ if hold < 200:
+ hold = 200
+ self._vc_local_play_active = 1
+ self._vc_local_play_until_ms = utime.ticks_add(utime.ticks_ms(), hold)
+ except Exception:
+ pass
+ try:
+ self._vc_set_playback_volume(reason="local_pcm")
+ fd, real_path = self._vc_open_static_file(path)
+ try:
+ self._vc_play_pcm_stream_begin(after_file=0)
+ chunk = 3200
+ total = 0
+ while True:
+ buf = fd.read(chunk)
+ if not buf:
+ break
+ if not isinstance(buf, bytearray):
+ buf = bytearray(buf)
+ n = self.play_pcm.stream_write(buf, len(buf))
+ total += n if n else 0
+ print("[VC] local_pcm wrote {} bytes path={}".format(total, real_path))
+ try:
+ sr = int(getattr(self, "VC_play_samplerate", 16000))
+ dur_ms = (total * 1000) // (sr * 2) + 50
+ if dur_ms > 500:
+ dur_ms = 500
+ utime.sleep_ms(dur_ms)
+ except Exception:
+ pass
+ finally:
+ fd.close()
+ except OSError as e:
+ try:
+ self._vc_local_play_active = 0
+ self._vc_local_play_until_ms = 0
+ except Exception:
+ pass
+ mp3_path = str(path)[:-4] + ".mp3"
+ print("[VC] local_pcm open fail err={} fallback mp3={}".format(e, mp3_path))
+ self._vc_local_play_mp3_file(mp3_path, hold_ms=hold_ms)
+ except Exception as e:
+ try:
+ self._vc_local_play_active = 0
+ self._vc_local_play_until_ms = 0
+ except Exception:
+ pass
+ usys.print_exception(e)
+
+ def _vc_local_play(self, path, play_mode=1, loop=1, hold_ms=650):
+ """本地短音:.pcm 走 play_pcm 复用;其他仍 aud.play(montage/开机铃等不变)。"""
+ if self._vc_is_local_pcm_path(path):
+ self._vc_local_play_pcm(path, hold_ms=hold_ms)
+ return
+ self._vc_local_play_mp3_file(path, play_mode=play_mode, loop=loop, hold_ms=hold_ms)
+ def _vc_merge_asr_stream_text(self, new_text):
+ """流式 asr.result 常为整句覆盖;同轮连续多句时拼接,避免 completed 只剩最后一句。"""
+ new_t = (new_text or "").strip()
+ if not new_t:
+ return new_t
+ old = (getattr(self, "AI_ASR_str", "") or "").strip()
+ if not old:
+ return new_t
+ if new_t == old:
+ return new_t
+ if len(new_t) >= len(old) and new_t.startswith(old):
+ return new_t
+ if old in new_t:
+ return new_t
+ if old[-1:] in ("。", "?", "!", ".", "?", "!"):
+ return old + new_t
+ if len(new_t) >= len(old):
+ return new_t
+ return old
+
+ def _vc_tts_playback_active(self):
+ """仅服务端 TTS/PCM 流在播(VC_Play_flag);常开 handle 不算在播。"""
+ try:
+ return getattr(self, "VC_Play_flag", 0) == 1
+ except Exception:
+ return False
def _vc_vad_uplink_stop_eligible(self):
# 对应旧 VC_state >= SpeechStart
try:
except Exception:
pass
try:
- if hasattr(self, "_vc_reset_long_idle_timer"):
- self._vc_reset_long_idle_timer("llm_without_tts")
+ if hasattr(self, "_vc_arm_l1_silence_timer"):
+ self._vc_arm_l1_silence_timer("llm_without_tts")
except Exception:
pass
return False
pass
print("[VC][uplink_ctrl] Request STOP by {}".format(reason))
self.VC_Speech_End()
+ def _vc_drop_reset(self):
+ self._vc_drop_cnt = {}
+ self._vc_drop_bytes = {}
+ self._vc_drop_log_ms = 0
+
+ def _vc_note_drop(self, reason, nbytes=0):
+ """录音/上行静默丢帧计数(与 tx_fail 分开统计)。"""
+ try:
+ r = str(reason or "?")
+ n = int(nbytes or 0)
+ dcnt = getattr(self, "_vc_drop_cnt", None)
+ if dcnt is None:
+ self._vc_drop_reset()
+ dcnt = self._vc_drop_cnt
+ dcnt[r] = dcnt.get(r, 0) + 1
+ if n > 0:
+ self._vc_drop_bytes[r] = getattr(self, "_vc_drop_bytes", {}).get(r, 0) + n
+ now = utime.ticks_ms()
+ last = getattr(self, "_vc_drop_log_ms", 0)
+ if not last or utime.ticks_diff(now, last) >= 800:
+ self._vc_drop_log_ms = now
+ self._vc_drop_log_summary()
+ except Exception:
+ pass
+
+ def _vc_drop_log_summary(self, tag=""):
+ try:
+ dcnt = getattr(self, "_vc_drop_cnt", None) or {}
+ dbytes = getattr(self, "_vc_drop_bytes", None) or {}
+ if not dcnt:
+ if tag:
+ print("[VC][DROP] {} total=0".format(tag))
+ return
+ parts = []
+ for r in sorted(dcnt.keys()):
+ parts.append("{}#{}b{}".format(r, dcnt[r], dbytes.get(r, 0)))
+ print("[VC][DROP] {} {}".format(tag or "tick", " ".join(parts)))
+ except Exception:
+ pass
+
+ def _vc_drop_snip(self):
+ try:
+ dcnt = getattr(self, "_vc_drop_cnt", None) or {}
+ dbytes = getattr(self, "_vc_drop_bytes", None) or {}
+ if not dcnt:
+ return ""
+ parts = []
+ for r in sorted(dcnt.keys()):
+ parts.append("{}:{}b{}".format(r, dcnt[r], dbytes.get(r, 0)))
+ return " drop[" + ",".join(parts) + "]"
+ except Exception:
+ return ""
+
+ def _vc_queue_put_record_tmp(self, buf):
+ try:
+ ok = self.AI_record_tmp_queue.put(buf)
+ if not ok:
+ self._vc_note_drop("queue_full", len(buf))
+ return ok
+ except Exception:
+ return False
+
def _uplink_mark_start(self, first_len=0):
try:
if self._uplink_active == 1:
except Exception:
pass
+ def _vc_pcm_bytes_per_sec(self):
+ try:
+ return int(getattr(self, "VC_record_samplerate", 16000)) * 2
+ except Exception:
+ return 32000
+
+ def _vc_uplink_pcm_audit_snip(self, dur_ms):
+ """PCM 上行:bytes↔时长换算,用于判定录音是否未上传。
+ pcm_ms = bytes*1000/bps;fill%=pcm_ms/dur_ms;rec_fill%=pcm_ms/硬件开麦墙钟。"""
+ try:
+ bps = self._vc_pcm_bytes_per_sec()
+ b = int(getattr(self, "_uplink_bytes", 0) or 0)
+ if b <= 0 or bps <= 0:
+ return ""
+ if hasattr(self, "_vc_opus_uplink_on") and self._vc_opus_uplink_on():
+ return " pcm_audit=opus_skip"
+ pcm_ms = (b * 1000) // bps
+ parts = ["pcm_ms={}".format(pcm_ms)]
+ d = int(dur_ms or 0)
+ if d > 0:
+ parts.append("fill={}%".format((pcm_ms * 100) // d))
+ rec0 = int(getattr(self, "_vc_rec_hw_start_ms", 0) or 0)
+ if rec0:
+ rec_span = utime.ticks_diff(utime.ticks_ms(), rec0)
+ if rec_span > 0:
+ parts.append("rec_span_ms={}".format(rec_span))
+ parts.append("rec_fill={}%".format((pcm_ms * 100) // rec_span))
+ return " " + " ".join(parts)
+ except Exception:
+ return ""
+
def _uplink_mark_end(self, reason=""):
try:
if self._uplink_active != 1:
dur = utime.ticks_diff(utime.ticks_ms(), self._uplink_start_ms) if self._uplink_start_ms else -1
fc = getattr(self, "_uplink_tx_fail_cnt", 0)
fb = getattr(self, "_uplink_tx_fail_bytes", 0)
- print("[VC][uplink] END reason={} bytes={} dur_ms={} tx_fail_cnt={} tx_fail_bytes={} {} VC_record_state_flag={}".format(
+ print("[VC][uplink] END reason={} bytes={} dur_ms={} tx_fail_cnt={} tx_fail_bytes={}{}{}{} VC_record_state_flag={}".format(
reason, self._uplink_bytes, dur, fc, fb,
- self._vc_fsm_log_snip(), self.VC_record_state_flag))
+ self._vc_fsm_log_snip(), self._vc_drop_snip(),
+ self._vc_uplink_pcm_audit_snip(dur), self.VC_record_state_flag))
+ self._vc_drop_log_summary("uplink_end")
except Exception:
pass
self._uplink_active = 0
if self.VC_record_state_flag == 0:
# 新一轮开始录音:允许上行(上一轮可能已置 silence 闸门)
+ self._vc_drop_reset()
self._server_vad_silence = 0
try:
if hasattr(self, "_vc_turn_wall_clear"):
raise e
if not _rec_started:
raise Exception("record stream_start failed")
+ try:
+ self._vc_rec_hw_start_ms = utime.ticks_ms()
+ except Exception:
+ self._vc_rec_hw_start_ms = 0
# if self.record_pcm != None:
# try:
# self.record_pcm.stream_stop()
usys.print_exception(e)
self.VC_record_thread_close()#关闭录音线程
if self.AI_record_tmp_queue.empty() != True:
- self.AI_record_tmp_queue.clear()
+ try:
+ _clear_bytes = 0
+ while self.AI_record_tmp_queue.empty() != True:
+ _d = self.AI_record_tmp_queue.get()
+ _clear_bytes += len(_d)
+ if _clear_bytes > 0:
+ self._vc_note_drop("end_clear", _clear_bytes)
+ except Exception:
+ try:
+ self.AI_record_tmp_queue.clear()
+ except Exception:
+ pass
self.VC_record_state_flag = 0
+ try:
+ self._vc_rec_hw_start_ms = 0
+ except Exception:
+ pass
+ try:
+ self._vc_drop_log_summary("Speech_End")
+ except Exception:
+ pass
# PTT 一轮录音结束:清长按语义,避免后续短按松手误走 end_speech 而不播提示音
try:
if getattr(self, "VC_mode", -1) == 1:
if getattr(self, "_vc_no_speech_idle", 0) == 1:
# 场景①:开麦后一直无有效语音,停录回 idle 省电(会话保持,不提交)
self._vc_publish_led("idle", "VC_Speech_End/no_speech")
+ try:
+ if hasattr(self, "_vc_arm_l1_silence_timer"):
+ self._vc_arm_l1_silence_timer("no_speech_idle")
+ except Exception:
+ pass
# 同步把 FSM 拉回 Idle,使 L1 空闲自动关(close_timerout)能开始累计;
# 否则 turn 卡在 Listening,会话只能等云端 IdleTimeout 报错
try:
if getattr(self, "_vad_thread_on", 0):
# 线程化:投递 PCM 拷贝给 worker(满则丢弃不阻塞),读最近一次结果(滞后约1帧)
try:
- self._vad_in_queue.put(bytes(Record_read_buf[:Record_len]))
+ if not self._vad_in_queue.put(bytes(Record_read_buf[:Record_len])):
+ self._vc_note_drop("vad_q_full", Record_len)
except Exception:
- pass
+ self._vc_note_drop("vad_q_full", Record_len)
vad_state = int(getattr(self, "_vad_async_state", 0))
else:
_vad_t0 = utime.ticks_us()
self._vad_proc_cnt, Record_len, vad_state, _vad_us, _vad_avg, self._vad_proc_us_max))
else:
vad_state = 0
- if self._vc_tts_pipeline_busy():
- pass
+ # 用户仍在录音(record>=2)时上行优先:think/TTS 播放不得整段跳过上行,
+ # 否则 merge 窗或关麦竞态会把句中/句末语音丢掉。
+ _record_uplink_active = getattr(self, "VC_record_state_flag", 0) >= 2
+ if self._vc_tts_pipeline_busy() and not _record_uplink_active:
if vad_state == 1 and not self._vc_local_vad_debug_only():
- self.AI_record_tmp_queue.put(read_buf)#入缓存队列
- AI_record_tmp_queue_len += len(read_buf)
-
-
+ if self._vc_queue_put_record_tmp(read_buf):
+ AI_record_tmp_queue_len += len(read_buf)
+ else:
+ self._vc_note_drop("pipe_skip", Record_len)
else:
try:
if hasattr(self, "_vc_endpoint_hangover_tick"):
if (not self._vc_vad_ready()) and not getattr(self, "_vad_thread_on", 0):
self._vc_vad_pcm_tail = bytearray()
if self.VC_record_state_flag <=1 :#没发送前先存起来
- self.AI_record_tmp_queue.put(read_buf)
- AI_record_tmp_queue_len += len(read_buf)
+ if self._vc_queue_put_record_tmp(read_buf):
+ AI_record_tmp_queue_len += len(read_buf)
else:
AI_record_tmp_queue_len = 0
+ if (
+ self._server_vad_silence == 1
+ and self.record_data_save_flag == 0
+ and _record_uplink_active
+ and vad_state == 1
+ and not self._vc_local_vad_debug_only()
+ and hasattr(self, "_vc_should_hold_mic_for_asr_continue")
+ and self._vc_should_hold_mic_for_asr_continue()
+ ):
+ self._vc_local_vad_resume_uplink_merge("speech_during_gate")
while True:
if self.AI_record_tmp_queue.empty() == True:
break
_ = self.AI_record_tmp_queue.get()
try:
self._uplink_note_tx_fail(len(_), "not_started_drop")
+ self._vc_note_drop("not_started", len(_))
except Exception:
pass
AI_record_tmp_queue_len = max(0, AI_record_tmp_queue_len - len(_))
if not (self._server_vad_silence == 1 and self.record_data_save_flag == 0):
self.AI_client_VC_send(self.AI_record_tmp_queue.get()) #发送之前存的数据
else:
+ _qd = self.AI_record_tmp_queue.get()
+ self._vc_note_drop("gate_q", len(_qd))
self._uplink_mark_end("silence_gate(drop_queue)")
- _ = self.AI_record_tmp_queue.get()
# 当前帧发送前协议门控(可用 VC_PROTO_GUARDS 临时关闭):
# - session started 前禁止发帧
# - Push-To-Talk 在 audio.create 前禁止发帧
):
# 未 ready:转入缓存,等待 started 再发(并做容量控制)
try:
- self.AI_record_tmp_queue.put(read_buf)
- AI_record_tmp_queue_len += len(read_buf)
+ if self._vc_queue_put_record_tmp(read_buf):
+ AI_record_tmp_queue_len += len(read_buf)
while AI_record_tmp_queue_len > 128*1024 and self.AI_record_tmp_queue.empty() != True:
d = self.AI_record_tmp_queue.get()
try:
self._uplink_note_tx_fail(len(d), "prestart_buf_overflow")
+ self._vc_note_drop("prestart_ovf", len(d))
except Exception:
pass
AI_record_tmp_queue_len = max(0, AI_record_tmp_queue_len - len(d))
except Exception:
pass
else:
+ if (
+ self._server_vad_silence == 1
+ and self.record_data_save_flag == 0
+ and _record_uplink_active
+ and vad_state == 1
+ and not self._vc_local_vad_debug_only()
+ and hasattr(self, "_vc_should_hold_mic_for_asr_continue")
+ and self._vc_should_hold_mic_for_asr_continue()
+ ):
+ # silence_gate 后用户已开口但 VAD 边沿未进回调:尽早恢复上行,少丢句首
+ self._vc_local_vad_resume_uplink_merge("speech_during_gate")
if not (self._server_vad_silence == 1 and self.record_data_save_flag == 0):
self.AI_client_VC_send(read_buf)
else:
+ self._vc_note_drop("gate_buf", len(read_buf))
self._uplink_mark_end("silence_gate(drop_buf)")
-
+ else:
+ self._vc_note_drop("read_zero", 0)
except Exception:
pass
if self.VC_play_TYPE == VC_play_TYPE_PCM:
+ self._vc_wait_local_pcm_drain()
if self.play_pcm == None:
# 刚停采集时延后打开播音 PCM,且失败时重试,降低「pcm open write only fail」
last_err = None
pass
utime.sleep_ms(_delay + i * 60)
self.play_pcm = audio.Audio.PCM(0, 1, self.VC_play_samplerate, 1, 1, 2)
+ # C 层 PCM_stream_task_id 全局唯一;软重启或测试脚本未 stream_stop 时
+ # 旧线程仍绑定旧 play_pcm,新缓冲写入后 pending 永不下降 → play_end_wait 死循环
+ try:
+ self.play_pcm.stream_stop()
+ except Exception:
+ pass
ret = self.play_pcm.stream_write_start()
- print("self.record_pcm.stream_write_start:ret:{}".format(ret))
+ print("play_pcm.stream_write_start:ret:{}".format(ret))
last_err = None
break
except Exception as e:
self.play_pcm = None
if last_err:
raise last_err
+ else:
+ try:
+ self.play_pcm.stream_pause(0)
+ except Exception:
+ pass
+ try:
+ self.play_pcm.stream_stop()
+ except Exception:
+ pass
+ ret = self.play_pcm.stream_write_start()
+ print("play_pcm.stream_write_start reuse:ret:{}".format(ret))
elif self.VC_play_TYPE == VC_play_TYPE_MP3:
pass
self.VC_Play_flag = 1
except Exception as e:
self.VC_Play_End()
usys.print_exception(e)
- def VC_Play_End(self):
- self.VC_Play_flag = 0
- if self.VC_play_TYPE == VC_play_TYPE_PCM:
- if self.VC_play_samplerate == 8000:
- self.aud.stopPlayStream()
- self.aud.stopAll()
- elif self.VC_play_samplerate == 16000:
- if self.play_pcm != None:
- print("Close:play_pcm.close()!!!!!!!!!!!!!!!!!!")
- try:
- self.play_pcm.stream_stop()
- self.play_pcm.close()
- except Exception as e:
- usys.print_exception(e)
- self.play_pcm=None
-
- self.aud.stopPlayStream()
- self.aud.stopAll()
- elif self.VC_play_TYPE == VC_play_TYPE_MP3:
- self.aud.stopPlayStream()
- self.aud.stopAll()
- if self.AI_audio_queue.empty()!=True:
- self.AI_audio_queue.clear()
- # TTS 播完:按真实 FSM/录音态恢复灯;本地短音不走此路径
+ def _vc_play_end_after_stop(self, tag="VC_Play_End"):
+ """TTS/下行停播后统一恢复灯与 notify 队列(软/硬关共用)。"""
try:
if self._ptt_should_force_idle_led():
- self._vc_publish_led("idle", "VC_Play_End/ptt_stop")
+ self._vc_publish_led("idle", tag + "/ptt_stop")
elif getattr(self, "_vc_play_end_suppress_idle_led", 0) or self._vc_should_hold_listening_led():
if self._vc_should_hold_listening_led():
- self._vc_publish_listening_for_mode("VC_Play_End/listen_hold")
+ self._vc_publish_listening_for_mode(tag + "/listen_hold")
elif self._vc_led_state_debug_enabled():
- print("[LED] VC_Play_End skip led (suppress)")
+ print("[LED] {} skip led (suppress)".format(tag))
elif getattr(self, "VC_record_state_flag", 0) != 0:
if getattr(self, "VC_mode", 0) == 0:
- self._vc_publish_listening_for_mode("VC_Play_End/recording")
+ self._vc_publish_listening_for_mode(tag + "/recording")
else:
- self._ptt_publish_listening_fast("VC_Play_End/recording")
+ self._ptt_publish_listening_fast(tag + "/recording")
elif (
getattr(self, "VC_mode", -1) == 1
and getattr(self, "VC_record_state_flag", 0) == 0
and not getattr(self, "_ptt_await_server", 0)
and not self._vc_tts_playback_active()
):
- self._vc_publish_led("idle", "VC_Play_End/ptt_round", prefer_off=1)
+ self._vc_publish_led("idle", tag + "/ptt_round", prefer_off=1)
else:
self._vc_led_sync_from_fsm()
except Exception:
self._vc_notify_queue_flush()
except Exception:
pass
- # 关键日志:定位“tts 结束绿灯仍不灭”究竟是 base 态(如 charging/full)还是 playing 未退出
if (
cfg_bool("LED_DEBUG", False)
or cfg_bool("KEY_LED_DEBUG", False)
led = EventMesh.publish("get_led_debug_state")
except Exception:
led = None
- print("[VC] VC_Play_End led={}".format(led))
+ print("[VC] {} led={}".format(tag, led))
+
+ def VC_Play_SoftEnd(self):
+ """对话内 TTS 播完/打断:停流保留 play_pcm,供 start/think/下轮 TTS reuse。"""
+ try:
+ print("[VC] VC_Play_SoftEnd keep_pcm=1")
+ except Exception:
+ pass
+ self._vc_soft_stop_tts_pcm("play_soft_end")
+ self._vc_play_end_after_stop("VC_Play_SoftEnd")
+
+ def VC_Play_End(self):
+ self.VC_Play_flag = 0
+ if self.VC_play_TYPE == VC_play_TYPE_PCM:
+ if self.VC_play_samplerate == 8000:
+ self.aud.stopPlayStream()
+ self.aud.stopAll()
+ elif self.VC_play_samplerate == 16000:
+ if self.play_pcm != None:
+ print("Close:play_pcm.close()!!!!!!!!!!!!!!!!!!")
+ try:
+ self.play_pcm.stream_stop()
+ self.play_pcm.close()
+ except Exception as e:
+ usys.print_exception(e)
+ self.play_pcm=None
+
+ self.aud.stopPlayStream()
+ self.aud.stopAll()
+ elif self.VC_play_TYPE == VC_play_TYPE_MP3:
+ self.aud.stopPlayStream()
+ self.aud.stopAll()
+ if self.AI_audio_queue.empty()!=True:
+ self.AI_audio_queue.clear()
+ self._vc_play_end_after_stop("VC_Play_End")
def _vc_tts_bytes_per_sec(self):
try:
return int(self.VC_play_samplerate) * 2
ready = True
except Exception:
pass
+ # pcm_pending 长时间不变:环形缓冲未被消费(常见于全局 PCM 线程绑错对象)
+ if not ready and pcm_pending > 0:
+ try:
+ last_p = getattr(self, "_tts_pcm_pending_last", -1)
+ stuck_ms = getattr(self, "_tts_pcm_pending_stuck_ms", 0)
+ if pcm_pending == last_p:
+ if not stuck_ms:
+ self._tts_pcm_pending_stuck_ms = now
+ elif utime.ticks_diff(now, stuck_ms) >= 3000:
+ print(
+ "[TTS] play_end_force pcm_stuck pending={} since_done={}ms".format(
+ pcm_pending, since_done))
+ ready = True
+ else:
+ self._tts_pcm_pending_stuck_ms = 0
+ self._tts_pcm_pending_last = pcm_pending
+ except Exception:
+ pass
if not ready:
try:
--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""将 usr/static 下 start.mp3 / think.mp3 转为 16kHz mono 16-bit PCM。"""
+import os
+import sys
+
+SR = 16000
+ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+STATIC = os.path.join(ROOT, "usr", "static")
+
+
+def main():
+ import subprocess
+ try:
+ import imageio_ffmpeg
+ ffmpeg = imageio_ffmpeg.get_ffmpeg_exe()
+ except ImportError:
+ print("pip install imageio-ffmpeg")
+ return 1
+ total_mp3 = 0
+ total_pcm = 0
+ for name in ("start", "think"):
+ mp3 = os.path.join(STATIC, name + ".mp3")
+ pcm = os.path.join(STATIC, name + ".pcm")
+ if not os.path.exists(mp3):
+ print("missing", mp3)
+ return 1
+ cmd = [ffmpeg, "-y", "-i", mp3, "-ar", str(SR), "-ac", "1", "-f", "s16le", pcm]
+ subprocess.check_call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+ msz = os.path.getsize(mp3)
+ psz = os.path.getsize(pcm)
+ total_mp3 += msz
+ total_pcm += psz
+ print("{} mp3={}B pcm={}B dur~{}ms".format(name, msz, psz, psz * 1000 // (SR * 2)))
+ print("total mp3={} pcm={} delta={}".format(total_mp3, total_pcm, total_pcm - total_mp3))
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())