-
Notifications
You must be signed in to change notification settings - Fork 2
/
midi_in_lib.ahk
180 lines (155 loc) · 3.72 KB
/
midi_in_lib.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
midi_in_Open(defaultDevID = -1)
{
global
if ((midi_in_hModule := DllCall("LoadLibrary", Str,A_ScriptDir . "\midi_in.dll")) == 0)
{
MsgBox "Cannot load library midi_in.dll"
return 1
}
if (defaultDevID >= DllCall("midi_in.dll\getNumDevs"))
defaultDevID := -1
midi_in_MakeTrayMenu(defaultDevID)
if (defaultDevID >= 0)
midi_in_OpenDevice(defaultDevID)
return 0
}
midi_in_MakeTrayMenu(defaultDevID)
{
numDevs := DllCall("midi_in.dll\getNumDevs")
global midi_in_lastSelectedMenuItem
Menu devNameMenu, Add, No input, sub_menu_openinput
Menu devNameMenu, Add ; separator
if (defaultDevID < 0)
midi_in_lastSelectedMenuItem := "No Input"
loop %numDevs%
{
devID := A_Index-1
if ((devName := DllCall("midi_in.dll\getDevName", Int,devID, Str)) == 0)
{
MsgBox, Error in creating midi input device list
return 1
}
Menu devNameMenu, Add, %devName%, sub_menu_openinput
if (devID == defaultDevID)
{
Menu devNameMenu, Check, %devName%
midi_in_lastSelectedMenuItem := devName
}
}
Menu TRAY, Add, MIDI-in device, :devNameMenu
}
sub_menu_openinput:
midi_in_OpenDevice(A_ThisMenuItemPos-3)
; Move the check mark to new position
Menu %A_ThisMenu%, Check, %A_ThisMenuItem%
Menu %A_ThisMenu%, Uncheck, %midi_in_lastSelectedMenuItem%
midi_in_lastSelectedMenuItem := A_ThisMenuItem
return
midi_in_OpenDevice(deviceID) ;deviceID < 0 means no input
{
Critical
midi_in_Stop()
Gui +LastFound
hWnd := WinExist()
curDevID := DllCall("midi_in.dll\getCurDevID", Int)
if (deviceID == curDevID)
return 0
if (curDevID >= 0)
result := DllCall("midi_in.dll\close")
if (result)
{
MsgBox Error closing midi device`nmidi_in.dll\close returned %result%
return 1
}
if (deviceID < 0)
return 0
result := DllCall("midi_in.dll\open", UInt,hWnd, Int,deviceID, Int)
if (result)
{
MsgBox Error opening midi device`nmidi_in.dll\open(%hWnd%, %deviceID%) returned %result%
return 1
}
; MsgBox Press OK to start midi input
midi_in_Start()
return 0
}
midi_in_Close()
{
global
if (midi_in_hModule)
DllCall("FreeLibrary", UInt,midi_in_hModule), midi_in_hModule := ""
}
midi_in_Start()
{
DllCall("midi_in.dll\start")
}
midi_in_Stop()
{
DllCall("midi_in.dll\stop")
}
listenNote(noteNumber, funcName, channel=0)
{
global msgNum
GoSub, sub_increase_msgnum
DllCall("midi_in.dll\listenNote", Int,noteNumber, Int,channel, Int,msgNum)
OnMessage(msgNum, funcName)
}
listenNoteRange(rangeStart, rangeEnd, funcName, flags=0, channel=0)
{
global msgNum
GoSub, sub_increase_msgnum
msgCount := DllCall("midi_in.dll\listenNoteRange", int,rangeStart, int,rangeEnd, int,(flags & 0x07), int,channel, int,msgNum)
if (msgCount <= 0)
return
if (flags & 0x01)
loop %msgCount%
{
OnMessage(msgNum, funcName . A_Index)
GoSub, sub_increase_msgnum
}
else
OnMessage(msgNum, funcName)
}
listenCC(ccNumber, funcName, channel=0)
{
global msgNum
GoSub, sub_increase_msgnum
DllCall("midi_in.dll\listenCC", Int,ccNumber, Int,channel, Int,msgNum)
OnMessage(msgNum, funcName)
}
listenWheel(funcName, channel=0)
{
global msgNum
GoSub, sub_increase_msgnum
DllCall("midi_in.dll\listenWheel", Int,channel, Int,msgNum)
OnMessage(msgNum, funcName)
}
listenChanAT(funcName, channel=0)
{
global msgNum
GoSub, sub_increase_msgnum
DllCall("midi_in.dll\listenChanAT", Int,channel, Int,msgNum)
OnMessage(msgNum, funcName)
}
getNoteOn(noteNumber, channel)
{
return DllCall("midi_in.dll\getNoteOn", Int,noteNumber, Int,channel)
}
getCC(ccNumber, channel)
{
return DllCall("midi_in.dll\getCC", Int,ccNumber, Int,channel)
}
getWheel(channel)
{
return DllCall("midi_in.dll\getWheel", Int,channel)
}
getChanAT(channel)
{
return DllCall("midi_in.dll\getChanAT", Int,channel)
}
sub_increase_msgnum:
if msgNum
msgNum++
else
msgNum := 0x2000
return