-
Notifications
You must be signed in to change notification settings - Fork 432
/
Copy pathindex.html
61 lines (54 loc) · 1.6 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
61
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Web Audio API examples: MediaStreamAudioDestination()</title>
</head>
<body>
<h1>Web Audio API examples: MediaStreamAudioDestination()</h1>
<p>Encoding a pure sine wave to an Opus file</p>
<p><button>Make sine wave</button></p>
<audio controls></audio>
<script>
const b = document.querySelector("button");
let clicked = false;
let chunks = [];
let ac;
let osc;
let dest;
let mediaRecorder;
function init() {
ac = new AudioContext();
osc = new OscillatorNode(ac);
dest = new MediaStreamAudioDestinationNode(ac);
mediaRecorder = new MediaRecorder(dest.stream);
osc.connect(dest);
mediaRecorder.ondataavailable = (evt) => {
// push each chunk (blobs) in an array
chunks.push(evt.data);
};
mediaRecorder.onstop = (evt) => {
// Make blob out of our blobs, and open it.
const blob = new Blob(chunks, { type: "audio/ogg; codecs=opus" });
document.querySelector("audio").src = URL.createObjectURL(blob);
};
}
b.addEventListener("click", (e) => {
if (!ac) {
init();
}
if (!clicked) {
mediaRecorder.start();
osc.start(0);
e.target.textContent = "Stop recording";
clicked = true;
} else {
mediaRecorder.requestData();
mediaRecorder.stop();
osc.stop(0);
e.target.disabled = true;
}
});
</script>
</body>
</html>