Skip to content

Commit

Permalink
control change dedup removed in dump, type hints and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Natooz committed Nov 15, 2023
1 parent c771a87 commit 6086348
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 57 deletions.
112 changes: 56 additions & 56 deletions miditoolkit/midi/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def __get_instrument(program, channel, track, create_new):
return instruments

def get_tick_to_time_mapping(self):
return _get_tick_to_time_mapping(
return _get_tick_to_second_mapping(
self.ticks_per_beat, self.max_tick, self.tempo_changes
)

Expand Down Expand Up @@ -473,8 +473,8 @@ def event_compare(event1, event2):

# 3. Lyrics
lyrics_list = []
for l in self.lyrics:
lyrics_list.append(mido.MetaMessage("lyrics", time=l.time, text=l.text))
for lyr in self.lyrics:
lyrics_list.append(mido.MetaMessage("lyrics", time=lyr.time, text=lyr.text))

# 4. Markers
markers_list = []
Expand Down Expand Up @@ -607,34 +607,33 @@ def event_compare(event1, event2):

# Add all note events
for note in instrument.notes:
if segment:
note = _check_note_within_range(
note, start_tick, end_tick, shift=True
)
if note:
track.append(
mido.Message(
"note_on",
time=note.start,
channel=channel,
note=note.pitch,
velocity=note.velocity,
end=note.end,
)
if segment and not _is_note_within_tick_range(
note, start_tick, end_tick, shift, True
):
continue
track.append(
mido.Message(
"note_on",
time=note.start,
channel=channel,
note=note.pitch,
velocity=note.velocity,
end=note.end,
)
# Also need a note-off event (note on with velocity 0)
track.append(
mido.Message(
"note_off",
time=note.end,
channel=channel,
note=note.pitch,
velocity=note.velocity,
)
)
# Also need a note-off event
track.append(
mido.Message(
"note_off",
time=note.end,
channel=channel,
note=note.pitch,
velocity=note.velocity,
)
)
track = sorted(track, key=functools.cmp_to_key(event_compare))

memo = 0
"""memo = 0
i = 0
while i < len(track):
# print(i)
Expand All @@ -647,7 +646,7 @@ def event_compare(event1, event2):
memo = track[i].value
i += 1
else:
i += 1
i += 1"""

# i = 0
# while i <= len(cc_list)-1:
Expand Down Expand Up @@ -683,21 +682,32 @@ def event_compare(event1, event2):
midi_parsed.save(file=file)


def _check_note_within_range(note, st, ed, shift=True):
tmp_st = max(st, note.start)
tmp_ed = max(st, min(note.end, ed))

def _is_note_within_tick_range(
note: Note,
start_tick: int,
end_tick: int,
shift: bool = True,
adapt_note_times: bool = False,
) -> bool:
# | |
# **** | *** **|* *****
tmp_st = max(start_tick, note.start)
tmp_ed = max(start_tick, min(note.end, end_tick))
if (tmp_ed - tmp_st) <= 0:
return None
return False

if shift:
tmp_st -= st
tmp_ed -= st
note.start = int(tmp_st)
note.end = int(tmp_ed)
return note
tmp_st -= start_tick
tmp_ed -= start_tick
if adapt_note_times:
note.start = tmp_st
note.end = tmp_ed
return True


def _include_meta_events_within_range(events, st, ed, shift=True, front=True):
def _include_meta_events_within_range(
events, st: int, ed: int, shift: bool = True, front: bool = True
):
"""
For time, key signature
"""
Expand Down Expand Up @@ -738,25 +748,15 @@ def _include_meta_events_within_range(events, st, ed, shift=True, front=True):
return proc_events


def _find_nearest_np(array, value):
return (np.abs(array - value)).argmin()


def _get_tick_index_by_seconds(sec, tick_to_time):
if not isinstance(sec, float):
raise ValueError("Seconds should be float")
def _get_tick_eq_of_second(sec: Union[float, int], tick_to_time: np.ndarray) -> int:
return int((np.abs(tick_to_time - sec)).argmin())

if isinstance(sec, list) or isinstance(sec, tuple):
return [_find_nearest_np(tick_to_time, s) for s in sec]
else:
return _find_nearest_np(tick_to_time, sec)


def _get_tick_to_time_mapping(ticks_per_beat, max_tick, tempo_changes):
def _get_tick_to_second_mapping(
ticks_per_beat: int, max_tick: int, tempo_changes: Sequence[TempoChange]
) -> np.ndarray:
tick_to_time = np.zeros(max_tick + 1)
num_tempi = len(tempo_changes)

fianl_tick = max_tick
acc_time = 0

for idx in range(num_tempi):
Expand All @@ -768,9 +768,9 @@ def _get_tick_to_time_mapping(ticks_per_beat, max_tick, tempo_changes):
seconds_per_tick = seconds_per_beat / float(ticks_per_beat)

# set end tick of interval
end_tick = tempo_changes[idx + 1].time if (idx + 1) < num_tempi else fianl_tick
end_tick = tempo_changes[idx + 1].time if (idx + 1) < num_tempi else max_tick

# wrtie interval
# write interval
ticks = np.arange(end_tick - start_tick + 1)
tick_to_time[start_tick : end_tick + 1] = acc_time + seconds_per_tick * ticks
acc_time = tick_to_time[end_tick]
Expand Down
1 change: 0 additions & 1 deletion miditoolkit/pianoroll/vis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import numpy as np
from matplotlib import pyplot as plt

Expand Down

0 comments on commit 6086348

Please sign in to comment.