mirror of
https://github.com/whscullin/apple1js.git
synced 2024-11-26 17:49:44 +00:00
Fix loading audio files.
This commit is contained in:
parent
6858c298cf
commit
d189fe98d5
43
js/apple1.js
43
js/apple1.js
@ -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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user