OPTIONS "C_CALL LABELCHECK NOSOURCE"
LIBRARY rexxgui, rxmidi
midierr = "ERROR"
midiheading = 1
guierr = "SYNTAX"
guiheading = 1
guicreatewindow('NORMAL')
tofilename = ""
fromfilename = ARG(1)
IF fromfilename \== "" THEN converttext()
again:
DO FOREVER
guigetmsg()
CATCH NOTREADY
CONDITION('M')
SIGNAL again
CATCH ERROR
CONDITION('M')
SIGNAL again
CATCH SYNTAX
CONDITION('M')
SIGNAL again
FINALLY
guidestroywindow()
END
RETURN
wm_click_pickbutton:
IF tofilename \== "" THEN DO
tofilename = ""
guiaddctltext("PickButton", "Aborting...")
RETURN
END
DO
guifile('FromFilename', 'EXISTING', 'Select a MIDI file to convert to text', 'MIDI files (*.mid, *.rmi) | *.mid;*.rmi | All files (*.*) | *.*')
CATCH SYNTAX
IF CONDITION('D') \== "CANCEL" THEN CONDITION('M')
RETURN
END
converttext:
tofilename = EDITNAME(fromfilename, "*.txt", "U")
DO
guiaddctltext("PickButton", "Loading...")
MIDIOpenFile(fromfilename)
STREAM(tofilename, 'C', 'OPEN WRITE REPLACE')
LINEOUT(tofilename, "MThd | Format="||midigetinfo('TYPE')||" | # of Tracks="MIDIGetInfo('TRKS')||" | Division="||midigetinfo('BASE'))
key = 0
DO WHILE MIDITrack("") \== 0
LINEOUT(tofilename,"")
LINEOUT(tofilename,"Track #"||miditrack() - 1 "******************************************")
measure = ""
beat = ""
clock = ""
guigetmsg('CLEAR')
IF tofilename == "" THEN RETURN
DO WHILE MIDIGetEvent() == ""
line = ""
IF measure \== midievent.!measure THEN DO
line = LEFT("", 4 - LENGTH(midievent.!measure)) || midievent.!measure || ':'
spaces = 0
END
ELSE spaces = 5
IF spaces == 0 | beat \== midievent.!beat THEN DO
line = line || LEFT("", (spaces + 2) - LENGTH(midievent.!beat)) || midievent.!beat || ':'
spaces = 0
END
ELSE spaces = spaces + 3
IF spaces == 0 | clock \== midievent.!clock THEN DO
line = line || LEFT("", (spaces + 3) - LENGTH(midievent.!clock)) || midievent.!clock
spaces = 0
END
ELSE spaces = spaces + 3
IF spaces \== 0 THEN line = LEFT("", spaces)
measure = midievent.!measure
beat = midievent.!beat
clock = midievent.!clock
type = midievent.!type
midievent.!type = MIDIEventProp()
IF midievent.!channel \== "" THEN DO
IF midievent.!channel < 10 THEN spaces = " "
ELSE spaces = ""
line = line || " |" || midievent.!type || LEFT("", 12 - LENGTH(midievent.!type)) || "| chan=" || spaces || midievent.!channel || " | "
line = EXPAND(line, 'T')
CHAROUT(tofilename, line)
SELECT type
WHEN 144 THEN DO
IF midievent.!data2 \== 0 THEN LINEOUT(tofilename, "pitch=" || MIDINoteName(key) "| vol=" || midievent.!data2)
ELSE LINEOUT(tofilename, "pitch=" || MIDINoteName(key))
END
WHEN 128 THEN LINEOUT(tofilename, "pitch=" || MIDINoteName(key) "| vol=" || midievent.!data2)
WHEN 160 THEN LINEOUT(tofilename, "pitch=" || MIDINoteName(key) "| press=" || midievent.!data2)
WHEN 176 THEN LINEOUT(tofilename, "contr=" || MIDICtlName() "| value=" || midievent.!data2)
WHEN 192 THEN DO
spaces = ""
IF midievent.!data1 < 100 THEN spaces = " "
IF midievent.!data1 < 10 THEN spaces = " "
LINEOUT(tofilename, "pgm #=" || spaces || midievent.!data1 + 1)
END
WHEN 208 THEN LINEOUT(tofilename, "press=" || midievent.!data1)
WHEN 224 THEN LINEOUT(tofilename, "bend=" || midievent.!data3)
END
END
ELSE DO
IF type == 33 THEN DO
type = 9
midievent.!type = MIDIEventProp(, 9)
midievent.!data1 = MIDIPortName()
END
line = line || " |" || midievent.!type || LEFT("", 12 - LENGTH(midievent.!type)) || "| "
line = EXPAND(line, 'T')
CHAROUT(tofilename, line)
IF (type \== 0 & type < 10) | type == 127 THEN DO
len = LENGTH(midievent.!data1)
LINEOUT(tofilename, "")
bytes = 1
line = " "
line2 = "<"
DO total = 1 TO len
chr = SUBSTR(midievent.!data1, total, 1)
IF bytes \== 1 THEN line = line || " "
line = line || "0x" || C2X(chr)
IF chr <<= '19'x | char >> '7F'x THEN chr = '.'
line2 = line2 || chr
bytes = bytes + 1
IF bytes > 11 THEN DO
LINEOUT(tofilename, EXPAND(line || LEFT("", 67 - LENGTH(line)) || line2 || ">", 'T'))
bytes = 1
line = " "
line2 = "<"
END
END
IF bytes \== 1 THEN LINEOUT(tofilename, EXPAND(line || LEFT("", 67 - LENGTH(line)) || line2 || ">", 'T'))
END
ELSE IF type == 240 | type == 247 | type == 255 THEN DO
LINEOUT(tofilename, "")
total = 0
DO WHILE total < midievent.!data1
chr = MIDISysex(11, total + 1, 'C')
LINEOUT(tofilename, " " || chr)
total = total + 11
END
END
ELSE SELECT type
WHEN 0 THEN LINEOUT(tofilename, midievent.!data1)
WHEN 32 THEN LINEOUT(tofilename, midievent.!data1)
WHEN 81 THEN LINEOUT(tofilename, "BPM=" || midievent.!data2 || LEFT(" ", 6 - LENGTH(midievent.!data2)) || "| micros\quarter=" || midievent.!data1)
WHEN 88 THEN DO
spaces = " "
pad = 1
IF LENGTH(midievent.!data1) > 1 THEN DO
spaces = ""
pad = 0
END
LINEOUT(tofilename, spaces || midievent.!data1 || "/" || midievent.!data2 || LEFT(" ", 9 - (LENGTH(midievent.!data1) + LENGTH(midievent.!data2) + pad)) || "| MIDI-clocks\click=" || midievent.!data3 "| 32nds\quarter=" || midievent.!data4)
END
WHEN 84 THEN LINEOUT(tofilename, "hour=" || midievent.!data1 "| min=" || midievent.!data2 "| sec=" || midievent.!data3 "| frame=" || midievent.!data4 "| subs=" || midievent.!data5)
WHEN 89 THEN DO
key = midievent.!data1
IF key = 0 & midievent.!data2 == 1 THEN key = -1
LINEOUT(tofilename, midievent.!data3)
END
WHEN 241 THEN LINEOUT(tofilename, midievent.!data1)
WHEN 242 THEN LINEOUT(tofilename, midievent.!data3)
WHEN 243 THEN LINEOUT(tofilename, midievent.!data1)
OTHERWISE LINEOUT(tofilename, "")
END
END
END
END
FINALLY
STREAM(tofilename, 'C', 'CLOSE')
guiaddctltext("PickButton", "Pick file")
guiaddctltext('Message', 'Done converting' TRANSLATE(tofilename, "/", "\"))
tofilename = ""
END
RETURN |
|