source: python/demos/demo_sink_create_woodblock.py @ 5ce504d

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 5ce504d was 4120fbc, checked in by Paul Brossier <piem@piem.org>, 9 years ago

python/demos: python3 and double precision compatibility

  • Property mode set to 100755
File size: 1.6 KB
Line 
1#! /usr/bin/env python
2
3import sys
4from math import pi, e
5from aubio import sink, float_type
6from numpy import arange, 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 = float_type)
29sinetable = 0.7 * sin(twopi * sinetable/tablelen)
30sinetone = zeros((duration,), dtype = float_type)
31
32# compute sinetone at floating point period
33for i in range(duration):
34    x = int((i % period) / float(period) * tablelen)
35    idx = int(x)
36    frac = x - idx
37    a = sinetable[idx]
38    b = sinetable[idx + 1]
39    sinetone[i] = a + frac * (b -a)
40
41# apply some envelope
42float_ramp = arange(duration, dtype = float_type)
43sinetone *= exp( - e * float_ramp / duration / decay)
44sinetone[:attack] *= exp( e * ( float_ramp[:attack] / attack - 1 ) )
45
46if 1:
47    import matplotlib.pyplot as plt
48    plt.plot(sinetone)
49    plt.show()
50
51my_sink = sink(sys.argv[1], samplerate)
52
53total_frames = 0
54while total_frames + blocksize < duration:
55    my_sink(sinetone[total_frames:total_frames+blocksize], blocksize)
56    total_frames += blocksize
57my_sink(sinetone[total_frames:duration], duration - total_frames)
Note: See TracBrowser for help on using the repository browser.