Avatar frames that follow the outline of the avatar (ring and parts hung on it), packs of frames that arrive from a catalog on the site by themselves, the frame shown to other members through the members service, save an avatar by user name, the diary of the mod, the seek bar on every video, the beta version line and its what is new, plus tests, the catalog builder, the sprites and the scripts that draw them. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
59 lines
2.5 KiB
Python
59 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
Finds the beats of a song so that a video can cut and move on them: the tempo, the time of every beat, the strong beats (the first of a
|
|
bar, guessed from the loudness) and the moments of sharp sounds. The result is written as JSON next to the audio.
|
|
|
|
usage (from the repository root):
|
|
py -X utf8 -B scripts/analyze_beats.py work/music/baka.mp3 # writes work/music/baka.beats.json
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import librosa
|
|
import numpy as np
|
|
|
|
|
|
def main() -> None:
|
|
path = Path(sys.argv[1])
|
|
y, sr = librosa.load(str(path), sr=22050, mono=True)
|
|
duration = float(len(y) / sr)
|
|
hop = 512
|
|
onset_env = librosa.onset.onset_strength(y=y, sr=sr, hop_length=hop)
|
|
tempo, beat_frames = librosa.beat.beat_track(onset_envelope=onset_env, sr=sr, hop_length=hop, tightness=120)
|
|
tempo = float(np.atleast_1d(tempo)[0])
|
|
beats = librosa.frames_to_time(beat_frames, sr=sr, hop_length=hop)
|
|
onsets = librosa.frames_to_time(librosa.onset.onset_detect(onset_envelope=onset_env, sr=sr, hop_length=hop, backtrack=False),
|
|
sr=sr, hop_length=hop)
|
|
# the strong beats: of the four beats of a bar, the group whose beats are louder on average
|
|
strength = [float(onset_env[min(int(f), len(onset_env) - 1)]) for f in beat_frames]
|
|
bar_start = 0
|
|
if len(strength) >= 8:
|
|
sums = [sum(strength[i::4]) for i in range(4)]
|
|
bar_start = int(np.argmax(sums))
|
|
strong = [round(float(t), 3) for i, t in enumerate(beats) if (i - bar_start) % 4 == 0]
|
|
loud = librosa.feature.rms(y=y, hop_length=hop)[0]
|
|
data = {
|
|
"file": path.name,
|
|
"duration": round(duration, 3),
|
|
"tempo": round(tempo, 2),
|
|
"beat_interval": round(60.0 / tempo, 4) if tempo else None,
|
|
"beats": [round(float(t), 3) for t in beats],
|
|
"strong_beats": strong,
|
|
"onsets": [round(float(t), 3) for t in onsets],
|
|
"loudness_per_second": [round(float(np.mean(loud[int(s * sr / hop):int((s + 1) * sr / hop)] if int(s * sr / hop) < len(loud) else 0)), 4)
|
|
for s in range(int(duration) + 1)],
|
|
}
|
|
out = path.with_suffix(".beats.json")
|
|
out.write_text(json.dumps(data, ensure_ascii=False, indent=1), encoding="utf-8")
|
|
print("tempo %.1f BPM, %d beats, %d strong, %d onsets, %.1f s -> %s" % (tempo, len(beats), len(strong), len(onsets), duration, out))
|
|
print("beats:", [round(float(t), 2) for t in beats][:40])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|