; ; Function: MasterRouter.exe ; ; AutoIt: v3.3.14.0 ; Author: S.J.L. ; Date: March 2017 #NoTrayIcon #include #include #include #include #include #include #include ;_Singleton("Master Router.au3", 0) Opt('GUIOnEventMode', 1) Global $hGUI, $idCombo_1, $idLabel_A, $idLabel_B, $idLabel_C, $aInputs Global $sOutString, $hMidiIn, $hMidiOut, $Cb, $Callback_function Local $iIn, $iX, $iOut, $hOpen, $aDevCaps, $iDevIn $iIn = (_MidiInGetNumDevs() - 1) If $iIn = -1 Then MsgBox(0, "", "No MIDI input devices were found.") Exit EndIf For $iX = 0 To $iIn $hOpen = _MidiInOpen($iX) If @error <> 0 Then MsgBox(16, "MIDI In Port # " & $iX, '"MIDI In" Failed to Open :' & @CRLF & @CRLF & " Error Code # - " & @error) Exit EndIf $aDevCaps = _MidiInGetDevCaps($hOpen) _MidiInClose($hOpen) If @error <> 0 Then MsgBox(16, "MIDI In Port # " & $iX, '"MIDI In" failed to Close :' & @CRLF & @CRLF & " Error Code # - " & @error) Exit EndIf If $aDevCaps[3] = "MIDISPORT Uno In" Then $iDevIn = $aDevCaps[3] ExitLoop EndIf Next If $iDevIn <> "MIDISPORT Uno In" Then MsgBox(16, "Input", "Unable to access a MIDISPORT Uno Input") Exit EndIf $iOut = (_MidiOutGetNumDevs() - 1) If $iOut = -1 Then MsgBox(16, "", "No MIDI output devices were found.") Exit EndIf For $iX = 0 To $iOut $hOpen = _MidiOutOpen($iX) ; <-----------------<<< If @error <> 0 Then MsgBox(16, "MIDI Out Port # " & $iX, '"MIDI Out" Failed to Open :' & @CRLF & @CRLF & " Error Code # - " & @error) Exit EndIf $aDevCaps = _MidiOutGetDevCaps($hOpen) _MidiOutClose($hOpen) If @error <> 0 Then MsgBox(16, "MIDI Out Port # " & $iX, '"MIDI Out" failed to Close :' & @CRLF & @CRLF & " Error Code # - " & @error) Exit EndIf If $aDevCaps[3] = "Microsoft MIDI Mapper" Then ContinueLoop $sOutString = ($sOutString & "|" & $iX & " " & $aDevCaps[3]) Next $sOutString = StringReplace($sOutString, "Microsoft ", "") $sOutString = StringReplace($sOutString, "0 ", "(Default) ") $hGUI = GUICreate("MasterRouter", 258, 85, 841, 5, $WS_POPUP, $WS_EX_DLGMODALFRAME) GUISetBkColor(0x252525) $idCombo_1 = GUICtrlCreateCombo("", 10, 56, 240) GUICtrlSetBkColor($idCombo_1,0xC0C0C0) GUICtrlSetFont($idCombo_1, 11, 800) GUICtrlSetData($idCombo_1, $sOutString) $idLabel_A = GUICtrlCreateLabel("Master Router", 10, 2, 120, 20) GUICtrlSetColor($idLabel_A, 0xFFFFFF) GUICtrlSetFont($idLabel_A, 12, 800) $idLabel_B = GUICtrlCreateLabel("Destination:", 10,36) GUICtrlSetColor($idLabel_B, 0xFFFFFF) $idButton_1 = GUICtrlCreateButton("Connect", 151, 28, 100, 22) GUICtrlSetBkColor($idButton_1,0xC0C0C0) GUICtrlSetOnEvent($idButton_1, "_Func_1") $idButton_2 = GUICtrlCreateButton("Close", 216, 2, 35, 22) GUICtrlSetBkColor($idButton_2, 0xFF1515) GUICtrlSetColor($idButton_2, 0xFFFFFF) GUICtrlSetOnEvent($idButton_2, "End") GUISetState(@SW_SHOWNORMAL, $hGUI) While 1 Sleep(100) ; Used to reduce CPU usage WEnd Func MidiInProc($MidiIn, $msg, $instance, $Param1, $Param2) EndFunc ;==>MidiInProc Func _Func_1() $iDevOut = StringStripWS(StringLeft(GUICtrlRead($idCombo_1), 2), 8) If $iDevOut <> "" Then $Cb = DllCallbackRegister("MidiInProc", "long", "ptr;int;dword;dword;dword") $hMidiIn = _MidiInOpen($iDevIn, DllCallbackGetPtr($Cb), 0, $Callback_function) $hMidiOut = _MidiOutOpen($iDevOut) _MidiConnect($hMidiIn, $hMidiOut) _MidiInReset($hMidiIn) _MidiInStart($hMidiIn) GUICtrlSetData($idButton_1, "Disconnect") GUICtrlSetBkColor($idButton_1,0xC0C0C0) GUICtrlSetOnEvent($idButton_1, "_Func_2") EndIf EndFunc ;==>_Func_1 Func _Func_2() _MidiInStop($hMidiIn) _MidiDisconnect($hMidiIn, $hMidiOut) _MidiInClose($hMidiIn) _MidiOutClose($hMidiOut) DllCallbackFree($Cb) GUICtrlSetData($idButton_1, "Connect") GUICtrlSetOnEvent($idButton_1, "_Func_1") EndFunc ;==>_Func_2 Func End() _MidiInStop($hMidiIn) _MidiDisconnect($hMidiIn, $hMidiOut) _MidiInClose($hMidiIn) _MidiOutClose($hMidiOut) DllCallbackFree($Cb) GUIDelete($hGUI) Exit EndFunc ;==>End Exit