pythonaudiolibrosasoundfile

I want to change a wav file with 44100 sr to 22050 sr with out change in pitch and speed in python


This is my code:

list_ = os.listdir("All_wavs")
for i,val in enumerate(list_):
  
    y, sr = librosa.load(f"All_wavs/{val}")
    data = librosa.resample(y, orig_sr =44100,target_sr= 22050)
    sf.write(f"wavs/{val}",data, samplerate =22050)

it works but the wav files sound different

Thanks for helping!


Solution

  • librosa.load automatically resamples the input signal to 22050 unless specified otherwise, you could modify that using sr if you want and just save it, you don't need to do a second downsampling.

    import librosa
    import soundfile as sf
    
    list_ = os.listdir("All_wavs")
    for i,val in enumerate(list_):
        y, sr = librosa.load(f"All_wavs/{val}", sr=22050)
        sf.write(f"wavs/{val}",y, samplerate =22050)