Skip to content

Instantly share code, notes, and snippets.

@agrif
Last active July 5, 2026 01:59
Show Gist options
  • Select an option

  • Save agrif/fccdcc4b94eb6aa76916442f71f796a5 to your computer and use it in GitHub Desktop.

Select an option

Save agrif/fccdcc4b94eb6aa76916442f71f796a5 to your computer and use it in GitHub Desktop.
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "click~=8.4",
# "librosa~=0.11.0",
# "vamp~=1.1.0",
# ]
#
# [tool.uv.extra-build-dependencies]
# vamp = ["numpy~=2.5"]
# ///
import pathlib
import click
import librosa
import numpy
import scipy
import vamp
TOLERANCE = 0.003
def evaluate_beats(x, ts):
offset, period = x
diffs = (ts - offset) / period
diffs %= 1.0
diffs[diffs > 0.5] -= 1.0
diffs = diffs * diffs
return diffs.mean()
@click.command
@click.argument('path', type=pathlib.Path)
def main(path):
data, rate = librosa.load(path)
info = vamp.collect(data, rate, 'qm-vamp-plugins:qm-barbeattracker')
info = info['list']
ts = numpy.array([b['timestamp'].to_float() for b in info[2:]])
ps = ts[1:] - ts[:-1]
period = ps.mean()
bpm = 60 / period
offs = ((ts / period) % 1.0) * period
offs[offs > period / 2] -= period
beat_offset = offs.mean()
opt = scipy.optimize.minimize(evaluate_beats, [beat_offset, period],
method='powell', args=(ts,),
options={
'xtol': 1e-8,
'ftol': 1e-8,
'disp': True,
})
opt_offset, opt_period = opt.x
opt_bpm = 60 / opt_period
print(f'raw: {bpm} bpm + {beat_offset * 1000} ms')
print(f'opt: {opt_bpm} bpm + {opt_offset * 1000} ms')
if __name__ == "__main__":
main()
{
"version": 1,
"uniqueId": 3395510822,
"songName": "RYTHM_OF_THE_NIGHT",
"performedBy": [],
"writtenBy": [],
"seed": 1147525942,
"tempo": 128.19999694824219,
"customTempoSections": [],
"beatOffset": 502,
"startSongOffset": 0,
"endSongOffset": 0,
"uEAssetName": "RYTHM_OF_THE_NIGHT",
"originalAudioFileHash": "a7e3160d51bff2fae56004a3fea7a8f0",
"originalAudioFilePath": "Z:/home/agrif/local/sprunk/nonstoppop/RYTHM_OF_THE_NIGHT.ogg"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment