diff --git a/partitura/io/importmidi.py b/partitura/io/importmidi.py index ecfe2be3..ff5421b6 100644 --- a/partitura/io/importmidi.py +++ b/partitura/io/importmidi.py @@ -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: diff --git a/tests/__init__.py b/tests/__init__.py index ce7bf09c..80e898ab 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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") ] \ No newline at end of file diff --git a/tests/data/midi/bach_midi_score.mid b/tests/data/midi/bach_midi_score.mid new file mode 100644 index 00000000..d2f945a4 Binary files /dev/null and b/tests/data/midi/bach_midi_score.mid differ diff --git a/tests/test_midi_import.py b/tests/test_midi_import.py index cf31f169..0255d38e 100644 --- a/tests/test_midi_import.py +++ b/tests/test_midi_import.py @@ -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__) @@ -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"]])) \ No newline at end of file