Skip to content

Instantly share code, notes, and snippets.

@jiaaro
Created February 9, 2017 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiaaro/b1c416b0e251aa1560d83dae2085362d to your computer and use it in GitHub Desktop.
Save jiaaro/b1c416b0e251aa1560d83dae2085362d to your computer and use it in GitHub Desktop.
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(
data=stereo_data,
frame_rate=sound1_y.frame_rate,
sample_width=sound1_y.sample_width,
channels=2
)
## -- 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