Skip to content

Commit

Permalink
fix audio_stream
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom committed Oct 25, 2023
1 parent f3ede73 commit dc8f80e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions livekit-rtc/livekit/rtc/audio_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def create(sample_rate: int, num_channels: int, samples_per_channel: int) -> 'Au
data = bytearray(size)
return AudioFrame(data, sample_rate, num_channels, samples_per_channel)

@staticmethod
def _from_owned_info(owned_info: proto_audio.OwnedAudioFrameBuffer) -> 'AudioFrame':
info = owned_info.info
size = info.num_channels * info.samples_per_channel * ctypes.sizeof(ctypes.c_int16)
data = (ctypes.c_int16 * size).from_address(info.data_ptr)
FfiHandle(owned_info.handle.id)
return AudioFrame(bytearray(data), info.sample_rate, info.num_channels, info.samples_per_channel)

def remix_and_resample(self, sample_rate: int, num_channels: int) -> 'AudioFrame':
""" Resample the audio frame to the given sample rate and number of channels."""

Expand All @@ -58,12 +66,7 @@ def remix_and_resample(self, sample_rate: int, num_channels: int) -> 'AudioFrame
resample_req.remix_and_resample.num_channels = num_channels

resp = ffi_client.request(resample_req)

size = num_channels * self.samples_per_channel * ctypes.sizeof(ctypes.c_int16)
data_ptr = resp.remix_and_resample.buffer.info.data_ptr
data = (ctypes.c_int16 * size).from_address(data_ptr)

return AudioFrame(bytearray(data), sample_rate, num_channels, self.samples_per_channel)
return AudioFrame._from_owned_info(resp.remix_and_resample.buffer)

def _proto_info(self) -> proto_audio.AudioFrameBufferInfo:
audio_info = proto_audio.AudioFrameBufferInfo()
Expand Down
2 changes: 1 addition & 1 deletion livekit-rtc/livekit/rtc/audio_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def _run(self):

if audio_event.HasField('frame_received'):
owned_buffer_info = audio_event.frame_received.frame
frame = AudioFrame(owned_buffer_info)
frame = AudioFrame._from_owned_info(owned_buffer_info)
self._queue.put(frame)
elif audio_event.HasField('eos'):
break
Expand Down

0 comments on commit dc8f80e

Please sign in to comment.