[87b0c03] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | import sys |
---|
[1e7a8f9] | 4 | from math import pi, e |
---|
[87b0c03] | 5 | from aubio import sink |
---|
[1e7a8f9] | 6 | from numpy import arange, resize, sin, exp, zeros |
---|
[87b0c03] | 7 | |
---|
[1e7a8f9] | 8 | if len(sys.argv) < 2: |
---|
| 9 | print 'usage: %s <outputfile> [samplerate]' % sys.argv[0] |
---|
[87b0c03] | 10 | sys.exit(1) |
---|
| 11 | |
---|
[1e7a8f9] | 12 | samplerate = 44100 # samplerate in Hz |
---|
| 13 | if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) |
---|
| 14 | |
---|
[87b0c03] | 15 | pitch = 2200 # in Hz |
---|
| 16 | blocksize = 256 # in samples |
---|
| 17 | duration = 0.02 # in seconds |
---|
| 18 | |
---|
| 19 | twopi = pi * 2. |
---|
| 20 | |
---|
[1e7a8f9] | 21 | duration = int ( samplerate * duration ) # convert to samples |
---|
| 22 | attack = int (samplerate * .001 ) |
---|
| 23 | decay = .5 |
---|
| 24 | |
---|
| 25 | period = float(samplerate) / pitch |
---|
| 26 | # create a sine lookup table |
---|
| 27 | tablelen = 1000 |
---|
| 28 | sinetable = arange(tablelen + 1, dtype = 'float32') |
---|
| 29 | sinetable = 0.7 * sin(twopi * sinetable/tablelen) |
---|
| 30 | sinetone = zeros((duration,), dtype = 'float32') |
---|
| 31 | |
---|
| 32 | # compute sinetone at floating point period |
---|
| 33 | for i in range(duration): |
---|
| 34 | x = int((i % period) / float(period) * tablelen) |
---|
| 35 | sinetone[i] = (sinetable[x] + sinetable[x+1]) / 2 |
---|
| 36 | |
---|
| 37 | # apply some envelope |
---|
| 38 | float_ramp = arange(duration, dtype = 'float32') |
---|
| 39 | sinetone *= exp( - e * float_ramp / duration / decay) |
---|
| 40 | sinetone[:attack] *= exp( e * ( float_ramp[:attack] / attack - 1 ) ) |
---|
| 41 | |
---|
| 42 | if 0: |
---|
| 43 | import matplotlib.pyplot as plt |
---|
| 44 | plt.plot(sinetone) |
---|
| 45 | plt.show() |
---|
| 46 | |
---|
| 47 | my_sink = sink(sys.argv[1], samplerate) |
---|
| 48 | |
---|
| 49 | total_frames = 0 |
---|
| 50 | while total_frames + blocksize < duration: |
---|
| 51 | my_sink(sinetone[total_frames:total_frames+blocksize], blocksize) |
---|
| 52 | total_frames += blocksize |
---|
| 53 | my_sink(sinetone[total_frames:duration], duration - total_frames) |
---|