[1d2cc5e] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | """aubio command line tool |
---|
| 5 | |
---|
| 6 | This file was written by Paul Brossier <piem@aubio.org> and is released under |
---|
| 7 | the GNU/GPL v3. |
---|
| 8 | |
---|
| 9 | Note: this script is mostly about parsing command line arguments. For more |
---|
| 10 | readable code examples, check out the `python/demos` folder.""" |
---|
| 11 | |
---|
| 12 | import sys |
---|
| 13 | import argparse |
---|
[dc74f69] | 14 | import warnings |
---|
[1d2cc5e] | 15 | import aubio |
---|
| 16 | |
---|
| 17 | def aubio_parser(): |
---|
| 18 | epilog = 'use "%(prog)s <command> --help" for more info about each command' |
---|
| 19 | parser = argparse.ArgumentParser(epilog=epilog) |
---|
| 20 | parser.add_argument('-V', '--version', help="show version", |
---|
| 21 | action="store_true", dest="show_version") |
---|
| 22 | |
---|
[2615dd9] | 23 | subparsers = parser.add_subparsers(title='commands', dest='command', |
---|
[c95062b] | 24 | parser_class= AubioArgumentParser, |
---|
[2615dd9] | 25 | metavar="") |
---|
[1d2cc5e] | 26 | |
---|
[8a3acad] | 27 | parser_add_subcommand_help(subparsers) |
---|
| 28 | |
---|
[ee123a0] | 29 | parser_add_subcommand_onset(subparsers) |
---|
| 30 | parser_add_subcommand_pitch(subparsers) |
---|
| 31 | parser_add_subcommand_beat(subparsers) |
---|
| 32 | parser_add_subcommand_tempo(subparsers) |
---|
| 33 | parser_add_subcommand_notes(subparsers) |
---|
| 34 | parser_add_subcommand_mfcc(subparsers) |
---|
| 35 | parser_add_subcommand_melbands(subparsers) |
---|
| 36 | parser_add_subcommand_quiet(subparsers) |
---|
[8dd5d40] | 37 | parser_add_subcommand_cut(subparsers) |
---|
[ee123a0] | 38 | |
---|
| 39 | return parser |
---|
| 40 | |
---|
[8a3acad] | 41 | def parser_add_subcommand_help(subparsers): |
---|
| 42 | # global help subcommand |
---|
| 43 | subparsers.add_parser('help', |
---|
| 44 | help='show help message', |
---|
| 45 | formatter_class = argparse.ArgumentDefaultsHelpFormatter) |
---|
| 46 | |
---|
[ee123a0] | 47 | def parser_add_subcommand_onset(subparsers): |
---|
[1d2cc5e] | 48 | # onset subcommand |
---|
| 49 | subparser = subparsers.add_parser('onset', |
---|
[86026a0] | 50 | help='estimate time of onsets (beginning of sound event)', |
---|
[1d2cc5e] | 51 | formatter_class = argparse.ArgumentDefaultsHelpFormatter) |
---|
[c95062b] | 52 | subparser.add_input() |
---|
| 53 | subparser.add_buf_hop_size() |
---|
[1d2cc5e] | 54 | helpstr = "onset novelty function" |
---|
| 55 | helpstr += " <default|energy|hfc|complex|phase|specdiff|kl|mkl|specflux>" |
---|
[c95062b] | 56 | subparser.add_method(helpstr=helpstr) |
---|
| 57 | subparser.add_threshold() |
---|
| 58 | subparser.add_silence() |
---|
| 59 | subparser.add_minioi() |
---|
| 60 | subparser.add_time_format() |
---|
| 61 | subparser.add_verbose_help() |
---|
[1d2cc5e] | 62 | subparser.set_defaults(process=process_onset) |
---|
| 63 | |
---|
[ee123a0] | 64 | def parser_add_subcommand_pitch(subparsers): |
---|
[1d2cc5e] | 65 | # pitch subcommand |
---|
| 66 | subparser = subparsers.add_parser('pitch', |
---|
[86026a0] | 67 | help='estimate fundamental frequency (monophonic)') |
---|
[c95062b] | 68 | subparser.add_input() |
---|
| 69 | subparser.add_buf_hop_size(buf_size=2048) |
---|
[1d2cc5e] | 70 | helpstr = "pitch detection method <default|yinfft|yin|mcomb|fcomb|schmitt>" |
---|
[c95062b] | 71 | subparser.add_method(helpstr=helpstr) |
---|
| 72 | subparser.add_threshold() |
---|
| 73 | subparser.add_pitch_unit() |
---|
| 74 | subparser.add_silence() |
---|
| 75 | subparser.add_time_format() |
---|
| 76 | subparser.add_verbose_help() |
---|
[1d2cc5e] | 77 | subparser.set_defaults(process=process_pitch) |
---|
| 78 | |
---|
[ee123a0] | 79 | def parser_add_subcommand_beat(subparsers): |
---|
[86026a0] | 80 | # beat subcommand |
---|
[1d2cc5e] | 81 | subparser = subparsers.add_parser('beat', |
---|
[86026a0] | 82 | help='estimate location of beats') |
---|
[c95062b] | 83 | subparser.add_input() |
---|
| 84 | subparser.add_buf_hop_size(buf_size=1024, hop_size=512) |
---|
| 85 | subparser.add_time_format() |
---|
| 86 | subparser.add_verbose_help() |
---|
[bd72039] | 87 | subparser.set_defaults(process=process_beat) |
---|
| 88 | |
---|
[ee123a0] | 89 | def parser_add_subcommand_tempo(subparsers): |
---|
[bd72039] | 90 | # tempo subcommand |
---|
| 91 | subparser = subparsers.add_parser('tempo', |
---|
[86026a0] | 92 | help='estimate overall tempo in bpm') |
---|
[c95062b] | 93 | subparser.add_input() |
---|
| 94 | subparser.add_buf_hop_size(buf_size=1024, hop_size=512) |
---|
| 95 | subparser.add_time_format() |
---|
| 96 | subparser.add_verbose_help() |
---|
[1d2cc5e] | 97 | subparser.set_defaults(process=process_tempo) |
---|
| 98 | |
---|
[ee123a0] | 99 | def parser_add_subcommand_notes(subparsers): |
---|
[1d2cc5e] | 100 | # notes subcommand |
---|
| 101 | subparser = subparsers.add_parser('notes', |
---|
[86026a0] | 102 | help='estimate midi-like notes (monophonic)') |
---|
[c95062b] | 103 | subparser.add_input() |
---|
| 104 | subparser.add_buf_hop_size() |
---|
[357f81e] | 105 | subparser.add_silence() |
---|
| 106 | subparser.add_release_drop() |
---|
[c95062b] | 107 | subparser.add_time_format() |
---|
| 108 | subparser.add_verbose_help() |
---|
[1d2cc5e] | 109 | subparser.set_defaults(process=process_notes) |
---|
| 110 | |
---|
[ee123a0] | 111 | def parser_add_subcommand_mfcc(subparsers): |
---|
[1d2cc5e] | 112 | # mfcc subcommand |
---|
| 113 | subparser = subparsers.add_parser('mfcc', |
---|
[86026a0] | 114 | help='extract Mel-Frequency Cepstrum Coefficients') |
---|
[c95062b] | 115 | subparser.add_input() |
---|
| 116 | subparser.add_buf_hop_size() |
---|
| 117 | subparser.add_time_format() |
---|
| 118 | subparser.add_verbose_help() |
---|
[1d2cc5e] | 119 | subparser.set_defaults(process=process_mfcc) |
---|
| 120 | |
---|
[ee123a0] | 121 | def parser_add_subcommand_melbands(subparsers): |
---|
[1d2cc5e] | 122 | # melbands subcommand |
---|
| 123 | subparser = subparsers.add_parser('melbands', |
---|
[86026a0] | 124 | help='extract energies in Mel-frequency bands') |
---|
[c95062b] | 125 | subparser.add_input() |
---|
| 126 | subparser.add_buf_hop_size() |
---|
| 127 | subparser.add_time_format() |
---|
| 128 | subparser.add_verbose_help() |
---|
[1d2cc5e] | 129 | subparser.set_defaults(process=process_melbands) |
---|
| 130 | |
---|
[ee123a0] | 131 | def parser_add_subcommand_quiet(subparsers): |
---|
[896c3a8] | 132 | # quiet subcommand |
---|
| 133 | subparser = subparsers.add_parser('quiet', |
---|
[70762c5] | 134 | help='extract timestamps of quiet and loud regions') |
---|
[c95062b] | 135 | subparser.add_input() |
---|
| 136 | subparser.add_hop_size() |
---|
| 137 | subparser.add_silence() |
---|
| 138 | subparser.add_time_format() |
---|
| 139 | subparser.add_verbose_help() |
---|
[896c3a8] | 140 | subparser.set_defaults(process=process_quiet) |
---|
| 141 | |
---|
[8dd5d40] | 142 | def parser_add_subcommand_cut(subparsers): |
---|
[917adb0] | 143 | # cut subcommand |
---|
[8dd5d40] | 144 | subparser = subparsers.add_parser('cut', |
---|
| 145 | help='slice at timestamps') |
---|
| 146 | subparser.add_input() |
---|
| 147 | helpstr = "onset novelty function" |
---|
| 148 | helpstr += " <default|energy|hfc|complex|phase|specdiff|kl|mkl|specflux>" |
---|
| 149 | subparser.add_method(helpstr=helpstr) |
---|
| 150 | subparser.add_buf_hop_size() |
---|
[930bfec] | 151 | subparser.add_silence() |
---|
[8dd5d40] | 152 | subparser.add_threshold(default=0.3) |
---|
| 153 | subparser.add_minioi() |
---|
| 154 | subparser.add_slicer_options() |
---|
[930bfec] | 155 | subparser.add_time_format() |
---|
[8dd5d40] | 156 | subparser.add_verbose_help() |
---|
| 157 | subparser.set_defaults(process=process_cut) |
---|
| 158 | |
---|
[c95062b] | 159 | class AubioArgumentParser(argparse.ArgumentParser): |
---|
| 160 | |
---|
| 161 | def add_input(self): |
---|
| 162 | self.add_argument("source_uri", default=None, nargs='?', |
---|
| 163 | help="input sound file to analyse", metavar = "<source_uri>") |
---|
| 164 | self.add_argument("-i", "--input", dest = "source_uri2", |
---|
| 165 | help="input sound file to analyse", metavar = "<source_uri>") |
---|
| 166 | self.add_argument("-r", "--samplerate", |
---|
| 167 | metavar = "<freq>", type=int, |
---|
| 168 | action="store", dest="samplerate", default=0, |
---|
| 169 | help="samplerate at which the file should be represented") |
---|
| 170 | |
---|
| 171 | def add_verbose_help(self): |
---|
[78ebc27] | 172 | self.add_argument("-v", "--verbose", |
---|
[c95062b] | 173 | action="count", dest="verbose", default=1, |
---|
| 174 | help="make lots of noise [default]") |
---|
[78ebc27] | 175 | self.add_argument("-q", "--quiet", |
---|
[c95062b] | 176 | action="store_const", dest="verbose", const=0, |
---|
| 177 | help="be quiet") |
---|
| 178 | |
---|
| 179 | def add_buf_hop_size(self, buf_size=512, hop_size=256): |
---|
| 180 | self.add_buf_size(buf_size=buf_size) |
---|
| 181 | self.add_hop_size(hop_size=hop_size) |
---|
| 182 | |
---|
| 183 | def add_buf_size(self, buf_size=512): |
---|
[78ebc27] | 184 | self.add_argument("-B", "--bufsize", |
---|
[c95062b] | 185 | action="store", dest="buf_size", default=buf_size, |
---|
| 186 | metavar = "<size>", type=int, |
---|
| 187 | help="buffer size [default=%d]" % buf_size) |
---|
| 188 | |
---|
| 189 | def add_hop_size(self, hop_size=256): |
---|
[78ebc27] | 190 | self.add_argument("-H", "--hopsize", |
---|
[c95062b] | 191 | metavar = "<size>", type=int, |
---|
| 192 | action="store", dest="hop_size", default=hop_size, |
---|
| 193 | help="overlap size [default=%d]" % hop_size) |
---|
| 194 | |
---|
| 195 | def add_method(self, method='default', helpstr='method'): |
---|
[78ebc27] | 196 | self.add_argument("-m", "--method", |
---|
[c95062b] | 197 | metavar = "<method>", type=str, |
---|
| 198 | action="store", dest="method", default=method, |
---|
| 199 | help="%s [default=%s]" % (helpstr, method)) |
---|
| 200 | |
---|
| 201 | def add_threshold(self, default=None): |
---|
[78ebc27] | 202 | self.add_argument("-t", "--threshold", |
---|
[c95062b] | 203 | metavar = "<threshold>", type=float, |
---|
| 204 | action="store", dest="threshold", default=default, |
---|
| 205 | help="threshold [default=%s]" % default) |
---|
| 206 | |
---|
| 207 | def add_silence(self): |
---|
| 208 | self.add_argument("-s", "--silence", |
---|
| 209 | metavar = "<value>", type=float, |
---|
| 210 | action="store", dest="silence", default=-70, |
---|
| 211 | help="silence threshold") |
---|
| 212 | |
---|
[357f81e] | 213 | def add_release_drop(self): |
---|
| 214 | self.add_argument("-d", "--release-drop", |
---|
| 215 | metavar = "<value>", type=float, |
---|
| 216 | action="store", dest="release_drop", default=10, |
---|
| 217 | help="release drop threshold") |
---|
| 218 | |
---|
[c95062b] | 219 | def add_minioi(self, default="12ms"): |
---|
| 220 | self.add_argument("-M", "--minioi", |
---|
| 221 | metavar = "<value>", type=str, |
---|
| 222 | action="store", dest="minioi", default=default, |
---|
| 223 | help="minimum Inter-Onset Interval [default=%s]" % default) |
---|
| 224 | |
---|
| 225 | def add_pitch_unit(self, default="Hz"): |
---|
| 226 | help_str = "frequency unit, should be one of Hz, midi, bin, cent" |
---|
| 227 | help_str += " [default=%s]" % default |
---|
| 228 | self.add_argument("-u", "--pitch-unit", |
---|
| 229 | metavar = "<value>", type=str, |
---|
| 230 | action="store", dest="pitch_unit", default=default, |
---|
| 231 | help=help_str) |
---|
| 232 | |
---|
| 233 | def add_time_format(self): |
---|
| 234 | helpstr = "select time values output format (samples, ms, seconds)" |
---|
| 235 | helpstr += " [default=seconds]" |
---|
| 236 | self.add_argument("-T", "--time-format", |
---|
| 237 | metavar='format', |
---|
| 238 | dest="time_format", |
---|
| 239 | default=None, |
---|
| 240 | help=helpstr) |
---|
[1d2cc5e] | 241 | |
---|
[8dd5d40] | 242 | def add_slicer_options(self): |
---|
[78ebc27] | 243 | self.add_argument("-o", "--output", type = str, |
---|
[8dd5d40] | 244 | metavar = "<outputdir>", |
---|
| 245 | action="store", dest="output_directory", default=None, |
---|
[ed298b2] | 246 | help="specify path where slices of the original file should" |
---|
| 247 | " be created") |
---|
[8dd5d40] | 248 | self.add_argument("--cut-until-nsamples", type = int, |
---|
| 249 | metavar = "<samples>", |
---|
| 250 | action = "store", dest = "cut_until_nsamples", default = None, |
---|
[ed298b2] | 251 | help="how many extra samples should be added at the end of" |
---|
| 252 | " each slice") |
---|
[8dd5d40] | 253 | self.add_argument("--cut-every-nslices", type = int, |
---|
| 254 | metavar = "<samples>", |
---|
| 255 | action = "store", dest = "cut_every_nslices", default = None, |
---|
| 256 | help="how many slices should be groupped together at each cut") |
---|
| 257 | self.add_argument("--cut-until-nslices", type = int, |
---|
| 258 | metavar = "<slices>", |
---|
| 259 | action = "store", dest = "cut_until_nslices", default = None, |
---|
[ed298b2] | 260 | help="how many extra slices should be added at the end of" |
---|
| 261 | " each slice") |
---|
[e126e65] | 262 | self.add_argument("--create-first", |
---|
| 263 | action = "store_true", dest = "create_first", default = False, |
---|
| 264 | help="always include first slice") |
---|
[8dd5d40] | 265 | |
---|
[1d2cc5e] | 266 | # some utilities |
---|
| 267 | |
---|
| 268 | def samples2seconds(n_frames, samplerate): |
---|
[53fbd58] | 269 | return "%f\t" % (n_frames / float(samplerate)) |
---|
[1d2cc5e] | 270 | |
---|
| 271 | def samples2milliseconds(n_frames, samplerate): |
---|
[53fbd58] | 272 | return "%f\t" % (1000. * n_frames / float(samplerate)) |
---|
[1d2cc5e] | 273 | |
---|
[70762c5] | 274 | def samples2samples(n_frames, _samplerate): |
---|
[1d2cc5e] | 275 | return "%d\t" % n_frames |
---|
| 276 | |
---|
| 277 | def timefunc(mode): |
---|
| 278 | if mode is None or mode == 'seconds' or mode == 's': |
---|
| 279 | return samples2seconds |
---|
| 280 | elif mode == 'ms' or mode == 'milliseconds': |
---|
| 281 | return samples2milliseconds |
---|
| 282 | elif mode == 'samples': |
---|
| 283 | return samples2samples |
---|
| 284 | else: |
---|
[c290245] | 285 | raise ValueError("invalid time format '%s'" % mode) |
---|
[1d2cc5e] | 286 | |
---|
| 287 | # definition of processing classes |
---|
| 288 | |
---|
| 289 | class default_process(object): |
---|
| 290 | def __init__(self, args): |
---|
| 291 | if 'time_format' in args: |
---|
| 292 | self.time2string = timefunc(args.time_format) |
---|
| 293 | if args.verbose > 2 and hasattr(self, 'options'): |
---|
| 294 | name = type(self).__name__.split('_')[1] |
---|
[78ebc27] | 295 | optstr = ' '.join(['running', name, 'with options', |
---|
| 296 | repr(self.options), '\n']) |
---|
[1d2cc5e] | 297 | sys.stderr.write(optstr) |
---|
[b24c909] | 298 | def flush(self, frames_read, samplerate): |
---|
[af98cb8] | 299 | # optionally called at the end of process |
---|
[5ab3c4e] | 300 | pass |
---|
[1d2cc5e] | 301 | |
---|
[af98cb8] | 302 | def parse_options(self, args, valid_opts): |
---|
| 303 | # get any valid options found in a dictionnary of arguments |
---|
[78ebc27] | 304 | options = {k: v for k, v in vars(args).items() if k in valid_opts} |
---|
[af98cb8] | 305 | self.options = options |
---|
| 306 | |
---|
| 307 | def remap_pvoc_options(self, options): |
---|
| 308 | # FIXME: we need to remap buf_size to win_s, hop_size to hop_s |
---|
| 309 | # adjust python/ext/py-phasevoc.c to understand buf_size/hop_size |
---|
| 310 | if 'buf_size' in options: |
---|
| 311 | options['win_s'] = options['buf_size'] |
---|
| 312 | del options['buf_size'] |
---|
| 313 | if 'hop_size' in options: |
---|
| 314 | options['hop_s'] = options['hop_size'] |
---|
| 315 | del options['hop_size'] |
---|
| 316 | self.options = options |
---|
| 317 | |
---|
[1d2cc5e] | 318 | class process_onset(default_process): |
---|
| 319 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
| 320 | def __init__(self, args): |
---|
[af98cb8] | 321 | self.parse_options(args, self.valid_opts) |
---|
[1d2cc5e] | 322 | self.onset = aubio.onset(**self.options) |
---|
| 323 | if args.threshold is not None: |
---|
| 324 | self.onset.set_threshold(args.threshold) |
---|
| 325 | if args.minioi: |
---|
| 326 | if args.minioi.endswith('ms'): |
---|
| 327 | self.onset.set_minioi_ms(float(args.minioi[:-2])) |
---|
| 328 | elif args.minioi.endswith('s'): |
---|
| 329 | self.onset.set_minioi_s(float(args.minioi[:-1])) |
---|
| 330 | else: |
---|
| 331 | self.onset.set_minioi(int(args.minioi)) |
---|
| 332 | if args.silence: |
---|
| 333 | self.onset.set_silence(args.silence) |
---|
| 334 | super(process_onset, self).__init__(args) |
---|
| 335 | def __call__(self, block): |
---|
| 336 | return self.onset(block) |
---|
[70762c5] | 337 | def repr_res(self, res, _frames_read, samplerate): |
---|
[1d2cc5e] | 338 | if res[0] != 0: |
---|
[6288806] | 339 | outstr = self.time2string(self.onset.get_last(), samplerate) |
---|
[1d2cc5e] | 340 | sys.stdout.write(outstr + '\n') |
---|
| 341 | |
---|
| 342 | class process_pitch(default_process): |
---|
| 343 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
| 344 | def __init__(self, args): |
---|
[af98cb8] | 345 | self.parse_options(args, self.valid_opts) |
---|
[1d2cc5e] | 346 | self.pitch = aubio.pitch(**self.options) |
---|
[3d9b41b] | 347 | if args.pitch_unit is not None: |
---|
| 348 | self.pitch.set_unit(args.pitch_unit) |
---|
[1d2cc5e] | 349 | if args.threshold is not None: |
---|
| 350 | self.pitch.set_tolerance(args.threshold) |
---|
| 351 | if args.silence is not None: |
---|
| 352 | self.pitch.set_silence(args.silence) |
---|
| 353 | super(process_pitch, self).__init__(args) |
---|
| 354 | def __call__(self, block): |
---|
| 355 | return self.pitch(block) |
---|
[6288806] | 356 | def repr_res(self, res, frames_read, samplerate): |
---|
| 357 | fmt_out = self.time2string(frames_read, samplerate) |
---|
[1d2cc5e] | 358 | sys.stdout.write(fmt_out + "%.6f\n" % res[0]) |
---|
| 359 | |
---|
[bd72039] | 360 | class process_beat(default_process): |
---|
[1d2cc5e] | 361 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
| 362 | def __init__(self, args): |
---|
[af98cb8] | 363 | self.parse_options(args, self.valid_opts) |
---|
[1d2cc5e] | 364 | self.tempo = aubio.tempo(**self.options) |
---|
[bd72039] | 365 | super(process_beat, self).__init__(args) |
---|
[1d2cc5e] | 366 | def __call__(self, block): |
---|
| 367 | return self.tempo(block) |
---|
[70762c5] | 368 | def repr_res(self, res, _frames_read, samplerate): |
---|
[1d2cc5e] | 369 | if res[0] != 0: |
---|
[6288806] | 370 | outstr = self.time2string(self.tempo.get_last(), samplerate) |
---|
[1d2cc5e] | 371 | sys.stdout.write(outstr + '\n') |
---|
| 372 | |
---|
[bd72039] | 373 | class process_tempo(process_beat): |
---|
| 374 | def __init__(self, args): |
---|
| 375 | super(process_tempo, self).__init__(args) |
---|
| 376 | self.beat_locations = [] |
---|
[70762c5] | 377 | def repr_res(self, res, _frames_read, samplerate): |
---|
[bd72039] | 378 | if res[0] != 0: |
---|
| 379 | self.beat_locations.append(self.tempo.get_last_s()) |
---|
| 380 | def flush(self, frames_read, samplerate): |
---|
| 381 | import numpy as np |
---|
[62a94b7] | 382 | if len(self.beat_locations) < 2: |
---|
| 383 | outstr = "unknown bpm" |
---|
| 384 | else: |
---|
[78ebc27] | 385 | bpms = 60. / np.diff(self.beat_locations) |
---|
[62a94b7] | 386 | median_bpm = np.mean(bpms) |
---|
| 387 | if len(self.beat_locations) < 10: |
---|
| 388 | outstr = "%.2f bpm (uncertain)" % median_bpm |
---|
| 389 | else: |
---|
| 390 | outstr = "%.2f bpm" % median_bpm |
---|
| 391 | sys.stdout.write(outstr + '\n') |
---|
[bd72039] | 392 | |
---|
[1d2cc5e] | 393 | class process_notes(default_process): |
---|
| 394 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
| 395 | def __init__(self, args): |
---|
[af98cb8] | 396 | self.parse_options(args, self.valid_opts) |
---|
[1d2cc5e] | 397 | self.notes = aubio.notes(**self.options) |
---|
[357f81e] | 398 | if args.silence is not None: |
---|
| 399 | self.notes.set_silence(args.silence) |
---|
| 400 | if args.release_drop is not None: |
---|
| 401 | self.notes.set_release_drop(args.release_drop) |
---|
[1d2cc5e] | 402 | super(process_notes, self).__init__(args) |
---|
| 403 | def __call__(self, block): |
---|
| 404 | return self.notes(block) |
---|
[6288806] | 405 | def repr_res(self, res, frames_read, samplerate): |
---|
[78ebc27] | 406 | if res[2] != 0: # note off |
---|
[6288806] | 407 | fmt_out = self.time2string(frames_read, samplerate) |
---|
[1d2cc5e] | 408 | sys.stdout.write(fmt_out + '\n') |
---|
[78ebc27] | 409 | if res[0] != 0: # note on |
---|
[1d2cc5e] | 410 | lastmidi = res[0] |
---|
| 411 | fmt_out = "%f\t" % lastmidi |
---|
[6288806] | 412 | fmt_out += self.time2string(frames_read, samplerate) |
---|
[78ebc27] | 413 | sys.stdout.write(fmt_out) # + '\t') |
---|
[5ab3c4e] | 414 | def flush(self, frames_read, samplerate): |
---|
| 415 | eof = self.time2string(frames_read, samplerate) |
---|
| 416 | sys.stdout.write(eof + '\n') |
---|
[1d2cc5e] | 417 | |
---|
| 418 | class process_mfcc(default_process): |
---|
| 419 | def __init__(self, args): |
---|
[af98cb8] | 420 | valid_opts1 = ['hop_size', 'buf_size'] |
---|
| 421 | self.parse_options(args, valid_opts1) |
---|
| 422 | self.remap_pvoc_options(self.options) |
---|
| 423 | self.pv = aubio.pvoc(**self.options) |
---|
| 424 | |
---|
| 425 | valid_opts2 = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate'] |
---|
| 426 | self.parse_options(args, valid_opts2) |
---|
| 427 | self.mfcc = aubio.mfcc(**self.options) |
---|
[1d2cc5e] | 428 | |
---|
[af98cb8] | 429 | # remember all options |
---|
| 430 | self.parse_options(args, list(set(valid_opts1 + valid_opts2))) |
---|
[1d2cc5e] | 431 | |
---|
| 432 | super(process_mfcc, self).__init__(args) |
---|
| 433 | |
---|
| 434 | def __call__(self, block): |
---|
| 435 | fftgrain = self.pv(block) |
---|
| 436 | return self.mfcc(fftgrain) |
---|
[6288806] | 437 | def repr_res(self, res, frames_read, samplerate): |
---|
| 438 | fmt_out = self.time2string(frames_read, samplerate) |
---|
[1d2cc5e] | 439 | fmt_out += ' '.join(["% 9.7f" % f for f in res.tolist()]) |
---|
| 440 | sys.stdout.write(fmt_out + '\n') |
---|
| 441 | |
---|
| 442 | class process_melbands(default_process): |
---|
| 443 | def __init__(self, args): |
---|
| 444 | self.args = args |
---|
| 445 | valid_opts = ['hop_size', 'buf_size'] |
---|
[af98cb8] | 446 | self.parse_options(args, valid_opts) |
---|
| 447 | self.remap_pvoc_options(self.options) |
---|
| 448 | self.pv = aubio.pvoc(**self.options) |
---|
[1d2cc5e] | 449 | |
---|
| 450 | valid_opts = ['buf_size', 'n_filters'] |
---|
[af98cb8] | 451 | self.parse_options(args, valid_opts) |
---|
| 452 | self.remap_pvoc_options(self.options) |
---|
| 453 | self.filterbank = aubio.filterbank(**self.options) |
---|
[1d2cc5e] | 454 | self.filterbank.set_mel_coeffs_slaney(args.samplerate) |
---|
| 455 | |
---|
| 456 | super(process_melbands, self).__init__(args) |
---|
| 457 | def __call__(self, block): |
---|
| 458 | fftgrain = self.pv(block) |
---|
| 459 | return self.filterbank(fftgrain) |
---|
[6288806] | 460 | def repr_res(self, res, frames_read, samplerate): |
---|
| 461 | fmt_out = self.time2string(frames_read, samplerate) |
---|
[1d2cc5e] | 462 | fmt_out += ' '.join(["% 9.7f" % f for f in res.tolist()]) |
---|
| 463 | sys.stdout.write(fmt_out + '\n') |
---|
| 464 | |
---|
[896c3a8] | 465 | class process_quiet(default_process): |
---|
| 466 | def __init__(self, args): |
---|
| 467 | self.args = args |
---|
| 468 | valid_opts = ['hop_size', 'silence'] |
---|
| 469 | self.parse_options(args, valid_opts) |
---|
| 470 | self.wassilence = 1 |
---|
| 471 | |
---|
| 472 | if args.silence is not None: |
---|
| 473 | self.silence = args.silence |
---|
| 474 | super(process_quiet, self).__init__(args) |
---|
| 475 | |
---|
| 476 | def __call__(self, block): |
---|
| 477 | if aubio.silence_detection(block, self.silence) == 1: |
---|
[70762c5] | 478 | if self.wassilence != 1: |
---|
| 479 | self.wassilence = 1 |
---|
[78ebc27] | 480 | return 2 # newly found silence |
---|
| 481 | return 1 # silence again |
---|
[896c3a8] | 482 | else: |
---|
[70762c5] | 483 | if self.wassilence != 0: |
---|
| 484 | self.wassilence = 0 |
---|
[78ebc27] | 485 | return -1 # newly found noise |
---|
| 486 | return 0 # noise again |
---|
[896c3a8] | 487 | |
---|
| 488 | def repr_res(self, res, frames_read, samplerate): |
---|
| 489 | fmt_out = None |
---|
[70762c5] | 490 | if res == -1: |
---|
[896c3a8] | 491 | fmt_out = "NOISY: " |
---|
[70762c5] | 492 | if res == 2: |
---|
[896c3a8] | 493 | fmt_out = "QUIET: " |
---|
| 494 | if fmt_out is not None: |
---|
| 495 | fmt_out += self.time2string(frames_read, samplerate) |
---|
| 496 | sys.stdout.write(fmt_out + '\n') |
---|
| 497 | |
---|
[8dd5d40] | 498 | class process_cut(process_onset): |
---|
| 499 | def __init__(self, args): |
---|
| 500 | super(process_cut, self).__init__(args) |
---|
| 501 | self.slices = [] |
---|
| 502 | self.options = args |
---|
| 503 | |
---|
| 504 | def __call__(self, block): |
---|
| 505 | ret = super(process_cut, self).__call__(block) |
---|
[78ebc27] | 506 | if ret: |
---|
| 507 | self.slices.append(self.onset.get_last()) |
---|
[8dd5d40] | 508 | return ret |
---|
| 509 | |
---|
| 510 | def flush(self, frames_read, samplerate): |
---|
| 511 | _cut_slice(self.options, self.slices) |
---|
[78ebc27] | 512 | duration = float(frames_read) / float(samplerate) |
---|
| 513 | base_info = '%(source_file)s' % \ |
---|
| 514 | {'source_file': self.options.source_uri} |
---|
[8dd5d40] | 515 | base_info += ' (total %(duration).2fs at %(samplerate)dHz)\n' % \ |
---|
[78ebc27] | 516 | {'duration': duration, 'samplerate': samplerate} |
---|
[8dd5d40] | 517 | info = "created %d slices from " % len(self.slices) |
---|
| 518 | info += base_info |
---|
| 519 | sys.stderr.write(info) |
---|
| 520 | |
---|
[dc74f69] | 521 | def _cut_slice(options, timestamps): |
---|
| 522 | # cutting pass |
---|
| 523 | nstamps = len(timestamps) |
---|
| 524 | if nstamps > 0: |
---|
| 525 | # generate output files |
---|
| 526 | timestamps_end = None |
---|
| 527 | if options.cut_every_nslices: |
---|
| 528 | timestamps = timestamps[::options.cut_every_nslices] |
---|
| 529 | nstamps = len(timestamps) |
---|
| 530 | if options.cut_until_nslices and options.cut_until_nsamples: |
---|
| 531 | msg = "using cut_until_nslices, but cut_until_nsamples is set" |
---|
| 532 | warnings.warn(msg) |
---|
| 533 | if options.cut_until_nsamples: |
---|
| 534 | lag = options.cut_until_nsamples |
---|
| 535 | timestamps_end = [t + lag for t in timestamps[1:]] |
---|
| 536 | timestamps_end += [1e120] |
---|
| 537 | if options.cut_until_nslices: |
---|
| 538 | slice_lag = options.cut_until_nslices |
---|
| 539 | timestamps_end = [t for t in timestamps[1 + slice_lag:]] |
---|
| 540 | timestamps_end += [1e120] * (options.cut_until_nslices + 1) |
---|
| 541 | aubio.slice_source_at_stamps(options.source_uri, |
---|
| 542 | timestamps, timestamps_end = timestamps_end, |
---|
| 543 | output_dir = options.output_directory, |
---|
| 544 | samplerate = options.samplerate, |
---|
| 545 | create_first = options.create_first) |
---|
| 546 | |
---|
[8e2f36a] | 547 | def main(): |
---|
[1d2cc5e] | 548 | parser = aubio_parser() |
---|
[f5921b9] | 549 | if sys.version_info[0] != 3: |
---|
| 550 | # on py2, create a dummy ArgumentParser to workaround the |
---|
| 551 | # optional subcommand issue. See https://bugs.python.org/issue9253 |
---|
| 552 | # This ensures that: |
---|
| 553 | # - version string is shown when only '-V' is passed |
---|
| 554 | # - help is printed if '-V' is passed with any other argument |
---|
| 555 | # - any other argument get forwarded to the real parser |
---|
| 556 | parser_root = argparse.ArgumentParser(add_help=False) |
---|
| 557 | parser_root.add_argument('-V', '--version', help="show version", |
---|
| 558 | action="store_true", dest="show_version") |
---|
| 559 | args, extras = parser_root.parse_known_args() |
---|
[78ebc27] | 560 | if not args.show_version: # no -V, forward to parser |
---|
[f5921b9] | 561 | args = parser.parse_args(extras, namespace=args) |
---|
[78ebc27] | 562 | elif len(extras) != 0: # -V with other arguments, print help |
---|
[f5921b9] | 563 | parser.print_help() |
---|
| 564 | sys.exit(1) |
---|
[78ebc27] | 565 | else: # in py3, we can simply use parser directly |
---|
[f5921b9] | 566 | args = parser.parse_args() |
---|
[2615dd9] | 567 | if 'show_version' in args and args.show_version: |
---|
[1d2cc5e] | 568 | sys.stdout.write('aubio version ' + aubio.version + '\n') |
---|
| 569 | sys.exit(0) |
---|
[2615dd9] | 570 | elif 'verbose' in args and args.verbose > 3: |
---|
| 571 | sys.stderr.write('aubio version ' + aubio.version + '\n') |
---|
[78ebc27] | 572 | if 'command' not in args or args.command is None \ |
---|
| 573 | or args.command in ['help']: |
---|
[2615dd9] | 574 | # no command given, print help and return 1 |
---|
[1d2cc5e] | 575 | parser.print_help() |
---|
[8a3acad] | 576 | if args.command and args.command in ['help']: |
---|
| 577 | sys.exit(0) |
---|
| 578 | else: |
---|
| 579 | sys.exit(1) |
---|
[1d2cc5e] | 580 | elif not args.source_uri and not args.source_uri2: |
---|
| 581 | sys.stderr.write("Error: a source is required\n") |
---|
| 582 | parser.print_help() |
---|
| 583 | sys.exit(1) |
---|
| 584 | elif args.source_uri2 is not None: |
---|
| 585 | args.source_uri = args.source_uri2 |
---|
| 586 | try: |
---|
| 587 | # open source_uri |
---|
| 588 | with aubio.source(args.source_uri, hop_size=args.hop_size, |
---|
| 589 | samplerate=args.samplerate) as a_source: |
---|
[af98cb8] | 590 | # always update args.samplerate to native samplerate, in case |
---|
| 591 | # source was opened with args.samplerate=0 |
---|
[1d2cc5e] | 592 | args.samplerate = a_source.samplerate |
---|
| 593 | # create the processor for this subcommand |
---|
| 594 | processor = args.process(args) |
---|
| 595 | frames_read = 0 |
---|
| 596 | while True: |
---|
| 597 | # read new block from source |
---|
| 598 | block, read = a_source() |
---|
| 599 | # execute processor on this block |
---|
| 600 | res = processor(block) |
---|
| 601 | # print results for this block |
---|
| 602 | if args.verbose > 0: |
---|
[6288806] | 603 | processor.repr_res(res, frames_read, a_source.samplerate) |
---|
[1d2cc5e] | 604 | # increment total number of frames read |
---|
| 605 | frames_read += read |
---|
| 606 | # exit loop at end of file |
---|
[78ebc27] | 607 | if read < a_source.hop_size: |
---|
| 608 | break |
---|
[5ab3c4e] | 609 | # flush the processor if needed |
---|
| 610 | processor.flush(frames_read, a_source.samplerate) |
---|
[1d2cc5e] | 611 | if args.verbose > 1: |
---|
| 612 | fmt_string = "read {:.2f}s" |
---|
| 613 | fmt_string += " ({:d} samples in {:d} blocks of {:d})" |
---|
| 614 | fmt_string += " from {:s} at {:d}Hz\n" |
---|
| 615 | sys.stderr.write(fmt_string.format( |
---|
[78ebc27] | 616 | frames_read / float(a_source.samplerate), |
---|
[1d2cc5e] | 617 | frames_read, |
---|
| 618 | frames_read // a_source.hop_size + 1, |
---|
| 619 | a_source.hop_size, |
---|
| 620 | a_source.uri, |
---|
| 621 | a_source.samplerate)) |
---|
[657a20a] | 622 | except KeyboardInterrupt: |
---|
[1d2cc5e] | 623 | sys.exit(1) |
---|