forked from cifkao/tonnetz-viz
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkeyboard.js
121 lines (93 loc) · 2.06 KB
/
keyboard.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
var keyboard = (function() {
"use strict";
var module = {};
var map = {};
var hold = [];
var keys = [
[49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 189, 187],
[81, 87, 69, 82, 84, 89, 85, 73, 79, 80, 219, 221],
[65, 83, 68, 70, 71, 72, 74, 75, 76, 186, 222],
[90, 88, 67, 86, 66, 78, 77, 188, 190, 191]
];
for (let i = 0; i < keys.length; i++) {
let row = keys[i];
for (let j = 0; j < row.length; j++) {
let key = row[j];
map[key] = (4 * i + 7 * j) % 12;
}
}
var base = 1;
module.init = function() {
$(window).keydown(onKeyDown);
$(window).keyup(onKeyUp);
};
var getPitchFromKeyboardEvent = function(key) {
var note = (base + map[key]) % 12;
if (isFinite(note))
return note;
else
return null;
};
var sustainNotes = {};
var sustain = false;
var sustainOn = function() {
sustain = true;
tonnetz.sustainOn(16);
};
var sustainOff = function() {
sustain = false;
tonnetz.sustainOff(16);
for (let n in sustainNotes)
noteOff(n);
};
var noteOn = function(note) {
tonnetz.noteOn(16, note);
audio.noteOn(note);
delete sustainNotes[note];
};
var noteOff = function(note) {
tonnetz.noteOff(16, note);
if (sustain)
sustainNotes[note] = true;
else
audio.noteOff(note);
};
var transpose = function(delta) {
base = (base + delta + 12) % 12;
tonnetz.panic();
};
var onKeyDown = function(event) {
var key = event.which;
if (hold[key])
return;
var note = getPitchFromKeyboardEvent(key);
var special = event.ctrlKey || event.altKey || event.metaKey;
if (special)
return;
if (note != null)
noteOn(note);
else if (16 == key)
sustainOn();
else if (32 == key)
tonnetz.panic();
else if (40 == key)
transpose(4);
else if (38 == key)
transpose(-4);
else if (39 == key)
transpose(7);
else if (37 == key)
transpose(-7);
hold[key] = true;
};
var onKeyUp = function(event) {
var key = event.which;
var note = getPitchFromKeyboardEvent(key);
if (note != null)
noteOff(note);
else if (16 == key)
sustainOff();
hold[key] = false;
};
return module;
})();