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