-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (52 loc) · 1.54 KB
/
script.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
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
const audioElement = document.querySelector("#input-audio");
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioContext();
const track = audioContext.createMediaElementSource(audioElement);
// track.connect(audioContext.destination);
const playButton = document.querySelector("button");
playButton.addEventListener(
"click",
function () {
// check if context is in suspended state (autoplay policy)
if (audioContext.state === "suspended") {
audioContext.resume();
}
// play or pause track depending on state
if (this.dataset.playing === "false") {
audioElement.play();
this.dataset.playing = "true";
} else if (this.dataset.playing === "true") {
audioElement.pause();
this.dataset.playing = "false";
}
},
false
);
audioElement.addEventListener(
"ended",
() => {
playButton.dataset.playing = "false";
},
false
);
const gainNode = audioContext.createGain();
// track.connect(gainNode).connect(audioContext.destination);
const volumeControl = document.querySelector("#volume");
volumeControl.addEventListener(
"input",
function () {
gainNode.gain.value = this.value;
},
false
);
const pannerOptions = { pan: 0 };
const panner = new StereoPannerNode(audioContext, pannerOptions);
const pannerControl = document.querySelector("#panner");
pannerControl.addEventListener(
"input",
function () {
panner.pan.value = this.value;
},
false
);
track.connect(gainNode).connect(panner).connect(audioContext.destination);