Skip to content

Commit c5486c8

Browse files
committed
Update audio_resampler.py
1 parent d6ea705 commit c5486c8

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

livekit-rtc/livekit/rtc/audio_resampler.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from ._utils import get_address
1010
from .audio_frame import AudioFrame
1111

12+
import numpy as np
13+
1214

1315
@unique
1416
class AudioResamplerQuality(str, Enum):
@@ -95,9 +97,18 @@ def push(self, data: bytearray | AudioFrame) -> list[AudioFrame]:
9597
"""
9698
bdata = data if isinstance(data, bytearray) else data.data.cast("b")
9799

100+
# temp fix:
101+
# define DITHERING + (1./32)*(int)(((ran1>>3)&31)-((ran2>>3)&31)
102+
# soxr dithering seems to overflow the int16 range (it's unclear why it happens on our builds
103+
# but not inside the soxr lib)
104+
audio_array = np.frombuffer(bdata, dtype=np.int16).astype(np.float32)
105+
scaled_int16 = (audio_array * 0.9).astype(np.int16)
106+
107+
audio_view = memoryview(scaled_int16)
108+
98109
req = proto_ffi.FfiRequest()
99110
req.push_sox_resampler.resampler_handle = self._ffi_handle.handle
100-
req.push_sox_resampler.data_ptr = get_address(memoryview(bdata))
111+
req.push_sox_resampler.data_ptr = get_address(audio_view)
101112
req.push_sox_resampler.size = len(bdata)
102113

103114
resp = FfiClient.instance.request(req)
@@ -111,6 +122,7 @@ def push(self, data: bytearray | AudioFrame) -> list[AudioFrame]:
111122
cdata = (ctypes.c_int8 * resp.push_sox_resampler.size).from_address(
112123
resp.push_sox_resampler.output_ptr
113124
)
125+
114126
output_data = bytearray(cdata)
115127
return [
116128
AudioFrame(

0 commit comments

Comments
 (0)