Midi2KeyStroke v 0.1
This is a stripped down version of generic midi app version 0.1
Removed all midi out.
This only works with midi input.
It has a list box to select midi port, the gui could/should be cleaned up and buttons re sized.
It will write midi input port to ini file.
I do not believe it will work with ahk - L unicode. Dll call must be changed for that to show ports properly....
In fact, whole file needs a good clean up. Sorry, no more time for this.
All in one file.
Shows 2 examples of midi note on to keypress(s) conversion.
It does NOT do all that you asked for, I must leave something for you to do.
Also - I just commented out some of the midi out parts, they could be removed totally... up to you.
Code:
/*
Midi2KeyStroke v. 0.1 by Genmce 6.4.11
Below is a hack of Generic midi program v. - 0.1 which is a hack of orbiks winmm.dll call
Removed midi out selection from generic midi program v 0.1
- select input
- open and close selected midi ports
- write port to ini file
- send keypress(s) based on midi input note number.
Currently set to midi note numbers 57 and 59.
- script will create an inifile first time it loads.
- that file can be deleted if needed via tray menu
- uses contributions by so many different people.
- post your creations back to this thread or the midi I/O thread - that way we can all build on it.
Notes - All midi in lib stuff is included here in one file, no external dlls, besides winmm.dll required.
Based on orbiks winmm.dll call and others' stuff.
comments need to be cleaned up
*/
#Persistent
#SingleInstance
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
if A_OSVersion in WIN_NT4,WIN_95,WIN_98,WIN_ME ; if not xp or 2000 quit - probably not needed but eh...
{
MsgBox This script requires Windows 2000/XP or later.
ExitApp
}
; ===============
version = Midi2KeyStroke.01 ; file name and version
; ===============
readini()
gosub, MidiPortRefresh ; used to refresh the input and output port lists - see label below
port_test(numports) ; test the ports - check for valid ports?
gosub, midiin_go ; opens the midi input port listening routine
;gosub, midiout ; opens the midi out port
gosub, midiMon ; see below - a monitor gui - for learning mostly - you will probably comment this out eventually.
; =============== end of auto execute section =====================
return
;*************************************************
;* MIDI input MESSAGE PARSE and filtering
;*************************************************
/*
PARSE - LAST MIDI MESSAGE RECEIVED -
- manipulate midi input message -
- send it to the output port
Edit the section below to process your midi data the way you want.
couple of examples provided.
You will need to use if elese or ifequal ... etc to determine which midi data you want to change and which to pass or to block.
These if statements can be nested (see below)
*/
MidiMsgDetect(hInput, midiMsg, wMsg) ; Midi input section calls this function each time a midi message is received. Then the midi message is broken up into parts for manipulation. See http://www.midi.org/techspecs/midimessages.php (decimal values).
{ ; * this function goes on for a while watch for the end brace...
global statusbyte, chan, note, cc, byte1, byte2
statusbyte := midiMsg & 0xFF ; EXTRACT THE STATUS BYTE (WHAT KIND OF MIDI MESSAGE IS IT?)
chan := (statusbyte & 0x0f) + 1 ; WHAT MIDI CHANNEL IS THE MESSAGE ON?
byte1 := (midiMsg >> 8) & 0xFF ; THIS IS DATA1 VALUE = NOTE NUMBER OR CC NUMBER
byte2 := (midiMsg >> 16) & 0xFF ; DATA2 VALUE IS NOTE VELEOCITY OR CC VALUE
;midi monitor - just a monitor showing all input
GuiControl,12:, MidiMsOut, MidiMon:%statusbyte% %chan% %byte1% %byte2% ; MidiMs (green text in gui) in midimon gui below - this is for display you will probably comment this out evenually.
; After there ok to edit - watch your " { } " - this is all part of the above MidiMsgDetect function -
;*************************************************
;* Midi filters and keypresses to send
;*************************************************
; Is midi input a Note On/off message?
if Statusbyte between 144 and 152 ;or (statusbyte between 128 and 143) ; note on message? You logic may vary...
{
if (byte1 = 57) ; test if note message is note number 57 with note on status byte
{
GuiControl,12:, MidiMs, convert1:%statusbyte% %chan% %byte1% %byte2% ; for display of filtered note passing to gui -testing only
;*************************************************
;* Send some keypresses - try testing with NOTEPAD with focus.
;*************************************************
send {tab 2}{1} ; send two tabs then the number 1
sleep, 1000 ; wait 1 seccond - sleep timers only shown for reference. May be needed for response.
send {enter}{tab 3}{2} ; send enter, then 3 tabs followed by the number 2
sleep, 2000
send {enter} {tab 4}{3} ; send enter, then tab 4 times followed by the number 3
}
if (byte1 = 59) ; test for note number 59
{
GuiControl,12:, MidiMs, convert2:%statusbyte% %chan% %byte1% %byte2% ; for display testing only
send {tab 2}{4}
sleep, 1000
send {enter}{tab 3}{5}
sleep, 2000
send {enter} {tab 4}{6}
}
} ; end of note on detection
;
;*************************************************
;* this is for cc detection statusbyte
;*************************************************
; Is midi input a CC?
if statusbyte between 176 and 192 ; check status byte for cc
{
GuiControl,12:, MidiMsOut, cc:%statusbyte% %chan% %cc% %byte2% ; just to update the gui
; do something here, if you want it to do anything with the cc except display it.
}
/*
;Here is an example of a cc on midi chan 1, operation that presses numlock or capslock depending on which cc (data1) is input.
; replace the cc statement above with this block
ifequal, statusbyte, 176 ; is data input a cc message, midi ch 1?
{
ifegual, byte1, 80 ; byte1 for cc is the cc# (is this cc, cc#80?)
{
send {NumLock} ; if it was cc80 then send a numlock keypress
}
ifequal, byte1, 90 ; is this cc, cc#90 ?
{
send {CapsLock} ; if it was cc90 then send a capslock
}
}
*/
;*************************************************
;* Is midi input a Program Change?
;*************************************************;
if statusbyte between 192 and 208 ; make this a between statement
{
;GuiControl,12:, MidiMsOut, ProgC:%statusbyte% %chan% %byte1% %byte2%
; need something for it to do here, could be converting to a cc or a note or changing the value of the pc
; however, at this point the only thing that happens is the gui change, not midi is output here.
; you may want to make a SendPc: label below
}
} ; end of MidiMsgDetect funciton
;*************************************************
;* midi monitor gui - this could be on the tray menu for use when needed.
;*************************************************;
midiMon: ;just a simple gui window for a midi monitor of sorts.
Gui, 12: +LastFound +AlwaysOnTop +Caption +ToolWindow ; +ToolWindow avoids a taskbar button and an alt-tab menu item.
Gui,12: Color, white ; %CustomColor% ;blue ;
Gui,12: Font, s15 ; Set a large font size (32-point).
gui, 12: add, text, w250 vMidiMsOut cblue, XXXXX YYYYY
Gui,12: Add, Text, w250 vMidiMs cgreen, XXXXX YYYYY ; XX & YY serve to auto-size the window.;gui, 12: add, text, w250 vMidiMsOutSend cred, XXXXX YYYYY
Gui,12: Show, xcenter y0 w500 NoActivate, %version% Midi Monitor - thingy ; NoActivate avoids deactivating the currently active window.
; =============== end monitor gui
;****************************************************************************************************************
;******************************************** midi "under the hood" *********************************************
/*
This part is meant to take care of the "under the hood" midi input and output selection and save selection to an ini file.
Hopefully it simplifies usage for others out here trying to do things with midi and ahk.
* use it as an include.
The code here was taken/modified from the work by TomB/Lazslo on Midi Output
http://www.autohotkey.com/forum/viewtopic.php?t=18711&highlight=midi+output
Orbik's Midi input thread
http://www.autohotkey.com/forum/topic30715.html
This method does NOT use the midi_in.dll, it makes direct calls to the winmm.dll
Many different people took part in the creation of this file.
; Last edited 6/17/2010 11:30 AM by genmce
*/
MidiPortRefresh: ; get the list of ports
MIlist := MidiInsList(NumPorts)
Loop Parse, MIlist, |
{
}
TheChoice := MidiInDevice + 1
/* ; midi out port stuff
MOlist := MidiOutsList(NumPorts2)
Loop Parse, MOlist, |
{
}
TheChoice2 := MidiOutDevice + 1
*/
return
;-----------------------------------------------------------------
ReadIni() ; also set up the tray Menu
{
Menu, tray, add, MidiSet ; set midi ports tray item
Menu, tray, add, ResetAll ; Delete the ini file for testing --------------------------------
global MidiInDevice, version ; MidiOutDevice ;version var is set at the beginning.
IfExist, %version%io.ini
{
IniRead, MidiInDevice, %version%io.ini, Settings, MidiInDevice , %MidiInDevice% ; read the midi In port from ini file
;IniRead, MidiOutDevice, %version%io.ini, Settings, MidiOutDevice , %MidiOutDevice% ; read the midi out port from ini file
}
Else ; no ini exists and this is either the first run or reset settings.
{
MsgBox, 1, No ini file found, Select midi ports?
IfMsgBox, Cancel
ExitApp
IfMsgBox, yes
gosub, midiset
;WriteIni()
}
}
;CALLED TO UPDATE INI WHENEVER SAVED PARAMETERS CHANGE
WriteIni()
{
global MidiInDevice, version ;MidiOutDevice
IfNotExist, %version%.ini ; if no ini
FileAppend,, %version%.ini ; make one with the following entries.
IniWrite, %MidiInDevice%, %version%io.ini, Settings, MidiInDevice
;IniWrite, %MidiOutDevice%, %version%io.ini, Settings, MidiOutDevice
}
;------------ port testing to make sure selected midi port is valid --------------------------------
port_test(numports) ; confirm selected ports exist ; CLEAN THIS UP STILL ,numports2
{
global midiInDevice, midiok ;midiOutDevice
; ----- In port selection test based on numports
If MidiInDevice not Between 0 and %numports%
{
MidiIn := 0 ; this var is just to show if there is an error - set if the ports are valid = 1, invalid = 0
;MsgBox, 0, , midi in port Error ; (this is left only for testing)
If (MidiInDevice = "") ; if there is no midi in device
MidiInerr = Midi In Port EMPTY. ; set this var = error message
;MsgBox, 0, , midi in port EMPTY
If (midiInDevice > %numports%) ; if greater than the number of ports on the system.
MidiInnerr = Midi In Port Invalid. ; set this error message
;MsgBox, 0, , midi in port out of range
}
Else
{
MidiIn := 1 ; setting var to non-error state or valid
}
/*
; ----- out port selection test based on numports2
If MidiOutDevice not Between 0 and %numports2%
{
MidiOut := 0 ; set var to 0 as Error state.
If (MidiOutDevice = "") ; if blank
MidiOuterr = Midi Out Port EMPTY. ; set this error message
;MsgBox, 0, , midi o port EMPTY
If (midiOutDevice > %numports2%) ; if greater than number of availble ports
MidiOuterr = Midi Out Port Out Invalid. ; set this error message
;MsgBox, 0, , midi out port out of range
}
*/
/*
Else
{
MidiOut := 1 ;set var to 1 as valid state.
}
; ---- test to see if ports valid, if either invalid load the gui to select.
;midicheck(MCUin,MCUout)
*/
If (%MidiIn% = 0)
{
MsgBox, 49, Midi Port Error!,%MidiInerr%`nLaunch Midi Port Selection!
IfMsgBox, Cancel
ExitApp
midiok = 0 ; Not sure if this is really needed now....
Gosub, MidiSet ;Gui, show Midi Port Selection
}
Else
{
midiok = 1
Return ; DO NOTHING - PERHAPS DO THE NOT TEST INSTEAD ABOVE.
}
}
Return
; ------------------ end of port testing ---------------------------
MidiSet: ; midi port selection gui
; ------------- MIDI INPUT SELECTION -----------------------
;Gui, Destroy
;Gosub, Suspendit
Gui, 6: Destroy
Gui, 2: Destroy
Gui, 3: Destroy
Gui, 4: Destroy
;Gui, 5: Destroy
Gui, 4: +LastFound +AlwaysOnTop +Caption +ToolWindow ;-SysMenu
Gui, 4: Font, s12
Gui, 4: add, text, x10 y10 w300 cmaroon, Select Midi Ports. ; Text title
Gui, 4: Font, s8
Gui, 4: Add, Text, x10 y+10 w175 Center , Midi In Port ;Just text label
Gui, 4: font, s8
; midi ins list box
Gui, 4: Add, ListBox, x10 w200 h100 Choose%TheChoice% vMidiInPort gDoneInChange AltSubmit, %MiList% ; --- midi in listing of ports
;Gui, Add, DropDownList, x10 w200 h120 Choose%TheChoice% vMidiInPort gDoneInChange altsubmit, %MiList% ; ( you may prefer this style, may need tweak)
; --------------- MidiOutSet ---------------------
; Gui, 4: Add, TEXT, x220 y40 w175 Center, Midi Out Port ; gDoneOutChange
; midi outlist box
;Gui, 4: Add, ListBox, x220 y62 w200 h100 Choose%TheChoice2% vMidiOutPort gDoneOutChange AltSubmit, %MoList% ; --- midi out listing
;Gui, Add, DropDownList, x220 y97 w200 h120 Choose%TheChoice2% vMidiOutPort gDoneOutChange altsubmit , %MoList%
Gui, 4: add, Button, x10 w205 gSet_Done, Done - Reload script.
Gui, 4: add, Button, xp+205 w205 gCancel, Cancel
;gui, 4: add, checkbox, x10 y+10 vNotShown gDontShow, Do Not Show at startup.
;IfEqual, NotShown, 1
;guicontrol, 4:, NotShown, 1
Gui, 4: show , , %version% Midi Port Selection ; main window title and command to show it.
Return
;-----------------gui done change stuff - see label in both gui listbox line
DoneInChange:
Gui, 4: Submit, NoHide
Gui, 4: Flash
If %MidiInPort%
UDPort:= MidiInPort - 1, MidiInDevice:= UDPort ; probably a much better way do this, I took this from JimF's qwmidi without out editing much.... it does work same with doneoutchange below.
GuiControl, 4:, UDPort, %MidiIndevice%
WriteIni()
;MsgBox, 32, , midi in device = %MidiInDevice%`nmidiinport = %MidiInPort%`nport = %port%`ndevice= %device% `n UDPort = %UDport% ; only for testing
Return
Set_Done: ; aka reload program, called from midi selection gui
Gui, 3: Destroy
Gui, 4: Destroy
sleep, 100
Reload
Return
Cancel:
Gui, Destroy
Gui, 2: Destroy
Gui, 3: Destroy
Gui, 4: Destroy
Gui, 5: Destroy
Return
; ********************** Midi output detection
/*
MidiOut: ; Function to load new settings from midi out menu item
OpenCloseMidiAPI()
h_midiout := midiOutOpen(MidiOutDevice) ; OUTPUT PORT 1 SEE BELOW FOR PORT 2
return
*/
ResetAll: ; for development only, leaving this in for a program reset if needed by user
MsgBox, 33, %version% - Reset All?, This will delete ALL settings`, and restart this program!
IfMsgBox, OK
{
FileDelete, %version%.ini ; delete the ini file to reset ports, probably a better way to do this ...
Reload ; restart the app.
}
IfMsgBox, Cancel
Return
GuiClose: ; on x exit app
Suspend, Permit ; allow Exit to work Paused. I just added this yesterday 3.16.09 Can now quit when Paused.
MsgBox, 4, Exit %version%, Exit %version%? ;
IfMsgBox No
Return
Else IfMsgBox Yes
;midiOutClose(h_midiout)
Gui, 6: Destroy
Gui, 2: Destroy
Gui, 3: Destroy
Gui, 4: Destroy
Gui, 5: Destroy
gui, 7: destroy
;gui,
Sleep 100
;winclose, Midi_in_2 ;close the midi in 2 ahk file
ExitApp
;############################################## MIDI LIB from orbik and lazslo#############
;-------- orbiks midi input code --------------
; Set up midi input and callback_window based on the ini file above.
; This code copied from ahk forum Orbik's post on midi input
; nothing below here to edit.
; =============== midi in =====================
Midiin_go:
DeviceID := MidiInDevice ; midiindevice from IniRead above assigned to deviceid
CALLBACK_WINDOW := 0x10000 ; from orbiks code for midi input
Gui, +LastFound ; set up the window for midi data to arrive.
hWnd := WinExist() ;MsgBox, 32, , line 176 - mcu-input is := %MidiInDevice% , 3 ; this is just a test to show midi device selection
hMidiIn =
VarSetCapacity(hMidiIn, 4, 0)
result := DllCall("winmm.dll\midiInOpen", UInt,&hMidiIn, UInt,DeviceID, UInt,hWnd, UInt,0, UInt,CALLBACK_WINDOW, "UInt")
If result
{
MsgBox, Error, midiInOpen Returned %result%`n
;GoSub, sub_exit
}
hMidiIn := NumGet(hMidiIn) ; because midiInOpen writes the value in 32 bit binary Number, AHK stores it as a string
result := DllCall("winmm.dll\midiInStart", UInt,hMidiIn)
If result
{
MsgBox, Error, midiInStart Returned %result%`nRight Click on the Tray Icon - Left click on MidiSet to select valid midi_in port.
;GoSub, sub_exit
}
OpenCloseMidiAPI()
; ----- the OnMessage listeners ----
; #define MM_MIM_OPEN 0x3C1 /* MIDI input */
; #define MM_MIM_CLOSE 0x3C2
; #define MM_MIM_DATA 0x3C3
; #define MM_MIM_LONGDATA 0x3C4
; #define MM_MIM_ERROR 0x3C5
; #define MM_MIM_LONGERROR 0x3C6
OnMessage(0x3C1, "MidiMsgDetect") ; calling the function MidiMsgDetect in get_midi_in.ahk
OnMessage(0x3C2, "MidiMsgDetect")
OnMessage(0x3C3, "MidiMsgDetect")
OnMessage(0x3C4, "MidiMsgDetect")
OnMessage(0x3C5, "MidiMsgDetect")
OnMessage(0x3C6, "MidiMsgDetect")
Return
;--- MIDI INS LIST FUNCTIONS - port handling -----
MidiInsList(ByRef NumPorts)
{ ; Returns a "|"-separated list of midi output devices
local List, MidiInCaps, PortName, result
VarSetCapacity(MidiInCaps, 50, 0)
VarSetCapacity(PortName, 32) ; PortNameSize 32
NumPorts := DllCall("winmm.dll\midiInGetNumDevs") ; #midi output devices on system, First device ID = 0
Loop %NumPorts%
{
result := DllCall("winmm.dll\midiInGetDevCapsA", UInt,A_Index-1, UInt,&MidiInCaps, UInt,50, UInt)
If (result OR ErrorLevel) {
List .= "|-Error-"
Continue
}
DllCall("RtlMoveMemory", Str,PortName, UInt,&MidiInCaps+8, UInt,32) ; PortNameOffset 8, PortNameSize 32
List .= "|" PortName
}
Return SubStr(List,2)
}
MidiInGetNumDevs() { ; Get number of midi output devices on system, first device has an ID of 0
Return DllCall("winmm.dll\midiInGetNumDevs")
}
MidiInNameGet(uDeviceID = 0) { ; Get name of a midiOut device for a given ID
;MIDIOUTCAPS struct
; WORD wMid;
; WORD wPid;
; MMVERSION vDriverVersion;
; CHAR szPname[MAXPNAMELEN];
; WORD wTechnology;
; WORD wVoices;
; WORD wNotes;
; WORD wChannelMask;
; DWORD dwSupport;
VarSetCapacity(MidiInCaps, 50, 0) ; allows for szPname to be 32 bytes
OffsettoPortName := 8, PortNameSize := 32
result := DllCall("winmm.dll\midiInGetDevCapsA", UInt,uDeviceID, UInt,&MidiInCaps, UInt,50, UInt)
If (result OR ErrorLevel) {
MsgBox Error %result% (ErrorLevel = %ErrorLevel%) in retrieving the name of midi Input %uDeviceID%
Return -1
}
VarSetCapacity(PortName, PortNameSize)
DllCall("RtlMoveMemory", Str,PortName, Uint,&MidiInCaps+OffsettoPortName, Uint,PortNameSize)
Return PortName
}
MidiInsEnumerate() { ; Returns number of midi output devices, creates global array MidiOutPortName with their names
local NumPorts, PortID
MidiInPortName =
NumPorts := MidiInGetNumDevs()
Loop %NumPorts% {
PortID := A_Index -1
MidiInPortName%PortID% := MidiInNameGet(PortID)
}
Return NumPorts
}
OpenCloseMidiAPI() { ; at the beginning to load, at the end to unload winmm.dll
static hModule
If hModule
DllCall("FreeLibrary", UInt,hModule), hModule := ""
If (0 = hModule := DllCall("LoadLibrary",Str,"winmm.dll")) {
MsgBox Cannot load libray winmm.dll
Exit
}
}