Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -58,6 +58,15 @@ def aubio_parser(): · 57 unmodified lines | |||
| 58 | parser_add_buf_hop_size(subparser, buf_size=1024, hop_size=512) | 58 | parser_add_buf_hop_size(subparser, buf_size=1024, hop_size=512) |
| 59 | parser_add_time_format(subparser) | 59 | parser_add_time_format(subparser) |
| 60 | parser_add_verbose_help(subparser) | 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 | subparser.set_defaults(process=process_tempo) | 70 | subparser.set_defaults(process=process_tempo) |
| 62 | 71 | ||
| 63 | # notes subcommand | 72 | # notes subcommand |
| @@ -237,12 +246,12 @@ class process_pitch(default_process): · 173 unmodified lines | |||
| 237 | fmt_out = self.time2string(frames_read, samplerate) | 246 | fmt_out = self.time2string(frames_read, samplerate) |
| 238 | sys.stdout.write(fmt_out + "%.6f\n" % res[0]) | 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 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] | 250 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
| 242 | def __init__(self, args): | 251 | def __init__(self, args): |
| 243 | self.options = parse_options(args, self.valid_opts) | 252 | self.options = parse_options(args, self.valid_opts) |
| 244 | self.tempo = aubio.tempo(**self.options) | 253 | self.tempo = aubio.tempo(**self.options) |
| 245 | super(process_tempo, self).__init__(args) | 254 | super(process_beat, self).__init__(args) |
| 246 | def __call__(self, block): | 255 | def __call__(self, block): |
| 247 | return self.tempo(block) | 256 | return self.tempo(block) |
| 248 | def repr_res(self, res, frames_read, samplerate): | 257 | def repr_res(self, res, frames_read, samplerate): |
| @@ -250,6 +259,19 @@ class process_tempo(default_process): · 1 unmodified line | |||
| 250 | outstr = self.time2string(self.tempo.get_last(), samplerate) | 259 | outstr = self.time2string(self.tempo.get_last(), samplerate) |
| 251 | sys.stdout.write(outstr + '\n') | 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 | class process_notes(default_process): | 275 | class process_notes(default_process): |
| 254 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] | 276 | valid_opts = ['method', 'hop_size', 'buf_size', 'samplerate'] |
| 255 | def __init__(self, args): | 277 | def __init__(self, args): |