-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
55 lines (52 loc) · 1.38 KB
/
script.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
//let ctx = new AudioContext();
let ctx;
let even = document.getElementById('chords');
function playNote(pitch, time) {
return new Promise((resolve, reject) => {
let oscillator = ctx.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.value = 256 * 2 ** (pitch / 12);
oscillator.connect(ctx.destination);
oscillator.start();
oscillator.stop(ctx.currentTime + time);
oscillator.onended = resolve;
})
}
async function playChord(name, time = 0.25) {
even.innerHTML += ' ' + name;
let cmap = {
'C': [0, 4, 7, 12],
'Dm': [2, 5, 9, 14],
'G': [-1, 2, 7, 11],
'F': [0, 5, 9, 12],
'Am': [0, 4, 9, 12],
'Em': [-1, 4, 7, 11]
}
for (let note of cmap[name]) await playNote(note, time);
}
async function start() {
ctx = new AudioContext();
let current = 'C';
let next = {
'C': ['F', 'G', 'Am'],
'F': ['C'],
'G': ['Am', 'C'],
'Am': ['F', 'G', 'Em', 'Dm'],
'Dm': ['G', 'Em', 'Am'],
'Em': ['Am']
}
while (true) {
await playChord(current);
let opts = next[current];
current = opts[Math.floor(Math.random() * opts.length)];
}
}
document.querySelectorAll('.label').forEach(e => {
let text = "";
for (let i = 0; i < 3; i++) {
let start = 0x4e00, end = 0x5500;
let rand = Math.floor(Math.random() * (end - start + 1)) + start;
text += String.fromCharCode(rand);
}
e.innerHTML = text;
})