tone.py 483 B

12345678910111213141516171819
  1. from numpy import linspace,sin,pi,int16
  2. # tone synthesis
  3. def note(freq, len, amp=1, rate=44100):
  4. t = linspace(0,len,len*rate)
  5. data = sin(2*pi*freq*t)*amp
  6. return data.astype(int16) # two byte integers
  7. from scipy.io.wavfile import write
  8. from pylab import plot,show,axis
  9. # A tone, 2 seconds, 44100 samples per second
  10. tone = note(440,2,amp=10000)
  11. write('440hzAtone.wav',44100,tone) # writing the sound to a file
  12. plot(linspace(0,2,2*44100),tone)
  13. axis([0,0.4,15000,-15000])
  14. show()