Skip to content

Instantly share code, notes, and snippets.

@jiaaro
Created March 19, 2015 16:17
Show Gist options
  • Save jiaaro/faa96fabd252b8552066 to your computer and use it in GitHub Desktop.
Save jiaaro/faa96fabd252b8552066 to your computer and use it in GitHub Desktop.
Super simple BPM detection with pydub
from pydub import AudioSegment
from pydub.silence import detect_nonsilent
seg = AudioSegment.from_file("./ghosts_and_stuff.m4a")
# reduce loudness of sounds over 120Hz (focus on bass drum, etc)
seg = seg.low_pass_filter(120.0)
# we'll call a beat: anything above average loudness
beat_loudness = seg.dBFS
# the fastest tempo we'll allow is 240 bpm (60000ms / 240beats)
minimum_silence = int(60000 / 240.0)
nonsilent_times = detect_nonsilent(seg, minimum_silence, beat_loudness)
spaces_between_beats = []
last_t = nonsilent_times[0][0]
for peak_start, _ in nonsilent_times[1:]:
spaces_between_beats.append(peak_start - last_t)
last_t = peak_start
# We'll base our guess on the median space between beats
spaces_between_beats = sorted(spaces_between_beats)
space = spaces_between_beats[len(spaces_between_beats) / 2]
bpm = 60000 / space
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment