-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathindex.html
60 lines (51 loc) · 1.85 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Web Audio API examples: MediaElementAudioSource()</title>
</head>
<body>
<h1>Web Audio API examples: MediaElementAudioSource()</h1>
<p>Position the cursor vertically to change the volume (down is louder).</p>
<audio controls>
<source src="viper.ogg" type="audio/ogg" />
<source src="viper.mp3" type="audio/mp3" />
<p>This demo needs a browser supporting the <audio> element.</p>
</audio>
</body>
<script>
let audioCtx;
const audioElement = document.querySelector("audio");
audioElement.addEventListener("play", () => {
audioCtx = new AudioContext();
// Create a MediaElementAudioSourceNode
// Feed the HTMLMediaElement into it
let source = new MediaElementAudioSourceNode(audioCtx, {
mediaElement: audioElement,
});
// Create a gain node
let gainNode = new GainNode(audioCtx);
// Create variables to store mouse pointer Y coordinate
// and HEIGHT of screen
let currentY;
let height = window.innerHeight;
// Get new mouse pointer coordinates when mouse is moved
// then set new gain value
document.onmousemove = (e) => {
currentY = window.Event
? e.pageY
: event.clientY +
(document.documentElement.scrollTop
? document.documentElement.scrollTop
: document.body.scrollTop);
gainNode.gain.value = currentY / height;
};
// connect the AudioBufferSourceNode to the gainNode
// and the gainNode to the destination, so we can play the
// music and adjust the volume using the mouse cursor
source.connect(gainNode);
gainNode.connect(audioCtx.destination);
});
</script>
</html>