Fixing smallwebrtc transport input audio resampling logic.
This commit is contained in:
@@ -233,9 +233,8 @@ class SmallWebRTCClient:
|
|||||||
self._out_sample_rate = None
|
self._out_sample_rate = None
|
||||||
self._leave_counter = 0
|
self._leave_counter = 0
|
||||||
|
|
||||||
# We are always resampling it for 16000 if the sample_rate that we receive is bigger than that.
|
# Audio resampler - will be configured during setup with target sample rate
|
||||||
# otherwise we face issues with Silero VAD
|
self._audio_in_resampler = None
|
||||||
self._pipecat_resampler = AudioResampler("s16", "mono", 16000)
|
|
||||||
|
|
||||||
@self._webrtc_connection.event_handler("connected")
|
@self._webrtc_connection.event_handler("connected")
|
||||||
async def on_connected(connection: SmallWebRTCConnection):
|
async def on_connected(connection: SmallWebRTCConnection):
|
||||||
@@ -375,32 +374,22 @@ class SmallWebRTCClient:
|
|||||||
await asyncio.sleep(0.01)
|
await asyncio.sleep(0.01)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if frame.sample_rate > self._in_sample_rate:
|
# Resample if needed, otherwise use the frame as-is
|
||||||
resampled_frames = self._pipecat_resampler.resample(frame)
|
frames_to_process = (
|
||||||
for resampled_frame in resampled_frames:
|
self._audio_in_resampler.resample(frame)
|
||||||
# 16-bit PCM bytes
|
if frame.sample_rate != self._in_sample_rate
|
||||||
pcm_array = resampled_frame.to_ndarray().astype(np.int16)
|
else [frame]
|
||||||
pcm_bytes = pcm_array.tobytes()
|
)
|
||||||
del pcm_array # free NumPy array immediately
|
|
||||||
|
|
||||||
audio_frame = InputAudioRawFrame(
|
for processed_frame in frames_to_process:
|
||||||
audio=pcm_bytes,
|
# Convert to 16-bit PCM bytes
|
||||||
sample_rate=resampled_frame.sample_rate,
|
pcm_array = processed_frame.to_ndarray().astype(np.int16)
|
||||||
num_channels=self._audio_in_channels,
|
|
||||||
)
|
|
||||||
audio_frame.pts = frame.pts
|
|
||||||
del pcm_bytes # reference kept in audio_frame
|
|
||||||
|
|
||||||
yield audio_frame
|
|
||||||
else:
|
|
||||||
# 16-bit PCM bytes
|
|
||||||
pcm_array = frame.to_ndarray().astype(np.int16)
|
|
||||||
pcm_bytes = pcm_array.tobytes()
|
pcm_bytes = pcm_array.tobytes()
|
||||||
del pcm_array # free NumPy array immediately
|
del pcm_array # free NumPy array immediately
|
||||||
|
|
||||||
audio_frame = InputAudioRawFrame(
|
audio_frame = InputAudioRawFrame(
|
||||||
audio=pcm_bytes,
|
audio=pcm_bytes,
|
||||||
sample_rate=frame.sample_rate,
|
sample_rate=self._in_sample_rate,
|
||||||
num_channels=self._audio_in_channels,
|
num_channels=self._audio_in_channels,
|
||||||
)
|
)
|
||||||
audio_frame.pts = frame.pts
|
audio_frame.pts = frame.pts
|
||||||
@@ -450,6 +439,7 @@ class SmallWebRTCClient:
|
|||||||
self._out_sample_rate = _params.audio_out_sample_rate or frame.audio_out_sample_rate
|
self._out_sample_rate = _params.audio_out_sample_rate or frame.audio_out_sample_rate
|
||||||
self._params = _params
|
self._params = _params
|
||||||
self._leave_counter += 1
|
self._leave_counter += 1
|
||||||
|
self._audio_in_resampler = AudioResampler("s16", "mono", self._in_sample_rate)
|
||||||
|
|
||||||
async def connect(self):
|
async def connect(self):
|
||||||
"""Establish the WebRTC connection."""
|
"""Establish the WebRTC connection."""
|
||||||
|
|||||||
Reference in New Issue
Block a user