diff --git a/docs/js/audio_looper.js b/docs/js/audio_looper.js index 38c074c..484b140 100644 --- a/docs/js/audio_looper.js +++ b/docs/js/audio_looper.js @@ -248,14 +248,22 @@ AudioLooper.prototype.addUri = function(loop, output, bpm) { if (loop.url.startsWith("assets")) { - fetch(loop.url) - .then(response => response.arrayBuffer()) - .then(buffer => this.audioContext.decodeAudioData(buffer)) - .then(sample => { - this.sample = sample; - console.debug("addUri", loop, sample); - if (this.cb_loaded) this.cb_loaded(); - }); + this.sample = window.loopCache[loop.url]; + + if (this.sample == undefined) { + fetch(loop.url, {cache: "force-cache"}) + .then(response => response.arrayBuffer()) + .then(buffer => this.audioContext.decodeAudioData(buffer)) + .then(sample => { + this.sample = sample; + window.loopCache[loop.url] = sample; + console.debug("addUri fetched", loop.url, sample); + if (this.cb_loaded) this.cb_loaded(false); + }); + } else { + console.debug("addUri cached", loop.url, this.sample); + if (this.cb_loaded) this.cb_loaded(true); + } } else { const dbName = loop.url; const store = new idbKeyval.Store(dbName, dbName); diff --git a/docs/js/main.js b/docs/js/main.js index ef58d81..d77c4fe 100644 --- a/docs/js/main.js +++ b/docs/js/main.js @@ -25,6 +25,7 @@ const WHAMMY = 2; const LOGO = 12; const CONTROL = 100; +var loopWait = 0; var activeStreams = null; var _converse = null; var recorderDestination = null; @@ -270,6 +271,7 @@ var idbKeyval = (function (exports) { }({})); +window.loopCache = {}; window.requestAnimFrame = window.requestAnimationFrame; window.addEventListener("load", onloadHandler); window.addEventListener("beforeunload", () => {if (!registration) saveConfig(); }); @@ -7854,27 +7856,24 @@ function setupRealInstruments() { bassLoop = null; chordLoop = null; - let wait = 2000; + loopWait = 2000; if (realInstrument.drums) { drumLoop = new AudioLooper("drum"); drumLoop.callback(soundsLoaded, eventStatus); drumLoop.addUri(realInstrument.drums, realdrumDevice, realInstrument.bpm); - wait+=1000; } if (realInstrument.basses) { bassLoop = new AudioLooper("bass"); bassLoop.callback(soundsLoaded, eventStatus); bassLoop.addUri(realInstrument.basses, realdrumDevice, realInstrument.bpm); - wait+=1000; } if (realInstrument.chords) { chordLoop = new AudioLooper("chord"); chordLoop.callback(soundsLoaded, eventStatus); - chordLoop.addUri(realInstrument.chords, realdrumDevice, realInstrument.bpm); - wait+=1000; + chordLoop.addUri(realInstrument.chords, realdrumDevice, realInstrument.bpm); } if (!registration && realInstrument.bpm) setTempo(realInstrument.bpm); @@ -7887,11 +7886,12 @@ function setupRealInstruments() { const slot = registration > 8 ? registration + 1 : (registration == 0 ? 0 : registration); setXTouchButton(slot, "flash"); } - }, wait); + }, loopWait); } -function soundsLoaded() { +function soundsLoaded(cached) { console.debug("audio loaded ok"); + if (!cached) loopWait+=1000; } function eventStatus(event, id) {