-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbeep.html
104 lines (88 loc) · 2.94 KB
/
beep.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Beep boop</title>
<style>
body {
background: #fee;
}
#wrapper {
border-radius: 5px;
padding: 0.5em;
background: #f0f0f0;
}
iframe {
border-radius: 5px;
width: 100%;
height: 60vh;
}
nav {
padding: 1em;
display: flex;
gap: 1em;
flex-wrap: wrap;
flex-basis: 15em;
}
nav>button {
background-color: #f0f0f0;
border: 1px solid;
border-radius: 5px;
flex-grow: 1;
}
nav>button.clicked {
background: linear-gradient(to bottom right, hotpink, lightblue);
border-style: double;
border-color: hotpink;
}
</style>
</head>
<body>
<h1>SET YOUR VOLUME VERY VERY LOW BEFORE CLICKING "ON"</h1>
<button id="on">GO</button>
<button id="off">STOP</button>
<button id="wave">NEW WAVE PLS</button>
<script>
let wave = []
function makeWave() {
wave = []
let tick = 0.6 + Math.random() * 4
// Array of frequencies or "wave"; integers from 0 to 666 and back
for (let wavepoint = 000; wavepoint < 666; wavepoint += tick) wave.push(wavepoint)
for (let wavepoint = 666; wavepoint > 000; wavepoint -= tick) wave.push(wavepoint)
}
document.querySelector("#wave").addEventListener("click", makeWave)
makeWave()
const audioCtx = new AudioContext() // Our speakers
const oscillator = audioCtx.createOscillator() // Controls frequency
const gainNode = audioCtx.createGain() // Controls volume
gainNode.connect(audioCtx.destination) // connect gain node to speakers
oscillator.connect(gainNode) // connect oscillator to gain
// ☠🕱☠ DANGER! ☠🕱☠
// BE CAREFUL WITH VOLUME!
gainNode.gain.value = 0.02 // 💀
// SERIOUSLY!
let interval
document.querySelector("#on").addEventListener("click", () => {
oscillator.start()
let index = 99999999999999
// The oscillator plays a constant frequency for X milliseconds
// The playNextFrequency controls the frequencey based on "wave" array
// So the speed at which we go through the wave is the delay of setInterval
function playNextFrequency() {
// index = which frequency of the wave is playing
index = index >= wave.length - 1 ? 0 : index + 1
console.log("Playing frequency " + wave[index])
oscillator.frequency.value = wave[index]
}
interval = setInterval(playNextFrequency, 1)
})
document.querySelector("#off").addEventListener("click", () => {
gainNode.gain.value = 0
clearInterval(interval)
})
</script>
</body>
</html>