From 2574ce5ea26e11be1b9b3931e1d5d5067d1c33d2 Mon Sep 17 00:00:00 2001 From: Alex Murkoff <79400603+alexlnkp@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:23:13 +0700 Subject: [PATCH] feat(audio): pre-allocate decoded_audio array in the load_audio function this should improve performance, even if just a little --- infer/lib/audio.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/infer/lib/audio.py b/infer/lib/audio.py index 61acc5b..43733af 100644 --- a/infer/lib/audio.py +++ b/infer/lib/audio.py @@ -42,7 +42,9 @@ def load_audio(file: str, sr: int) -> np.ndarray: try: container = av.open(file) resampler = AudioResampler(format='fltp', layout='mono', rate=sr) - decoded_audio = [] + + # AV stores duration in nanoseconds + decoded_audio = (((container.duration * sr / container.bit_rate) // 1_000_000) + 1)*[] for frame in container.decode(audio=0): frame.pts = None # Clear presentation timestamp to avoid resampling issues