def test_packet_with_recovery(self): midi_list = [[[128, 10, 0], 1000], [[144, 32, 110], 1000]] recovery_journal_system = RecoveryJournal() seq = 12 #midi session part (sending) packet = OldPacket(seq, midi_list, 0) midi_list_formated, length = MidiCommand().encode_midi_commands( midi_list) header = MidiCommand().header(0, 1, 0, 0, length) #Building Chunk recovery_journal_system.update(packet) recovery_journal = recovery_journal_system.content chunk = header + midi_list_formated + recovery_journal #protocol part packet = RTPPacket(424242, seq, 10, chunk, 96, marker=1) bytes = packet.netbytes() packet = parse_rtppacket(bytes) #midisession part (receiving) marker_b, marker_j, marker_z, marker_p, length = MidiCommand( ).parse_header(packet.data[0]) if marker_p: #silent packet with recovery midi_list = [] else: #normal packet #Extract Midi Note (length en nb notes) print "test 1:", len(packet.data[1:length * 7 + 1]) midi_list = packet.data[1:length * 7 + 1] #Decoding midi commands midi_list = MidiCommand().decode_midi_commands(midi_list, length) #Saving feed history packet_to_save = OldPacket(seq, midi_list, 0) #Extract Midi Recovery Journal if is present in the packet and #the previous packet has been lost if marker_j: print "recovery journal" print len(packet.data[length * 7 + 1:]) journal = packet.data[length * 7 + 1:] #Parse Recovery journal r_journal = recovery_journal_system.parse(journal)
def setUp(self): self.midi_command = MidiCommand()
class TestMidiCommand(unittest.TestCase): """Testing MidiCommand class""" def setUp(self): self.midi_command = MidiCommand() def tearDown(self): del self.midi_command def test_header(self): """Testing header for MIDICommand""" marker_b, recovery, timestamp, phantom, length = 0, 0, 0, 0, 10 res = self.midi_command.header(marker_b, recovery, timestamp, phantom, length) assert(type(res)==str), self.fail("Wrong type returned") assert(len(res)==1), self.fail("Wrong size returned") def test_parse_header(self): """Testing parse header for MIDICommand""" marker_b, recovery, timestamp, phantom, length = 0, 0, 0, 0, 10 res = self.midi_command.header(marker_b, recovery, timestamp, phantom, length) marker_b, marker_j, marker_z, marker_p, length = self.midi_command.parse_header(res) assert(marker_b==0), self.fail("Wrong value returned for marker_b") assert(marker_j==0), self.fail("Wrong value returned for marker_j") assert(marker_z==0), self.fail("Wrong value returned for marker_z") assert(marker_p==0), self.fail("Wrong value returned for marker_p") assert(length==10), self.fail("Wrong value returned for length") def test_encode_midi_commands(self): plist = [[[192, 120, 100],1069],[[144, 104, 50],1030], [[145, 110, 0],10], [[145, 112, 0],1044], [[144, 124, 50],19],[[145, 114, 0],8], [[145, 12, 0],999]] decorate = [((x[0][0]&15), (x[0][0]&240), x[0][1],x) for x in plist] decorate.sort() plist = [x[3] for x in decorate] res, nb_notes = self.midi_command.encode_midi_commands(plist) assert(nb_notes==7), \ self.fail("Problem with nb_notes , it's not corresponding" \ + " to reality") assert(len(res) == nb_notes*7), \ self.fail("Problem of size with formated command") def test_decode_midi_commands(self): plist = [[[192, 120, 100],1069],[[144, 104, 50],1069], \ [[145, 110, 0],1070], [[145, 112, 0],1071], \ [[144, 124, 50],1071],[[145, 114, 0],1072], \ [[145, 12, 0],1072]] decorate = [((x[0][0]&15), (x[0][0]&240), x[0][1],x) for x in plist] decorate.sort() plist = [x[3] for x in decorate] res, nb_notes = self.midi_command.encode_midi_commands(plist) midi_cmd = self.midi_command.decode_midi_commands(res, nb_notes) assert(len(plist)==len(midi_cmd)), \ self.fail("list haven't got the same size") for i in range(len(midi_cmd)): if midi_cmd[i][0][0] != plist[i][0][0]: self.fail("Problem with event encoding") if midi_cmd[i][0][1] != plist[i][0][1]: self.fail("Problem with note encoding") if midi_cmd[i][0][2] != plist[i][0][2]: self.fail("Problem with velocity encoding") if midi_cmd[i][1] != plist[i][1] - plist[0][1]: self.fail("Problem with timestamp encoding")
class TestMidiCommand(unittest.TestCase): """Testing MidiCommand class""" def setUp(self): self.midi_command = MidiCommand() def tearDown(self): del self.midi_command def test_header(self): """Testing header for MIDICommand""" marker_b, recovery, timestamp, phantom, length = 0, 0, 0, 0, 10 res = self.midi_command.header(marker_b, recovery, timestamp, phantom, length) assert (type(res) == str), self.fail("Wrong type returned") assert (len(res) == 1), self.fail("Wrong size returned") def test_parse_header(self): """Testing parse header for MIDICommand""" marker_b, recovery, timestamp, phantom, length = 0, 0, 0, 0, 10 res = self.midi_command.header(marker_b, recovery, timestamp, phantom, length) marker_b, marker_j, marker_z, marker_p, length = self.midi_command.parse_header( res) assert (marker_b == 0), self.fail("Wrong value returned for marker_b") assert (marker_j == 0), self.fail("Wrong value returned for marker_j") assert (marker_z == 0), self.fail("Wrong value returned for marker_z") assert (marker_p == 0), self.fail("Wrong value returned for marker_p") assert (length == 10), self.fail("Wrong value returned for length") def test_encode_midi_commands(self): plist = [[[192, 120, 100], 1069], [[144, 104, 50], 1030], [[145, 110, 0], 10], [[145, 112, 0], 1044], [[144, 124, 50], 19], [[145, 114, 0], 8], [[145, 12, 0], 999]] decorate = [((x[0][0] & 15), (x[0][0] & 240), x[0][1], x) for x in plist] decorate.sort() plist = [x[3] for x in decorate] res, nb_notes = self.midi_command.encode_midi_commands(plist) assert(nb_notes==7), \ self.fail("Problem with nb_notes , it's not corresponding" \ + " to reality") assert(len(res) == nb_notes*7), \ self.fail("Problem of size with formated command") def test_decode_midi_commands(self): plist = [[[192, 120, 100],1069],[[144, 104, 50],1069], \ [[145, 110, 0],1070], [[145, 112, 0],1071], \ [[144, 124, 50],1071],[[145, 114, 0],1072], \ [[145, 12, 0],1072]] decorate = [((x[0][0] & 15), (x[0][0] & 240), x[0][1], x) for x in plist] decorate.sort() plist = [x[3] for x in decorate] res, nb_notes = self.midi_command.encode_midi_commands(plist) midi_cmd = self.midi_command.decode_midi_commands(res, nb_notes) assert(len(plist)==len(midi_cmd)), \ self.fail("list haven't got the same size") for i in range(len(midi_cmd)): if midi_cmd[i][0][0] != plist[i][0][0]: self.fail("Problem with event encoding") if midi_cmd[i][0][1] != plist[i][0][1]: self.fail("Problem with note encoding") if midi_cmd[i][0][2] != plist[i][0][2]: self.fail("Problem with velocity encoding") if midi_cmd[i][1] != plist[i][1] - plist[0][1]: self.fail("Problem with timestamp encoding")