History

demos/demo_onset_file.py: added onset example

Paul Brossier committed 13 years ago

Commit cf80e591 parent 08bc7141 file · +53 −0Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/demos/demo_onset_file.py+53−0
@@ -0,0 +1,53 @@
1+#! /usr/bin/env python
2+
3+import sys
4+from aubio import onset, source
5+from numpy import array, hstack, zeros
6+
7+win_s = 512 # fft size
8+hop_s = win_s / 2 # hop size
9+samplerate = 44100
10+downsample = 2 # used to plot n samples / hop_s
11+
12+if len(sys.argv) < 2:
13+ print "Usage: %s <filename>" % sys.argv[0]
14+ sys.exit(1)
15+
16+filename = sys.argv[1]
17+onsets = []
18+oldonsets = []
19+
20+s = source(filename, samplerate, hop_s)
21+o = onset("default", win_s, hop_s)
22+
23+block_read = 0
24+allsamples_max = zeros(0,)
25+while True:
26+ samples, read = s()
27+ new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0)
28+ allsamples_max = hstack([allsamples_max, new_maxes])
29+ isbeat = o(samples)
30+ if isbeat:
31+ thisbeat = (block_read - 4. + isbeat[0]) * hop_s / samplerate
32+ print "%.4f" % thisbeat
33+ onsets.append (thisbeat)
34+ # old onset
35+ thisbeat = (block_read - 3. ) * hop_s / samplerate
36+ oldonsets.append (thisbeat)
37+ block_read += 1
38+ if read < hop_s: break
39+
40+# do plotting
41+from numpy import arange
42+from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot, axis
43+allsamples_max = (allsamples_max > 0) * allsamples_max
44+allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ]
45+plot(allsamples_max_times, allsamples_max, '-b')
46+plot(allsamples_max_times, -allsamples_max, '-b')
47+axis(xmin = 0., xmax = max(allsamples_max_times) )
48+for stamp in onsets: plot([stamp, stamp], [-1., 1.], '.-r')
49+for stamp in oldonsets: plot([stamp, stamp], [-1., 1.], '.-g')
50+xlabel('time (s)')
51+ylabel('amplitude')
52+show()
53+

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t