-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyrecognizer.js
626 lines (539 loc) · 17.1 KB
/
keyrecognizer.js
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
import { MidiConstants, PianoKeyConstants } from "./constants.js";
import KeyData from "./keydata.js";
import _ from "lodash";
import robot from "robotjs";
const Modes = {
LETTERS: "LETTERS",
COMMANDS: "COMMANDS",
NUMBERS: "NUMBERS",
};
class KeyRecognizer {
constructor() {
this.intervalModeKeyNumber = PianoKeyConstants.F2;
/**
* Keys: the "data1" property
* Values: KeyData objects
*
* A key/value pair is added when a note is pressed and removed when the
* note is released.
* @type {Object<number, KeyData>}
*/
this.keys = {};
/**
* History:
* 3/29/19 - changing from 90 to 80 since it's too high
* 3/30/19 - changing from 80 to 85 since it's too low
*/
this.velocityThreshold = 85;
// This gets added to velocityThreshold for some keys. This is only intended
// for use for keys where you either end up naturally mashing that key or
// the key itself is broken.
this.additionalVelocityThreshold = 10;
/**
* When true, we are only in Numbers Mode for a single keypress. After that
* keypress, we enter Letters Mode regardless of where we came from.
*/
this.temporarilyInNumbersMode = false;
this.mode = Modes.LETTERS;
this.handlers = {
[Modes.LETTERS]: this.handleLetterInput,
[Modes.COMMANDS]: this.handleCommandInput,
[Modes.NUMBERS]: this.handleNumberInput,
};
/**
* The keys of this object are the piano key numbers. The values are
* handlers for those keys. This is populated by
* formPianoKeyToLetterMapping.
*
* The functions will get two arguments: velocity (number) and
* hasExceededVelocityThreshold (boolean).
* @type {Object<string, Function>}
* @see formPianoKeyToLetterMapping
*/
this.pianoKeyToLetterMapping = {};
/**
* See pianoKeyToLetterMapping. This structure is used when in NUMBERS mode.
*
* This could be combined with pianoKeyToLetterMapping by just making the
* values be objects with different handlers based on the mode that you're
* in.
*/
this.pianoKeyToNumberMapping = {};
/**
* See pianoKeyToLetterMapping. This structure is used when in COMMANDS
* mode.
*/
this.pianoKeyToCommandMapping = {};
this.formPianoKeyToLetterMapping();
this.formPianoKeyToNumberMapping();
this.formPianoKeyToCommandMapping();
setInterval(() => {
if (this.isTemporarilyHandlingIntervalsAndChords()) {
this.handleKeyPress();
}
}, 16);
}
/**
* This just populates the key-mapping dictionary.
*/
formPianoKeyToNumberMapping() {
this.pianoKeyToNumberMapping[PianoKeyConstants.DS3] = (
vel,
hasExceededVelocityThreshold
) => {
if (hasExceededVelocityThreshold) robot.keyTap("\\");
else robot.keyTap("/");
};
this.pianoKeyToNumberMapping[PianoKeyConstants.DS4] = (
vel,
hasExceededVelocityThreshold
) => robot.keyTap("\\", "shift");
this.pianoKeyToNumberMapping[PianoKeyConstants.FS4] = (
vel,
hasExceededVelocityThreshold
) => {
if (hasExceededVelocityThreshold) robot.keyTap("]", "shift");
else robot.keyTap("[", "shift");
};
this.pianoKeyToNumberMapping[PianoKeyConstants.GS4] = (
vel,
hasExceededVelocityThreshold
) => {
if (hasExceededVelocityThreshold) robot.keyTap("]");
else robot.keyTap("[");
};
this.pianoKeyToNumberMapping[PianoKeyConstants.AS4] = (
vel,
hasExceededVelocityThreshold
) => {
if (hasExceededVelocityThreshold) robot.keyTap(".");
else robot.keyTap(",");
};
const pianoKeyToNumberMap = {
[PianoKeyConstants.G2]: "1",
[PianoKeyConstants.GS2]: ";",
[PianoKeyConstants.A2]: "2",
[PianoKeyConstants.B2]: "3",
[PianoKeyConstants.C3]: "4",
[PianoKeyConstants.D3]: "5",
[PianoKeyConstants.C4]: "6",
[PianoKeyConstants.D4]: "7",
[PianoKeyConstants.E4]: "8",
[PianoKeyConstants.F4]: "9",
[PianoKeyConstants.G4]: "0",
[PianoKeyConstants.A4]: "-",
[PianoKeyConstants.B4]: "=",
};
_.forEach(pianoKeyToNumberMap, (number, pianoKeyNumber) => {
this.pianoKeyToNumberMapping[pianoKeyNumber] = (
vel,
hasExceededVelocityThreshold
) =>
robot.keyTap(number, hasExceededVelocityThreshold ? "shift" : "none");
});
}
formPianoKeyToCommandMapping() {
this.pianoKeyToCommandMapping[PianoKeyConstants.G2] = () =>
robot.keyTap("down");
this.pianoKeyToCommandMapping[PianoKeyConstants.GS2] = () =>
robot.keyTap("up");
this.pianoKeyToCommandMapping[PianoKeyConstants.E4] = () =>
robot.keyTap("z", "control");
this.pianoKeyToCommandMapping[PianoKeyConstants.F4] = () =>
robot.keyTap("y", "control");
this.pianoKeyToCommandMapping[PianoKeyConstants.C3] = (
vel,
hasExceededVelocityThreshold
) => {
if (hasExceededVelocityThreshold) {
robot.keyTap("backspace", ["control", "shift"]);
} else {
robot.keyTap("home");
}
};
this.pianoKeyToCommandMapping[PianoKeyConstants.CS3] = (
vel,
hasExceededVelocityThreshold
) => {
if (hasExceededVelocityThreshold) robot.keyTap("backspace", "control");
else robot.keyTap("left", "control");
};
this.pianoKeyToCommandMapping[PianoKeyConstants.D3] = (
vel,
hasExceededVelocityThreshold
) => {
if (hasExceededVelocityThreshold) robot.keyTap("backspace");
else robot.keyTap("left");
};
this.pianoKeyToCommandMapping[PianoKeyConstants.C4] = (
vel,
hasExceededVelocityThreshold
) => {
if (hasExceededVelocityThreshold) robot.keyTap("delete");
else robot.keyTap("right");
};
this.pianoKeyToCommandMapping[PianoKeyConstants.CS4] = (
vel,
hasExceededVelocityThreshold
) => {
if (hasExceededVelocityThreshold) robot.keyTap("delete", "control");
else robot.keyTap("right", "control");
};
this.pianoKeyToCommandMapping[PianoKeyConstants.D4] = (
vel,
hasExceededVelocityThreshold
) => {
if (hasExceededVelocityThreshold) {
robot.keyTap("delete", ["control", "shift"]);
} else {
robot.keyTap("end");
}
};
}
formPianoKeyToLetterMapping() {
this.pianoKeyToLetterMapping[PianoKeyConstants.FS2] = (
vel,
hasExceededVelocityThreshold
) => robot.keyTap("space");
this.pianoKeyToLetterMapping[PianoKeyConstants.A4] = (
vel,
hasExceededVelocityThreshold
) => robot.keyTap("backspace");
const pianoKeyToLetterMap = {
[PianoKeyConstants.G2]: "L",
[PianoKeyConstants.GS2]: "P",
[PianoKeyConstants.A2]: "R",
[PianoKeyConstants.AS2]: "M",
[PianoKeyConstants.B2]: "N",
[PianoKeyConstants.C3]: "O",
[PianoKeyConstants.CS3]: "C",
[PianoKeyConstants.D3]: "T",
[PianoKeyConstants.DS3]: "W",
[PianoKeyConstants.E3]: "B",
[PianoKeyConstants.F3]: "K",
[PianoKeyConstants.FS3]: "J",
[PianoKeyConstants.G3]: "Q",
[PianoKeyConstants.GS3]: "X",
[PianoKeyConstants.A3]: "Z",
[PianoKeyConstants.AS3]: "Y",
[PianoKeyConstants.B3]: "G",
[PianoKeyConstants.C4]: "E",
[PianoKeyConstants.CS4]: "D",
[PianoKeyConstants.D4]: "A",
[PianoKeyConstants.DS4]: "U",
[PianoKeyConstants.E4]: "I",
[PianoKeyConstants.F4]: "S",
[PianoKeyConstants.FS4]: "F",
[PianoKeyConstants.G4]: "H",
[PianoKeyConstants.GS4]: "V",
};
_.forEach(pianoKeyToLetterMap, (letter, pianoKeyNumber) => {
this.pianoKeyToLetterMapping[pianoKeyNumber] = (
vel,
hasExceededVelocityThreshold
) => this.typeLetter(letter, hasExceededVelocityThreshold);
});
}
hasExceededVelocityThreshold = (data1, data2) => {
let threshold = this.velocityThreshold;
if (data1 === PianoKeyConstants.B2) {
threshold += this.additionalVelocityThreshold;
}
return data2 > threshold;
};
handleNumberInput = (data1, data2) => {
const exceededVelocityThreshold = this.hasExceededVelocityThreshold(
data1,
data2
);
let handled = false;
if (data1 in this.pianoKeyToNumberMapping) {
const handlerFn = this.pianoKeyToNumberMapping[data1];
handlerFn(data2, exceededVelocityThreshold);
handled = true;
}
if (handled && this.temporarilyInNumbersMode) {
this.temporarilyInNumbersMode = false;
this.setMode(Modes.LETTERS);
}
return handled;
};
handleCommandInput = (data1, data2) => {
const exceededVelocityThreshold = this.hasExceededVelocityThreshold(
data1,
data2
);
if (data1 in this.pianoKeyToCommandMapping) {
const handlerFn = this.pianoKeyToCommandMapping[data1];
handlerFn(data2, exceededVelocityThreshold);
return true;
}
return false;
};
handleLetterInput = (data1, data2) => {
const exceededVelocityThreshold = this.hasExceededVelocityThreshold(
data1,
data2
);
if (data1 in this.pianoKeyToLetterMapping) {
const handlerFn = this.pianoKeyToLetterMapping[data1];
handlerFn(data2, exceededVelocityThreshold);
return true;
}
return false;
};
printAllKeysPressed() {
if (_.isEmpty(this.keys)) {
console.log("(you have no keys pressed right now)");
} else {
console.log("All keys pressed:");
const sortedKeys = _.orderBy(this.keys, "timePressed");
for (const data1 in sortedKeys) {
console.log(`\t${data1}: ${sortedKeys[data1]}`);
}
}
}
/**
* This function will return all of the keys pressed within the last X
* milliseconds as they should all be considered part of the same
* chord/interval.
* @return {Array<KeyData>}
*/
getChordKeys() {
const curTime = Date.now();
if (this.isTemporarilyHandlingIntervalsAndChords()) {
// The head of the array will be the oldest key.
//
// We need to wait a long enough time after the last key before trying to
// handle them as an interval or chord. That way, pressing C-E isn't
// interpreted as a major third when a G would follow shortly thereafter
// (thus making it a major triad),
const sortedKeys = _.orderBy(this.keys, "timePressed");
const lastKeyPressed = _.last(sortedKeys);
if (
!_.isNil(lastKeyPressed) &&
curTime < lastKeyPressed.timePressed + 25
) {
return [];
}
// return _.filter(sortedKeys, (keyData, index) => {
// const onLastKey = index === _.size(sortedKeys);
// const waitTime = onLastKey - 1 ? 50 : 0;
// return curTime >= keyData.timePressed + waitTime && !keyData.handled;
// });
}
return _.filter(this.keys, (keyData, index) => !keyData.handled);
}
getOldestKeyHeld() {
// The head of the array will be the oldest key
const sortedKeys = _.orderBy(this.keys, "timePressed");
const numKeys = _.size(sortedKeys);
if (numKeys < 2) {
return null;
}
return _.head(sortedKeys);
}
handleTriadQuality(chordKeys) {
// Check for major triads
if (_.size(chordKeys) !== 3) {
return false;
}
// 1. Sort from order of note (low to high)
// 2. Get the two intervals (bass --> second, bass --> third)
// 3. Check if the intervals correspond to a major triad
const sortedAscending = _.orderBy(chordKeys, "data1");
const interval1 = sortedAscending[0].getIntervalTo(sortedAscending[1]);
const interval2 = sortedAscending[0].getIntervalTo(sortedAscending[2]);
const isMajor =
(interval1 === 4 && interval2 === 7) ||
(interval1 === 3 && interval2 === 8) ||
(interval1 === 5 && interval2 === 9);
if (isMajor) {
console.log("MAJOR CHORD PRESSED");
this.setMode(Modes.LETTERS);
return true;
}
const isMinor =
(interval1 === 3 && interval2 === 7) ||
(interval1 === 4 && interval2 === 9) ||
(interval1 === 5 && interval2 === 8);
if (isMinor) {
console.log("MINOR CHORD PRESSED");
this.setMode(Modes.COMMANDS);
return true;
}
const isAugmented = interval1 === 4 && interval2 === 8;
if (isAugmented) {
console.log("AUGMENTED CHORD PRESSED");
this.setMode(Modes.NUMBERS);
return true;
}
return false;
}
setMode(mode) {
this.mode = mode;
}
handleInterval(chordKeys) {
if (_.size(chordKeys) !== 2) {
return false;
}
const exceededVelocityThreshold =
chordKeys[0].data2 > this.velocityThreshold ||
chordKeys[1].data2 > this.velocityThreshold;
const interval = chordKeys[0].getIntervalTo(chordKeys[1]);
// An interval of 24 is two whole octaves, which is where the pinkies naturally rest.
if (interval === 1 || interval === 2 || interval === 24) {
robot.keyTap("space");
// console.log('SPACE PRESSED');
return true;
}
if (interval === 3 || interval === 4) {
console.log(" ENTER PRESSED");
robot.keyTap("enter");
return true;
}
if (interval === 5) {
console.log(" COMMA PRESSED");
robot.typeString(", ");
return true;
}
if (interval === 6) {
console.log(" BACKSPACE PRESSED");
robot.keyTap("backspace");
return true;
}
if (interval === 7) {
console.log(" PERIOD PRESSED");
robot.typeString(". ");
return true;
}
if (interval === 8) {
console.log(" - PRESSED");
robot.keyTap("-");
return true;
}
if (interval === 9) {
console.log(" APOSTROPHE PRESSED");
robot.keyTap("'", exceededVelocityThreshold ? "shift" : "none");
return true;
}
if (interval === 10) {
console.log(" ? PRESSED");
robot.keyTap("/", "shift");
robot.typeString(" ");
return true;
}
if (interval === 11) {
console.log(" ! PRESSED");
robot.keyTap("1", "shift");
robot.typeString(" ");
return true;
}
// An interval of 14 is a major 9th, which is where both pointer fingers naturally rest.
if (interval === 14) {
this.temporarilyInNumbersMode = true;
this.setMode(Modes.NUMBERS);
console.log("In Numbers Mode temporarily");
return true;
}
if (interval === 12) {
console.log(" ctrl+bckspc");
robot.keyTap("backspace", "control");
return true;
}
return false;
}
handleChord(chordKeys) {
let handled = false;
if (this.handleTriadQuality(chordKeys)) {
handled = true;
}
if (!handled && this.handleInterval(chordKeys)) {
handled = true;
}
if (handled) {
_.forEach(chordKeys, (keyData) => {
keyData.handled = handled;
});
}
return handled;
}
typeLetter(letter, hasExceededVelocityThreshold) {
if (hasExceededVelocityThreshold) {
letter = letter.toUpperCase();
} else {
letter = letter.toLowerCase();
}
robot.typeString(letter);
process.stdout.write(letter);
}
handleNonChord(chordKeys) {
if (_.size(chordKeys) !== 1) {
return false;
}
const { data1, data2, handled: wasAlreadyHandled } = chordKeys[0];
// Don't handle one of these twice.
if (wasAlreadyHandled) {
return true;
}
const handled = this.handlers[this.mode](data1, data2);
chordKeys[0].handled = handled;
return handled;
}
isTemporarilyHandlingIntervalsAndChords() {
return !_.isNil(this.keys[this.intervalModeKeyNumber]);
}
handleKeyPress() {
// Figure out how much time passed between the last key that was held and
// this one so that we can figure out if it should be part of the chord.
const chordKeys = this.getChordKeys();
if (this.isTemporarilyHandlingIntervalsAndChords()) {
this.handleChord(chordKeys);
return;
}
// If we didn't find a chord handler, then interpret all of the keys we
// collected as individual keys.
const unhandled = [];
if (_.size(chordKeys) > 1) {
console.log(
"Interpreting multiple chord keys as single keys chordKeys: " +
JSON.stringify(chordKeys, null, 2)
);
}
// Play them back from oldest to newest
const sortedKeys = _.orderBy(chordKeys, "timePressed");
_.forEach(sortedKeys, (chordKey) => {
const handled = this.handleNonChord([chordKey]);
if (!handled) {
unhandled.push(chordKey);
}
});
if (_.isEmpty(unhandled)) {
return;
}
console.log("Found NO handler for this");
this.printAllKeysPressed();
console.log("chordKeys: " + JSON.stringify(chordKeys, null, 2));
}
addKeyData(keyData) {
this.keys[keyData.data1] = keyData;
// Mark it as handled immediately so that we don't try handling it twice.
if (keyData.data1 === this.intervalModeKeyNumber) {
keyData.handled = true;
}
}
processMessage(status, data1, data2, deltaTime) {
if (status === MidiConstants.NOTE_ON) {
const keyData = new KeyData(data1, data2);
this.addKeyData(keyData);
this.handleKeyPress();
} else if (status === MidiConstants.NOTE_OFF) {
delete this.keys[data1];
} else {
console.log(
`status: ${status} data1: ${data1} data2: ${data2} deltaTime: ${deltaTime}`
);
}
}
}
export default KeyRecognizer;