-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbooper.html
101 lines (86 loc) · 3.04 KB
/
booper.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
<!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>
* {
margin: 0;
padding: 0;
}
button {
padding: 1em;
width: 20em;
}
[type="range"] { width: 80vw; }
div, body { padding: 1em; }
</style>
</head>
<body>
<h1>SET YOUR VOLUME VERY VERY LOW BEFORE CLICKING "GO"</h1>
<div>Freq <input type="range" id="freq" min="1" max="18000" value="200"></div>
<div>Gain <input type="range" id="gain" min="1" max="100" value="1"></div>
<div id="radios">
<input type="radio" name="type" data-type="sine" checked>sine
<input type="radio" name="type" data-type="square">square
<input type="radio" name="type" data-type="sawtooth">sawtooth
<input type="radio" name="type" data-type="triangle">triangle
<input type="radio" name="type" data-type="custom">custom
</div>
<button id="on">GO</button>
<script>
class VoiceEmitter {
constructor(){
this.started = false
this.play = false
this.gain = 0
this.audioCtx = new AudioContext()
this.oscillator = this.audioCtx.createOscillator()
this.gainNode = this.audioCtx.createGain()
this.gainNode.connect(this.audioCtx.destination)
this.oscillator.connect(this.gainNode)
this.oscillator.frequency.value = 200 // default freq
this.oscillator.type = "sine" // default wave type
this.gainNode.value = 0 // default vol
}
setFreq(x) { this.oscillator.frequency.value = x }
setType(x) { this.oscillator.type = x }
setGain(x) {
this.gain = x/100;
console.log(x, this.gain)
if (this.play) {
this.gainNode.gain.value = this.gain
}
}
togglePlay() {
console.log("Toggleplay")
if (!this.started) {
console.log("Initial")
this.oscillator.start(0)
this.started = true
}
this.play = !this.play
if (this.play) {
console.log("playing")
this.gainNode.gain.value = this.gain
} else {
console.log("not playing")
this.gainNode.gain.value = 0
}
}
}
const out = new VoiceEmitter()
window.out = out
const freqE = document.getElementById("freq")
const gainE = document.getElementById("gain")
const toggleE = document.getElementById("on")
const radios = document.getElementById("radios")
freqE.addEventListener("change", e => out.setFreq(Number(e.target.value)))
gainE.addEventListener("change", e => out.setGain(Number(e.target.value)))
toggleE.addEventListener("click", e => out.togglePlay())
radios.addEventListener("change", e => out.setType(e.target.dataset.type))
</script>
</body>
</html>