[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', |
---|
| 23 | metavar="") |
---|
[1d2cc5e] | 24 | |
---|
[8a3acad] | 25 | parser_add_subcommand_help(subparsers) |
---|
| 26 | |
---|
[ee123a0] | 27 | parser_add_subcommand_onset(subparsers) |
---|
| 28 | parser_add_subcommand_pitch(subparsers) |
---|
| 29 | parser_add_subcommand_beat(subparsers) |
---|
| 30 | parser_add_subcommand_tempo(subparsers) |
---|
| 31 | parser_add_subcommand_notes(subparsers) |
---|
| 32 | parser_add_subcommand_mfcc(subparsers) |
---|
| 33 | parser_add_subcommand_melbands(subparsers) |
---|
| 34 | parser_add_subcommand_quiet(subparsers) |
---|
| 35 | |
---|
| 36 | return parser |
---|
| 37 | |
---|
[8a3acad] | 38 | def parser_add_subcommand_help(subparsers): |
---|
| 39 | # global help subcommand |
---|
| 40 | subparsers.add_parser('help', |
---|
| 41 | help='show help message', |
---|
| 42 | formatter_class = argparse.ArgumentDefaultsHelpFormatter) |
---|
| 43 | |
---|
[ee123a0] | 44 | def parser_add_subcommand_onset(subparsers): |
---|
[1d2cc5e] | 45 | # onset subcommand |
---|
| 46 | subparser = subparsers.add_parser('onset', |
---|
[86026a0] | 47 | help='estimate time of onsets (beginning of sound event)', |
---|
[1d2cc5e] | 48 | formatter_class = argparse.ArgumentDefaultsHelpFormatter) |
---|
| 49 | parser_add_input(subparser) |
---|
| 50 | parser_add_buf_hop_size(subparser) |
---|
| 51 | helpstr = "onset novelty function" |
---|
| 52 | helpstr += " <default|energy|hfc|complex|phase|specdiff|kl|mkl|specflux>" |
---|
| 53 | parser_add_method(subparser, helpstr=helpstr) |
---|
| 54 | parser_add_threshold(subparser) |
---|
| 55 | parser_add_silence(subparser) |
---|
| 56 | parser_add_minioi(subparser) |
---|
| 57 | parser_add_time_format(subparser) |
---|
| 58 | parser_add_verbose_help(subparser) |
---|
| 59 | subparser.set_defaults(process=process_onset) |
---|
| 60 | |
---|
[ee123a0] | 61 | def parser_add_subcommand_pitch(subparsers): |
---|
[1d2cc5e] | 62 | # pitch subcommand |
---|
| 63 | subparser = subparsers.add_parser('pitch', |
---|
[86026a0] | 64 | help='estimate fundamental frequency (monophonic)') |
---|
[1d2cc5e] | 65 | parser_add_input(subparser) |
---|
| 66 | parser_add_buf_hop_size(subparser, buf_size=2048) |
---|
| 67 | helpstr = "pitch detection method <default|yinfft|yin|mcomb|fcomb|schmitt>" |
---|
| 68 | parser_add_method(subparser, helpstr=helpstr) |
---|
| 69 | parser_add_threshold(subparser) |
---|
[3d9b41b] | 70 | parser_add_pitch_unit(subparser) |
---|
[1d2cc5e] | 71 | parser_add_silence(subparser) |
---|
| 72 | parser_add_time_format(subparser) |
---|
| 73 | parser_add_verbose_help(subparser) |
---|
| 74 | subparser.set_defaults(process=process_pitch) |
---|
| 75 | |
---|
[ee123a0] | 76 | def parser_add_subcommand_beat(subparsers): |
---|
[86026a0] | 77 | # beat subcommand |
---|
[1d2cc5e] | 78 | subparser = subparsers.add_parser('beat', |
---|
[86026a0] | 79 | help='estimate location of beats') |
---|
[1d2cc5e] | 80 | parser_add_input(subparser) |
---|
| 81 | parser_add_buf_hop_size(subparser, buf_size=1024, hop_size=512) |
---|
| 82 | parser_add_time_format(subparser) |
---|
| 83 | parser_add_verbose_help(subparser) |
---|
[bd72039] | 84 | subparser.set_defaults(process=process_beat) |
---|
| 85 | |
---|
[ee123a0] | 86 | def parser_add_subcommand_tempo(subparsers): |
---|
[bd72039] | 87 | # tempo subcommand |
---|
| 88 | subparser = subparsers.add_parser('tempo', |
---|
[86026a0] | 89 | help='estimate overall tempo in bpm') |
---|
[bd72039] | 90 | parser_add_input(subparser) |
---|
| 91 | parser_add_buf_hop_size(subparser, buf_size=1024, hop_size=512) |
---|
| 92 | parser_add_time_format(subparser) |
---|
| 93 | parser_add_verbose_help(subparser) |
---|
[1d2cc5e] | 94 | subparser.set_defaults(process=process_tempo) |
---|
| 95 | |
---|
[ee123a0] | 96 | def parser_add_subcommand_notes(subparsers): |
---|
[1d2cc5e] | 97 | # notes subcommand |
---|
| 98 | subparser = subparsers.add_parser('notes', |
---|
[86026a0] | 99 | help='estimate midi-like notes (monophonic)') |
---|
[1d2cc5e] | 100 | parser_add_input(subparser) |
---|
| 101 | parser_add_buf_hop_size(subparser) |
---|
| 102 | parser_add_time_format(subparser) |
---|
| 103 | parser_add_verbose_help(subparser) |
---|
| 104 | subparser.set_defaults(process=process_notes) |
---|
| 105 | |
---|
[ee123a0] | 106 | def parser_add_subcommand_mfcc(subparsers): |
---|
[1d2cc5e] | 107 | # mfcc subcommand |
---|
| 108 | subparser = subparsers.add_parser('mfcc', |
---|
[86026a0] | 109 | help='extract Mel-Frequency Cepstrum Coefficients') |
---|
[1d2cc5e] | 110 | parser_add_input(subparser) |
---|
| 111 | parser_add_buf_hop_size(subparser) |
---|
| 112 | parser_add_time_format(subparser) |
---|
| 113 | parser_add_verbose_help(subparser) |
---|
| 114 | subparser.set_defaults(process=process_mfcc) |
---|
| 115 | |
---|
[ee123a0] | 116 | def parser_add_subcommand_melbands(subparsers): |
---|
[1d2cc5e] | 117 | # melbands subcommand |
---|
| 118 | subparser = subparsers.add_parser('melbands', |
---|
[86026a0] | 119 | help='extract energies in Mel-frequency bands') |
---|
[1d2cc5e] | 120 | parser_add_input(subparser) |
---|
| 121 | parser_add_buf_hop_size(subparser) |
---|
| 122 | parser_add_time_format(subparser) |
---|
| 123 | parser_add_verbose_help(subparser) |
---|
| 124 | subparser.set_defaults(process=process_melbands) |
---|
| 125 | |
---|
[ee123a0] | 126 | def parser_add_subcommand_quiet(subparsers): |
---|
[896c3a8] | 127 | # quiet subcommand |
---|
| 128 | subparser = subparsers.add_parser('quiet', |
---|
[70762c5] | 129 | help='extract timestamps of quiet and loud regions') |
---|
[896c3a8] | 130 | parser_add_input(subparser) |
---|
| 131 | parser_add_hop_size(subparser) |
---|
| 132 | parser_add_silence(subparser) |
---|
| 133 | parser_add_time_format(subparser) |
---|
| 134 | parser_add_verbose_help(subparser) |
---|
| 135 | subparser.set_defaults(process=process_quiet) |
---|
| 136 | |
---|
[1d2cc5e] | 137 | def parser_add_input(parser): |
---|
| 138 | parser.add_argument("source_uri", default=None, nargs='?', |
---|
| 139 | help="input sound file to analyse", metavar = "<source_uri>") |
---|
| 140 | parser.add_argument("-i", "--input", dest = "source_uri2", |
---|
| 141 | help="input sound file to analyse", metavar = "<source_uri>") |
---|
| 142 | parser.add_argument("-r", "--samplerate", |
---|
| 143 | metavar = "<freq>", type=int, |
---|
| 144 | action="store", dest="samplerate", default=0, |
---|
| 145 | help="samplerate at which the file should be represented") |
---|
| 146 | |
---|
| 147 | def parser_add_verbose_help(parser): |
---|
| 148 | parser.add_argument("-v","--verbose", |
---|
| 149 | action="count", dest="verbose", default=1, |
---|
| 150 | help="make lots of noise [default]") |
---|
| 151 | parser.add_argument("-q","--quiet", |
---|
| 152 | action="store_const", dest="verbose", const=0, |
---|
| 153 | help="be quiet") |
---|
| 154 | |
---|
| 155 | def parser_add_buf_hop_size(parser, buf_size=512, hop_size=256): |
---|
[67ee29f] | 156 | parser_add_buf_size(parser, buf_size=buf_size) |
---|
| 157 | parser_add_hop_size(parser, hop_size=hop_size) |
---|
| 158 | |
---|
| 159 | def parser_add_buf_size(parser, buf_size=512): |
---|
[1d2cc5e] | 160 | parser.add_argument("-B","--bufsize", |
---|
| 161 | action="store", dest="buf_size", default=buf_size, |
---|
| 162 | metavar = "<size>", type=int, |
---|
| 163 | help="buffer size [default=%d]" % buf_size) |
---|
[67ee29f] | 164 | |
---|
| 165 | def parser_add_hop_size(parser, hop_size=256): |
---|
[1d2cc5e] | 166 | parser.add_argument("-H","--hopsize", |
---|
| 167 | metavar = "<size>", type=int, |
---|
| 168 | action="store", dest="hop_size", default=hop_size, |
---|
| 169 | help="overlap size [default=%d]" % hop_size) |
---|
| 170 | |
---|
| 171 | def parser_add_method(parser, method='default', helpstr='method'): |
---|
| 172 | parser.add_argument("-m","--method", |
---|
| 173 | metavar = "<method>", type=str, |
---|
| 174 | action="store", dest="method", default=method, |
---|
| 175 | help="%s [default=%s]" % (helpstr, method)) |
---|
| 176 | |
---|
| 177 | def parser_add_threshold(parser, default=None): |
---|
| 178 | parser.add_argument("-t","--threshold", |
---|
| 179 | metavar = "<threshold>", type=float, |
---|
| 180 | action="store", dest="threshold", default=default, |
---|
| 181 | help="threshold [default=%s]" % default) |
---|
| 182 | |
---|
| 183 | def parser_add_silence(parser): |
---|
| 184 | parser.add_argument("-s", "--silence", |
---|
| 185 | metavar = "<value>", type=float, |
---|
| 186 | action="store", dest="silence", default=-70, |
---|
| 187 | help="silence threshold") |
---|
| 188 | |
---|
| 189 | def parser_add_minioi(parser): |
---|
| 190 | parser.add_argument("-M", "--minioi", |
---|
| 191 | metavar = "<value>", type=str, |
---|
| 192 | action="store", dest="minioi", default="12ms", |
---|
| 193 | help="minimum Inter-Onset Interval") |
---|
| 194 | |
---|
[3d9b41b] | 195 | def parser_add_pitch_unit(parser, default="Hz"): |
---|
| 196 | help_str = "frequency unit, should be one of Hz, midi, bin, cent" |
---|
| 197 | help_str += " [default=%s]" % default |
---|
| 198 | parser.add_argument("-u", "--pitch-unit", |
---|
| 199 | metavar = "<value>", type=str, |
---|
| 200 | action="store", dest="pitch_unit", default=default, |
---|
| 201 | help=help_str) |
---|
| 202 | |
---|
[1d2cc5e] | 203 | def parser_add_time_format(parser): |
---|
| 204 | helpstr = "select time values output format (samples, ms, seconds)" |
---|
| 205 | helpstr += " [default=seconds]" |
---|
| 206 | parser.add_argument("-T", "--time-format", |
---|
| 207 | metavar='format', |
---|
| 208 | dest="time_format", |
---|
| 209 | default=None, |
---|
| 210 | help=helpstr) |
---|
| 211 | |
---|
| 212 | # some utilities |
---|
| 213 | |
---|
| 214 | def samples2seconds(n_frames, samplerate): |
---|
[53fbd58] | 215 | return "%f\t" % (n_frames / float(samplerate)) |
---|
[1d2cc5e] | 216 | |
---|
| 217 | def samples2milliseconds(n_frames, samplerate): |
---|
[53fbd58] | 218 | return "%f\t" % (1000. * n_frames / float(samplerate)) |
---|
[1d2cc5e] | 219 | |
---|
[70762c5] | 220 | def samples2samples(n_frames, _samplerate): |
---|
[1d2cc5e] | 221 | return "%d\t" % n_frames |
---|
| 222 | |
---|
| 223 | def timefunc(mode): |
---|
| 224 | if mode is None or mode == 'seconds' or mode == 's': |
---|
| 225 | return samples2seconds |
---|
| 226 | elif mode == 'ms' or mode == 'milliseconds': |
---|
| 227 | return samples2milliseconds |
---|
| 228 | elif mode == 'samples': |
---|
| 229 | return samples2samples |
---|
| 230 | else: |
---|
[c290245] | 231 | raise ValueError("invalid time format '%s'" % mode) |
---|
[1d2cc5e] | 232 | |
---|
| 233 | # definition of processing classes |
---|
| 234 | |
---|
| 235 | class default_process(object): |
---|
| 236 | def __init__(self, args): |
---|
| 237 | if 'time_format' in args: |
---|
| 238 | self.time2string = timefunc(args.time_format) |
---|
| 239 | if args.verbose > 2 and hasattr(self, 'options'): |
---|
| 240 | name = type(self).__name__.split('_')[1] |
---|
| 241 | optstr = ' '.join(['running', name, 'with options', repr(self.options), '\n']) |
---|
| 242 | sys.stderr.write(optstr) |
---|
[b24c909] | 243 | def flush(self, frames_read, samplerate): |
---|
[af98cb8] | 244 | # optionally called at the end of process |
---|
[5ab3c4e] | 245 | pass |
---|
[1d2cc5e] | 246 | |
---|
[af98cb8] | 247 | def parse_options(self, args, valid_opts): |
---|
| 248 | # get any valid options found in a dictionnary of arguments |
---|
| 249 | options = {k :v for k,v in vars(args).items() if k in valid_opts} |
---|
| 250 | self.options = options |
---|
| 251 | |
---|
| 252 | def remap_pvoc_options(self, options): |
---|
| 253 | # FIXME: we need to remap buf_size to win_s, hop_size to hop_s |
---|
| 254 | # adjust python/ext/py-phasevoc.c to understand buf_size/hop_size |
---|
| 255 | if 'buf_size' in options: |
---|
| 256 | options['win_s'] = options['buf_size'] |
---|
| 257 | del options['buf_size'] |
---|
| 258 | if 'hop_size' in options: |
---|
| 259 | options['hop_s'] = options['hop_size'] |
---|
| 260 | del options['hop_size'] |
---|
| 261 | self.options = options |
---|
| 262 | |
---|
[1d2cc5e] | 263 | class process_onset(default_process): |
---|
| 264 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
| 265 | def __init__(self, args): |
---|
[af98cb8] | 266 | self.parse_options(args, self.valid_opts) |
---|
[1d2cc5e] | 267 | self.onset = aubio.onset(**self.options) |
---|
| 268 | if args.threshold is not None: |
---|
| 269 | self.onset.set_threshold(args.threshold) |
---|
| 270 | if args.minioi: |
---|
| 271 | if args.minioi.endswith('ms'): |
---|
| 272 | self.onset.set_minioi_ms(float(args.minioi[:-2])) |
---|
| 273 | elif args.minioi.endswith('s'): |
---|
| 274 | self.onset.set_minioi_s(float(args.minioi[:-1])) |
---|
| 275 | else: |
---|
| 276 | self.onset.set_minioi(int(args.minioi)) |
---|
| 277 | if args.silence: |
---|
| 278 | self.onset.set_silence(args.silence) |
---|
| 279 | super(process_onset, self).__init__(args) |
---|
| 280 | def __call__(self, block): |
---|
| 281 | return self.onset(block) |
---|
[70762c5] | 282 | def repr_res(self, res, _frames_read, samplerate): |
---|
[1d2cc5e] | 283 | if res[0] != 0: |
---|
[6288806] | 284 | outstr = self.time2string(self.onset.get_last(), samplerate) |
---|
[1d2cc5e] | 285 | sys.stdout.write(outstr + '\n') |
---|
| 286 | |
---|
| 287 | class process_pitch(default_process): |
---|
| 288 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
| 289 | def __init__(self, args): |
---|
[af98cb8] | 290 | self.parse_options(args, self.valid_opts) |
---|
[1d2cc5e] | 291 | self.pitch = aubio.pitch(**self.options) |
---|
[3d9b41b] | 292 | if args.pitch_unit is not None: |
---|
| 293 | self.pitch.set_unit(args.pitch_unit) |
---|
[1d2cc5e] | 294 | if args.threshold is not None: |
---|
| 295 | self.pitch.set_tolerance(args.threshold) |
---|
| 296 | if args.silence is not None: |
---|
| 297 | self.pitch.set_silence(args.silence) |
---|
| 298 | super(process_pitch, self).__init__(args) |
---|
| 299 | def __call__(self, block): |
---|
| 300 | return self.pitch(block) |
---|
[6288806] | 301 | def repr_res(self, res, frames_read, samplerate): |
---|
| 302 | fmt_out = self.time2string(frames_read, samplerate) |
---|
[1d2cc5e] | 303 | sys.stdout.write(fmt_out + "%.6f\n" % res[0]) |
---|
| 304 | |
---|
[bd72039] | 305 | class process_beat(default_process): |
---|
[1d2cc5e] | 306 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
| 307 | def __init__(self, args): |
---|
[af98cb8] | 308 | self.parse_options(args, self.valid_opts) |
---|
[1d2cc5e] | 309 | self.tempo = aubio.tempo(**self.options) |
---|
[bd72039] | 310 | super(process_beat, self).__init__(args) |
---|
[1d2cc5e] | 311 | def __call__(self, block): |
---|
| 312 | return self.tempo(block) |
---|
[70762c5] | 313 | def repr_res(self, res, _frames_read, samplerate): |
---|
[1d2cc5e] | 314 | if res[0] != 0: |
---|
[6288806] | 315 | outstr = self.time2string(self.tempo.get_last(), samplerate) |
---|
[1d2cc5e] | 316 | sys.stdout.write(outstr + '\n') |
---|
| 317 | |
---|
[bd72039] | 318 | class process_tempo(process_beat): |
---|
| 319 | def __init__(self, args): |
---|
| 320 | super(process_tempo, self).__init__(args) |
---|
| 321 | self.beat_locations = [] |
---|
[70762c5] | 322 | def repr_res(self, res, _frames_read, samplerate): |
---|
[bd72039] | 323 | if res[0] != 0: |
---|
| 324 | self.beat_locations.append(self.tempo.get_last_s()) |
---|
| 325 | def flush(self, frames_read, samplerate): |
---|
| 326 | import numpy as np |
---|
[62a94b7] | 327 | if len(self.beat_locations) < 2: |
---|
| 328 | outstr = "unknown bpm" |
---|
| 329 | else: |
---|
| 330 | bpms = 60./ np.diff(self.beat_locations) |
---|
| 331 | median_bpm = np.mean(bpms) |
---|
| 332 | if len(self.beat_locations) < 10: |
---|
| 333 | outstr = "%.2f bpm (uncertain)" % median_bpm |
---|
| 334 | else: |
---|
| 335 | outstr = "%.2f bpm" % median_bpm |
---|
| 336 | sys.stdout.write(outstr + '\n') |
---|
[bd72039] | 337 | |
---|
[1d2cc5e] | 338 | class process_notes(default_process): |
---|
| 339 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
---|
| 340 | def __init__(self, args): |
---|
[af98cb8] | 341 | self.parse_options(args, self.valid_opts) |
---|
[1d2cc5e] | 342 | self.notes = aubio.notes(**self.options) |
---|
| 343 | super(process_notes, self).__init__(args) |
---|
| 344 | def __call__(self, block): |
---|
| 345 | return self.notes(block) |
---|
[6288806] | 346 | def repr_res(self, res, frames_read, samplerate): |
---|
[1d2cc5e] | 347 | if res[2] != 0: # note off |
---|
[6288806] | 348 | fmt_out = self.time2string(frames_read, samplerate) |
---|
[1d2cc5e] | 349 | sys.stdout.write(fmt_out + '\n') |
---|
| 350 | if res[0] != 0: # note on |
---|
| 351 | lastmidi = res[0] |
---|
| 352 | fmt_out = "%f\t" % lastmidi |
---|
[6288806] | 353 | fmt_out += self.time2string(frames_read, samplerate) |
---|
[1d2cc5e] | 354 | sys.stdout.write(fmt_out) # + '\t') |
---|
[5ab3c4e] | 355 | def flush(self, frames_read, samplerate): |
---|
| 356 | eof = self.time2string(frames_read, samplerate) |
---|
| 357 | sys.stdout.write(eof + '\n') |
---|
[1d2cc5e] | 358 | |
---|
| 359 | class process_mfcc(default_process): |
---|
| 360 | def __init__(self, args): |
---|
[af98cb8] | 361 | valid_opts1 = ['hop_size', 'buf_size'] |
---|
| 362 | self.parse_options(args, valid_opts1) |
---|
| 363 | self.remap_pvoc_options(self.options) |
---|
| 364 | self.pv = aubio.pvoc(**self.options) |
---|
| 365 | |
---|
| 366 | valid_opts2 = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate'] |
---|
| 367 | self.parse_options(args, valid_opts2) |
---|
| 368 | self.mfcc = aubio.mfcc(**self.options) |
---|
[1d2cc5e] | 369 | |
---|
[af98cb8] | 370 | # remember all options |
---|
| 371 | self.parse_options(args, list(set(valid_opts1 + valid_opts2))) |
---|
[1d2cc5e] | 372 | |
---|
| 373 | super(process_mfcc, self).__init__(args) |
---|
| 374 | |
---|
| 375 | def __call__(self, block): |
---|
| 376 | fftgrain = self.pv(block) |
---|
| 377 | return self.mfcc(fftgrain) |
---|
[6288806] | 378 | def repr_res(self, res, frames_read, samplerate): |
---|
| 379 | fmt_out = self.time2string(frames_read, samplerate) |
---|
[1d2cc5e] | 380 | fmt_out += ' '.join(["% 9.7f" % f for f in res.tolist()]) |
---|
| 381 | sys.stdout.write(fmt_out + '\n') |
---|
| 382 | |
---|
| 383 | class process_melbands(default_process): |
---|
| 384 | def __init__(self, args): |
---|
| 385 | self.args = args |
---|
| 386 | valid_opts = ['hop_size', 'buf_size'] |
---|
[af98cb8] | 387 | self.parse_options(args, valid_opts) |
---|
| 388 | self.remap_pvoc_options(self.options) |
---|
| 389 | self.pv = aubio.pvoc(**self.options) |
---|
[1d2cc5e] | 390 | |
---|
| 391 | valid_opts = ['buf_size', 'n_filters'] |
---|
[af98cb8] | 392 | self.parse_options(args, valid_opts) |
---|
| 393 | self.remap_pvoc_options(self.options) |
---|
| 394 | self.filterbank = aubio.filterbank(**self.options) |
---|
[1d2cc5e] | 395 | self.filterbank.set_mel_coeffs_slaney(args.samplerate) |
---|
| 396 | |
---|
| 397 | super(process_melbands, self).__init__(args) |
---|
| 398 | def __call__(self, block): |
---|
| 399 | fftgrain = self.pv(block) |
---|
| 400 | return self.filterbank(fftgrain) |
---|
[6288806] | 401 | def repr_res(self, res, frames_read, samplerate): |
---|
| 402 | fmt_out = self.time2string(frames_read, samplerate) |
---|
[1d2cc5e] | 403 | fmt_out += ' '.join(["% 9.7f" % f for f in res.tolist()]) |
---|
| 404 | sys.stdout.write(fmt_out + '\n') |
---|
| 405 | |
---|
[896c3a8] | 406 | class process_quiet(default_process): |
---|
| 407 | def __init__(self, args): |
---|
| 408 | self.args = args |
---|
| 409 | valid_opts = ['hop_size', 'silence'] |
---|
| 410 | self.parse_options(args, valid_opts) |
---|
| 411 | self.wassilence = 1 |
---|
| 412 | |
---|
| 413 | if args.silence is not None: |
---|
| 414 | self.silence = args.silence |
---|
| 415 | super(process_quiet, self).__init__(args) |
---|
| 416 | |
---|
| 417 | def __call__(self, block): |
---|
| 418 | if aubio.silence_detection(block, self.silence) == 1: |
---|
[70762c5] | 419 | if self.wassilence != 1: |
---|
| 420 | self.wassilence = 1 |
---|
| 421 | return 2 # newly found silence |
---|
| 422 | return 1 # silence again |
---|
[896c3a8] | 423 | else: |
---|
[70762c5] | 424 | if self.wassilence != 0: |
---|
| 425 | self.wassilence = 0 |
---|
| 426 | return -1 # newly found noise |
---|
| 427 | return 0 # noise again |
---|
[896c3a8] | 428 | |
---|
| 429 | def repr_res(self, res, frames_read, samplerate): |
---|
| 430 | fmt_out = None |
---|
[70762c5] | 431 | if res == -1: |
---|
[896c3a8] | 432 | fmt_out = "NOISY: " |
---|
[70762c5] | 433 | if res == 2: |
---|
[896c3a8] | 434 | fmt_out = "QUIET: " |
---|
| 435 | if fmt_out is not None: |
---|
| 436 | fmt_out += self.time2string(frames_read, samplerate) |
---|
| 437 | sys.stdout.write(fmt_out + '\n') |
---|
| 438 | |
---|
[8e2f36a] | 439 | def main(): |
---|
[1d2cc5e] | 440 | parser = aubio_parser() |
---|
| 441 | args = parser.parse_args() |
---|
[2615dd9] | 442 | if 'show_version' in args and args.show_version: |
---|
[1d2cc5e] | 443 | sys.stdout.write('aubio version ' + aubio.version + '\n') |
---|
| 444 | sys.exit(0) |
---|
[2615dd9] | 445 | elif 'verbose' in args and args.verbose > 3: |
---|
| 446 | sys.stderr.write('aubio version ' + aubio.version + '\n') |
---|
[8a3acad] | 447 | if 'command' not in args or args.command is None or args.command in ['help']: |
---|
[2615dd9] | 448 | # no command given, print help and return 1 |
---|
[1d2cc5e] | 449 | parser.print_help() |
---|
[8a3acad] | 450 | if args.command and args.command in ['help']: |
---|
| 451 | sys.exit(0) |
---|
| 452 | else: |
---|
| 453 | sys.exit(1) |
---|
[1d2cc5e] | 454 | elif not args.source_uri and not args.source_uri2: |
---|
| 455 | sys.stderr.write("Error: a source is required\n") |
---|
| 456 | parser.print_help() |
---|
| 457 | sys.exit(1) |
---|
| 458 | elif args.source_uri2 is not None: |
---|
| 459 | args.source_uri = args.source_uri2 |
---|
| 460 | try: |
---|
| 461 | # open source_uri |
---|
| 462 | with aubio.source(args.source_uri, hop_size=args.hop_size, |
---|
| 463 | samplerate=args.samplerate) as a_source: |
---|
[af98cb8] | 464 | # always update args.samplerate to native samplerate, in case |
---|
| 465 | # source was opened with args.samplerate=0 |
---|
[1d2cc5e] | 466 | args.samplerate = a_source.samplerate |
---|
| 467 | # create the processor for this subcommand |
---|
| 468 | processor = args.process(args) |
---|
| 469 | frames_read = 0 |
---|
| 470 | while True: |
---|
| 471 | # read new block from source |
---|
| 472 | block, read = a_source() |
---|
| 473 | # execute processor on this block |
---|
| 474 | res = processor(block) |
---|
| 475 | # print results for this block |
---|
| 476 | if args.verbose > 0: |
---|
[6288806] | 477 | processor.repr_res(res, frames_read, a_source.samplerate) |
---|
[1d2cc5e] | 478 | # increment total number of frames read |
---|
| 479 | frames_read += read |
---|
| 480 | # exit loop at end of file |
---|
| 481 | if read < a_source.hop_size: break |
---|
[5ab3c4e] | 482 | # flush the processor if needed |
---|
| 483 | processor.flush(frames_read, a_source.samplerate) |
---|
[1d2cc5e] | 484 | if args.verbose > 1: |
---|
| 485 | fmt_string = "read {:.2f}s" |
---|
| 486 | fmt_string += " ({:d} samples in {:d} blocks of {:d})" |
---|
| 487 | fmt_string += " from {:s} at {:d}Hz\n" |
---|
| 488 | sys.stderr.write(fmt_string.format( |
---|
| 489 | frames_read/float(a_source.samplerate), |
---|
| 490 | frames_read, |
---|
| 491 | frames_read // a_source.hop_size + 1, |
---|
| 492 | a_source.hop_size, |
---|
| 493 | a_source.uri, |
---|
| 494 | a_source.samplerate)) |
---|
[657a20a] | 495 | except KeyboardInterrupt: |
---|
[1d2cc5e] | 496 | sys.exit(1) |
---|