-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.js
38 lines (31 loc) · 1.2 KB
/
sound.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 Sound {
constructor() {
this.audioContext = new AudioContext();
this.createOscillators();
this.isPlaying = false;
}
createOscillators() {
this.sineOscillator = this.audioContext.createOscillator();
this.sineOscillator.type = 'sine';
this.sineOscillator.frequency.value = 440;
this.sawOscillator = this.audioContext.createOscillator();
this.sawOscillator.type = 'sawtooth';
this.sawOscillator.frequency.value = 440;
this.gain = this.audioContext.createGain();
this.sineOscillator.connect(this.gain);
this.sawOscillator.connect(this.gain);
this.gain.connect(this.audioContext.destination);
}
playNote(frequency, duration) {
if (!this.isPlaying) {
this.sineOscillator.start();
this.sawOscillator.start();
this.isPlaying = true;
}
this.sineOscillator.frequency.value = frequency;
this.sawOscillator.frequency.value = frequency;
this.gain.gain.setValueAtTime(0.2, this.audioContext.currentTime);
this.gain.gain.linearRampToValueAtTime(0, this.audioContext.currentTime + duration);
}
}
export default Sound;