In the following simple example, we create a MediaStreamAudioDestinationNode
, an OscillatorNode
and a MediaRecorder
(the example will therefore only work in Firefox and Chrome at this time.) The MediaRecorder
is set up to record information from the MediaStreamDestinationNode
.
When the button is clicked, the oscillator starts, and the MediaRecorder
is started. When the button is stopped, the oscillator and MediaRecorder
both stop. Stopping the MediaRecorder
causes the dataavailable
event to fire, and the event data is pushed into the chunks
array. After that, the stop
event fires, a new blob
is made of type opus â which contains the data in the chunks
array, and a new window (tab) is then opened that points to a URL created from the blob.
From here, you can play and save the opus file.
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>createMediaStreamDestination() demo</title>
</head>
<body>
<h1>createMediaStreamDestination() demo</h1>
<p>Encoding a pure sine wave to an Opus file</p>
<button>Make sine wave</button>
<audio controls></audio>
<script>
const b = document.querySelector("button");
let clicked = false;
const chunks = [];
const ac = new AudioContext();
const osc = ac.createOscillator();
const dest = ac.createMediaStreamDestination();
const mediaRecorder = new MediaRecorder(dest.stream);
osc.connect(dest);
b.addEventListener("click", (e) => {
if (!clicked) {
mediaRecorder.start();
osc.start(0);
e.target.textContent = "Stop recording";
clicked = true;
} else {
mediaRecorder.stop();
osc.stop(0);
e.target.disabled = true;
}
});
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);
};
</script>
</body>
</html>
Note: You can view this example live, or study the source code, on GitHub.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3