mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 00:53:54 +08:00
feat: 服务端AEC功能
ref: 入口解码为pcm音频数据,vad和asr可直接使用
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
import time
|
||||
import asyncio
|
||||
import opuslib_next
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -81,13 +82,24 @@ async def _send_to_mqtt_gateway(
|
||||
conn: "ConnectionHandler", opus_packet, timestamp, sequence
|
||||
):
|
||||
"""
|
||||
发送带16字节头部的opus数据包给mqtt_gateway
|
||||
发送带16字节头部的opus数据包给mqtt_gateway,同时缓存音频用于AEC处理
|
||||
Args:
|
||||
conn: 连接对象
|
||||
opus_packet: opus数据包
|
||||
timestamp: 时间戳
|
||||
sequence: 序列号
|
||||
"""
|
||||
# 如果启用了服务端AEC,缓存PCM数据用于后续AEC处理
|
||||
if conn.client_aec and timestamp > 0:
|
||||
if not hasattr(conn, "aec_audio_cache"):
|
||||
conn.aec_audio_cache = {}
|
||||
conn.aec_audio_cache_time = {}
|
||||
conn._send_opus_decoder = opuslib_next.Decoder(16000, 1)
|
||||
# 解码opus为PCM后缓存
|
||||
pcm_data = conn._send_opus_decoder.decode(bytes(opus_packet), 960)
|
||||
conn.aec_audio_cache[timestamp] = bytes(pcm_data)
|
||||
conn.aec_audio_cache_time[timestamp] = time.time()
|
||||
|
||||
# 为opus数据包添加16字节头部
|
||||
header = bytearray(16)
|
||||
header[0] = 1 # type
|
||||
|
||||
Reference in New Issue
Block a user