Skip to content

Commit

Permalink
Merge pull request #29 from JvPeek/develop
Browse files Browse the repository at this point in the history
Crash Protection + rtttl bpm clamp
  • Loading branch information
awsdcrafting authored Feb 18, 2024
2 parents 8843b25 + d706412 commit e6ccd3a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
59 changes: 49 additions & 10 deletions src/bongocat.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,18 @@ function clamp(value, min, max)
//return Math.min(Math.max(value, min), max)
}

function clampBpm(bpm) {
return clamp(bpm, minBpm, maxBpm)
}

function setBPM(targetBPM, username)
{
targetBPM = Number(targetBPM);
if (isNaN(targetBPM))
{
return;
}
if (targetBPM < minBpm)
{
targetBPM = minBpm;
}
if (targetBPM > maxBpm)
{
targetBPM = maxBpm;
}
targetBPM = clampBpm(targetBPM)
if (username === undefined)
{
console.log("<Global> current BPM: " + bpm.global + ". Target: " + Math.floor(60000 / targetBPM));
Expand Down Expand Up @@ -238,7 +235,12 @@ function introAnimation(song)
function outroAnimation()
{
document.getElementById("bongocat").style.left = "-1920px";
document.getElementById("dedications").style.visibility = "hidden";
setInstrument("none");
for (let id of currentSong.timeoutIDs)
{
clearTimeout(id);
}
if (mainGainNode)
{
mainGainNode.gain.value = 0;
Expand All @@ -255,6 +257,28 @@ function outroAnimation()
}, 1000);
}

function errorAnimation(error)
{
document.getElementById("nametag").innerHTML = document.getElementById("nametag").innerHTML.split(" ")[0] + " crashed the cat :(";
document.getElementById("dedications").style.visibility = "visible";
document.getElementById("dedications").innerHTML = "Tag scisneromam to fix: " + error;
setInstrument("none");
for (let id of currentSong.timeoutIDs)
{
clearTimeout(id);
}
if (mainGainNode)
{
mainGainNode.gain.value = 0;
}
if (oscillatorNode && synthStarted)
{
oscillatorNode.stop();
synthStarted = false;
}
setTimeout(outroAnimation, 5000);
}

function setInstrument(instrument)
{
var c = document.getElementById("instruments").children;
Expand Down Expand Up @@ -282,12 +306,27 @@ function releasePaw(paw)
currentPaw.style.backgroundPosition = "top left";
}

function saveCmd(cmd)
{
return (...args) =>
{
try
{
cmd(...args);
} catch (error)
{
errorAnimation(error);
console.error(error);
}
};
}

function preparePlaybackObject(cmd, time, ...args)
{
return {time: time, cmd: cmd, args: args};
return {time: time, cmd: saveCmd(cmd), args: args};
}

var helperMethods = {clamp, setBPM, getBPM, playSound, prepareSynth, playSynthSound, muteSynth, introAnimation, outroAnimation, setInstrument, setPaw, releasePaw, preparePlaybackObject};
var helperMethods = {clamp, clampBpm, setBPM, getBPM, playSound, prepareSynth, playSynthSound, muteSynth, introAnimation, outroAnimation, setInstrument, setPaw, releasePaw, preparePlaybackObject};
for (const key in helperMethods)
{
window[key] = helperMethods[key];
Expand Down
2 changes: 1 addition & 1 deletion src/experimental/bongox.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function rtttl(song)
switch (key)
{
case "b":
bpm = numberValue;
bpm = clampBpm(numberValue);
break;
case "o":
octave = numberValue;
Expand Down

0 comments on commit e6ccd3a

Please sign in to comment.