Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading and Parsing a .MID file with Lua?

I am trying to read a .MID file with Lua and then parsing it into a table with all of the notes (ie {"A", "B#", "Cb", etc.}) but I cannot manage to read the file correctly. I use io.open and file:lines() but writing those same lines into another midi file results in a non-working midi file.

Is there any easier way to read and parse a .MID file with Lua?

like image 612
user809559 Avatar asked Jan 30 '26 04:01

user809559


2 Answers

The Standard MIDI File format is binary, not text. So you cannot expect to read it as "lines" at all. Instead, you'll need to use the read function to get bytes and inspect them. You might be better off finding a C library for MIDI files and binding it to Lua.

like image 188
John Zwinck Avatar answered Jan 31 '26 21:01

John Zwinck


.MID files (presumably Standard MIDI format) are binary, not text. Reading them with file:lines() will not work. You need to read the entire thing into a "string" (Lua strings can hold arbitrary bytes of data) with file:read("*a") instead; this will read the entire file into a single string. You also need to make sure that you open the file in binary mode (for platforms where this makes a difference).

like image 31
Nicol Bolas Avatar answered Jan 31 '26 21:01

Nicol Bolas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!