Generating Sound with Python

With Python Standard Library Methods

http://www.swharden.com/blog/2011-07-08-create-mono-and-stereo-wave-files-with-python/

from struct import pack
from math import sin, pi
import wave
import random

RATE=44100

## GENERATE MONO FILE ##
wv = wave.open('test_mono.wav', 'w')
wv.setparams((1, 2, RATE, 0, 'NONE', 'not compressed'))
maxVol=2**15-1.0 #maximum amplitude
wvData=b""
for i in range(0, RATE*3):
   wvData+=pack('h', round(maxVol*sin(i*2*pi*500.0/RATE))) #500Hz
wv.writeframes(wvData)
wv.close()

## GENERATE STERIO FILE ##
wv = wave.open('test_stereo.wav', 'w')
wv.setparams((2, 2, RATE, 0, 'NONE', 'not compressed'))
maxVol=2**15-1.0 #maximum amplitude
wvData=b""
for i in range(0, RATE*3):
   wvData+=pack('h', round(maxVol*sin(i*2*pi*500.0/RATE))) #500Hz left
   wvData+=pack('h', round(maxVol*sin(i*2*pi*200.0/RATE))) #200Hz right
wv.writeframes(wvData)
wv.close()

With scikits.audiolab

References

Comments