I have to downsample a wav file from 44100Hz to 16000Hz without using any external python libraries, so preferably wave
and/or audioop
. I tried just changing the wav files framerate to 16000 by using setframerate
function but that just slows down the entire recording. How can I just downsample the audio file to 16kHz and maintain the same length of the audio?
Thank you very much in advance
You can use resample in
scipy
. It's a bit of a headache to do, because there's some type conversion to be done between thebytestring
native to python and the arrays needed inscipy
. There's another headache, because in the wave module in Python, there is no way to tell if the data is signed or not (only if it's 8 or 16 bits). It might (should) work for both, but I haven't tested it.Here's a small program which converts (unsigned) 8 and 16 bits mono from 44.1 to 16. If you have stereo, or use other formats, it shouldn't be that difficult to adapt. Edit the input/output names at the start of the code. Never got around to use the command line arguments.
You can use Librosa's load() function,
The extra effort to install Librosa is probably worth the peace of mind.
Pro-tip: when installing Librosa on Anaconda, you need to install ffmpeg as well, so
This saves you the NoBackendError() error.
Thank you all for your answers. I found a solution already and it works very nice. Here is the whole function.