-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynth.js
More file actions
67 lines (54 loc) · 1.46 KB
/
synth.js
File metadata and controls
67 lines (54 loc) · 1.46 KB
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
let synth;
let dist;
let pingPong;
// Workaround for the audio context warning
const start = () => {
window.removeEventListener('click', start);
//create a synth and connect it to the main output (your speakers)
// const chorus = new Tone.Chorus(40, 2.5, 1000);
dist = new Tone.Distortion(2)
pingPong = new Tone.PingPongDelay("4n", 0.2).connect(dist);
synth = new Tone.Synth().connect(pingPong);
dist.toDestination();
// Register the buttons to play the notes
document.querySelectorAll('[data-key]').forEach(k => {
const key = k.getAttribute('data-key');
k.addEventListener('click', () => playNote(key));
});
document.getElementById('waveform').addEventListener('change', e => {
synth.oscillator.type = e.target.value;
});
document.getElementById('distortion').addEventListener('change', e => {
dist.distortion = e.target.value;
});
document.getElementById('pingPong').addEventListener('change', e => {
pingPong.delayTime.setValueAtTime(e.target.value);
});
};
window.addEventListener('click', start);
const playNote = (key) => {
synth.triggerAttackRelease(key, '8n');
}
const keyMap = {
"a" :"c4",
"w" :"c#4",
"s" :"d4",
"e" :"d#4",
"d" :"e4",
"f" :"f4",
"t" :"f#4",
"g" :"g4",
"y" :"g#4",
"h" :"a4",
"u" :"a#4",
"j" :"b4",
"k" :"c5",
"o" :"c#5",
"l" :"d5",
"p" :"d#5",
";" :"e5",
"'" :"f5",
}
document.addEventListener('keydown', (e) => {
if (keyMap[e.key]) playNote(keyMap[e.key]);
});