Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
In [1]: paste
import array
from pydub import AudioSegment
from pydub.generators import Sine, Square
# make two mono sounds (300 Hz tones)
sound1 = Sine(300).to_audio_segment(duration=30000)
sound2 = Square(300).to_audio_segment(duration=30000)
def x():
sound1_left = sound1.set_channels(2).pan(-1)
sound2_right = sound2.set_channels(2).pan(+1)
stereo_sound = sound1_left.overlay(sound2_right)
def y():
sound1_y, sound2_y = AudioSegment._sync(sound1, sound2)
# initialize an array, and write the data
stereo_data = array.array(sound1_y.array_type, sound1_y.raw_data + sound2_y.raw_data)
stereo_data[0::2] = sound1_y.get_array_of_samples()
stereo_data[1::2] = sound2_y.get_array_of_samples()
stereo_sound = AudioSegment(
## -- End pasted text --
In [2]: %timeit x()
10 loops, best of 3: 149 ms per loop
In [3]: %timeit y()
100 loops, best of 3: 10.7 ms per loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.