type note_thing
   note as integer
   speed as integer
   volume as integer
endtype
 
dim master_note(-1) as note_thing
 
make_midi("R",63,7,"q c5 c5 c5 r c5 c5 c5 r c5 d#5 g#4 a#4 c5 r c#5 c#5 c#5 c#5 c#5 c5 c5 r c5 c5 c5 a#4 a#4 c5 a#4 h d#5 r q c5 c5 c5 r c5 c5 c5 r c5 d#5 g#4 a#4 c5 r c#5 c#5 c#5 c#5 c#5 c5 c5 r c5 c5 c5 d#5 d#5 c#5 a#4 h g#4 r")
 
load music "r.mid",1
loop music 1
 
do
 sync
loop
 
 
 
 
 
 
 
function make_midi(file as string,instrument as integer,tempo as integer,notes as string)
SPEED=135
notes=" "+notes
for a = 1 to len(notes)
 
   watch$=mid$(notes,a)
   if watch$=" "
      start=a:test_note$=""
      repeat
         inc start
         new$=mid$(notes,start)
         if new$<>" "
            test_note$=test_note$+new$
         endif
      until new$=" " or start=len(notes)
    note_on=1
    if len(test_note$)=1
      select lower$(test_note$)
         case "q":SPEED=135:endcase
         case "8":speed=134:endcase
         case "h":speed=140:endcase
         case "r":array insert at bottom master_note():count=array count(master_note())
            master_note(count).note=0:master_note(count).speed=SPEED:master_note(count).volume=0
         endcase
 
      endselect
      note_on=0
    else
      temp_note$=left$(test_note$,1):temp_octave=val(right$(test_note$,1))
      select lower$(temp_note$)
         case "c":note_base=0:endcase
         case "d":note_base=2:endcase
         case "e":note_base=4:endcase
         case "f":note_base=5:endcase
         case "g":note_base=7:endcase
         case "a":note_base=9:endcase
         case "b":note_base=11:endcase
      endselect
      inc note_base,Temp_octave*12
      if len(test_note$)=3 then inc note_base
 
 
 
    endif
      if note_on=1
        array insert at bottom master_note()
        count=array count(master_note())
        master_note(count).note=note_base
        master_note(count).speed=SPEED
        master_note(count).volume=127
      endif
    endif
   next a
 
   `figure bytes in the track
   track_total=20+((array count(master_note())+1)*9)
 
   if file exist("making_bytes.txt")=1 then delete file "making_bytes.txt"
   open to write 1,"making_bytes.txt"
   write long 1,track_total
   close file 1
   open to read 1,"making_bytes.txt"
      read byte 1,track_bytesD
      read byte 1,track_bytesc
      read byte 1,track_bytesB
      read byte 1,track_bytesA
   close file 1
 
   `create the actual midi file
   file=file+".mid"
   if file exist(file)=1 then delete file file
      open to write 1,file
      `write the header
      write byte 1,77:write byte 1,84:write byte 1,104:write byte 1,100:write byte 1,0:write byte 1,0:write byte 1,0:write byte 1,6
      write byte 1,0:write byte 1,0:write byte 1,0:write byte 1,1
      `write track header
      write byte 1,TEMPO:write byte 1,192 `affects duration
      write byte 1,77:write byte 1,84:write byte 1,114:write byte 1,107
      `number of bytes in the track
      write byte 1,track_bytesa:write byte 1,track_bytesb:write byte 1,track_bytesc:write byte 1,track_bytesd
      `instrument and delta set up
      write byte 1,0:write byte 1,192:write byte 1,INSTRUMENT `instrument
      write byte 1,0:write byte 1,255:write byte 1,89:write byte 1,2:write byte 1,0:write byte 1,0:write byte 1,0:write byte 1,255:write byte 1,81:write byte 1,3:write byte 1,9:write byte 1,39:write byte 1,192:write byte 1,0
      `write the notes
      `turn the note on
      for count = 0 to array count(master_note())
         write byte 1,144:write byte 1,master_note(count).note:write byte 1,master_note(count).volume:write byte 1,master_note(count).speed:write byte 1,64
         `turn it off
         write byte 1,128:write byte 1,master_note(count).note:write byte 1,0:write byte 1,0
      next count
 
      `write track end
      write byte 1,255,:write byte 1,47,:write byte 1,0,:write byte 1,1
      close file 1
 
endfunction