Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -58,6 +58,15 @@ def aubio_parser(): · 57 unmodified lines | ||
| 58 | 58 | parser_add_buf_hop_size(subparser, buf_size=1024, hop_size=512) |
| 59 | 59 | parser_add_time_format(subparser) |
| 60 | 60 | parser_add_verbose_help(subparser) |
| 61 | + subparser.set_defaults(process=process_beat) | |
| 62 | + | |
| 63 | + # tempo subcommand | |
| 64 | + subparser = subparsers.add_parser('tempo', | |
| 65 | + help='get locations of beats') | |
| 66 | + parser_add_input(subparser) | |
| 67 | + parser_add_buf_hop_size(subparser, buf_size=1024, hop_size=512) | |
| 68 | + parser_add_time_format(subparser) | |
| 69 | + parser_add_verbose_help(subparser) | |
| 61 | 70 | subparser.set_defaults(process=process_tempo) |
| 62 | 71 | |
| 63 | 72 | # notes subcommand |
| @@ -237,12 +246,12 @@ class process_pitch(default_process): · 173 unmodified lines | ||
| 237 | 246 | fmt_out = self.time2string(frames_read, samplerate) |
| 238 | 247 | sys.stdout.write(fmt_out + "%.6f\n" % res[0]) |
| 239 | 248 | |
| 240 | −class process_tempo(default_process): | |
| 249 | +class process_beat(default_process): | |
| 241 | 250 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
| 242 | 251 | def __init__(self, args): |
| 243 | 252 | self.options = parse_options(args, self.valid_opts) |
| 244 | 253 | self.tempo = aubio.tempo(**self.options) |
| 245 | − super(process_tempo, self).__init__(args) | |
| 254 | + super(process_beat, self).__init__(args) | |
| 246 | 255 | def __call__(self, block): |
| 247 | 256 | return self.tempo(block) |
| 248 | 257 | def repr_res(self, res, frames_read, samplerate): |
| @@ -250,6 +259,19 @@ class process_tempo(default_process): · 1 unmodified line | ||
| 250 | 259 | outstr = self.time2string(self.tempo.get_last(), samplerate) |
| 251 | 260 | sys.stdout.write(outstr + '\n') |
| 252 | 261 | |
| 262 | +class process_tempo(process_beat): | |
| 263 | + def __init__(self, args): | |
| 264 | + super(process_tempo, self).__init__(args) | |
| 265 | + self.beat_locations = [] | |
| 266 | + def repr_res(self, res, frames_read, samplerate): | |
| 267 | + if res[0] != 0: | |
| 268 | + self.beat_locations.append(self.tempo.get_last_s()) | |
| 269 | + def flush(self, frames_read, samplerate): | |
| 270 | + import numpy as np | |
| 271 | + bpms = 60./ np.diff(self.beat_locations) | |
| 272 | + median_bpm = np.mean(bpms) | |
| 273 | + sys.stdout.write('%.2f bpm' % median_bpm + '\n') | |
| 274 | + | |
| 253 | 275 | class process_notes(default_process): |
| 254 | 276 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
| 255 | 277 | def __init__(self, args): |