Skip to content Skip to sidebar Skip to footer

(python Wave Module) Can't Change Audio Sampling Rate Without Affecting Playback Speed

I'm currently working on a project which includes using pygame to play sounds on a button press. Since I didn't find a good way to record sound from the application (I tried PyAudi

Solution 1:

I found the aswer here: Python - downsampling wav audio file

It needed a little tweaking to get it work, just use converted[0] instead of converted when writing frames since method doesn't accept tuples.

Solution 2:

You can change the sample rate using librosa However, librosa usually runs into issues while installing

INSTALLATION

using conda environment

python --> 3.6.8
conda install ffmpeg=4.3.1
conda install numba=0.48
pip install librosa==0.6.0

CODE

import librosa

audio_file = "Original.wav"#48KHz#SAME PLAYBACK SPEED
x, sr = librosa.load(audio_file, sr=44100)
librosa.output.write_wav("Test1.wav", x, sr=22050, norm=False)

#SAME PLAYBACK SPEED
x, sr = librosa.load(audio_file, sr=48000)
y = librosa.resample(x, 48000, 44100)
librosa.output.write_wav("Test3.wav", y, sr=44100, norm=False)

#SLOW PLAYBACK SPEED
x, sr = librosa.load(audio_file, sr=48000)
librosa.output.write_wav("Test2.wav", x, sr=44100, norm=False)

UPDATE

libroa when saving the output changes the datatype to 32bit float by default. Hence, to save the array, use soundwrite and specify the datatype while saving the audio

import soundfile as sf
data, samplerate = soundfile.read('old.wav')
sf.write("Test4.wav", x, 22050, subtype='PCM_16')

Post a Comment for "(python Wave Module) Can't Change Audio Sampling Rate Without Affecting Playback Speed"