-
Notifications
You must be signed in to change notification settings - Fork 1
/
bpm_facet.py
31 lines (24 loc) · 898 Bytes
/
bpm_facet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from orchard.utils.dig import try_dig
from orchard.vitals.arguments_decorator import with_arguments
def is_bpm_event(evt):
return evt["type"] in ["PlaySong", "SetBeatsPerMinute"]
def get_bpm_from_event(evt):
# then, the bpm is either in the ["bpm"] or ["beatsPerMinute"] key
try:
return evt["bpm"]
except KeyError:
return evt["beatsPerMinute"]
@with_arguments("obj", "toml")
def bpm_facet(obj, toml):
# is it declared in the TOML?
if (
toml is not None
and try_dig(["bpm", "max"], toml)
and try_dig(["bpm", "min"], toml)
):
return try_dig(["bpm", "max"], toml), try_dig(["bpm", "min"], toml)
bpms = [get_bpm_from_event(e) for e in obj["events"] if is_bpm_event(e)]
# then, the bpm is either in the ["bpm"] or ["beatsPerMinute"] key
if len(bpms) == 0:
return 0, 0
return max(bpms), min(bpms)