def testTimeSignature(self): time = 0 track = 0 numerator = 4 denominator = 2 clocks_per_tick = 24 MyMIDI = MIDIFile(1, file_format=2) MyMIDI.addTimeSignature(track, time, numerator, denominator, clocks_per_tick) MyMIDI.close() data = Decoder(MyMIDI.tracks[0].MIDIdata) self.assertEqual(MyMIDI.tracks[0].MIDIEventList[0].evtname, 'TimeSignature') self.assertEqual(data.unpack_into_byte(0), 0x00) # time self.assertEqual(data.unpack_into_byte(1), 0xFF) # Code self.assertEqual(data.unpack_into_byte(2), 0x58) # subcode self.assertEqual(data.unpack_into_byte(3), 0x04) # Data length self.assertEqual(data.unpack_into_byte(4), numerator) self.assertEqual(data.unpack_into_byte(5), denominator) self.assertEqual(data.unpack_into_byte(6), clocks_per_tick) # Data length self.assertEqual(data.unpack_into_byte(7), 0x08) # 32nd notes per quarter note # We also want to check with a format 1 file, make sure it ends up in # the tempo track time = 0 track = 1 numerator = 4 denominator = 2 clocks_per_tick = 24 MyMIDI = MIDIFile(2, file_format=1) MyMIDI.addTimeSignature(track, time, numerator, denominator, clocks_per_tick) MyMIDI.close() data = Decoder(MyMIDI.tracks[0].MIDIdata) self.assertEqual(MyMIDI.tracks[0].MIDIEventList[0].evtname, 'TimeSignature') self.assertEqual(data.unpack_into_byte(0), 0x00) # time self.assertEqual(data.unpack_into_byte(1), 0xFF) # Code self.assertEqual(data.unpack_into_byte(2), 0x58) # subcode self.assertEqual(data.unpack_into_byte(3), 0x04) # Data length self.assertEqual(data.unpack_into_byte(4), numerator) self.assertEqual(data.unpack_into_byte(5), denominator) self.assertEqual(data.unpack_into_byte(6), clocks_per_tick) # Data length self.assertEqual(data.unpack_into_byte(7), 0x08) # 32nd notes per quarter note
def testEndOfTrack(self): MyMIDI = MIDIFile(1, ticks_per_quarternote=480) MyMIDI.addTimeSignature(0, 0, 4, 2, 8) MyMIDI.addNote(0, 0, 60, 0, 0.75, 55) MyMIDI.addNote(0, 0, 64, 1, 0.75, 55) MyMIDI.addNote(0, 0, 67, 2, 0.75, 55) MyMIDI.addNote(0, 0, 60, 4, 0.75, 55) MyMIDI.addNote(0, 0, 64, 5, 0.75, 55) MyMIDI.addNote(0, 0, 67, 6, 0.75, 55) with open(os.path.join(gettempdir(), "TestEndOfTrack_1.mid"), "wb") as f_midi: MyMIDI.writeFile(f_midi) MyMIDI = MIDIFile(1, ticks_per_quarternote=480) MyMIDI.addTimeSignature(0, 0, 4, 2, 8) MyMIDI.addNote(0, 0, 60, 0, 0.75, 55) MyMIDI.addNote(0, 0, 64, 1, 0.75, 55) MyMIDI.addNote(0, 0, 67, 2, 0.75, 55) MyMIDI.addNote(0, 0, 60, 4, 0.75, 55) MyMIDI.addNote(0, 0, 64, 5, 0.75, 55) MyMIDI.addNote(0, 0, 67, 6, 0.75, 55) MyMIDI.addEndOfTrack(0, 6.65) with open(os.path.join(gettempdir(), "TestEndOfTrack_2.mid"), "wb") as f_midi: MyMIDI.writeFile(f_midi) # No "End-Of-Track" and "End-Of-Track" placed before the final note event # should give the same result # --- Actually, not correct. The system track end comes at a different # time. This test is commented for now. #self.assertTrue(filecmp.cmp(os.path.join(gettempdir(), "TestEndOfTrack_1.mid"), os.path.join(gettempdir(), "TestEndOfTrack_2.mid"))) MyMIDI = MIDIFile(1, ticks_per_quarternote=480) MyMIDI.addTimeSignature(0, 0, 4, 2, 8) MyMIDI.addNote(0, 0, 60, 0, 0.75, 55) MyMIDI.addNote(0, 0, 64, 1, 0.75, 55) MyMIDI.addNote(0, 0, 67, 2, 0.75, 55) MyMIDI.addNote(0, 0, 60, 4, 0.75, 55) MyMIDI.addNote(0, 0, 64, 5, 0.75, 55) MyMIDI.addNote(0, 0, 67, 6, 0.75, 55) MyMIDI.addEndOfTrack(0, 8) with open(os.path.join(gettempdir(), "TestEndOfTrack_3.mid"), "wb") as f_midi: MyMIDI.writeFile(f_midi) # No "End-Of-Track" and "End-Of-Track" placed after the final note event # should give a different result self.assertFalse( filecmp.cmp(os.path.join(gettempdir(), "TestEndOfTrack_1.mid"), os.path.join(gettempdir(), "TestEndOfTrack_3.mid"))) os.remove(os.path.join(gettempdir(), "TestEndOfTrack_1.mid")) os.remove(os.path.join(gettempdir(), "TestEndOfTrack_2.mid")) os.remove(os.path.join(gettempdir(), "TestEndOfTrack_3.mid"))