Fix loading audio files.

This commit is contained in:
Will Scullin 2019-09-05 21:42:40 -07:00
parent 6858c298cf
commit d189fe98d5
No known key found for this signature in database
GPG Key ID: 9092A5C0A673416B

View File

@ -74,17 +74,22 @@ aci.setData(window.tapes['Microchess'].tracks);
// Audio Buffer Source // Audio Buffer Source
var context; var context;
if (typeof window.webkitAudioContext != 'undefined') { if (typeof window.webkitAudioContext !== 'undefined') {
context = new window.webkitAudioContext(); context = window.webkitAudioContext;
} else if (typeof window.AudioContext !== 'undefined') {
context = new window.AudioContext();
} }
export function doLoadLocal() { export function doLoadLocal() {
context.resume();
var files = document.querySelector('#local_file').files; var files = document.querySelector('#local_file').files;
if (files.length == 1) { if (files.length == 1) {
var file = files[0]; var file = files[0];
var fileReader = new FileReader(); var fileReader = new FileReader();
fileReader.onload = function(ev) { fileReader.onload = function(ev) {
context.decodeAudioData(ev.target.result, function(buffer) { context.decodeAudioData(
ev.target.result,
function(buffer) {
var buf = []; var buf = [];
var data = buffer.getChannelData(0); var data = buffer.getChannelData(0);
var old = (data[0] > 0.25); var old = (data[0] > 0.25);
@ -100,7 +105,11 @@ export function doLoadLocal() {
} }
aci.buffer = buf; aci.buffer = buf;
MicroModal.close('local-modal'); MicroModal.close('local-modal');
}); },
function(error) {
window.alert(error.message);
}
);
}; };
fileReader.readAsArrayBuffer(file); fileReader.readAsArrayBuffer(file);
} }