Posted: 17th Nov 2002 3:55
Later Once I get enough info on reading variable-length quantities I'll fnish making it also snatch the midi running events such as note on and note off and the other pitch wheel and sound control functions. IF there was a way to sync a midi to play at the same time as you could scan through the DETLA-TIME ticks ou could theoretically track midi position "but only in theory it would be impractile and wouldnt be very reliable" however this is a great start to a midi loader all in due time:

+ Code Snippet
open to read 1,"midifile.mid"
Header$=ByteChunk$(1,3)
print Header$
HeaderSize$=ByteHex$(1,3)
print HeaderSize$
MidiFormat$=ByteHex$(1,1)
print MidiFormat$
MidiTracks$=ByteHex$(1,1)
print MidiTracks$
DeltaTime$=ByteHex$(1,1)
print DeltaTime$

tracknex:
print
TrackHeader$=ByteChunk$(1,3)
print TrackHeader$
MidiData$=ByteHex$(1,3)
print MidiData$

recall:
Delta=Readvarlen(1)
`cls:print "DeltaTime - "+str$(Delta)
`For now lets not display anything but meta events
`until i figure out how to read the delta times
`enough to seprate the running commands from the delta times

aftercall:
Dat$=ByteHex$(1,0)
if Dat$="FF " then goto metaevent:
goto recall:

`---------------- MIDI META EVENTS ----------------------------

metaevent:
Print "Midi Meta Event FF:"
Cmd$=ByteHex$(1,0):TrackGo=0:print cmd$

if Cmd$="00 "
print "Set Sequence Number (00)":Lng$=ByteHex$(1,0)
print "Parameter Length:"+Lng$:Para$=ByteHex$(1,1)
print "Parameters:"+Para$
endif

if Cmd$="03 "
print "Sequence Or Track Name (03)":Lng$=ByteHex$(1,0)
print "Parameter Length:"+Lng$:TxtBytes=HexVal(Lng$)
Para$=ByteChunk$(1,TxtBytes-1)
print "Name:"+Para$
endif

if Cmd$="04 "
print "Track Instrument Name (04)":Lng$=ByteHex$(1,0)
print "Parameter Length:"+Lng$:TxtBytes=HexVal(Lng$)
Para$=ByteChunk$(1,TxtBytes-1)
print "Name:"+Para$
endif

if Cmd$="51 "
print "Set Tempo (51)":Lng$=ByteHex$(1,0)
print "Parameter Length:"+Lng$:Para$=ByteHex$(1,2)
print "Parameters:"+Para$
endif

if Cmd$="58 "
print "Time Singature (58)":Lng$=ByteHex$(1,0)
print "Parameter Length:"+Lng$:Para$=ByteHex$(1,3)
print "Parameters:"+Para$
endif

if Cmd$="59 "
print "Key Singature (59)":Lng$=ByteHex$(1,0)
print "Parameter Length:"+Lng$:Para$=ByteHex$(1,1)
print "Parameters:"+Para$
endif

if Cmd$="2F "
print "End Track (2F)":Lng$=ByteHex$(1,0)
print "Parameter Length:"+Lng$:TrackGo=1
endif

wait key:cls
if TrackGo=1 then goto tracknex:
goto recall:




`-------------------------------------------------------------------------

function ByteChunk$(Filenumber,Chunklength)
tmp$=""
for i=0 to Chunklength
read byte Filenumber,N
tmp$=tmp$+chr$(N)
next i
endfunction tmp$

function ByteHex$(Filenumber,Chunklength)
tmp$=""
for i=0 to Chunklength
read byte Filenumber,N
if len(Hex$(N))=1 then fore$="0"+Hex$(N) else fore$=Hex$(N)
tmp$=tmp$+fore$+" "
next i
endfunction tmp$

function HexVal(hexnum$)
if len(hexnum$)=1 then h$="0"+hexnum$:hexnum$=h$
NYBBLE$=mid$(hexnum$,1):V=0
if val(NYBBLE$)>0 then V=Val(NYBBLE$)
if NYBBLE$="A" then V=10
if NYBBLE$="B" then V=11
if NYBBLE$="C" then V=12
if NYBBLE$="D" then V=13
if NYBBLE$="E" then V=14
if NYBBLE$="F" then V=15
NYBBLE$=mid$(hexnum$,2):V2=V*16:V=0
if val(NYBBLE$)>0 then V=Val(NYBBLE$)
if NYBBLE$="A" then V=10
if NYBBLE$="B" then V=11
if NYBBLE$="C" then V=12
if NYBBLE$="D" then V=13
if NYBBLE$="E" then V=14
if NYBBLE$="F" then V=15
endfunction V2+V


function Readvarlen(Filenumber)
read byte Filenumber,value
if value & hexval("80")
value=value & hexval("7F")
while (c & hexval("80"))
read byte Filenumber,c
value=(BSL(value,7) + (c & hexval("7F")))
endwhile
endif
endfunction value


function BSL(Initial,ShiftAmount)
ANW=Initial*(2^ShiftAmount)
endfunction ANW

function MSB(H$)
if HexVal(H$)>128 then toggle=1 else toggle=0
endfunction toggle
Posted: 17th Nov 2002 7:33
Oh um also here is how I plan on playing the midi during the editor Im creating of corse the ony diffrence is I'll create a mixer using make memblock and blotching the sounds together" then it will all play simulataniously all 16 available "channels".

+ Code Snippet
load sound "epiano.wav",1


TEMPO=150

do
if a=0 then N$="C"
if a=1 then N$="C#"
if a=2 then N$="D"
if a=3 then N$="D#"
if a=4 then N$="E"
if a=5 then N$="F"
if a=6 then N$="F#"
if a=7 then N$="G"
if a=8 then N$="G#"
if a=9 then N$="A"
if a=10 then N$="A#"
if a=11 then N$="B"
if a=12 then N$="A#"
if a=13 then N$="A"
if a=14 then N$="G#"
if a=15 then N$="G"
if a=16 then N$="F#"
if a=17 then N$="F"
if a=18 then N$="E"
if a=19 then N$="D#"
if a=20 then N$="D"
if a=21 then N$="C#"
if a=22 then N$="C"

if N$="C" then FREQ=67450
if N$="C#" then FREQ=63664
if N$="D" then FREQ=60091
if N$="D#" then FREQ=56718
if N$="E" then FREQ=53535
if N$="F" then FREQ=50530
if N$="F#" then FREQ=47694
if N$="G" then FREQ=45017
if N$="G#" then FREQ=42491
if N$="A" then FREQ=40106
if N$="A#" then FREQ=37855
if N$="B" then FREQ=35730
Set Sound Speed 1,FREQ

play sound 1
b=SetActive(TEMPO)
while ActiveTimer(b)=0
sync:endwhile

inc a,1:if a>22 then a=0
loop

function SetActive(t)
tim=timer()+t
endfunction tim

function ActiveTimer(t)
if t>timer() then n=0 else n=1
endfunction n