source: python/demos/demo_onset_file.py @ a4cc8e5

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

python/demos/demo_onset_file.py: also plot descriptor and thresholded descriptor

  • Property mode set to 100755
File size: 2.0 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
41# do plotting
42from numpy import arange
43import matplotlib.pyplot as plt
44allsamples_max = (allsamples_max > 0) * allsamples_max
45allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ]
46plt1 = plt.axes([0.1, 0.75, 0.8, 0.19])
47plt2 = plt.axes([0.1, 0.1, 0.8, 0.65], sharex = plt1)
48plt.rc('lines',linewidth='.8')
49plt1.plot(allsamples_max_times,  allsamples_max, '-b')
50plt1.plot(allsamples_max_times, -allsamples_max, '-b')
51for stamp in onsets: plt1.plot([stamp, stamp], [-1., 1.], '-r')
52plt1.axis(xmin = 0., xmax = max(allsamples_max_times) )
53plt1.xaxis.set_visible(False)
54desc_times = [ float(t) * hop_s / samplerate for t in range(len(desc)) ]
55desc_plot = [d / max(desc) for d in desc]
56plt2.plot(desc_times, desc_plot, '-g')
57tdesc_plot = [d / max(desc) for d in tdesc]
58for stamp in onsets: plt2.plot([stamp, stamp], [min(tdesc_plot), max(desc_plot)], '-r')
59plt2.plot(desc_times, tdesc_plot, '-y')
60plt2.axis(ymin = min(tdesc_plot), ymax = max(desc_plot))
61plt.xlabel('time (s)')
62#plt.savefig('/tmp/t.png', dpi=200)
63plt.show()
Note: See TracBrowser for help on using the repository browser.