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 authored and Sebastian Böck committed Feb 18, 2016
1 parent ce4cfb0 commit e4282a1
Showing 1 changed file with 18 additions and 39 deletions.
57 changes: 18 additions & 39 deletions madmom/utils/midi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,41 +1073,35 @@ class MIDITrack(object):
----------
events : list
MIDI events.
relative_timing : bool, optional
Indicate if the ticks of the events are in relative or absolute timing.
"""

def __init__(self, events=None, relative_timing=True):
def __init__(self, events=None):
if events is None:
self.events = []
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.
"""
if self.relative_timing:
running_tick = 0
for event in self.events:
event.tick += running_tick
running_tick = event.tick
self.relative_timing = False
running_tick = 0
for event in self.events:
event.tick += running_tick
running_tick = event.tick

def make_ticks_rel(self):
def _make_ticks_rel(self):
"""
Make the track's timing information relative.
"""
if not self.relative_timing:
running_tick = 0
for event in self.events:
event.tick -= running_tick
running_tick += event.tick
self.relative_timing = True
running_tick = 0
for event in self.events:
event.tick -= running_tick
running_tick += event.tick

@property
def data_stream(self):
Expand All @@ -1116,7 +1110,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 +1140,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 +1336,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 +1380,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 +1419,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 +1460,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 +1543,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 e4282a1

Please sign in to comment.