| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import io |
| 6 |
|
| 7 |
from pytest import raises |
| 8 |
|
| 9 |
from mido.messages import Message |
| 10 |
from mido.midifiles.meta import KeySignatureError, MetaMessage |
| 11 |
from mido.midifiles.midifiles import MidiFile, MidiTrack |
| 12 |
|
| 13 |
HEADER_ONE_TRACK = """ |
| 14 |
4d 54 68 64 # MThd |
| 15 |
00 00 00 06 # Chunk size |
| 16 |
00 01 # Type 1 |
| 17 |
00 01 # 1 track |
| 18 |
00 78 # 120 ticks per beat |
| 19 |
""" |
| 20 |
|
| 21 |
|
| 22 |
def parse_hexdump(hexdump): |
| 23 |
data = bytearray() |
| 24 |
for line in hexdump.splitlines(): |
| 25 |
data += bytearray.fromhex(line.split('#')[0]) |
| 26 |
return data |
| 27 |
|
| 28 |
|
| 29 |
def read_file(hexdump, clip=False): |
| 30 |
return MidiFile(file=io.BytesIO(parse_hexdump(hexdump)), clip=clip) |
| 31 |
|
| 32 |
|
| 33 |
def test_no_tracks(): |
| 34 |
assert read_file(""" |
| 35 |
4d 54 68 64 # MThd |
| 36 |
00 00 00 06 # Chunk size |
| 37 |
00 01 # Type 1 |
| 38 |
00 00 # 0 tracks |
| 39 |
00 78 # 120 ticks per beat |
| 40 |
""").tracks == [] |
| 41 |
|
| 42 |
|
| 43 |
def test_single_message(): |
| 44 |
assert read_file(HEADER_ONE_TRACK + """ |
| 45 |
4d 54 72 6b # MTrk |
| 46 |
00 00 00 04 |
| 47 |
20 90 40 40 # note_on |
| 48 |
""").tracks[0] == [Message('note_on', note=64, velocity=64, time=32)] |
| 49 |
|
| 50 |
|
| 51 |
def test_too_long_message(): |
| 52 |
with raises(IOError): |
| 53 |
read_file(HEADER_ONE_TRACK + """ |
| 54 |
4d 54 72 6b # MTrk |
| 55 |
00 00 00 04 |
| 56 |
00 ff 03 ff ff 7f # extremely long track name message |
| 57 |
""") |
| 58 |
|
| 59 |
|
| 60 |
def test_two_tracks(): |
| 61 |
mid = read_file(""" |
| 62 |
4d54 6864 0000 0006 0001 0002 0040 # Header |
| 63 |
4d54 726b 0000 0008 00 90 40 10 40 80 40 10 # Track 0 |
| 64 |
4d54 726b 0000 0008 00 90 47 10 40 80 47 10 # Track 1 |
| 65 |
""") |
| 66 |
assert len(mid.tracks) == 2 |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
def test_empty_file(): |
| 71 |
with raises(EOFError): |
| 72 |
read_file("") |
| 73 |
|
| 74 |
|
| 75 |
def test_eof_in_track(): |
| 76 |
with raises(EOFError): |
| 77 |
read_file(HEADER_ONE_TRACK + """ |
| 78 |
4d 54 72 6b # MTrk |
| 79 |
00 00 00 01 # Chunk size |
| 80 |
# Oops, no data here. |
| 81 |
""") |
| 82 |
|
| 83 |
|
| 84 |
def test_invalid_data_byte_no_clipping(): |
| 85 |
|
| 86 |
with raises(IOError): |
| 87 |
read_file(HEADER_ONE_TRACK + """ |
| 88 |
4d 54 72 6b # MTrk |
| 89 |
00 00 00 04 # Chunk size |
| 90 |
00 90 ff 40 # note_on note=255 velocity=64 |
| 91 |
""") |
| 92 |
|
| 93 |
|
| 94 |
def test_invalid_data_byte_with_clipping_high(): |
| 95 |
midi_file = read_file(HEADER_ONE_TRACK + """ |
| 96 |
4d 54 72 6b # MTrk |
| 97 |
00 00 00 04 # Chunk size |
| 98 |
00 90 ff 40 # note_on note=255 velocity=64 |
| 99 |
""", clip=True) |
| 100 |
assert midi_file.tracks[0][0].note == 127 |
| 101 |
|
| 102 |
|
| 103 |
def test_meta_messages(): |
| 104 |
|
| 105 |
mid = read_file(HEADER_ONE_TRACK + """ |
| 106 |
4d 54 72 6b # MTrk |
| 107 |
00 00 00 0c # Chunk size |
| 108 |
00 ff 03 04 54 65 73 74 # track_name name='Test' |
| 109 |
00 ff 2f 00 # end_of_track |
| 110 |
""") |
| 111 |
|
| 112 |
track = mid.tracks[0] |
| 113 |
|
| 114 |
assert track[0] == MetaMessage('track_name', name='Test') |
| 115 |
assert track[1] == MetaMessage('end_of_track') |
| 116 |
|
| 117 |
|
| 118 |
def test_meta_message_bad_key_sig_throws_key_signature_error_sharps(): |
| 119 |
with raises(KeySignatureError): |
| 120 |
read_file(HEADER_ONE_TRACK + """ |
| 121 |
4d 54 72 6b # MTrk |
| 122 |
00 00 00 09 # Chunk size |
| 123 |
00 ff 59 02 08 # Key Signature with 8 sharps |
| 124 |
00 ff 2f 00 # end_of_track |
| 125 |
""") |
| 126 |
|
| 127 |
|
| 128 |
def test_meta_message_bad_key_sig_throws_key_signature_error_flats(): |
| 129 |
with raises(KeySignatureError): |
| 130 |
read_file(HEADER_ONE_TRACK + """ |
| 131 |
4d 54 72 6b # MTrk |
| 132 |
00 00 00 09 # Chunk size |
| 133 |
00 ff 59 02 f8 # Key Signature with 8 flats |
| 134 |
00 ff 2f 00 # end_of_track |
| 135 |
""") |
| 136 |
|
| 137 |
|
| 138 |
def test_meta_messages_with_length_0(): |
| 139 |
"""sequence_number and midi_port with no data bytes should be accepted. |
| 140 |
|
| 141 |
In rare cases these messages have length 0 and thus no data |
| 142 |
bytes. (See issues 42 and 93.) It is unclear why these messages |
| 143 |
are missing their data. It could be cased by a bug in the software |
| 144 |
that created the files. |
| 145 |
|
| 146 |
So far this has been fixed by adding a test to each of these two |
| 147 |
meta message classes. If the problem appears with other message |
| 148 |
types it may be worth finding a more general solution. |
| 149 |
""" |
| 150 |
mid = read_file(HEADER_ONE_TRACK + """ |
| 151 |
4d 54 72 6b # MTrk |
| 152 |
00 00 00 17 |
| 153 |
|
| 154 |
00 ff 00 00 # sequence_number with no data bytes (should be 2). |
| 155 |
00 ff 21 00 # midi_port with no data bytes (should be 1). |
| 156 |
|
| 157 |
00 ff 00 02 00 01 # sequence_number with correct number of data bytes (2). |
| 158 |
00 ff 21 01 01 # midi_port with correct number of data bytes (1). |
| 159 |
|
| 160 |
00 ff 2f 00 |
| 161 |
""") |
| 162 |
|
| 163 |
assert mid.tracks[0] == [ |
| 164 |
MetaMessage('sequence_number', number=0), |
| 165 |
MetaMessage('midi_port', port=0), |
| 166 |
|
| 167 |
MetaMessage('sequence_number', number=1), |
| 168 |
MetaMessage('midi_port', port=1), |
| 169 |
|
| 170 |
MetaMessage('end_of_track'), |
| 171 |
] |
| 172 |
|
| 173 |
|
| 174 |
def test_midifile_repr(): |
| 175 |
midifile = MidiFile(type=1, ticks_per_beat=123, tracks=[ |
| 176 |
MidiTrack([ |
| 177 |
Message('note_on', channel=1, note=2, time=3), |
| 178 |
Message('note_off', channel=1, note=2, time=3)]), |
| 179 |
MidiTrack([ |
| 180 |
MetaMessage('sequence_number', number=5), |
| 181 |
Message('note_on', channel=2, note=6, time=9), |
| 182 |
Message('note_off', channel=2, note=6, time=9)]), |
| 183 |
]) |
| 184 |
midifile_eval = eval(repr(midifile)) |
| 185 |
for track, track_eval in zip(midifile.tracks, midifile_eval.tracks): |
| 186 |
for m1, m2 in zip(track, track_eval): |
| 187 |
assert m1 == m2 |