Skip to content

Commit

Permalink
Tune-Shark-V3 -- Garbo Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkPool-SP authored Jan 22, 2025
1 parent 4018990 commit f688c08
Showing 1 changed file with 20 additions and 47 deletions.
67 changes: 20 additions & 47 deletions extensions/SharkPool/Tune-Shark-V3.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
extraIcons[key];

// Modified Pizzicato Library (Web Audio API, but with Premade Effects and Stuff)
// Modified Version: https://github.com/SharkPool-SP/pizzicato/
// Original: https://github.com/alemangui/pizzicato
/*
The MIT License (MIT)
Expand Down Expand Up @@ -87,7 +89,7 @@

let deltaTime = 0,
prevFrameTime = 0;
let soundBank = {};
let soundBank = Object.create(null);
let settings = { flagCtrl: false, canSave: false };

class SPtuneShark3 {
Expand Down Expand Up @@ -196,7 +198,7 @@
sound.stop();
bank.currentTime = lastTime;
sound.play(0, lastTime);
this.patchLinks(sound.sourceNode, bank);
this.fixAudioNodes(sound.sourceNode, bank);
}
}
}
Expand Down Expand Up @@ -245,24 +247,6 @@
},
},
},
{
opcode: "convertSound",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate(
"convert sound [NAME1] from URL to URI and save to [NAME2]"
),
blockIconURI: extraIcons.set,
arguments: {
NAME1: {
type: Scratch.ArgumentType.STRING,
defaultValue: Scratch.translate("MySound"),
},
NAME2: {
type: Scratch.ArgumentType.STRING,
defaultValue: Scratch.translate("MySound2"),
},
},
},
{
opcode: "bindSound",
blockType: Scratch.BlockType.COMMAND,
Expand Down Expand Up @@ -824,6 +808,10 @@
}

// Helper Funcs
getSound(name) {
return soundBank[name]
}

startHats(data) {
let newThreads = [];
runtime.allScriptsByOpcodeDo(
Expand Down Expand Up @@ -884,7 +872,7 @@
return isNaN(value) ? 0 : value;
}

patchLinks(src, sound) {
fixAudioNodes(src, sound) {
src.playbackRate.value = sound.pitch;
src.detune.value = sound.detune;
src.gainSuccessor.gain.value = sound.gain;
Expand Down Expand Up @@ -920,7 +908,8 @@
return;
}
case "DISTORTION": {
return (thisEffect.gain = options.gain);
thisEffect.gain = options.gain;
return;
}
case "BITCRUSH": {
thisEffect.frequency = Math.max(30000, Cast.toNumber(args.FREQ));
Expand Down Expand Up @@ -995,14 +984,14 @@
if (!sound.playing) con.currentTime = atTime;
sound.play(0, atTime);
const srcNode = sound.sourceNode;
this.patchLinks(srcNode, con);
this.fixAudioNodes(srcNode, con);
if (Object.keys(con.binds).length > 0) {
Object.keys(con.binds).forEach((key) => {
const thisSound = con.binds[key];
const context = thisSound.context;
if (!context.playing) thisSound.currentTime = atTime;
context.play(0, atTime);
this.patchLinks(context.sourceNode, thisSound);
this.fixAudioNodes(context.sourceNode, thisSound);
});
}
if (sound.loop)
Expand All @@ -1015,7 +1004,7 @@
this.startHats({ name: con.name, type: "starts" });
} catch {
console.warn(
Scratch.translate("Audio has not loaded yet, ignore next error.")
Scratch.translate("Audio has not loaded yet!")
);
sound.stop(); // Reset
}
Expand All @@ -1042,7 +1031,7 @@
ctx.stop();
sound.currentTime = lastTime;
ctx.play(0, lastTime);
this.patchLinks(ctx.sourceNode, sound);
this.fixAudioNodes(ctx.sourceNode, sound);
}
}

Expand Down Expand Up @@ -1101,12 +1090,13 @@
});
// this part of the Library was modified to work like this
engine.sourceNode = engine.getSourceNode();
const bank = (soundBank[args.NAME] = this.generateData(
const bank = this.generateData(
args.NAME,
sourceURL,
engine,
true
));
);
soundBank[args.NAME] = bank;
engine.on("stop", () => {
bank.currentTime =
engine.loop && bank.loopParm[1]
Expand All @@ -1116,23 +1106,6 @@
}
}

async convertSound(args, util) {
const sound = soundBank[args.NAME1];
if (sound === undefined) return;
try {
const response = await Scratch.fetch(sound.src);
const audioBlob = await response.blob();
const audioDataURL = await new Promise((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(audioBlob);
});
await this.importURL({ NAME: args.NAME2, URL: audioDataURL }, util);
} catch (e) {
console.error(e);
}
}

bindSound(args) {
const sound1 = soundBank[args.NAME];
const sound2 = soundBank[args.NAME2];
Expand Down Expand Up @@ -1473,7 +1446,7 @@
delete sound.effects[name];
}
sound.rate = sound.pitch * sound.speed * Math.pow(2, sound.detune / 1200);
this.patchLinks(ctx.sourceNode, sound);
this.fixAudioNodes(ctx.sourceNode, sound);
}

setThingNew(args) {
Expand All @@ -1499,7 +1472,7 @@
return this.updateEffect(distort, sound, "DISTORTION", args);
}
sound.rate = sound.pitch * sound.speed * Math.pow(2, sound.detune / 1200);
this.patchLinks(ctx.sourceNode, sound);
this.fixAudioNodes(ctx.sourceNode, sound);
}

setReverb(args) {
Expand Down

0 comments on commit f688c08

Please sign in to comment.