-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyboard.js
192 lines (172 loc) · 4.26 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
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
/*
* Isomorphic keyboard player class.
*
*
* created by Piers Titus van der Torren, 2010 - http://www.toverlamp.org/static/wickisynth/
*
* Changelog
* 2010-10-28: added key repeat array for disabling key repeat.
* Previous versions of FF4 for linux didn't have key repeat on keyDown, but since now they have it.
* 2014-11-18: move sustain to this class
* send note location to keydown event
*/
Keyboard = function Keyboard(document, notePlayListener, noteStopListener, noteSustainListener) {
this.repeat = [];
this.document = document;
this.notePlayListener = notePlayListener;
this.noteStopListener = noteStopListener;
this.sustain = false;
this.sustainedNotes = [];
this.octave = 0;
this.generator = [700,1200]; // fifth and octave, in cents. Default is 12tet tuning
this.baseFreq = 293.66; // D3 (or Re)
// wicki key layout
this.keyMap = {
'Z': [-9,4],
'X': [-7,3],
'C': [-5,2],
'V': [-3,1],
'B': [-1,0],
'N': [1,-1],
'M': [3,-2],
'A': [-10,5],
'S': [-8,4],
'D': [-6,3],
'F': [-4,2],
'G': [-2,1],
'H': [0, 0],
'J': [2,-1],
'K': [4,-2],
'L': [6,-3],
';': [8,-4],
'Q': [-11,6],
'W': [-9,5],
'E': [-7,4],
'R': [-5,3],
'T': [-3,2],
'Y': [-1,1],
'U': [1,0],
'I': [3,-1],
'O': [5,-2],
'P': [7,-3],
'1': [-12,7],
'2': [-10,6],
'3': [-8,5],
'4': [-6,4],
'5': [-4,3],
'6': [-2,2],
'7': [0,1],
'8': [2,0],
'9': [4,-1],
'0': [6,-2],
'q': [-11,7], // F2
'r': [-9,6], // F3
's': [-7,5], // F4
't': [-5,4], // F5
'u': [-3,3], // F6
'v': [-1,2], // F7
'w': [1,1], // F8
'x': [3,0], // F9
'y': [5,-1], // F10
'z': [7,-2], // F11
};
this.keyMap[ String.fromCharCode(188) ] = [5,-3]; // ','
this.keyMap[ String.fromCharCode(190) ] = [7,-4]; // '.'
this.keyMap[ String.fromCharCode(191) ] = [9,-5]; // '/'
this.keyMap[ String.fromCharCode(219) ] = [9,-4]; // '['
var that = this;
this.document.addEventListener('keydown', function(event){
that.keyDown.apply(that, arguments);
}, false);
this.document.addEventListener('keyup', function(event){
that.keyUp.apply(that, arguments);
}, false);
};
Keyboard.prototype.setGenerator = function(generator) {
this.generator = generator;
};
Keyboard.prototype.setSustain = function(sustain) {
switch(sustain) {
case true:
case 'on':
this.sustain = true;
break;
case false:
case 'off':
this.sustain = false;
break;
case 'switch':
this.sustain = !this.sustain;
break;
}
if (!this.sustain) {
for (var i = this.sustainedNotes.length - 1; i >= 0; i--) {
this.noteStopListener({keyId:this.sustainedNotes[i]});
};
this.sustainedNotes = [];
}
};
Keyboard.prototype.keyDown = function(event) {
var keyId = String.fromCharCode(event.keyCode);
//console.log('keyCode: ' + event.keyCode + ', keyId: ' + keyId);
// disable key repeat
if ( this.repeat[keyId] ) {
return;
}
this.repeat[keyId] = true;
var loc = this.keyMap[keyId];
if(loc) {
loc = [loc[0], loc[1] + this.octave];
event.preventDefault();
var hz = this.baseFreq * Math.pow(2.0,(loc[1]*this.generator[1] + loc[0]*this.generator[0])/1200.0);
var noteId = 'k' + loc[0] + '_' + loc[1];
this.notePlayListener({
keyId:noteId,
loc:loc,
hz:hz,
volume:1.0
});
}
else
{
switch(event.keyCode){
case 61: // +
this.octave++;
return;
case 173: // -
this.octave--;
return;
case 107: // keypad +
this.octave++;
return;
case 109: // keypad -
this.octave--;
return;
case 32: // ' '
event.preventDefault();
//this.setSustain(true);
this.setSustain('switch');
return;
}
}
};
Keyboard.prototype.keyUp = function(event) {
var keyId = String.fromCharCode(event.keyCode);
this.repeat[keyId] = false; // to disable key repeat
//if (keyId == ' ') {
// this.setSustain(false);
// return;
//}
loc = this.keyMap[keyId];
if(loc) {
var noteId = 'k'+loc[0] +'_' + (loc[1]+this.octave);
console.log(noteId);
if (!this.sustain) {
this.noteStopListener({keyId:noteId});
} else {
if (this.sustainedNotes.indexOf(noteId) < 0) {
this.sustainedNotes.push(noteId);
}
}
}
};