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
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 = []
18oldonsets = []
19
20s = source(filename, samplerate, hop_s)
21o = onset("default", win_s, hop_s)
22
23block_read = 0
24allsamples_max = zeros(0,)
25while 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
41from numpy import arange
42from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot, axis
43allsamples_max = (allsamples_max > 0) * allsamples_max
44allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ]
45plot(allsamples_max_times, allsamples_max, '-b')
46plot(allsamples_max_times, -allsamples_max, '-b')
47axis(xmin = 0., xmax = max(allsamples_max_times) )
48for stamp in onsets: plot([stamp, stamp], [-1., 1.], '.-r')
49for stamp in oldonsets: plot([stamp, stamp], [-1., 1.], '.-g')
50xlabel('time (s)')
51ylabel('amplitude')
52show()
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