source: python/demos/demo_onset_file.py @ 129b269

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

demos/demo_onset_file.py: indent, hide y-axis

  • Property mode set to 100755
File size: 2.2 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
9samplerate = 44100
10downsample = 2              # used to plot n samples / hop_s
11
12if len(sys.argv) < 2:
13    print "Usage: %s <filename>" % sys.argv[0]
14    sys.exit(1)
15
16filename = sys.argv[1]
17onsets = []
18
19s = source(filename, samplerate, hop_s)
20o = onset("default", win_s, hop_s)
21
22desc = []
23tdesc = []
24
25block_read = 0
26allsamples_max = zeros(0,)
27while True:
28    samples, read = s()
29    new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0)
30    allsamples_max = hstack([allsamples_max, new_maxes])
31    isbeat = o(samples)
32    desc.append(o.get_descriptor())
33    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
39    if read < hop_s: break
40
41if 1:
42    # do plotting
43    from numpy import arange
44    import matplotlib.pyplot as plt
45    allsamples_max = (allsamples_max > 0) * allsamples_max
46    allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ]
47    plt1 = plt.axes([0.1, 0.75, 0.8, 0.19])
48    plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1)
49    plt.rc('lines',linewidth='.8')
50    plt1.plot(allsamples_max_times,  allsamples_max, '-b')
51    plt1.plot(allsamples_max_times, -allsamples_max, '-b')
52    for stamp in onsets: plt1.plot([stamp, stamp], [-1., 1.], '-r')
53    plt1.axis(xmin = 0., xmax = max(allsamples_max_times) )
54    plt1.xaxis.set_visible(False)
55    plt1.yaxis.set_visible(False)
56    desc_times = [ float(t) * hop_s / samplerate for t in range(len(desc)) ]
57    desc_plot = [d / max(desc) for d in desc]
58    plt2.plot(desc_times, desc_plot, '-g')
59    tdesc_plot = [d / max(desc) for d in tdesc]
60    for stamp in onsets: plt2.plot([stamp, stamp], [min(tdesc_plot), max(desc_plot)], '-r')
61    plt2.plot(desc_times, tdesc_plot, '-y')
62    plt2.axis(ymin = min(tdesc_plot), ymax = max(desc_plot))
63    plt.xlabel('time (s)')
64    #plt.savefig('/tmp/t.png', dpi=200)
65    plt.show()
Note: See TracBrowser for help on using the repository browser.