From 1601ba64945727dc70695776e39e95221c478063 Mon Sep 17 00:00:00 2001 From: Maarten Grachten Date: Thu, 4 Jul 2019 18:21:44 +0200 Subject: [PATCH] add key_signatures property to MidiFile, analogous to time_signatures --- madmom/io/midi.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/madmom/io/midi.py b/madmom/io/midi.py index 9d03fa043..34644954b 100644 --- a/madmom/io/midi.py +++ b/madmom/io/midi.py @@ -14,6 +14,7 @@ DEFAULT_TEMPO = 500000 # microseconds per quarter note (i.e. 120 bpm in 4/4) DEFAULT_TICKS_PER_BEAT = 480 # ticks per quarter note DEFAULT_TIME_SIGNATURE = (4, 4) +DEFAULT_KEY_SIGNATURE = 'C' # TODO: remove these unit conversion functions after upstream PR is merged @@ -323,6 +324,34 @@ def time_signatures(self): # return time signatures return np.asarray(signatures, dtype=np.float) + @property + def key_signatures(self): + """ + Key signatures of the MIDI file. + + Returns + ------- + key_signatures : numpy array + Structured array with key signatures (time, key). + + Notes + ----- + The time will be given in the unit set by `unit`. + + """ + # list for all key signatures + signatures = [] + # process all events + for msg in self: + if msg.type == 'key_signature': + signatures.append((msg.time, msg.key)) + # # make sure a signature is set (and occurs at time 0) + if not signatures or signatures[0][0] > 0: + signatures.insert(0, (0, DEFAULT_KEY_SIGNATURE)) + + # return key signatures + return np.array(signatures, dtype=[('time', '