Unable to Flush the Audio Resampler
Created by: khwong-c
Overview
The Audio Resampler Cannot be flushed with
Expected behavior
I expect the resampler can be reused and resample audio files with different input sampling rates, by applying a flush.
resampler.resample(None)
.
Actual behavior
The resampler crashed right at the flush. File "/home/khwong/projects/audio_feature_extraction/audio_utils/audio_input.py", line 38, in post_process return self.resampler.resample(audio)[0] File "av/audio/resampler.pyx", line 34, in av.audio.resampler.AudioResampler.resample File "av/audio/resampler.pyx", line 101, in av.audio.resampler.AudioResampler.resample File "av/filter/graph.pyx", line 211, in av.filter.graph.Graph.push File "av/filter/context.pyx", line 89, in av.filter.context.FilterContext.push File "av/error.pyx", line 336, in av.error.err_check av.error.ValueError: [Errno 22] Invalid argument
Build report:
It is a release from PyAV v10.0.0.
Investigation
Try not to flush the resampler by resample(None) The code is able to run until an audio file with a different sampling rate is loaded.
Reproduction
class BatchAudioLoader:
def __init__(self,
sample_rate=16000, layout='mono',
output_format='flt', output_planar=False
):
self._output_format = f'{output_format}{"p" if output_planar else ""}'
self.resampler = av.audio.resampler.AudioResampler(self._output_format, layout, sample_rate)
def post_process(self, audio):
self.resampler.resample(None)
return self.resampler.resample(audio)[0]
def load(self, filename):
buffer = av.audio.fifo.AudioFifo()
with av.open(filename) as source:
for frame in source.decode(source.streams.audio[0]):
buffer.write(frame)
whole_audio = buffer.read(0)
if isinstance(whole_audio, list):
whole_audio = whole_audio[0]
whole_audio = self.post_process(whole_audio)
sample_rate = whole_audio.sample_rate
audio_arr = whole_audio.to_ndarray(format=self._output_format)
return audio_arr, sample_rate
loader = BatchAudioLoader(sample_rate=16000)
loader.load('file1')
loader.load('file2')
Versions
Python 3.10
- OS: Ubuntu Linux 20.04
- PyAV runtime:
PyAV v10.0.0
library configuration: --disable-static --enable-shared --libdir=/tmp/vendor/lib --prefix=/tmp/vendor --disable-alsa --disable-doc --disable-mediafoundation --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-lzma --enable-version3 --enable-zlib
library license: GPL version 3 or later
libavcodec 59. 37.100
libavdevice 59. 7.100
libavfilter 8. 44.100
libavformat 59. 27.100
libavutil 57. 28.100
libswresample 4. 7.100
libswscale 6. 7.100
Research
I have done the following:
-
Checked the PyAV documentation -
Searched on Google -
Searched on Stack Overflow -
Looked through old GitHub issues -
Asked on PyAV Gitter -
... and waited 72 hours for a response.