source: python/demos/demo_onset_plot.py @ e6f7a4a

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

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

  • Property mode set to 100755
File size: 2.4 KB
Line 
1#! /usr/bin/env python
2
3import sys
4from aubio import onset, source
5from numpy import array, hstack, zeros
6
7win_s = 512                 # fft size
8hop_s = win_s / 2           # hop size
9
10if len(sys.argv) < 2:
11    print "Usage: %s <filename> [samplerate]" % sys.argv[0]
12    sys.exit(1)
13
14filename = 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
26onsets = []
27
28# storage for plotted data
29desc = []
30tdesc = []
31allsamples_max = zeros(0,)
32downsample = 2  # to plot n samples / hop_s
33
34total_frames = 0
35while True:
36    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
43    new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0)
44    allsamples_max = hstack([allsamples_max, new_maxes])
45    desc.append(o.get_descriptor())
46    tdesc.append(o.get_thresholded_descriptor())
47    total_frames += read
48    if read < hop_s: break
49
50if 1:
51    # do plotting
52    from numpy import arange
53    import matplotlib.pyplot as plt
54    allsamples_max = (allsamples_max > 0) * allsamples_max
55    allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ]
56    plt1 = plt.axes([0.1, 0.75, 0.8, 0.19])
57    plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1)
58    plt.rc('lines',linewidth='.8')
59    plt1.plot(allsamples_max_times,  allsamples_max, '-b')
60    plt1.plot(allsamples_max_times, -allsamples_max, '-b')
61    for stamp in onsets: plt1.plot([stamp, stamp], [-1., 1.], '-r')
62    plt1.axis(xmin = 0., xmax = max(allsamples_max_times) )
63    plt1.xaxis.set_visible(False)
64    plt1.yaxis.set_visible(False)
65    desc_times = [ float(t) * hop_s / samplerate for t in range(len(desc)) ]
66    desc_plot = [d / max(desc) for d in desc]
67    plt2.plot(desc_times, desc_plot, '-g')
68    tdesc_plot = [d / max(desc) for d in tdesc]
69    for stamp in onsets: plt2.plot([stamp, stamp], [min(tdesc_plot), max(desc_plot)], '-r')
70    plt2.plot(desc_times, tdesc_plot, '-y')
71    plt2.axis(ymin = min(tdesc_plot), ymax = max(desc_plot))
72    plt.xlabel('time (s)')
73    #plt.savefig('/tmp/t.png', dpi=200)
74    plt.show()
Note: See TracBrowser for help on using the repository browser.