$ filename "PlayNotes.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 playNotes() as uint
   int err
   sys hMidiOut   
    uint PPQN_CLOCK = 15

   int i
   dword notes[] = {
         0x007f3c90,
         0x60003c90,
         0x007f3e90,
         0x60003e90,
         0x007f4090,
         0x60004090,
         0x007f4190,
         0x60004190,
         0x007f4390,
         0x60004390,
         0x007f4590,
         0x60004590,
         0x007f4790,
         0x60004790,
         0x007f4890,
         0x60004890,
         0}

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

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

   i = 1   
   while (notes[i])

      dword time = notes[i] >> 24 '0x60 or 0
      Sleep(time * PPQN_CLOCK)

      err = midiOutShortMsg(hMidiOut, notes[i])
      if(err != MMSYSERR_NOERROR) then
         printl "Error sending command, Error code: " err
      end if   

      i++
   wend

   midiOutClose(hMidiOut)
   return 0
end function

playNotes()

printl "Enter ..."
waitkey