3

I'm trying to create a single audio file out of multiple wav files. Using tkinter and pygame.mixer, I've converted key presses into a dictionary that stores audio samples and the time they're invoked. {sound1:10000, sound2:10001, ect...}

So far I've devised a way to add blocks of silence:

def change_speed(seconds):
    '''modifies the metronome beat to loop at different speeds. This is done by creating a new wav file.'''
    #original wav file is 0.1 seconds long, so subtract that from time added
    seconds-=0.1
    #read the original wav file
    original = scipy.io.wavfile.read('Downloads\\sounds\\metronome_original.wav')
    #use sample rate of the original file (should be 44100) to create a new block of silence
    add_secs = np.array([[0]]*round(original[0]*seconds))
    add_secs.dtype='int16'
    #concatenate new block to original
    new = np.concatenate((original[1], add_secs))
    scipy.io.wavfile.write('Downloads\\sounds\\metronome.wav', original[0], new)

Is there some way to combine overlapping arrays like [[0,0,1,1,2,0], [0,0,0,3,2,1]] into a single wav file?

Update: To be more specific, I'm trying to merge two audio samples that overlap in playtime, like a DJ who starts playing one song before the other one finishes. Is there a way to do this with integer or byte arrays generated in python? Like so: enter image description here

Bugbeeb
  • 2,021
  • 1
  • 9
  • 26

1 Answers1

0

Here's how I'd do it:

wav1 = [0,0,1,1,2,0]
wav2 = [0,0,0,3,2,1]
combined = np.hstack([wav1, wav2])

from scipy.io import wavfile
import numpy as np

N = 2400  # Samples per second.

wavfile.write('combined.wav', rate=N, data=combined.astype(np.int16))
Matt Hall
  • 7,614
  • 1
  • 23
  • 36