diff --git a/livekit-rtc/livekit/rtc/_utils.py b/livekit-rtc/livekit/rtc/_utils.py index fd7e452f..e2cc6b60 100644 --- a/livekit-rtc/livekit/rtc/_utils.py +++ b/livekit-rtc/livekit/rtc/_utils.py @@ -40,11 +40,8 @@ def task_done_logger(task: asyncio.Task) -> None: return -def get_address(data: memoryview) -> int: - """Get the address of a buffer using ctypes""" - nbytes = data.nbytes - buffer = (ctypes.c_int8 * nbytes).from_buffer(data) - return ctypes.addressof(buffer) +def get_address(mv: memoryview) -> int: + return ctypes.addressof(ctypes.c_char.from_buffer(mv)) T = TypeVar("T") diff --git a/livekit-rtc/livekit/rtc/audio_frame.py b/livekit-rtc/livekit/rtc/audio_frame.py index 4d83c7a1..4c3d6ede 100644 --- a/livekit-rtc/livekit/rtc/audio_frame.py +++ b/livekit-rtc/livekit/rtc/audio_frame.py @@ -48,6 +48,8 @@ def __init__( Raises: ValueError: If the length of `data` is smaller than the required size. """ + data = memoryview(data).cast("B") + if len(data) < num_channels * samples_per_channel * ctypes.sizeof( ctypes.c_int16 ): @@ -59,7 +61,9 @@ def __init__( # can happen if data is bigger than needed raise ValueError("data length must be a multiple of sizeof(int16)") - self._data = bytearray(data) + n = len(data) // ctypes.sizeof(ctypes.c_int16) + self._data = (ctypes.c_int16 * n).from_buffer_copy(data) + self._sample_rate = sample_rate self._num_channels = num_channels self._samples_per_channel = samples_per_channel @@ -133,7 +137,7 @@ def data(self) -> memoryview: Returns: memoryview: A memory view of the audio data. """ - return memoryview(self._data).cast("h") + return memoryview(self._data).cast("B").cast("h") @property def sample_rate(self) -> int: