Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -0,0 +1,54 @@ | |||
| 1 | #! /usr/bin/env python | ||
| 2 | |||
| 3 | from aubio import source, tempo | ||
| 4 | from numpy import median, diff | ||
| 5 | |||
| 6 | def get_file_bpm(path, params = {}): | ||
| 7 | """ Calculate the beats per minute (bpm) of a given file. | ||
| 8 | path: path to the file | ||
| 9 | param: dictionary of parameters | ||
| 10 | """ | ||
| 11 | try: | ||
| 12 | win_s = params['win_s'] | ||
| 13 | samplerate = params['samplerate'] | ||
| 14 | hop_s = params['hop_s'] | ||
| 15 | except: | ||
| 16 | """ | ||
| 17 | # super fast | ||
| 18 | samplerate, win_s, hop_s = 4000, 128, 64 | ||
| 19 | # fast | ||
| 20 | samplerate, win_s, hop_s = 8000, 512, 128 | ||
| 21 | """ | ||
| 22 | # default: | ||
| 23 | samplerate, win_s, hop_s = 44100, 1024, 512 | ||
| 24 | |||
| 25 | s = source(path, samplerate, hop_s) | ||
| 26 | samplerate = s.samplerate | ||
| 27 | o = tempo("specdiff", win_s, hop_s, samplerate) | ||
| 28 | # List of beats, in samples | ||
| 29 | beats = [] | ||
| 30 | # Total number of frames read | ||
| 31 | total_frames = 0 | ||
| 32 | |||
| 33 | while True: | ||
| 34 | samples, read = s() | ||
| 35 | is_beat = o(samples) | ||
| 36 | if is_beat: | ||
| 37 | this_beat = o.get_last_s() | ||
| 38 | beats.append(this_beat) | ||
| 39 | #if o.get_confidence() > .2 and len(beats) > 2.: | ||
| 40 | # break | ||
| 41 | total_frames += read | ||
| 42 | if read < hop_s: | ||
| 43 | break | ||
| 44 | |||
| 45 | # Convert to periods and to bpm | ||
| 46 | bpms = 60./diff(beats) | ||
| 47 | b = median(bpms) | ||
| 48 | return b | ||
| 49 | |||
| 50 | if __name__ == '__main__': | ||
| 51 | import sys | ||
| 52 | for f in sys.argv[1:]: | ||
| 53 | bpm = get_file_bpm(f) | ||
| 54 | print "%6s" % ("%.2f" % bpm), f | ||