Showing the changes to python/demos (1 of 1 file). Show all
Unified · Split
| @@ -3,7 +3,7 @@ · 2 unmodified lines | ||
| 3 | 3 | from aubio import source, tempo |
| 4 | 4 | from numpy import median, diff |
| 5 | 5 | |
| 6 | −def get_file_bpm(path, params = None): | |
| 6 | +def get_file_bpm(path, params=None): | |
| 7 | 7 | """ Calculate the beats per minute (bpm) of a given file. |
| 8 | 8 | path: path to the file |
| 9 | 9 | param: dictionary of parameters |
| @@ -51,16 +51,18 @@ def get_file_bpm(path, params = None): · 41 unmodified lines | ||
| 51 | 51 | if read < hop_s: |
| 52 | 52 | break |
| 53 | 53 | |
| 54 | − # Convert to periods and to bpm | |
| 55 | − if len(beats) > 1: | |
| 56 | − if len(beats) < 4: | |
| 57 | − print("few beats found in {:s}".format(path)) | |
| 58 | − bpms = 60./diff(beats) | |
| 59 | − b = median(bpms) | |
| 60 | − else: | |
| 61 | − b = 0 | |
| 62 | − print("not enough beats found in {:s}".format(path)) | |
| 63 | − return b | |
| 54 | + def beats_to_bpm(beats, path): | |
| 55 | + # if enough beats are found, convert to periods then to bpm | |
| 56 | + if len(beats) > 1: | |
| 57 | + if len(beats) < 4: | |
| 58 | + print("few beats found in {:s}".format(path)) | |
| 59 | + bpms = 60./diff(beats) | |
| 60 | + return median(bpms) | |
| 61 | + else: | |
| 62 | + print("not enough beats found in {:s}".format(path)) | |
| 63 | + return 0 | |
| 64 | + | |
| 65 | + return beats_to_bpm(beats, path) | |
| 64 | 66 | |
| 65 | 67 | if __name__ == '__main__': |
| 66 | 68 | import argparse |