#!/usr/bin/python2 ## ./wavmerge.py in1.wav in2.wav in3.wav outfile.wav import wave import sys import os def main(): files = sys.argv[1:] firstfile = files.pop(0) ff = wave.open(firstfile, 'r') params = ff.getparams() (nchannels, sampwidth, framerate, nframes, comptype, compname) = params checkparams = (nchannels, sampwidth, framerate, comptype, compname) data = ff.readframes(nframes) ff.close() newfile = files.pop() if os.path.exists(newfile): sys.stderr.write('ERROR: output file exists: %s\n' % newfile) nf = wave.open(newfile, 'w') nf.setparams(params) # this ALSO sets nframes nf.writeframesraw(data) for file in files: f = wave.open(file, 'r') fparams = f.getparams() (nchannels, sampwidth, framerate, nframes, comptype, compname) = fparams cparams = (nchannels, sampwidth, framerate, comptype, compname) if not cparams == checkparams: f = 'WARNING: file parameters differ\n %20s: %s\n %20s:%s\n' sys.stderr.write(f % (firstfile, checkparams, file, cparams)) data = f.readframes(nframes) nf.writeframesraw(data) f.close() nf.close() # this corrects nframes if __name__ == '__main__': main()