-
Notifications
You must be signed in to change notification settings - Fork 3
/
Auxillary_stuff.lua
323 lines (295 loc) · 7.24 KB
/
Auxillary_stuff.lua
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
maxUndos = 10
noteNamesInternational = {
nederlands = {
"feses", "ceses", "geses", "deses", "ases", "eses", "beses", -- double flats
"fes", "ces", "ges", "des", "as", "es", "bes",
"f", "c", "g", "d", "a", "e", "b",
"fis", "cis", "gis", "dis", "ais", "eis", "bis",
"fisis", "cisis", "gisis", "disis", "aisis", "eisis", "bisis",
},
}
offsetsToIntervals = {
[-12] = 0,
[-11] = 5,
[-10] = -2,
[-9] = 3,
[-8] = -4,
[-7] = 1,
[-6] = 6,
[-5] = -1, --des to c is one semitone down
[-4] = 4,
[-3] = -3,
[-2] = 2,
[-1] = -5,
[0] = 0,
[1] = 5,
[2] = -2,
[3] = 3,
[4] = -4,
[5] = 1,
[6] = -6,
[7] = -1,
[8] = 4,
[9] = -3,
[10] = 2,
[11] = -5,
[12] = 0
}
offsets = {
[0] = 0,
7, -- c# is plus 7
2,
-3,
4,
-1,
6,
1,
8,
3,
-2,
5,
}
keyNotes = {}
do
local note = 11
for i = -7, 7 do
keyNotes[i] = (( note + 11 ) % 12 ) + 61
note = ( note + 7 ) % 12
end
end
steps = {
c = 0,
d = 1,
e = 2,
f = 3,
g = 4,
a = 5,
b = 6,
}
local char = string.char
local byte = string.byte
local tunpack = table.unpack
function RealSendMidiEvent(...)
local s = ...
if type(s) == "number" then
s = char(...)
end
--print(GetTime(), s:byte(1,3))
SendMidiData(s)
end
function SendMidiEvent(...)
-- send patch first
RealSendMidiEvent(char(0xc0 | MIDIOutputChannel, PreferredPatch))
RealSendMidiEvent(...)
SendMidiEvent = RealSendMidiEvent
RealSendMidiEvent = nil
MIDIOutputChannel = nil
end
-- this is sent as the program is exiting
function AllNotesOff() -- actually all sounds off
for channel = 0, 15 do
SendMidiEvent(0xb0 | channel, 120, 0)
end
end
-- Keyboard output
local lower = string.lower
local shiftMask = 0x1000
local removeMask = shiftMask - 1
-- this is for US keyboard layouts (for '!', press shift and '1' for example)
-- Exceptions or special characters can be entered below
local shiftCodes = "~`!1@2#3$4%5^6&7*8(9)0_-+={[}]|\\:;\"'<,>.?/"
-- this table of keycodes is from /usr/include/linux/input.h
local keyCodes = {
["1"] = 2,
["2"] = 3,
["3"] = 4,
["4"] = 5,
["5"] = 6,
["6"] = 7,
["7"] = 8,
["8"] = 9,
["9"] = 10,
["0"] = 11,
["-"] = 12,
["="] = 13,
["\127"] = 14,
["\t"] =15,
["q"] = 16,
["w"] = 17,
["e"] = 18,
["r"] = 19,
["t"] = 20,
["y"] = 21,
["u"] = 22,
["i"] = 23,
["o"] = 24,
["p"] = 25,
["["] = 26,
["]"] = 27,
["\n"] = 28,
-- KEY_LEFTCTRL 29
["a"] = 30,
["s"] = 31,
["d"] = 32,
["f"] = 33,
["g"] = 34,
["h"] = 35,
["j"] = 36,
["k"] = 37,
["l"] = 38,
[";"] = 39,
["'"] = 40,
["`"] = 41,
-- KEY_LEFTSHIFT 42
["\\"] = 43,
["z"] = 44,
["x"] = 45,
["c"] = 46,
["v"] = 47,
["b"] = 48,
["n"] = 49,
["m"] = 50,
[","] = 51,
["."] = 52,
["/"] = 53,
[" "] = 57,
-- KEY_102D, the German key that is missing on English layouts
-- it should be "<" but I don’t want other layouts to pick it
-- up when they don’t need to, so I’m using a dummy character
-- that would never need to be typed.
["\x02"] = 86,
}
setmetatable(keyCodes, {
__index = function(t,c)
local n
if c:match("%u") then
n = keyCodes[lower(c)]
else
n = keyCodes[shiftCodes:match("%" .. c .. "(.)")]
end
if n then
n = n | shiftMask
rawset(t, c, n) -- cache it
return n
end
return false -- cause an error
end })
local charpattern = utf8.charpattern
-- Keyboard exceptions can be used for special characters, or for characters
-- that don’t type properly on your local keyboard. To find your keyboard
-- layout type:
--
-- > setxkbmap -query
--
-- One line should say:
-- layout: us,us
-- for example. The format for the keypresses is a set of parenthesis
-- (mandatory) with modifier keys
-- () means no modifier. S = shift,
-- A = alt, C = control, G = AltGr
-- This is followed by a single character (the key to be pressed), or one
-- of the capital words in extraKeys below.
-- (It turns out alt-control is not the same as AltGr, only on Windows)
keyboardExceptions = {
["us"] = {
-- For right single quote, compose key, quote, shift dot (for '>')
["’"] = "()COMPOSE ()' (S).",
},
["de"] = {
-- For double quotes on German keyboards, shift 2
["\""] = "(S)2",
["'"] = "(S)\\",
["{"] = "(G)7",
["}"] = "(G)0",
["/"] = "(S)7",
["\\"] = "(G)-",
["("] = "(S)8",
[")"] = "(S)9",
["|"] = "(G)\x02", -- \x02 just a placeholder character -- see line 171
["<"] = "()\x02",
[">"] = "(S)\x02",
},
}
function SendString(s, deletingFlag)
local myGap = gapBetweenKeystrokes or #s > 7
if not deletingFlag then
myKeyStrokesSent[#myKeyStrokesSent+1] = s
end
for c in s:gmatch(charpattern) do
local ke = specialKeyboardLayout and keyboardExceptions[c]
if ke then
SendKeyCombos(ke)
else
local shift = false
local code = keyCodes[c]
if code then
if code & shiftMask ~= 0 then
shift = true
code = code & removeMask
end
SendKeystroke(code, shift, myGap)
end
end
end
if (not deletingFlag) and currentUndo then
lastStringSent = s
--currentUndo.codeSent = s
local n = eventsSent.n + 1
eventsSent[n] = currentUndo
eventsSent.n = n
eventsSent[n - maxUndos] = nil
currentUndo = nil
end
end
--[[
function PrintMyTable()
for k, v in pairs(MyTable) do
print(k, v)
end
end
--]]
function DoScheduledEvent(eventTable)
local f = eventTable[1]
if type(f) == "function" then
f(tunpack(eventTable, 2))
end
end
local modifierKeys = {
S = 42, -- KEY_LEFTSHIFT
A = 56, -- KEY_LEFTALT
C = 29, -- KEY_LEFTCTRL
G = 100, -- KEY_RIGHTALT
}
local extraKeys = {
BACKSPACE = 14, -- left delete
UP = 103,
DOWN = 108,
LEFT = 105,
RIGHT = 106,
DELETE = 111, -- right delete
HOME = 102,
END = 107,
PAGEUP = 104,
PAGEDOWN = 109,
-- according to input.h, KEY_COMPOSE is 127. For me 127 is the menu key.
-- if the compose key doesn’t work, perhaps try 127
COMPOSE = 126,
RETURN = 28, -- Enter key on the main keyboard
}
setmetatable(extraKeys, { __index = keyCodes })
-- This function takes a formatted string and converts it to raw key presses.
-- Each key press may be preceeded with shift, alt or control in brackets.
-- So "(AC)DOWN" sends the down arrow with alt and control pressed
-- Use () for no modifiers
function SendKeyCombos(s, code)
for modifiers, key in s:gmatch("%((%u*)%)(.%u*)") do
local codes = {}
for m in modifiers:gmatch("%u") do
codes[#codes+1] = modifierKeys[m]
end
codes[#codes+1] = extraKeys[key]
if codes[1] then
SendKeyCombo(table.unpack(codes))
end
end
end