-
Notifications
You must be signed in to change notification settings - Fork 0
/
Speaker.js
38 lines (31 loc) · 943 Bytes
/
Speaker.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
(() => {
class Speaker {
constructor() {
const context = new window.AudioContext()
const gain = context.createGain()
gain.connect(context.destination)
this.context = context
this.gain = gain
this.oscillator = null
}
play(frequency) {
if (this.oscillator) {
return
}
this.oscillator = this.context.createOscillator()
this.oscillator.frequency.value = 440
this.oscillator.type = this.oscillator.TRIANGLE
this.oscillator.connect(this.gain)
this.oscillator.start(0)
}
stop() {
if (!this.oscillator) {
return
}
this.oscillator.stop(0)
this.oscillator.disconnect(0)
this.oscillator = null
}
}
window.chip8.Speaker = Speaker
})()