-
Notifications
You must be signed in to change notification settings - Fork 0
/
penctone.js
105 lines (86 loc) · 2.24 KB
/
penctone.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
const CONF = {
octavePos: 4,
transpose: 0,
};
const key2relnote = e => {
if (e.code === 'IntlYen') return;
if (e.isComposing === true) return;
if (e.ctrlKey === true) return;
if (e.altKey === true) return;
if (e.shiftKey === true) return;
if (e.repeat === true) return;
const OCT_0_KEYS = 'zsxdcvgbhnjm,l.;/\\';
const n0 = OCT_0_KEYS.indexOf(e.key);
if (n0 !== -1) return n0;
const OCT_1_KEYS = 'q2w3er5t6y7ui9o0p@^[';
const n1 = OCT_1_KEYS.indexOf(e.key);
if (n1 !== -1) return n1 + 12;
return;
};
const isPedal = e => {
if (e.isComposing === true) return false;
if (e.ctrlKey === true) return false;
if (e.altKey === true) return false;
if (e.shiftKey === true) return false;
if (e.repeat === true) return false;
if (e.key !== ' ') return false;
return true;
};
const keyEvent = (e, state) => {
if (state) {
if (e.key == "ArrowUp") {
CONF.transpose ++;
e.preventDefault();
return;
} else if (e.key == "ArrowDown") {
CONF.transpose --;
e.preventDefault();
return;
} else if (e.key == "ArrowLeft") {
CONF.octavePos --;
e.preventDefault();
return;
} else if (e.key == "ArrowRight") {
CONF.octavePos ++;
e.preventDefault();
return;
}
}
if (isPedal(e)) {
synth.send({ type: 'Pedal', state: state });
logger.send({ type: 'Pedal', state: state });
e.preventDefault();
return;
}
const relnote = key2relnote(e);
if (relnote !== undefined) {
const note = relnote + CONF.octavePos * 12 + CONF.transpose;
synth.send({ type: state ? 'NoteOn' : 'NoteOff' , note: note });
logger.send({ type: state ? 'NoteOn' : 'NoteOff' , note: note });
e.preventDefault();
return;
}
};
class Logger {
constructor(target) {
this.target = target;
}
send(m) {
const e = document.createElement('option');
switch (m.type) {
case 'NoteOn':
case 'NoteOff':
e.innerHTML = `${m.type} ${m.note}`;
break;
case 'Pedal':
e.innerHTML = `${m.type} ${m.state}`;
break;
}
this.target.appendChild(e);
this.target.scrollTo(0, this.target.scrollHeight);
}
}
document.addEventListener('keydown', e => { keyEvent(e, true); });
document.addEventListener('keyup', e => { keyEvent(e, false); });
const synth = new Synthesizer();
const logger = new Logger(document.getElementById('event_log'));