mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 09:03:54 +08:00
fix: 方法修正
This commit is contained in:
@@ -5,12 +5,10 @@ Opus编码工具类
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from typing import List, Optional
|
|
||||||
from opuslib_next import Encoder
|
from opuslib_next import Encoder
|
||||||
from opuslib_next import constants
|
from opuslib_next import constants
|
||||||
|
from typing import Optional, Callable, Any
|
||||||
|
|
||||||
class OpusEncoderUtils:
|
class OpusEncoderUtils:
|
||||||
"""PCM到Opus的编码器"""
|
"""PCM到Opus的编码器"""
|
||||||
@@ -56,13 +54,14 @@ class OpusEncoderUtils:
|
|||||||
self.encoder.reset_state()
|
self.encoder.reset_state()
|
||||||
self.buffer = np.array([], dtype=np.int16)
|
self.buffer = np.array([], dtype=np.int16)
|
||||||
|
|
||||||
def encode_pcm_to_opus(self, pcm_data: bytes, end_of_stream: bool) -> List[bytes]:
|
def encode_pcm_to_opus_stream(self, pcm_data: bytes, end_of_stream: bool, callback: Callable[[Any], Any]):
|
||||||
"""
|
"""
|
||||||
将PCM数据编码为Opus格式
|
将PCM数据编码为Opus格式,以流式方式进行处理
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
pcm_data: PCM字节数据
|
pcm_data: PCM字节数据
|
||||||
end_of_stream: 是否为流的结束
|
end_of_stream: 是否为流的结束,
|
||||||
|
callback: opus处理方法
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Opus数据包列表
|
Opus数据包列表
|
||||||
@@ -76,7 +75,6 @@ class OpusEncoderUtils:
|
|||||||
# 将新数据追加到缓冲区
|
# 将新数据追加到缓冲区
|
||||||
self.buffer = np.append(self.buffer, new_samples)
|
self.buffer = np.append(self.buffer, new_samples)
|
||||||
|
|
||||||
opus_packets = []
|
|
||||||
offset = 0
|
offset = 0
|
||||||
|
|
||||||
# 处理所有完整帧
|
# 处理所有完整帧
|
||||||
@@ -84,7 +82,7 @@ class OpusEncoderUtils:
|
|||||||
frame = self.buffer[offset : offset + self.total_frame_size]
|
frame = self.buffer[offset : offset + self.total_frame_size]
|
||||||
output = self._encode(frame)
|
output = self._encode(frame)
|
||||||
if output:
|
if output:
|
||||||
opus_packets.append(output)
|
callback(output)
|
||||||
offset += self.total_frame_size
|
offset += self.total_frame_size
|
||||||
|
|
||||||
# 保留未处理的样本
|
# 保留未处理的样本
|
||||||
@@ -98,11 +96,9 @@ class OpusEncoderUtils:
|
|||||||
|
|
||||||
output = self._encode(last_frame)
|
output = self._encode(last_frame)
|
||||||
if output:
|
if output:
|
||||||
opus_packets.append(output)
|
callback(output)
|
||||||
self.buffer = np.array([], dtype=np.int16)
|
self.buffer = np.array([], dtype=np.int16)
|
||||||
|
|
||||||
return opus_packets
|
|
||||||
|
|
||||||
def _encode(self, frame: np.ndarray) -> Optional[bytes]:
|
def _encode(self, frame: np.ndarray) -> Optional[bytes]:
|
||||||
"""编码一帧音频数据"""
|
"""编码一帧音频数据"""
|
||||||
try:
|
try:
|
||||||
@@ -133,4 +129,4 @@ class OpusEncoderUtils:
|
|||||||
def close(self):
|
def close(self):
|
||||||
"""关闭编码器并释放资源"""
|
"""关闭编码器并释放资源"""
|
||||||
# opuslib没有明确的关闭方法,Python的垃圾回收会处理
|
# opuslib没有明确的关闭方法,Python的垃圾回收会处理
|
||||||
pass
|
pass
|
||||||
Reference in New Issue
Block a user