-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
66 lines (56 loc) · 1.28 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Beep Boop | Psychic Waddle</title>
<script>
window.onload = function(){
// one context per document
const ctx = new (window.AudioContext || window.webkitAudioContext)();
loop(ctx, 1, 1000);
loop(ctx, .5, 500);
}
function tone(ctx,type,length){
let osc = ctx.createOscillator();
let vol = ctx.createGain();
osc.connect(vol);
vol.connect(ctx.destination);
vol.gain.value = 1;
osc.frequency.value = frequency();
osc.type = type;
osc.start();
osc.stop(ctx.currentTime + length);
}
function frequency(){
// fn = f0 * (a)n
// f0 = 440 A above middle C
// a = (2)^1/12 a constant
const f0 = 440;
const a = Math.pow(2,(1/12));
let n = getNote('major');
let fn = f0 * Math.pow(a,n)
return fn;
}
function getNote(key) {
const major = [0, 2, 4, 5, 7, 9, 11, 12];
const minor = [0, 2, 3, 5, 7, 8, 10, 12];
let min = 0;
let max = 7;
let index = Math.floor(Math.random() * (max - min + 1)) + min;
if(key === 'major'){
return (major[index] - 12);
}
if(key === 'minor'){
return (minor[index] - 12);
}
}
function loop(ctx, noteLength,wait){
setTimeout(function(){
tone(ctx,'sine', noteLength);
loop(ctx, noteLength, wait);
}, wait)
}
</script>
</head>
<body>
</body>
</html>