I'm using the following code which will generate a wav file which contains a tone at 440 Hz lasting for 2 seconds.
from scipy.io.wavfile import write
from numpy import linspace,sin,pi,int16
def note(freq, len, amp=1, rate=44100):
t = linspace(0,len,len*rate)
data = sin(2*pi*freq*t)*amp
return data.astype(int16) # two byte integers
tone = note(440,2,amp=10000)
write('440hzAtone.wav',44100,tone) # writing the sound to a file
I was wondering if I could modify the code, basing it off the note method, in order to actually generate a tune with python.
I tried adding two different tones, and as expected the two tones play simultaneously, creating something which sounds a bit like a dial tone:
tone1 = note(440,2,amp=10000)
tone2 = note(480,2,amp=10000)
tone = tone1+tone2
write('440hzAtone.wav',44100,tone)
I also tried multiplying the two tones, but this just generates static.
I also tried genreating tones of different lengths and adding them, however this causes an exception to be raised, like so:
tone1 = note(440,2,amp=10000)
tone2 = note(480,1,amp=10000)
tone = tone1+tone2
write('440hzAtone.wav',44100,tone)
causes:
ValueError: operands could not be broadcast together with shapes (88200) (44100)
So, I was wondering - how can I concatenate different tones like this to make a tune?