alexa-voice-service
Version:
Alexa Voice Service wrapper for the browser.
24 lines (17 loc) • 661 B
JavaScript
;
function typedArrayToAudioBuffer(typedArray, context) {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
return new Promise((resolve, reject) => {
if (context) {
if (Object.prototype.toString.call(context) !== '[object AudioContext]') {
throw new TypeError('`context` must be an AudioContext');
}
} else {
context = new AudioContext();
}
const arrayBuffer = new ArrayBuffer(typedArray.byteLength);
new Uint8Array(arrayBuffer).set(new Uint8Array(typedArray));
context.decodeAudioData(arrayBuffer, resolve, reject);
});
}
module.exports = typedArrayToAudioBuffer;