Skip to content

Commit f7092b6

Browse files
committed
Fix MIDI input in Python 3 [#64]
Several points were missed when migrating this module to Python 3. Thanks to Sam Gould (samg7b5) for providing part of the needed changes.
1 parent 754dad7 commit f7092b6

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

mingus/midi/midi_file_in.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@
2020
from __future__ import absolute_import
2121
from __future__ import print_function
2222

23-
from mingus.containers.note import Note
24-
from mingus.containers.note_container import NoteContainer
23+
import binascii
24+
25+
from six import binary_type
26+
from six.moves import range
27+
28+
import mingus.core.intervals as intervals
29+
import mingus.core.notes as notes
2530
from mingus.containers.bar import Bar
26-
from mingus.containers.track import Track
2731
from mingus.containers.composition import Composition
2832
from mingus.containers.instrument import MidiInstrument
29-
import mingus.core.notes as notes
30-
import mingus.core.intervals as intervals
31-
import binascii
32-
from six.moves import range
33+
from mingus.containers.note import Note
34+
from mingus.containers.note_container import NoteContainer
35+
from mingus.containers.track import Track
36+
from mingus.core.keys import Key
3337

3438

3539
def MIDI_to_Composition(file):
@@ -102,7 +106,7 @@ def MIDI_to_Composition(self, file):
102106
# note on
103107
n = Note(
104108
notes.int_to_note(event["param1"] % 12),
105-
event["param1"] / 12 - 1,
109+
event["param1"] // 12 - 1,
106110
)
107111
n.channel = event["channel"]
108112
n.velocity = event["param2"]
@@ -127,7 +131,7 @@ def MIDI_to_Composition(self, file):
127131
pass
128132
elif event["meta_event"] == 3:
129133
# Track name
130-
t.name = event["data"]
134+
t.name = event["data"].decode("ascii")
131135
elif event["meta_event"] == 6:
132136
# Marker
133137
pass
@@ -141,7 +145,7 @@ def MIDI_to_Composition(self, file):
141145
# Set tempo warning Only the last change in bpm will get
142146
# saved currently
143147
mpqn = self.bytes_to_int(event["data"])
144-
bpm = 60000000 / mpqn
148+
bpm = 60000000 // mpqn
145149
elif event["meta_event"] == 88:
146150
# Time Signature
147151
d = event["data"]
@@ -165,7 +169,7 @@ def MIDI_to_Composition(self, file):
165169
key = intervals.major_fourth(key)
166170
else:
167171
key = intervals.major_fifth(key)
168-
b.key = Note(key)
172+
b.key = Key(key)
169173
else:
170174
print("Unsupported META event", event["meta_event"])
171175
else:
@@ -179,7 +183,7 @@ def parse_midi_file_header(self, fp):
179183
format type, number of tracks and parsed time division information."""
180184
# Check header
181185
try:
182-
if fp.read(4) != "MThd":
186+
if fp.read(4) != b"MThd":
183187
raise HeaderError(
184188
"Not a valid MIDI file header. Byte %d." % self.bytes_read
185189
)
@@ -218,12 +222,18 @@ def parse_midi_file_header(self, fp):
218222
chunk_size -= 6
219223
if chunk_size % 2 == 1:
220224
raise FormatError("Won't parse this.")
221-
fp.read(chunk_size / 2)
222-
self.bytes_read += chunk_size / 2
225+
byte_size = chunk_size // 2
226+
fp.read(byte_size)
227+
self.bytes_read += byte_size
223228
return (format_type, number_of_tracks, time_division)
224229

225-
def bytes_to_int(self, bytes):
226-
return int(binascii.b2a_hex(bytes), 16)
230+
def bytes_to_int(self, _bytes):
231+
if isinstance(_bytes, binary_type):
232+
return int(binascii.b2a_hex(_bytes), 16)
233+
elif isinstance(_bytes, int):
234+
return _bytes
235+
else:
236+
raise TypeError("Unexpected type: %s" % type(_bytes))
227237

228238
def parse_time_division(self, bytes):
229239
"""Parse the time division found in the header of a MIDI file and

0 commit comments

Comments
 (0)