This one gave me a special migraine, but I think this could possibly help anyone interested in making some of their own tunes
# create a soundfile in AU format playing a sine wave
# of a given frequency, duration and volume
# tested with Python25 by vegaseat 29jan2008
from struct import pack
from math import pi, sin, exp
a = 2**(1./12)
def scale_sin(i, period):
return (1+sin(i/float(period)*2*pi))*.5
print [400 * a**i for i in xrange(50)]
def au_file(name='test.au', freq=440, dur=1000, vol=0.5):
"""
creates an AU format audio file of a sine wave
of frequency freq (Hz)
for duration dur (milliseconds)
at volume vol (max is 1.0)
"""
fout = open(name, 'wb')
# header needs size, encoding=2, sampling_rate=8000, channel=1
sampling_rate = 8000.
fout.write('.snd' + pack('>5L', 24, 8*dur, 2, sampling_rate, 1))
factor = 2 * pi / sampling_rate
# write data
a = 2**(1./12)
freqs = [300 * a**i for i in xrange(24)]
notes = [scale_sin(i+7,3)*.2+scale_sin(i,11)*.4+scale_sin(i+2,13)*.4 for i in range(100)]
for i in xrange(len(notes)):
print "*" * (int(notes[i]*24))
music = [freqs[int(notes[i]*len(freqs))] for i in range(len(notes))]
print music
print len(music)
for seg in range(8 * dur):
# for seg in range(10):
t = seg / sampling_rate
w = t * 2 * pi
# sine wave calculations
i = int(seg / 2000) % len(music)
freq = music[i]
sin_seg = sin(freq * w)
fout.write(pack('b', vol * 127 * sin_seg))
fout.close()
# test the module ...
if __name__ == '__main__':
au_file(name='sound800.au', freq=800, dur=8000*50, vol=0.8)
# if you have Windows, you can test the audio file
# otherwise comment this code out
# import os
# os.startfile('sound800.au')