source: python/demos/demo_onset_file.py @ cf80e59

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

demos/demo_onset_file.py: added onset example

  • Property mode set to 100755
File size: 1.6 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 = []
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
Note: See TracBrowser for help on using the repository browser.