source: python/aubiocut @ 80c0417

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

merged aubioplot-onsets into aubiocut, added some more options

  • Property mode set to 100755
File size: 6.2 KB
Line 
1#! /usr/bin/python
2
3""" this file was written by Paul Brossier
4  it is released under the GNU/GPL license.
5"""
6
7import sys
8import numarray
9from aubio.aubioclass import *
10
11usage = "usage: %s [options] -i soundfile" % sys.argv[0]
12
13def check_mode(option, opt, value, parser):
14        nvalue = parser.rargs[0]
15        if   nvalue == 'complexdomain' :
16                 setattr(parser.values, option.dest, complexdomain)
17        elif nvalue == 'hfc'           :
18                 setattr(parser.values, option.dest, hfc)
19        elif nvalue == 'phase'         :
20                 setattr(parser.values, option.dest, phase)
21        elif nvalue == 'specdiff'      :
22                 setattr(parser.values, option.dest, specdiff)
23        elif nvalue == 'energy'        :
24                 setattr(parser.values, option.dest, energy)
25        elif nvalue == 'dual'          :
26                 setattr(parser.values, option.dest, 'dual')
27
28def parse_args():
29        from optparse import OptionParser
30        parser = OptionParser(usage=usage)
31        parser.add_option("-i","--input",
32                          action="store", dest="filename",
33                          help="input sound file")
34        parser.add_option("-m","--mode", action="callback",
35                          callback=check_mode, dest="mode", default='dual',
36                          help="onset detection mode [default=dual] \
37                          complexdomain|hfc|phase|specdiff|energy|dual")
38        parser.add_option("-B","--bufsize",
39                          action="store", dest="bufsize", default=1024,
40                          help="buffer size [default=1024]")
41        parser.add_option("-H","--hopsize",
42                          action="store", dest="hopsize", default=512,
43                          help="overlap size [default=512]")
44        parser.add_option("-t","--threshold",
45                          action="store", dest="threshold", default=0.3,
46                          help="onset peak picking threshold [default=0.3]")
47        parser.add_option("-s","--silence",
48                          action="store", dest="silence", default=-70,
49                          help="silence threshold [default=-70]")
50        parser.add_option("-M","--mintol",
51                          action="store", dest="mintol", default=0.048,
52                          help="minimum inter onset interval [default=0.048]")
53        parser.add_option("-D","--delay",
54                          action="store", dest="delay", 
55                          help="number of seconds to take back [default=system]\
56                          default system delay is 2*hopsize/samplerate")
57        parser.add_option("-L","--localmin",
58                          action="store_true", dest="localmin", default=False,
59                          help="use local minima after peak detection")
60        parser.add_option("-c","--cut",
61                          action="store_true", dest="cut", default=False,
62                          help="cut input sound file at detected labels \
63                          best used with option -L")
64        parser.add_option("-d","--derivate",
65                          action="store_true", dest="derivate", default=False,
66                          help="derivate onset detection function")
67        # to be implemented
68        parser.add_option("-z","--zerocross",
69                          action="store_true", dest="zerocross", default=False,
70                          help="NOT IMPLEMENTED zero crossing matching")
71        parser.add_option("-b","--beat",
72                          action="store_true", dest="beat", default=False,
73                          help="NOT IMPLEMENTED output beat locations")
74        # plotting functions
75        parser.add_option("-p","--plot",
76                          action="store_true", dest="plot", default=False,
77                          help="draw plot")
78        parser.add_option("-O","--outplot",
79                          action="store", dest="outplot", default=None,
80                          help="save plot to output.{ps,png}")
81        parser.add_option("-v","--verbose",
82                          action="store_true", dest="verbose", default=False,
83                          help="make lots of noise [default]")
84        parser.add_option("-q","--quiet",
85                          action="store_false", dest="verbose", default=False,
86                          help="be quiet")
87        (options, args) = parser.parse_args()
88        if not options.filename:
89                 print "no file name given\n", usage
90                 sys.exit(1)
91        return options, args
92
93options, args = parse_args()
94
95filename   = options.filename
96samplerate = float(sndfile(filename).samplerate())
97hopsize    = int(options.hopsize)
98bufsize    = int(options.bufsize)
99step       = float(samplerate)/float(hopsize)
100threshold  = float(options.threshold)
101silence    = float(options.silence)
102mintol     = float(options.mintol)*step
103# default take back system delay
104if options.delay: delay = float(options.delay)
105else:             delay = 2./step
106
107if options.beat:
108        #onsets = getbeats(filename,threshold,silence,mode=options.mode)
109        exit("not implemented yet")
110elif options.plot:
111        onsets, ofunc = getonsets(filename,threshold,silence,
112                mode=options.mode,localmin=options.localmin,
113                derivate=options.derivate,
114                bufsize=bufsize,hopsize=hopsize,storefunc=True)
115else:
116        onsets = getonsets(filename,threshold,silence,
117                mode=options.mode,localmin=options.localmin,
118                derivate=options.derivate,
119                bufsize=bufsize,hopsize=hopsize)
120
121# take back system delay
122if delay != 0:
123        for i in range(len(onsets)):
124                onsets[i] -= delay*step
125
126# prune doubled
127if mintol > 0:
128        last = -2*mintol
129        newonsets = []
130        for new in onsets:
131                if (new - last > mintol):
132                        newonsets.append(new)
133                last = new
134        onsets = newonsets
135
136# print times in second
137if options.verbose:
138        for i in onsets: print "%f" % (i/step)
139
140if options.plot:
141        from aubio.gnuplot import plot_onsets
142        plot_onsets(filename, onsets, ofunc,
143                samplerate=samplerate, hopsize=hopsize, outplot=options.outplot)
144
145if options.cut:
146        cutfile(filename,onsets,bufsize=bufsize,hopsize=hopsize)
Note: See TracBrowser for help on using the repository browser.