source: python/demos/demo_onset_file.py @ ae81726

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

demos/demo_onset_file.py: remove old onsets

  • Property mode set to 100755
File size: 1.4 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
22block_read = 0
23allsamples_max = zeros(0,)
24while True:
25    samples, read = s()
26    new_maxes = (abs(samples.reshape(hop_s/downsample, downsample))).max(axis=0)
27    allsamples_max = hstack([allsamples_max, new_maxes])
28    isbeat = o(samples)
29    if isbeat:
30        thisbeat = (block_read - 4. + isbeat[0]) * hop_s / samplerate
31        print "%.4f" % thisbeat
32        onsets.append (thisbeat)
33    block_read += 1
34    if read < hop_s: break
35
36# do plotting
37from numpy import arange
38from pylab import plot, show, xlabel, ylabel, legend, ylim, subplot, axis
39allsamples_max = (allsamples_max > 0) * allsamples_max
40allsamples_max_times = [ float(t) * hop_s / downsample / samplerate for t in range(len(allsamples_max)) ]
41plot(allsamples_max_times,  allsamples_max, '-b')
42plot(allsamples_max_times, -allsamples_max, '-b')
43axis(xmin = 0., xmax = max(allsamples_max_times) )
44for stamp in onsets: plot([stamp, stamp], [-1., 1.], '.-r')
45xlabel('time (s)')
46ylabel('amplitude')
47show()
48
Note: See TracBrowser for help on using the repository browser.