Skip to content

Commit

Permalink
fix tempo handling of multitrack MIDI files; fixes #218
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Böck committed Oct 3, 2016
1 parent 184bdcf commit aea4451
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Release Notes
Version 0.15.dev0
-----------------

Other changes:

* Fix tempo handling of multi-track MIDI files (#219)

Version 0.14.1 (release date: 2016-08-01)
-----------------------------------------

Expand Down
8 changes: 5 additions & 3 deletions madmom/utils/midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1488,14 +1488,16 @@ def tempi(self, suppress_warnings=False):
warnings.warn('this method will be removed soon, do not rely on '
'its output, rather fix issue #192 ;)')
# create an empty tempo list
tempo_events = []
for i, track in enumerate(self.tracks):
# get a list with tempo events
tempo_events = [e for e in track.events if
isinstance(e, SetTempoEvent)]
track_tempo_events = [e for e in track.events if
isinstance(e, SetTempoEvent)]
# tempo events should be only in the first track of a MIDI file
if tempo_events and i > 0:
if track_tempo_events and i > 0:
raise ValueError('SetTempoEvents should be only in the first '
'track of a MIDI file.')
tempo_events.extend(track_tempo_events)

# convert to desired format (tick, microseconds per tick)
tempi = [(e.tick, e.microseconds_per_quarter_note /
Expand Down
Binary file added tests/data/annotations/multitrack.mid
Binary file not shown.
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
pj(ANNOTATIONS_PATH, 'stereo_sample.notes'),
pj(ANNOTATIONS_PATH, 'stereo_sample.notes.mirex'),
pj(ANNOTATIONS_PATH, 'stereo_sample.sv'),
pj(ANNOTATIONS_PATH, 'multitrack.mid'),
pj(ANNOTATIONS_PATH, 'piano_sample.mid'),
pj(ANNOTATIONS_PATH, 'piano_sample.notes_in_beats')]

Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils_midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ def test_notes_in_beats(self):
notes_ = midi.notes(unit='b')[:, :4]
self.assertTrue(np.allclose(notes, notes_))

def test_multitrack(self):
# read a multi-track MIDI file
midi = MIDIFile.from_file(pj(ANNOTATIONS_PATH, 'multitrack.mid'))
notes = midi.notes(unit='b')
self.assertTrue(np.allclose(notes[:4], [[0, 60, 0.5, 90, 2],
[0, 72, 2, 90, 1],
[0.5, 67, 0.5, 90, 2],
[1, 64, 0.5, 90, 2]],
atol=1e-2))
notes = midi.notes(unit='s')
self.assertTrue(np.allclose(notes[:4],
[[0, 60, 0.2272725, 90, 2],
[0, 72, 0.90814303, 90, 1],
[0.2272725, 67, 0.22632553, 90, 2],
[0.45359803, 64, 0.22821947, 90, 2]]))


# clean up
def teardown():
Expand Down

0 comments on commit aea4451

Please sign in to comment.