Skip to content

Instantly share code, notes, and snippets.

@Tolsi
Last active June 14, 2023 16:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tolsi/2c7580d30e6b45a598be1ccce95d55bc to your computer and use it in GitHub Desktop.
Save Tolsi/2c7580d30e6b45a598be1ccce95d55bc to your computer and use it in GitHub Desktop.
Converts two mono wav files to one stereo
import wave, array
# changed from https://stackoverflow.com/a/43163543/699934
def make_stereo(file1, file2, output):
ifile1 = wave.open(file1)
ifile2 = wave.open(file2)
print(ifile1.getparams())
# (1, 2, 44100, 2013900, 'NONE', 'not compressed')
(nchannels, sampwidth, framerate, nframes, comptype, compname) = ifile1.getparams()
assert ifile1.getparams() == ifile2.getparams()
assert comptype == 'NONE' # Compressed not supported yet
array_type = {1:'B', 2: 'h', 4: 'l'}[sampwidth]
left_channel = array.array(array_type, ifile1.readframes(nframes))[::nchannels]
right_channel = array.array(array_type, ifile2.readframes(nframes))[::nchannels]
ifile1.close()
ifile2.close()
stereo = 2 * left_channel
stereo[0::2] = left_channel
stereo[1::2] = right_channel
ofile = wave.open(output, 'w')
ofile.setparams((2, sampwidth, framerate, nframes, comptype, compname))
ofile.writeframes(stereo)
ofile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment