Reputation: 3828
I want to resample a recording in 32000 KHz to 16000 KHz. I have done this with the code below. But the output audio is somewhat messed up.
You can find the original audio as well output in the following folder https://drive.google.com/drive/folders/1vr-ib8zvZagH_QeE4JSUtAUpp3EG75va
Any ideas what I am doing wrong?
import os
import librosa
import soundfile as sf
folder_name = "trial_sess"
os.chdir(os.path.join("process",folder_name))
for file in os.listdir():
if file.endswith(".m4a") or file.endswith(".mp4") or file.endswith(".mp3"):
nm,ext = file.split(".")
sr = librosa.get_samplerate(file)
y, sr = librosa.load(file, sr = sr)
sf.write(os.path.join(zoom_loc,"sessions",folder_name,"output_resampled_audio" + "." + "wav"), data = y, samplerate=16000)
Upvotes: 1
Views: 1110
Reputation: 6259
There is no resampling performed in your code, and the incorrect samplerate
is passed to write
.
librosa.load
will resample on-demand if the sr
argument is different from that of the original file. So the code should be something like:
target_sr = 16000
y, sr = librosa.load(file, sr=target_sr)
assert sr == target_sr # check that librosa did resample
out_path = os.path.join(zoom_loc,"sessions",folder_name,"output_resampled_audio" + "." + "wav")
sf.write(out_path, data = y, samplerate=target_sr)
Upvotes: 1
Reputation: 1793
When you use librosa
to load the file, make sure to add sr=None
:
y, sr = librosa.load(file_path,sr=None)
This will tell librosa
to not resample on load.
Upvotes: 0