Skip to content

Instantly share code, notes, and snippets.

@kylemacfarlane
Last active July 27, 2018 20:43
Show Gist options
  • Save kylemacfarlane/290208f70ff4b67599289e9ab101d23c to your computer and use it in GitHub Desktop.
Save kylemacfarlane/290208f70ff4b67599289e9ab101d23c to your computer and use it in GitHub Desktop.
Workaround to get a low latency audio context by accessing the microphone. https://bugs.chromium.org/p/chromium/issues/detail?id=635686
var audio_context = new window.AudioContext(),
mic_stream;
var fix_webaudio = function(max_attempts, interval, a) {
var attempt = a || 0;
if (audio_context.baseLatency < 0.2) {
if (attempt) {
if (mic_stream) {
mic_stream.getTracks()[0].stop();
mic_stream = null;
}
window.alert(
'Fixed WebAudio after ' + attempt + ' attempts, you ' +
'may need to adjust your volume.'
);
}
return;
}
if (!attempt) {
if (!window.confirm(
'It looks like you\'re using Bluetooth headphones which ' +
'has broken WebAudio. Attempt to fix by turning the ' +
'Bluetooth microphone on and off?'
)) {
return;
}
navigator.getUserMedia({audio: true}, function(s) {
mic_stream = s;
}, function() {});
}
if (attempt < max_attempts) {
audio_context.close();
audio_context = new window.AudioContext();
setTimeout(function() {
fix_webaudio(max_attempts, interval, attempt + 1);
}, interval);
} else {
window.alert(
'Failed to fix WebAudio after ' + attempt + ' attempts.'
);
}
};
fix_webaudio(20, 500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment