Skip to content

Commit

Permalink
improve loop caching
Browse files Browse the repository at this point in the history
  • Loading branch information
deleolajide committed Jan 6, 2025
1 parent 9afb82c commit f1da799
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
24 changes: 16 additions & 8 deletions docs/js/audio_looper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions docs/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -270,6 +271,7 @@ var idbKeyval = (function (exports) {

}({}));

window.loopCache = {};
window.requestAnimFrame = window.requestAnimationFrame;
window.addEventListener("load", onloadHandler);
window.addEventListener("beforeunload", () => {if (!registration) saveConfig(); });
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit f1da799

Please sign in to comment.