3

Here's what brought this question up:

with open(path + "/OneChance1.mid") as f:
  for line in f.readline():
        print(line)

Here I am simply trying to read a midi file to scour its contents. I then receive this error message: UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 153: character maps to <undefined>

If I use open()'s second param like so: with open(path + "/OneChance1.mid"m encoding='utf-8) as f: then I receive this error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 13: invalid start byte

If I change the encoding param to ascii I get another error about an ordinal being out of range. Lastly I tried utf-16 and it said that the file didn't start with BOM (which made me smile for some reason). Also, if I ignore the errors I get characters that resemble nothing of the kind of data I am expecting. My expectations are based on this source: http://www.sonicspot.com/guide/midifiles.html

Anyway, does anyone know what kind of encoding a midi file uses? My research is coming up short in that regard so I thought it would be worth asking on SO. Or maybe someone can point out some other possibilities or blunders?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
lonious
  • 676
  • 9
  • 25
  • Things like UTF-8, UTF-16 and ASCII are *text* encodings. [MIDI](http://www.midi.org/aboutmidi/tut_midifiles.php) is not a text format. – Konrad Rudolph Aug 15 '14 at 09:32
  • Ideas abot the dead link (`www.sonicspot.com` not found)? BTW: the encoding of text in meta events may be worth a question. – Wolf Jul 20 '17 at 13:35

2 Answers2

7

MIDI files are binary content. By opening the file as a text file however, Python applies the default system encoding in trying to decode the text as Unicode.

Open the file in binary mode instead:

with open(midifile, 'rb') as mfile:
    leader = mfile.read(4)
    if leader != b'MThd':
        raise ValueError('Not a MIDI file!')

You'd have to study the MIDI standard file format if you wanted to learn more from the file. Also see What is the structure of a MIDI file?

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
3

It's a binary file, it's not text using a text encoding like you seem to expect.

To open a file in binary mode in Python, pass a string containing "b" as the second argument to open().

This page contains a description of the format.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • That makes sense. You wouldn't know how to specify this when reading a file in python would you? If I leave out that parameter it defaults to 'charmap'. 'binary' and 'none' also do nothing. – lonious Aug 15 '14 at 09:35