Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add key_signatures property to MidiFile, analogous to time_signatures #437

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions madmom/io/midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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
Expand Down Expand Up @@ -324,6 +325,34 @@ def time_signatures(self):
# return time signatures
return np.asarray(signatures, dtype=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', '<f4'), ('key', 'U3')])

@property
def notes(self):
"""
Expand Down