Changeset e6f7a4a


Ignore:
Timestamp:
Mar 8, 2013, 4:12:17 PM (11 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:
459e46f
Parents:
92c83ccc
Message:

demos/demo_onset.py: add simple onset example, update demo_onset_plot.py

Location:
python/demos
Files:
1 added
1 moved

Legend:

Unmodified
Added
Removed
  • python/demos/demo_onset_plot.py

    r92c83ccc re6f7a4a  
    77win_s = 512                 # fft size
    88hop_s = win_s / 2           # hop size
    9 samplerate = 44100
    10 downsample = 2              # used to plot n samples / hop_s
    119
    1210if len(sys.argv) < 2:
    13     print "Usage: %s <filename>" % sys.argv[0]
     11    print "Usage: %s <filename> [samplerate]" % sys.argv[0]
    1412    sys.exit(1)
    1513
    1614filename = sys.argv[1]
     15
     16samplerate = 0
     17if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
     18
     19s = source(filename, samplerate, hop_s)
     20samplerate = s.samplerate
     21o = onset("default", win_s, hop_s)
     22
     23# onset detection delay, in blocks
     24delay = 4. * hop_s
     25
    1726onsets = []
    1827
    19 s = source(filename, samplerate, hop_s)
    20 o = onset("default", win_s, hop_s)
    21 
     28# storage for plotted data
    2229desc = []
    2330tdesc = []
     31allsamples_max = zeros(0,)
     32downsample = 2  # to plot n samples / hop_s
    2433
    25 block_read = 0
    26 allsamples_max = zeros(0,)
     34total_frames = 0
    2735while True:
    2836    samples, read = s()
     37    is_onset = o(samples)
     38    if is_onset:
     39        this_onset = int(total_frames - delay + is_onset[0] * hop_s)
     40        print "%f" % (this_onset / float(samplerate))
     41        onsets.append(this_onset / float(samplerate))
     42    # keep some data to plot it later
    2943    new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0)
    3044    allsamples_max = hstack([allsamples_max, new_maxes])
    31     isbeat = o(samples)
    3245    desc.append(o.get_descriptor())
    3346    tdesc.append(o.get_thresholded_descriptor())
    34     if isbeat:
    35         thisbeat = (block_read - 4. + isbeat[0]) * hop_s / samplerate
    36         print "%.4f" % thisbeat
    37         onsets.append (thisbeat)
    38     block_read += 1
     47    total_frames += read
    3948    if read < hop_s: break
    4049
Note: See TracChangeset for help on using the changeset viewer.