I'm looking for a way to take slices out of a wav file in python. That is, for some wav file f, extract all of the audio from t=x to t=y and write it to a new file. Googling around, there doesn't seem to be any off-the-shelf solutions, and I'm not opposed to rolling my own implementation (it's not too hard), but I'd rather not reinvent the wheel and inevitably run into a bunch of issues someone else has already dealt with.
I've included the code I'm currently using - feel free to critique if nothing else. It uses wave.py (easy_install wave if you have setuptools)
Code:
import wave
def slice(infile, outfilename, start_ms, end_ms):
width = infile.getsampwidth()
rate = infile.getframerate()
fpms = rate / 1000 # frames per ms
length = (end_ms - start_ms) * fpms
start_index = start_ms * fpms
out = wave.open(outfilename, "w")
out.setparams((infile.getnchannels(), width, rate, length, infile.getcomptype(), infile.getcompname()))
infile.rewind()
anchor = infile.tell()
infile.setpos(anchor + start_index)
out.writeframes(infile.readframes(length))
if __name__ == "__main__":
slice(wave.open("onetwothree.wav", "r"), "out.wav", 500, 3000)
Bookmarks