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