6 from numpy import arange, resize, sin, exp, zeros
9 print 'usage: %s <outputfile> [samplerate]' % sys.argv[0]
12 samplerate = 44100 # samplerate in Hz
13 if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
16 blocksize = 256 # in samples
17 duration = 0.02 # in seconds
21 duration = int ( samplerate * duration ) # convert to samples
22 attack = int (samplerate * .001 )
25 period = float(samplerate) / pitch
26 # create a sine lookup table
28 sinetable = arange(tablelen + 1, dtype = 'float32')
29 sinetable = 0.7 * sin(twopi * sinetable/tablelen)
30 sinetone = zeros((duration,), dtype = 'float32')
32 # compute sinetone at floating point period
33 for i in range(duration):
34 x = int((i % period) / float(period) * tablelen)
38 b = sinetable[idx + 1]
39 sinetone[i] = a + frac * (b -a)
42 float_ramp = arange(duration, dtype = 'float32')
43 sinetone *= exp( - e * float_ramp / duration / decay)
44 sinetone[:attack] *= exp( e * ( float_ramp[:attack] / attack - 1 ) )
47 import matplotlib.pyplot as plt
51 my_sink = sink(sys.argv[1], samplerate)
54 while total_frames + blocksize < duration:
55 my_sink(sinetone[total_frames:total_frames+blocksize], blocksize)
56 total_frames += blocksize
57 my_sink(sinetone[total_frames:duration], duration - total_frames)