Skip to content

Commit

Permalink
fix MMM when not using time signatures, adding a test file
Browse files Browse the repository at this point in the history
  • Loading branch information
Natooz committed Oct 10, 2023
1 parent 70a2a45 commit 9ef5c23
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion miditok/tokenizations/mmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def _add_time_events(self, events: List[Event]) -> List[Event]:
all_events = [Event("Bar", "Start", 0)]

# Time events
time_sig_change = self._current_midi_metadata["time_sig_changes"][0]
if self.config.use_time_signatures and len(self._current_midi_metadata["time_sig_changes"]) > 0:
time_sig_change = self._current_midi_metadata["time_sig_changes"][0]
else:
time_sig_change = TimeSignature(*TIME_SIGNATURE, 0)
ticks_per_bar = self._compute_ticks_per_bar(time_sig_change, time_division)
bar_at_last_ts_change = 0
previous_tick = 0
Expand Down
Binary file added tests/One_track_MIDIs/6338816_Etude No. 4.mid
Binary file not shown.
5 changes: 4 additions & 1 deletion tests/test_one_track.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)

TIME_SIGNATURE_RANGE.update({2: [2, 3, 4]})
TIME_SIGNATURE_RANGE[4].append(8)
BEAT_RES_TEST = {(0, 16): 8}
TOKENIZER_PARAMS = {
"beat_res": BEAT_RES_TEST,
Expand Down Expand Up @@ -66,6 +67,7 @@ def test_one_track_midi_to_tokens_to_midi(
for i, file_path in enumerate(tqdm(files, desc="Testing One Track")):
# Reads the midi
midi = MidiFile(file_path)
# midi.instruments = [midi.instruments[0]]
# Will store the tracks tokenized / detokenized, to be saved in case of errors
for ti, track in enumerate(midi.instruments):
track.name = f"original {ti} not quantized"
Expand Down Expand Up @@ -105,8 +107,9 @@ def test_one_track_midi_to_tokens_to_midi(
# For Octuple, as tempo is only carried at notes times, we need to adapt their times for comparison
# Same for CPWord which carries tempo with Position (for notes)
if tokenization in ["Octuple", "CPWord"]:
# We use the first track only, as it is the one for which tempos are decoded
adapt_tempo_changes_times(
midi_to_compare.instruments, midi_to_compare.tempo_changes
[midi_to_compare.instruments[0]], midi_to_compare.tempo_changes
)
# When the tokenizer only decoded tempo changes different from the last tempo val
if tokenization in ["CPWord"]:
Expand Down

0 comments on commit 9ef5c23

Please sign in to comment.