Use Python to trim and pad wav file using sox without saving wav file each time

2.4k views Asked by At

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!

1

There are 1 answers

0
ruff09 On BEST ANSWER

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.

durationSeconds = 0.5
data, sr = librosa.load("test.wav", sr=None, mono=False)
trimmed = librosa.util.fix_length(data, int(sr * durationSeconds))

if you later want to save the padded wav file:

librosa.output.write_wav("padded.wav", trimmed, sr)