//display-name: "Test"

midi-events: {
	0x8: { "data-length": 2, "desc": "Note-off"},
	0x9: { "data-length": 2, "desc": "Note-on"},
	0xA: { "data-length": 2, "desc": "Polyphonic Key Pressure"},
	0xB: { "data-length": 2, "desc": "Channel mode message"},
	0xC: { "data-length": 1, "desc": "Controller change"},
	0xD: { "data-length": 1, "desc": "Channel Key Pressure"},
	0xE: { "data-length": 2, "desc": "Pitch Bend"}
}

file-format music-midi
{
	midi-container
	{
		fixed-string(4) header == "MThd"

		unsigned32be header-size
		unsigned16be format
		unsigned16be track-count
		unsigned16be division

		loop(track-count)
		{
			midi-track
			{
				fixed-string(4) header == "MTrk"
				unsigned32be track-size

				/*go-back: file.position

				running: true

				loop while (running == true) {
					midi-vlf delta
					unsigned8 type

					if ((type == 0xF0) or (type == 0xF7)) //sysex
					{
						midi-vlf sysex-length
						data(sysex-length) value
					}
					else if (type == 0xFF) //meta
					{
						unsigned8 meta-type
						midi-vlf meta-length
						data(meta-length) value

						if (meta-type == 0x2F) {
							running = false
						}
					}
					else
					{
						data-length: midi-events[type >> 4].data-length
						log(midi-events[type >> 4].desc)

						data(data-length) midi-bytes
					}
				}

				at go-back:*/
			}
		}
	}	
}

if (file.name like "*.*")
{
	interpret-as "music-midi"
}