Skip to content

Instantly share code, notes, and snippets.

@Tolsi
Last active June 14, 2023 16:08
Show Gist options
  • Save Tolsi/2c7580d30e6b45a598be1ccce95d55bc to your computer and use it in GitHub Desktop.
Save Tolsi/2c7580d30e6b45a598be1ccce95d55bc to your computer and use it in GitHub Desktop.

Revisions

  1. Tolsi revised this gist Dec 14, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions two_mono_wav_to_stereo.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    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)
  2. Tolsi created this gist Dec 14, 2021.
    24 changes: 24 additions & 0 deletions two_mono_wav_to_stereo.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import wave, array

    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()