Skip to content

Commit

Permalink
Merge pull request #308 from CPJKU/score_midi_bug
Browse files Browse the repository at this point in the history
bugfix for midi scores with ts only in one track
  • Loading branch information
CarlosCancino-Chacon authored Sep 20, 2023
2 parents 03a88ef + 5f18fd8 commit b821956
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions partitura/io/importmidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,18 @@ def load_score_midi(
else:
note_ids = [None for i in range(len(note_array))]

## sanitize time signature, when they are only present in one track, and no global is set
# find the number of ts per each track
number_of_time_sig_per_track = [len(time_sigs_by_track[t]) for t in key_sigs_by_track.keys()]
# if one track has 0 ts, and another has !=0 ts, and no global_time_sigs is present, sanitize
# all key signatures are copied to global, and the track ts are removed
if len(global_time_sigs) == 0 and min(number_of_time_sig_per_track) == 0 and max(number_of_time_sig_per_track)!= 0:
warnings.warn("Sanitizing time signatures. They will be shared across all tracks.")
for ts in [ts for ts_track in time_sigs_by_track.values() for ts in ts_track]: #flattening all track time signatures to a list of ts
global_time_sigs.append(ts)
# now clear all track_ts
time_sigs_by_track.clear()

time_sigs_by_part = defaultdict(set)
for tr, ts_list in time_sigs_by_track.items():
for ts in ts_list:
Expand Down
4 changes: 4 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,8 @@

MIDIEXPORT_TESTFILES = [
os.path.join(DATA_PATH, "musicxml", "test_anacrusis.xml")
]

MIDIINPORT_TESTFILES = [
os.path.join(DATA_PATH, "midi", "bach_midi_score.mid")
]
Binary file added tests/data/midi/bach_midi_score.mid
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test_midi_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from partitura import load_score_midi
from partitura.utils import partition
import partitura.score as score
from tests import MIDIINPORT_TESTFILES

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -310,3 +311,11 @@ def test_midi_import_mode_5(self):
def tearDown(self):
# remove tmp file
self.tmpfile = None

class TestScoreMidi(unittest.TestCase):
def test_time_signature(self):
score = load_score_midi(MIDIINPORT_TESTFILES[0])
self.assertEqual(score.note_array()["onset_beat"][2], 0.5)
na = score.note_array(include_time_signature=True)
self.assertTrue(all([n==3 for n in na["ts_beats"]]))
self.assertTrue(all([d==8 for d in na["ts_beat_type"]]))

0 comments on commit b821956

Please sign in to comment.