python-3.xtorchaudio

torchaudio load audio with specific sampling rate


From documentation, https://pytorch.org/audio/stable/backend.html#torchaudio.backend.sox_io_backend.load it seems there is no parameter for loading audio with a fixed sampling rate which is important for training models.

How to load a pytorch audio tensor with a fixed sampling rate with torchaudio?


Solution

  • You can resample with torchaudio.functional.resample

    arr, org_sr = torchaudio.load('path')
    arr = torchaudio.functional.resample(arr, orig_freq=org_sr, new_freq=new_sr)