Skip to content

Instantly share code, notes, and snippets.

@porst17
Created October 12, 2018 13:25
Show Gist options
  • Save porst17/f309bf8652c45fb67819d16755a3d116 to your computer and use it in GitHub Desktop.
Save porst17/f309bf8652c45fb67819d16755a3d116 to your computer and use it in GitHub Desktop.
WebAudio tests
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>WebAudio Latency Checker</title>
<meta name="description" content="Connects user input directly with WebAudio output in order to check for the minimal achievable latency">
<meta name="author" content="Christian Stussak">
</head>
<body>
Please use headphones for this test. Otherwise, it might cause a feedback loop!
<br />
<input type=button onClick=start() value="Start" />
<input type=button onClick=stop() value="Stop" />
<br />
<tt>AudioContext.baseLatency=<span id="baseLatency"></span></tt><br />
<tt>AudioContext.outputLatency=<span id="outputLatency"></span></tt>
<script>
const AudioContext = window.AudioContext || window.webkitAudioContext;
let audioCtx = undefined;
let stream = undefined;
let source = undefined;
function start() {
stop();
audioCtx = new AudioContext();
document.getElementById("baseLatency").innerHTML = audioCtx.baseLatency;
document.getElementById("outputLatency").innerHTML = audioCtx.outputLatency;
navigator.mediaDevices.getUserMedia({
audio: {
latency: 0.001
},
video: false
})
.then(s => {
stream = s;
source = audioCtx.createMediaStreamSource(stream);
source.connect(audioCtx.destination);
stream.getAudioTracks().forEach(t => console.log(t.getSettings()));
});
}
function stop() {
if (audioCtx != undefined) {
source.disconnect();
stream.getAudioTracks().forEach(t => t.stop());
audioCtx.close();
audioCtx = undefined;
stream = undefined;
source = undefined;
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment