Changeset 1e7a8f9 for python/demos
- Timestamp:
- Mar 9, 2013, 2:26:24 AM (12 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- 93acd9f
- Parents:
- daa0d5d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/demos/demo_sink_create_woodblock.py
rdaa0d5d r1e7a8f9 2 2 3 3 import sys 4 from math import sin, pi4 from math import pi, e 5 5 from aubio import sink 6 from numpy import ar ray6 from numpy import arange, resize, sin, exp, zeros 7 7 8 if len(sys.argv) !=2:9 print 'usage: %s <outputfile> ' % sys.argv[0]8 if len(sys.argv) < 2: 9 print 'usage: %s <outputfile> [samplerate]' % sys.argv[0] 10 10 sys.exit(1) 11 11 12 samplerate = 44100 # in Hz 12 samplerate = 44100 # samplerate in Hz 13 if len( sys.argv ) > 2: samplerate = int(sys.argv[2]) 14 13 15 pitch = 2200 # in Hz 14 16 blocksize = 256 # in samples … … 17 19 twopi = pi * 2. 18 20 19 duration = int ( 44100 * duration ) # convert to samples 20 attack = 3 21 duration = int ( samplerate * duration ) # convert to samples 22 attack = int (samplerate * .001 ) 23 decay = .5 21 24 22 period = int ( float(samplerate) / pitch ) 23 sinetone = [ 0.7 * sin(twopi * i/ period) for i in range(period) ] 24 sinetone *= int ( duration / period ) 25 sinetone = array(sinetone, dtype = 'float32') 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') 26 31 27 from math import exp, e 28 for i in range(len(sinetone)): 29 sinetone[i] *= exp( - e * float(i) / len(sinetone)) 30 for i in range(attack): 31 sinetone[i] *= exp( e * (float(i) / attack - 1 ) ) 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 32 36 33 my_sink = sink(sys.argv[1], 44100) 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 ) ) 34 41 35 i = 0 36 while i + blocksize < duration: 37 my_sink(sinetone[i:i+blocksize], blocksize) 38 i += blocksize 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)
Note: See TracChangeset
for help on using the changeset viewer.