# Opus 编解码(需固件 CONFIG_OPUS=y)。默认关:与原 PCM 链路行为完全一致。
# 开启后联动 RVC V3 start.audio_config:上行→input.format=opus,下行→output.format=opus。
# 上下行可独立开:仅上行=省上行带宽;仅下行=省下行带宽。
-VC_OPUS_UPLINK_ENABLED = False # 上行:录音 PCM 分帧 opus 编码后发送
+VC_OPUS_UPLINK_ENABLED = True # 上行:录音 PCM 分帧 opus 编码后发送(联调中)
VC_OPUS_DOWNLINK_ENABLED = True # 下行:服务端 opus 包解码为 PCM 后播放(联调中)
# Opus 帧时长(ms):合法 2.5/5/10/20/40/60;16kHz 单声道。上行按此分帧(60ms=1920字节PCM)。
VC_OPUS_FRAME_MS = 60
# Opus 编解码(需固件 CONFIG_OPUS=y)。默认关:与原 PCM 链路行为完全一致。
# 开启后联动 RVC V3 start.audio_config:上行→input.format=opus,下行→output.format=opus。
# 上下行可独立开:仅上行=省上行带宽;仅下行=省下行带宽。
-VC_OPUS_UPLINK_ENABLED = False # 上行:录音 PCM 分帧 opus 编码后发送
+VC_OPUS_UPLINK_ENABLED = True # 上行:录音 PCM 分帧 opus 编码后发送(联调中)
VC_OPUS_DOWNLINK_ENABLED = True # 下行:服务端 opus 包解码为 PCM 后播放(联调中)
# Opus 帧时长(ms):合法 2.5/5/10/20/40/60;16kHz 单声道。上行按此分帧(60ms=1920字节PCM)。
VC_OPUS_FRAME_MS = 60
if (err != OPUS_OK || self->enc == NULL) {\r
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("opus_encoder_create failed"));\r
}\r
- opus_encoder_ctl(self->enc, OPUS_SET_COMPLEXITY(5));\r
+ opus_encoder_ctl(self->enc, OPUS_SET_COMPLEXITY(0)); /* 低复杂度降低 SILK 栈占用与 CPU,ASR 场景音质足够 */\r
opus_encoder_ctl(self->enc, OPUS_SET_BITRATE(24000));\r
self->has_enc = 1;\r
}\r
return c
def _vc_opus_uplink_feed(self, pcm):
- # 累积 PCM 按固定帧编码;不足一帧留待下次,避免帧长不齐导致编码失败
+ # 累积 PCM 按固定帧编码;不足一帧留待下次。
+ # 注意:MicroPython 的 bytearray 不支持 del buf[:n],用偏移+尾部切片重建。
buf = getattr(self, "_opus_up_buf", None)
if buf is None:
buf = bytearray()
- self._opus_up_buf = buf
buf.extend(pcm)
fb = self._vc_opus_frame_bytes()
try:
codec = self._vc_opus_up_codec()
except Exception as e:
usys.print_exception(e)
+ self._opus_up_buf = buf
return
- while len(buf) >= fb:
- frame = bytes(buf[:fb])
- del buf[:fb]
+ off = 0
+ total = len(buf)
+ while total - off >= fb:
+ frame = bytes(buf[off:off + fb])
+ off += fb
try:
pkt = codec.encode(frame)
except Exception as e:
+ print("[VC][opus_up] encode fail in={}".format(fb))
usys.print_exception(e)
continue
l = len(pkt)
if l <= 0:
continue
+ self._opus_up_cnt = getattr(self, "_opus_up_cnt", 0) + 1
+ if self._opus_up_cnt <= 5 or (self._opus_up_cnt % 50) == 0:
+ print("[VC][opus_up] #{} in={} out={}".format(self._opus_up_cnt, fb, l))
if self.AI_client_VC_isconnect != 1 or self.AI_client_VC is None:
self._uplink_note_tx_fail(l, "opus_enqueue_ws_down")
+ self._opus_up_buf = bytearray(buf[off:])
return
self._uplink_mark_start(l)
self._uplink_bytes += l
self.AI_VC_send_data_queue.put([AI_client_VC_num, pkt])
+ self._opus_up_buf = bytearray(buf[off:]) if off < total else bytearray()
def AI_client_VC_send(self,data_str):
# 上行 opus:PCM(bytes) 累积分帧编码后入队;开关关闭时完全走原 PCM 逻辑
pass
self.VC_record_thread_close()#关闭录音线程
- _thread.stack_size(self.Audio_buf_size+8*1024)
+ # 上行 opus 编码(含 SILK)在录音线程内执行,调用栈很深,需额外栈,否则 mp_thread 栈溢出
+ _rec_extra = 32*1024 if self._vc_opus_uplink_on() else 8*1024
+ _thread.stack_size(self.Audio_buf_size+_rec_extra)
self.VC_record_thread_id = _thread.start_new_thread(self.VC_record_thread,())
_thread.stack_size(8*1024)
print("self.VC_record_thread_id:{}".format(self.VC_record_thread_id))