PyTorch – How to clip wav file using torchaudio with start and end time in ms?

How can I load a .wav file and clip it with a start and end time in milliseconds? Currently, I clip it with pydub save it, and then load it in pytorch, but this is incredibly inefficient. Code below.

from pydub import AudioSegment
import torchaudio

newAudio = AudioSegment.from_wav("./test.wav")
t1 = 115870.0
t2 = 117970.0
newAudio = newAudio[t1:t2]
newAudio.export('./test_clip.wav', format="wav")
out, _ = torchaudio.load("./test_clip.wav")

Leave a Comment