source: python/demos/demo_sink_create_woodblock.py @ f3617e7

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since f3617e7 was 1e7a8f9, checked in by Paul Brossier <piem@piem.org>, 12 years ago

demos/demo_sink_create_woodblock.py: table lookup to improve synthesis

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