source: python/aubiocut @ 437fa65

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

add kl and mkl, change default threshold

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