Downsampling wav audio file

You can use Librosa’s load() function, import librosa y, s = librosa.load(‘test.wav’, sr=8000) # Downsample 44.1kHz to 8kHz 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 pip install librosa conda install -c conda-forge ffmpeg This saves … Read more

How to create a numpy array from a pydub AudioSegment?

Pydub has a facility for getting the audio data as an array of samples, it is an array.array instance (not a numpy array) but you should be able to convert it to a numpy array relatively easily: from pydub import AudioSegment sound = AudioSegment.from_file(“sound1.wav”) # this is an array samples = sound.get_array_of_samples() You may be … Read more

How to split a .wav file into multiple .wav files?

This is a python code snippet that I use for splitting files as per necessity. I use the pydub library from https://github.com/jiaaro/pydub. You can modify the snippet to suit your requirement. from pydub import AudioSegment t1 = t1 * 1000 #Works in milliseconds t2 = t2 * 1000 newAudio = AudioSegment.from_wav(“oldSong.wav”) newAudio = newAudio[t1:t2] newAudio.export(‘newSong.wav’, … Read more

Reading *.wav files in Python

Per the documentation, scipy.io.wavfile.read(somefile) returns a tuple of two items: the first is the sampling rate in samples per second, the second is a numpy array with all the data read from the file: from scipy.io import wavfile samplerate, data = wavfile.read(‘./output/audio.wav’)