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,33 +74,42 @@ 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(
var buf = []; ev.target.result,
var data = buffer.getChannelData(0); function(buffer) {
var old = (data[0] > 0.25); var buf = [];
var last = 0; var data = buffer.getChannelData(0);
for (var idx = 1; idx < data.length; idx++) { var old = (data[0] > 0.25);
var current = (data[idx] > 0.25); var last = 0;
if (current != old) { for (var idx = 1; idx < data.length; idx++) {
var delta = idx - last; var current = (data[idx] > 0.25);
buf.push(parseInt(delta / buffer.sampleRate * 1023000)); if (current != old) {
old = current; var delta = idx - last;
last = idx; buf.push(parseInt(delta / buffer.sampleRate * 1023000));
old = current;
last = idx;
}
} }
aci.buffer = buf;
MicroModal.close('local-modal');
},
function(error) {
window.alert(error.message);
} }
aci.buffer = buf; );
MicroModal.close('local-modal');
});
}; };
fileReader.readAsArrayBuffer(file); fileReader.readAsArrayBuffer(file);
} }