$ filename "PlayMidiNotes.exe"

'uses rtl32
'uses rtl64

uses console

'uses minmidi
'octave 0..10
extern lib "Winmm.dll"

sys midiOutOpen(sys *phmo, uDeviceID,dwCallback, dwInstance,fdwOpen)
sys midiOutShortMsg(sys hmo, dwMsg)
sys midiOutClose(sys hmo)
sys midiOutReset(sys hmo)

% MIDIMAPPER = 0xFFFFFFFF
% MIDI_MAPPER = 0xFFFFFFFF
% CALLBACK_NULL = 0
% MMSYSERR_NOERROR = 0

function freq(int d) as double
   return pow(2,(d-69)/12)*440
end function

function playNotes() as uint
   int err
   sys hMidiOut   
   uint PPQN_CLOCK = 5

   err = midiOutOpen(hMidiOut, MIDIMAPPER, 0, 0, CALLBACK_NULL)
   if (err != MMSYSERR_NOERROR) then
      printl "Error opening MIDI Mapper, Error code: " err
   end if

   int Instrument = 72  'Piccolo, 0x48
   err = midiOutShortMsg(hMidiOut, (0x100 * Instrument) + 0xc0)
   'err = midiOutShortMsg(hMidiOut, 0x48c0)

   dword noteOn
   dword noteOff
   int i = 20
 
   printl "Midi"
   printl "Num   NoteOn      NoteOff" 
   while i < 127   
       i+=1 : print i ") "       
       
       '         velocity           midi num     cmd: on
       noteOn = (0x7f * 0x10000) + (i * 0x100) + 0x90 : print "0x00" hex (noteOn) ", "
       
       err = midiOutShortMsg(hMidiOut, noteOn)
       if(err != MMSYSERR_NOERROR) then
          printl "Error sending command, Error code: " err
       end if   
       
       noteOff = (i * 0x100) + 0x80 : print "0x0000" hex(noteOff) ", "
       print " Hertz: " str(freq(i),2) + cr
       Sleep(100 * PPQN_CLOCK)

       err = midiOutShortMsg(hMidiOut, noteOff)
       if(err != MMSYSERR_NOERROR) then
          printl "Error sending command, Error code: " err
       end if         
   wend

   midiOutReset(hMidiOut)
   midiOutClose(hMidiOut)
   return 0
end function

playNotes()

printl "Enter ..."
waitkey