I found that the best way to trim and pad wav
files is to use sox
. However, instead of saving the transformed file each time, how to go about just generating a variable for the transformed wav
file? That is, do not save the transformed wav
file to the harddrive each time, but instead use a variable within Python.
CODE
import librosa
import sox
# get the sample rate
sample_rate = sox.file_info.sample_rate(input_file)
# create transformer
tfm = sox.Transformer()
# trim the audio between 0 and 0.25 seconds.
tfm.trim(0, 0.25)
xx = 'test.wav'
tfm.build(input_file, xx) # create the output file
tfm.pad(0, 0.75)
tfm.build(input_file, xx) # create the output file
duration2 = sox.file_info.duration(xx)
Any help and guidance is sincerely appreciated!
Thanks!
you can entirely omit using sox and work with the numpy array returned by librosa.
librosa.util.fix_length
conveniently pads with zeros if the file is shorter than the desired length.if you later want to save the padded wav file: