Changeset 1e7a8f9 for python/demos


Ignore:
Timestamp:
Mar 9, 2013, 2:26:24 AM (12 years ago)
Author:
Paul Brossier <piem@piem.org>
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
Message:

demos/demo_sink_create_woodblock.py: table lookup to improve synthesis

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/demos/demo_sink_create_woodblock.py

    rdaa0d5d r1e7a8f9  
    22
    33import sys
    4 from math import sin, pi
     4from math import pi, e
    55from aubio import sink
    6 from numpy import array
     6from numpy import arange, resize, sin, exp, zeros
    77
    8 if len(sys.argv) != 2:
    9     print 'usage: %s <outputfile>' % sys.argv[0]
     8if len(sys.argv) < 2:
     9    print 'usage: %s <outputfile> [samplerate]' % sys.argv[0]
    1010    sys.exit(1)
    1111
    12 samplerate = 44100      # in Hz
     12samplerate = 44100 # samplerate in Hz
     13if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
     14
    1315pitch = 2200            # in Hz
    1416blocksize = 256         # in samples
     
    1719twopi = pi * 2.
    1820
    19 duration = int ( 44100 * duration ) # convert to samples
    20 attack = 3
     21duration = int ( samplerate * duration ) # convert to samples
     22attack = int (samplerate * .001 )
     23decay = .5
    2124
    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')
     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')
    2631
    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
     33for i in range(duration):
     34    x = int((i % period) / float(period) * tablelen)
     35    sinetone[i] = (sinetable[x] + sinetable[x+1]) / 2
    3236
    33 my_sink = sink(sys.argv[1], 44100)
     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 ) )
    3441
    35 i = 0
    36 while i + blocksize < duration:
    37     my_sink(sinetone[i:i+blocksize], blocksize)
    38     i += blocksize
     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 TracChangeset for help on using the changeset viewer.