[9582713] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | """ this file was written by Paul Brossier |
---|
| 4 | it is released under the GNU/GPL license. |
---|
| 5 | """ |
---|
| 6 | |
---|
| 7 | import sys |
---|
| 8 | #from aubio.task import * |
---|
| 9 | |
---|
| 10 | usage = "usage: %s [options] -i soundfile" % sys.argv[0] |
---|
[7fc5ba2] | 11 | usage += "\n help: %s -h" % sys.argv[0] |
---|
[9582713] | 12 | |
---|
| 13 | def parse_args(): |
---|
| 14 | from optparse import OptionParser |
---|
| 15 | parser = OptionParser(usage=usage) |
---|
| 16 | parser.add_option("-i", "--input", action = "store", dest = "source_file", |
---|
| 17 | help="input sound file to analyse", metavar = "<source_file>") |
---|
[49e40cc] | 18 | parser.add_option("-O","--onset-method", |
---|
[9582713] | 19 | action="store", dest="onset_method", default='default', |
---|
| 20 | metavar = "<onset_method>", |
---|
| 21 | help="onset detection method [default=default] \ |
---|
| 22 | complexdomain|hfc|phase|specdiff|energy|kl|mkl") |
---|
| 23 | # cutting methods |
---|
| 24 | parser.add_option("-b","--beat", |
---|
| 25 | action="store_true", dest="beat", default=False, |
---|
| 26 | help="use beat locations") |
---|
[7fc5ba2] | 27 | """ |
---|
[9582713] | 28 | parser.add_option("-S","--silencecut", |
---|
| 29 | action="store_true", dest="silencecut", default=False, |
---|
| 30 | help="use silence locations") |
---|
| 31 | parser.add_option("-s","--silence", |
---|
| 32 | metavar = "<value>", |
---|
| 33 | action="store", dest="silence", default=-70, |
---|
| 34 | help="silence threshold [default=-70]") |
---|
| 35 | """ |
---|
| 36 | # algorithm parameters |
---|
[49e40cc] | 37 | parser.add_option("-r", "--samplerate", |
---|
[9582713] | 38 | metavar = "<freq>", type='int', |
---|
| 39 | action="store", dest="samplerate", default=0, |
---|
| 40 | help="samplerate at which the file should be represented") |
---|
| 41 | parser.add_option("-B","--bufsize", |
---|
| 42 | action="store", dest="bufsize", default=512, |
---|
[7fc5ba2] | 43 | metavar = "<size>", type='int', |
---|
[9582713] | 44 | help="buffer size [default=512]") |
---|
| 45 | parser.add_option("-H","--hopsize", |
---|
[7fc5ba2] | 46 | metavar = "<size>", type='int', |
---|
[9582713] | 47 | action="store", dest="hopsize", default=256, |
---|
| 48 | help="overlap size [default=256]") |
---|
[49e40cc] | 49 | parser.add_option("-t","--onset-threshold", |
---|
[9582713] | 50 | metavar = "<value>", type="float", |
---|
| 51 | action="store", dest="threshold", default=0.3, |
---|
| 52 | help="onset peak picking threshold [default=0.3]") |
---|
| 53 | parser.add_option("-c","--cut", |
---|
| 54 | action="store_true", dest="cut", default=False, |
---|
| 55 | help="cut input sound file at detected labels \ |
---|
| 56 | best used with option -L") |
---|
[4c0a1db] | 57 | |
---|
| 58 | # minioi |
---|
| 59 | parser.add_option("-M","--minioi", |
---|
| 60 | metavar = "<value>", type='string', |
---|
| 61 | action="store", dest="minioi", default="12ms", |
---|
| 62 | help="minimum inter onset interval [default=12ms]") |
---|
| 63 | |
---|
[9582713] | 64 | """ |
---|
| 65 | parser.add_option("-D","--delay", |
---|
| 66 | action = "store", dest = "delay", type = "float", |
---|
| 67 | metavar = "<seconds>", default=0, |
---|
| 68 | help="number of seconds to take back [default=system]\ |
---|
| 69 | default system delay is 3*hopsize/samplerate") |
---|
| 70 | parser.add_option("-C","--dcthreshold", |
---|
| 71 | metavar = "<value>", |
---|
| 72 | action="store", dest="dcthreshold", default=1., |
---|
| 73 | help="onset peak picking DC component [default=1.]") |
---|
| 74 | parser.add_option("-L","--localmin", |
---|
| 75 | action="store_true", dest="localmin", default=False, |
---|
| 76 | help="use local minima after peak detection") |
---|
| 77 | parser.add_option("-d","--derivate", |
---|
| 78 | action="store_true", dest="derivate", default=False, |
---|
| 79 | help="derivate onset detection function") |
---|
| 80 | parser.add_option("-z","--zerocross", |
---|
| 81 | metavar = "<value>", |
---|
| 82 | action="store", dest="zerothres", default=0.008, |
---|
| 83 | help="zero-crossing threshold for slicing [default=0.00008]") |
---|
| 84 | """ |
---|
| 85 | # plotting functions |
---|
| 86 | """ |
---|
| 87 | parser.add_option("-p","--plot", |
---|
| 88 | action="store_true", dest="plot", default=False, |
---|
| 89 | help="draw plot") |
---|
| 90 | parser.add_option("-x","--xsize", |
---|
| 91 | metavar = "<size>", |
---|
| 92 | action="store", dest="xsize", default=1., |
---|
| 93 | type='float', help="define xsize for plot") |
---|
| 94 | parser.add_option("-y","--ysize", |
---|
| 95 | metavar = "<size>", |
---|
| 96 | action="store", dest="ysize", default=1., |
---|
| 97 | type='float', help="define ysize for plot") |
---|
| 98 | parser.add_option("-f","--function", |
---|
| 99 | action="store_true", dest="func", default=False, |
---|
| 100 | help="print detection function") |
---|
| 101 | parser.add_option("-n","--no-onsets", |
---|
| 102 | action="store_true", dest="nplot", default=False, |
---|
| 103 | help="do not plot detected onsets") |
---|
| 104 | parser.add_option("-O","--outplot", |
---|
| 105 | metavar = "<output_image>", |
---|
| 106 | action="store", dest="outplot", default=None, |
---|
| 107 | help="save plot to output.{ps,png}") |
---|
| 108 | parser.add_option("-F","--spectrogram", |
---|
| 109 | action="store_true", dest="spectro", default=False, |
---|
| 110 | help="add spectrogram to the plot") |
---|
| 111 | """ |
---|
[3f9e8e5] | 112 | parser.add_option("-o","--output", type = str, |
---|
| 113 | metavar = "<outputdir>", |
---|
| 114 | action="store", dest="output_directory", default=None, |
---|
| 115 | help="specify path where slices of the original file should be created") |
---|
[94b16e89] | 116 | parser.add_option("--cut-until-nsamples", type = int, |
---|
| 117 | metavar = "<samples>", |
---|
| 118 | action = "store", dest = "cut_until_nsamples", default = None, |
---|
| 119 | help="how many extra samples should be added at the end of each slice") |
---|
| 120 | parser.add_option("--cut-until-nslices", type = int, |
---|
| 121 | metavar = "<slices>", |
---|
| 122 | action = "store", dest = "cut_until_nslices", default = None, |
---|
| 123 | help="how many extra slices should be added at the end of each slice") |
---|
| 124 | |
---|
[9582713] | 125 | parser.add_option("-v","--verbose", |
---|
| 126 | action="store_true", dest="verbose", default=True, |
---|
| 127 | help="make lots of noise [default]") |
---|
| 128 | parser.add_option("-q","--quiet", |
---|
| 129 | action="store_false", dest="verbose", default=True, |
---|
| 130 | help="be quiet") |
---|
| 131 | (options, args) = parser.parse_args() |
---|
| 132 | if not options.source_file: |
---|
[1e1a2c9] | 133 | import os.path |
---|
| 134 | if len(args) == 1: |
---|
| 135 | options.source_file = args[0] |
---|
| 136 | else: |
---|
[681663e] | 137 | print ("no file name given\n" + usage) |
---|
[1e1a2c9] | 138 | sys.exit(1) |
---|
[9582713] | 139 | return options, args |
---|
| 140 | |
---|
| 141 | if __name__ == '__main__': |
---|
| 142 | options, args = parse_args() |
---|
| 143 | |
---|
| 144 | hopsize = options.hopsize |
---|
| 145 | bufsize = options.bufsize |
---|
| 146 | samplerate = options.samplerate |
---|
| 147 | source_file = options.source_file |
---|
| 148 | |
---|
[7fc5ba2] | 149 | from aubio import onset, tempo, source, sink |
---|
[9582713] | 150 | |
---|
| 151 | s = source(source_file, samplerate, hopsize) |
---|
| 152 | if samplerate == 0: samplerate = s.get_samplerate() |
---|
| 153 | |
---|
[7fc5ba2] | 154 | if options.beat: |
---|
| 155 | o = tempo(options.onset_method, bufsize, hopsize) |
---|
| 156 | else: |
---|
| 157 | o = onset(options.onset_method, bufsize, hopsize) |
---|
[4c0a1db] | 158 | if options.minioi: |
---|
| 159 | if options.minioi.endswith('ms'): |
---|
| 160 | o.set_minioi_ms(int(options.minioi[:-2])) |
---|
| 161 | elif options.minioi.endswith('s'): |
---|
| 162 | o.set_minioi_s(int(options.minioi[:-1])) |
---|
| 163 | else: |
---|
| 164 | o.set_minioi(int(options.minioi)) |
---|
[9582713] | 165 | o.set_threshold(options.threshold) |
---|
| 166 | |
---|
| 167 | timestamps = [] |
---|
[dee4164] | 168 | total_frames = 0 |
---|
| 169 | # analyze pass |
---|
[9582713] | 170 | while True: |
---|
| 171 | samples, read = s() |
---|
[dee4164] | 172 | if o(samples): |
---|
[7fc5ba2] | 173 | timestamps.append (o.get_last()) |
---|
[1c6fe64] | 174 | if options.verbose: print ("%.4f" % o.get_last_s()) |
---|
[dee4164] | 175 | total_frames += read |
---|
[9582713] | 176 | if read < hopsize: break |
---|
[7fc5ba2] | 177 | del s |
---|
[9582713] | 178 | # print some info |
---|
| 179 | nstamps = len(timestamps) |
---|
[dee4164] | 180 | duration = float (total_frames) / float(samplerate) |
---|
[9582713] | 181 | info = 'found %(nstamps)d timestamps in %(source_file)s' % locals() |
---|
[dee4164] | 182 | info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() |
---|
[9582713] | 183 | sys.stderr.write(info) |
---|
[dee4164] | 184 | |
---|
| 185 | # cutting pass |
---|
| 186 | if options.cut and nstamps > 0: |
---|
[94b16e89] | 187 | # generate output files |
---|
[bc24e9c] | 188 | from aubio.slicing import slice_source_at_stamps |
---|
[94b16e89] | 189 | timestamps_end = None |
---|
| 190 | if options.cut_until_nslices and options.cut_until_nsamples: |
---|
[1c6fe64] | 191 | print ("warning: using cut_until_nslices, but cut_until_nsamples is set") |
---|
[94b16e89] | 192 | if options.cut_until_nsamples: |
---|
| 193 | timestamps_end = [t + options.cut_until_nsamples for t in timestamps[1:]] |
---|
| 194 | timestamps_end += [ 1e120 ] |
---|
| 195 | if options.cut_until_nslices: |
---|
| 196 | timestamps_end = [t for t in timestamps[1 + options.cut_until_nslices:]] |
---|
| 197 | timestamps_end += [ 1e120 ] * (options.cut_until_nslices + 1) |
---|
| 198 | slice_source_at_stamps(source_file, timestamps, timestamps_end = timestamps_end, |
---|
[a2ca72a] | 199 | output_dir = options.output_directory, |
---|
| 200 | samplerate = samplerate) |
---|
[dee4164] | 201 | |
---|
| 202 | # print some info |
---|
| 203 | duration = float (total_frames) / float(samplerate) |
---|
| 204 | info = 'created %(nstamps)d slices from %(source_file)s' % locals() |
---|
| 205 | info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() |
---|
| 206 | sys.stderr.write(info) |
---|