[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") |
---|
| 57 | """ |
---|
| 58 | parser.add_option("-D","--delay", |
---|
| 59 | action = "store", dest = "delay", type = "float", |
---|
| 60 | metavar = "<seconds>", default=0, |
---|
| 61 | help="number of seconds to take back [default=system]\ |
---|
| 62 | default system delay is 3*hopsize/samplerate") |
---|
| 63 | parser.add_option("-C","--dcthreshold", |
---|
| 64 | metavar = "<value>", |
---|
| 65 | action="store", dest="dcthreshold", default=1., |
---|
| 66 | help="onset peak picking DC component [default=1.]") |
---|
| 67 | parser.add_option("-M","--mintol", |
---|
| 68 | metavar = "<value>", |
---|
| 69 | action="store", dest="mintol", default=0.048, |
---|
| 70 | help="minimum inter onset interval [default=0.048]") |
---|
| 71 | parser.add_option("-L","--localmin", |
---|
| 72 | action="store_true", dest="localmin", default=False, |
---|
| 73 | help="use local minima after peak detection") |
---|
| 74 | parser.add_option("-d","--derivate", |
---|
| 75 | action="store_true", dest="derivate", default=False, |
---|
| 76 | help="derivate onset detection function") |
---|
| 77 | parser.add_option("-z","--zerocross", |
---|
| 78 | metavar = "<value>", |
---|
| 79 | action="store", dest="zerothres", default=0.008, |
---|
| 80 | help="zero-crossing threshold for slicing [default=0.00008]") |
---|
| 81 | """ |
---|
| 82 | # plotting functions |
---|
| 83 | """ |
---|
| 84 | parser.add_option("-p","--plot", |
---|
| 85 | action="store_true", dest="plot", default=False, |
---|
| 86 | help="draw plot") |
---|
| 87 | parser.add_option("-x","--xsize", |
---|
| 88 | metavar = "<size>", |
---|
| 89 | action="store", dest="xsize", default=1., |
---|
| 90 | type='float', help="define xsize for plot") |
---|
| 91 | parser.add_option("-y","--ysize", |
---|
| 92 | metavar = "<size>", |
---|
| 93 | action="store", dest="ysize", default=1., |
---|
| 94 | type='float', help="define ysize for plot") |
---|
| 95 | parser.add_option("-f","--function", |
---|
| 96 | action="store_true", dest="func", default=False, |
---|
| 97 | help="print detection function") |
---|
| 98 | parser.add_option("-n","--no-onsets", |
---|
| 99 | action="store_true", dest="nplot", default=False, |
---|
| 100 | help="do not plot detected onsets") |
---|
| 101 | parser.add_option("-O","--outplot", |
---|
| 102 | metavar = "<output_image>", |
---|
| 103 | action="store", dest="outplot", default=None, |
---|
| 104 | help="save plot to output.{ps,png}") |
---|
| 105 | parser.add_option("-F","--spectrogram", |
---|
| 106 | action="store_true", dest="spectro", default=False, |
---|
| 107 | help="add spectrogram to the plot") |
---|
| 108 | """ |
---|
[3f9e8e5] | 109 | parser.add_option("-o","--output", type = str, |
---|
| 110 | metavar = "<outputdir>", |
---|
| 111 | action="store", dest="output_directory", default=None, |
---|
| 112 | help="specify path where slices of the original file should be created") |
---|
[9582713] | 113 | parser.add_option("-v","--verbose", |
---|
| 114 | action="store_true", dest="verbose", default=True, |
---|
| 115 | help="make lots of noise [default]") |
---|
| 116 | parser.add_option("-q","--quiet", |
---|
| 117 | action="store_false", dest="verbose", default=True, |
---|
| 118 | help="be quiet") |
---|
| 119 | (options, args) = parser.parse_args() |
---|
| 120 | if not options.source_file: |
---|
[1e1a2c9] | 121 | import os.path |
---|
| 122 | if len(args) == 1: |
---|
| 123 | options.source_file = args[0] |
---|
| 124 | else: |
---|
| 125 | print "no file name given\n", usage |
---|
| 126 | sys.exit(1) |
---|
[9582713] | 127 | return options, args |
---|
| 128 | |
---|
| 129 | if __name__ == '__main__': |
---|
| 130 | options, args = parse_args() |
---|
| 131 | |
---|
| 132 | hopsize = options.hopsize |
---|
| 133 | bufsize = options.bufsize |
---|
| 134 | samplerate = options.samplerate |
---|
| 135 | source_file = options.source_file |
---|
| 136 | |
---|
[7fc5ba2] | 137 | from aubio import onset, tempo, source, sink |
---|
[9582713] | 138 | |
---|
| 139 | s = source(source_file, samplerate, hopsize) |
---|
| 140 | if samplerate == 0: samplerate = s.get_samplerate() |
---|
| 141 | |
---|
[7fc5ba2] | 142 | if options.beat: |
---|
| 143 | o = tempo(options.onset_method, bufsize, hopsize) |
---|
| 144 | else: |
---|
| 145 | o = onset(options.onset_method, bufsize, hopsize) |
---|
[9582713] | 146 | o.set_threshold(options.threshold) |
---|
| 147 | |
---|
| 148 | timestamps = [] |
---|
[dee4164] | 149 | total_frames = 0 |
---|
| 150 | # analyze pass |
---|
[9582713] | 151 | while True: |
---|
| 152 | samples, read = s() |
---|
[dee4164] | 153 | if o(samples): |
---|
[7fc5ba2] | 154 | timestamps.append (o.get_last()) |
---|
| 155 | if options.verbose: print "%.4f" % o.get_last_s() |
---|
[dee4164] | 156 | total_frames += read |
---|
[9582713] | 157 | if read < hopsize: break |
---|
[7fc5ba2] | 158 | del s |
---|
[9582713] | 159 | # print some info |
---|
| 160 | nstamps = len(timestamps) |
---|
[dee4164] | 161 | duration = float (total_frames) / float(samplerate) |
---|
[9582713] | 162 | info = 'found %(nstamps)d timestamps in %(source_file)s' % locals() |
---|
[dee4164] | 163 | info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() |
---|
[9582713] | 164 | sys.stderr.write(info) |
---|
[dee4164] | 165 | |
---|
| 166 | # cutting pass |
---|
| 167 | if options.cut and nstamps > 0: |
---|
| 168 | # generate output filenames |
---|
| 169 | import os |
---|
| 170 | source_base_name, source_ext = os.path.splitext(os.path.basename(source_file)) |
---|
[3f9e8e5] | 171 | if options.output_directory != None: |
---|
| 172 | if not os.path.isdir(options.output_directory): |
---|
| 173 | os.makedirs(options.output_directory) |
---|
| 174 | source_base_name = os.path.join(options.output_directory, source_base_name) |
---|
[dee4164] | 175 | def new_sink_name(source_base_name, timestamp): |
---|
| 176 | return source_base_name + '_%02.3f' % (timestamp) + '.wav' |
---|
| 177 | # reopen source file |
---|
| 178 | s = source(source_file, samplerate, hopsize) |
---|
[7fc5ba2] | 179 | if samplerate == 0: samplerate = s.get_samplerate() |
---|
[dee4164] | 180 | # create first sink at 0 |
---|
| 181 | g = sink(new_sink_name(source_base_name, 0.), samplerate) |
---|
| 182 | total_frames = 0 |
---|
| 183 | # get next region |
---|
[9d1606d] | 184 | next_stamp = int(timestamps.pop(0)) |
---|
[dee4164] | 185 | while True: |
---|
| 186 | vec, read = s() |
---|
[9d1606d] | 187 | remaining = next_stamp - total_frames |
---|
[dee4164] | 188 | if remaining <= read: |
---|
| 189 | # write remaining samples from current region |
---|
| 190 | g(vec[0:remaining], remaining) |
---|
| 191 | # close this file |
---|
| 192 | del g |
---|
| 193 | # create a new file for the new region |
---|
[9d1606d] | 194 | g = sink(new_sink_name(source_base_name, next_stamp / float(samplerate)), samplerate) |
---|
[dee4164] | 195 | # write the remaining samples in the new file |
---|
| 196 | g(vec[remaining:read], read - remaining) |
---|
| 197 | #print "new slice", total_frames_written, "+", remaining, "=", start_of_next_region |
---|
| 198 | if len(timestamps): |
---|
[9d1606d] | 199 | next_stamp = int(timestamps.pop(0)) |
---|
[dee4164] | 200 | else: |
---|
[9d1606d] | 201 | next_stamp = 1e120 |
---|
[dee4164] | 202 | else: |
---|
| 203 | g(vec[0:read], read) |
---|
| 204 | total_frames += read |
---|
| 205 | if read < hopsize: break |
---|
| 206 | |
---|
| 207 | # print some info |
---|
| 208 | duration = float (total_frames) / float(samplerate) |
---|
| 209 | info = 'created %(nstamps)d slices from %(source_file)s' % locals() |
---|
| 210 | info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % locals() |
---|
| 211 | sys.stderr.write(info) |
---|