Skip to content

Commit

Permalink
MIDI Tracks maintain always absolute ticks, except when creating the
Browse files Browse the repository at this point in the history
data stream
  • Loading branch information
Filip Korzeniowski committed Feb 18, 2016
1 parent 041702f commit c344bbe
Showing 1 changed file with 9 additions and 25 deletions.
34 changes: 9 additions & 25 deletions madmom/utils/midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,8 +1073,6 @@ class MIDITrack(object):
----------
events : list
MIDI events.
relative_timing : bool, optional
Indicate if the ticks of the events are in relative or absolute timing.
"""

Expand All @@ -1084,8 +1082,9 @@ def __init__(self, events=None, relative_timing=True):
else:
self.events = events
self.relative_timing = relative_timing
self._make_ticks_abs()

def make_ticks_abs(self):
def _make_ticks_abs(self):
"""
Make the track's timing information absolute.
Expand All @@ -1097,7 +1096,7 @@ def make_ticks_abs(self):
running_tick = event.tick
self.relative_timing = False

def make_ticks_rel(self):
def _make_ticks_rel(self):
"""
Make the track's timing information relative.
Expand All @@ -1116,7 +1115,7 @@ def data_stream(self):
"""
# first make sure the timing information is relative
self.make_ticks_rel()
self._make_ticks_rel()
# and unset the status message
status = None
# then encode all events of the track
Expand Down Expand Up @@ -1146,6 +1145,10 @@ def data_stream(self):
raise ValueError("Unknown MIDI Event: " + str(event))
# prepare the track header
track_header = b'MTrk%s' % struct.pack(">L", len(track_data))

# convert back to absolute ticks
self._make_ticks_abs()

# return the track header + data
return track_header + track_data

Expand Down Expand Up @@ -1338,8 +1341,6 @@ def tempi(self):
Array with tempi (tick, seconds per tick, cumulative time).
"""
# first convert all events to have absolute tick counts
self.make_ticks_abs()
# create an empty tempo list
tempi = None
for track in self.tracks:
Expand Down Expand Up @@ -1384,7 +1385,6 @@ def time_signatures(self):
Array with time signatures (tick, numerator, denominator).
"""
self.make_ticks_abs()
signatures = None
for track in self.tracks:
# get a list with time signature events
Expand Down Expand Up @@ -1424,7 +1424,6 @@ def notes(self, note_time_unit='s'):
Array with notes (onset time, pitch, duration, velocity).
"""
self.make_ticks_abs()
# list for all notes
notes = []
# dictionaries for storing the last onset and velocity per pitch
Expand Down Expand Up @@ -1466,6 +1465,7 @@ def notes(self, note_time_unit='s'):
notes = np.asarray(notes, dtype=np.float)

# convert onset times and durations from ticks to a meaningful unit
# and return the notes
if note_time_unit == 's':
return self._note_ticks_to_seconds(notes)
elif note_time_unit == 'b':
Expand Down Expand Up @@ -1548,22 +1548,6 @@ def _note_ticks_to_seconds(self, notes):
# return notes
return notes

def make_ticks_abs(self):
"""
Make the timing information of all tracks absolute.
"""
for track in self.tracks:
track.make_ticks_abs()

def make_ticks_rel(self):
"""
Make the timing information of all tracks relative.
"""
for track in self.tracks:
track.make_ticks_rel()

# methods for writing MIDI stuff
@property
def data_stream(self):
Expand Down

0 comments on commit c344bbe

Please sign in to comment.