Last active
June 14, 2023 16:08
-
-
Save Tolsi/2c7580d30e6b45a598be1ccce95d55bc to your computer and use it in GitHub Desktop.
Converts two mono wav files to one stereo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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