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