In the realtime_api.py file
Added this function:
def inject_from_wav(self, path: str) -> None:
# Load audio (always returns float32 by default)
audio_array, sr = sf.read(path, dtype="int16")
# Resample if needed
if sr != 16000:
logger.info("resampling")
audio_array = librosa.resample(
audio_array.astype(np.float32), orig_sr=sr, target_sr=16000
).astype(np.int16)
sr = 16000
# Ensure shape = (samples, channels)
if audio_array.ndim == 1:
audio_array = np.expand_dims(audio_array, axis=-1)
channels = audio_array.shape[1]
# Chunk into ~20ms frames (320 samples @ 16kHz)
frame_size = 320
for start in range(0, len(audio_array), frame_size):
chunk = audio_array[start:start+frame_size]
if len(chunk) < frame_size:
break # drop trailing partial frame
frame = rtc.AudioFrame(
data=chunk.tobytes(), # ✅ convert ndarray → bytes
sample_rate=sr,
num_channels=channels,
samples_per_channel=frame_size,
)
self.push_audio(frame)
logger.info(f"Injected audio from {path}")
and then called it in the on_timeout() function, using an audio file that plays the phrase "Please continue" as the argument