Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sound Expanded: Add Sound Expanded extension #672

Merged
merged 27 commits into from
Aug 25, 2023
Merged
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7827430
Add files via upload
LilyMakesThings Jul 2, 2023
c37b1f8
Actually, this is better.
LilyMakesThings Jul 2, 2023
38e6d36
Update SoundExpanded.js
LilyMakesThings Jul 2, 2023
dfc0ff4
Update SoundExpanded.js
LilyMakesThings Jul 2, 2023
53106a4
shut up, eslint
LilyMakesThings Jul 2, 2023
5759061
Update SoundExpanded.js
LilyMakesThings Jul 2, 2023
7a22532
Update SoundExpanded.js
LilyMakesThings Jul 2, 2023
4328bf6
Update SoundExpanded.js
LilyMakesThings Jul 2, 2023
f606f04
Update SoundExpanded.js
LilyMakesThings Jul 17, 2023
c342af5
Update and rename SoundExpanded.js to SoundPlus.js
LilyMakesThings Jul 17, 2023
9afb799
Update SoundPlus.js
LilyMakesThings Jul 18, 2023
216041c
Update SoundExpanded.js
LilyMakesThings Jul 18, 2023
76392f4
Merge branch 'master' into SoundExpanded
LilyMakesThings Jul 23, 2023
4c2f787
Update and rename SoundPlus.js to SoundExpanded.js
LilyMakesThings Jul 30, 2023
af59276
Update SoundExpanded.js
LilyMakesThings Aug 14, 2023
419e9d7
Fix weird discrepencies
LilyMakesThings Aug 14, 2023
3cb8008
Update SoundExpanded.js
LilyMakesThings Aug 14, 2023
85f1222
Update SoundExpanded.js
LilyMakesThings Aug 14, 2023
d9ca249
Update SoundExpanded.js
LilyMakesThings Aug 14, 2023
e510f3c
Update SoundExpanded.js
LilyMakesThings Aug 14, 2023
6a68c2d
Update SoundExpanded.js
LilyMakesThings Aug 14, 2023
6381e68
Merge branch 'TurboWarp:master' into SoundExpanded
LilyMakesThings Aug 22, 2023
b9014d6
Update SoundExpanded.js
LilyMakesThings Aug 23, 2023
ef21660
unused variable
GarboMuffin Aug 25, 2023
b17c2b3
Update SoundExpanded.js
LilyMakesThings Aug 25, 2023
0233680
Update SoundExpanded.js
LilyMakesThings Aug 25, 2023
09a45ae
Add to homepage
GarboMuffin Aug 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
355 changes: 355 additions & 0 deletions extensions/Lily/SoundExpanded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,355 @@
(function(Scratch) {
"use strict";

const vm = Scratch.vm;
const runtime = vm.runtime;
const soundCategory = runtime.ext_scratch3_sound;

/**
* @param {VM.BlockUtility} util
* @param {unknown} targetName
*/
const getSpriteTargetByName = (util, targetName) => {
const nameString = Scratch.Cast.toString(targetName);
const target = util.target;
if (target.getName() === nameString) {
return target;
}
return util.runtime.getSpriteTargetByName(nameString);
};

class SoundExpanded {
getInfo() {
return {
id: 'lmsSoundExpanded',
color1: "#CF63CF",
name: "Sound Expanded",
blocks: [
{
opcode: 'startSoundOnLoop',
blockType: Scratch.BlockType.COMMAND,
text: 'start sound [SOUND] on loop',
arguments: {
SOUND: {
type: Scratch.ArgumentType.SOUND
},
START: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 0
}
}
},
{
opcode: 'stopSound',
blockType: Scratch.BlockType.COMMAND,
text: 'stop sound [SOUND]',
arguments: {
SOUND: {
type: Scratch.ArgumentType.SOUND
}
}
},

'---',

{
opcode: 'pauseSounds',
blockType: Scratch.BlockType.COMMAND,
text: 'pause all sounds',
arguments: {
SOUND: {
type: Scratch.ArgumentType.SOUND
}
}
},
{
opcode: 'resumeSounds',
blockType: Scratch.BlockType.COMMAND,
text: 'resume all sounds',
arguments: {
SOUND: {
type: Scratch.ArgumentType.SOUND
}
}
},

'---',

{
opcode: 'startLooping',
blockType: Scratch.BlockType.COMMAND,
text: 'start looping [SOUND]',
arguments: {
SOUND: {
type: Scratch.ArgumentType.SOUND
}
}
},
{
opcode: 'stopLooping',
blockType: Scratch.BlockType.COMMAND,
text: 'stop looping [SOUND]',
arguments: {
SOUND: {
type: Scratch.ArgumentType.SOUND
}
}
},
{
opcode: 'isLooping',
blockType: Scratch.BlockType.BOOLEAN,
text: '[SOUND] is looping?',
arguments: {
SOUND: {
type: Scratch.ArgumentType.SOUND
}
}
},

'---',

{
opcode: 'isSoundPlaying',
blockType: Scratch.BlockType.BOOLEAN,
text: 'sound [SOUND] is playing?',
arguments: {
SOUND: {
type: Scratch.ArgumentType.SOUND
}
}
},
{
opcode: 'attributeOfSound',
blockType: Scratch.BlockType.REPORTER,
text: '[ATTRIBUTE] of [SOUND]',
arguments: {
ATTRIBUTE: {
type: Scratch.ArgumentType.STRING,
menu: 'attribute'
},
SOUND: {
type: Scratch.ArgumentType.SOUND
}
}
},
{
opcode: 'getSoundEffect',
blockType: Scratch.BlockType.REPORTER,
text: '[EFFECT] of [TARGET]',
arguments: {
EFFECT: {
type: Scratch.ArgumentType.STRING,
menu: 'effect'
},
TARGET: {
type: Scratch.ArgumentType.STRING,
menu: 'targets'
}
}
}
],
menus: {
attribute: {
acceptReporters: false,
items: ['length', 'channels', 'sample rate']
},
effect: {
acceptReporters: false,
items: ['pitch', 'pan']
},
targets: {
acceptReporters: true,
items: '_getTargets'
}
}
};
}

startSoundOnLoop(args, util) {
const index = this._getSoundIndex(args.SOUND, util);
if (index < 0) return 0;
const target = util.target;
const sprite = target.sprite;
const duration = args.START;

const soundId = sprite.sounds[index].soundId;
soundCategory._addWaitingSound(target.id, soundId);
sprite.soundBank.playSound(util.target, soundId);

const soundPlayer = sprite.soundBank.soundPlayers[soundId];

if (!soundPlayer.outputNode) return;
soundPlayer.outputNode.loop = true;
}

stopSound(args, util) {
const index = this._getSoundIndex(args.SOUND, util);
if (index < 0) return 0;
const target = util.target;
const sprite = target.sprite;

const soundId = sprite.sounds[index].soundId;
const soundBank = sprite.soundBank;
soundBank.stop(target, soundId);
}

pauseSounds(args, util) {
this._toggleSoundState(args, util, true);
}

resumeSounds(args, util) {
this._toggleSoundState(args, util, false);
}

_toggleSoundState(args, util, state) {
const sprite = util.target.sprite;
const audioContext = sprite.soundBank.audioEngine.audioContext;

if (state) {
audioContext.suspend();
return;
} else {
audioContext.resume();
return;
}
}

startLooping(args, util) {
const index = this._getSoundIndex(args.SOUND, util);
if (index < 0) return false;
const sprite = util.target.sprite;

const soundId = sprite.sounds[index].soundId;
const soundPlayer = sprite.soundBank.soundPlayers[soundId];

if (!soundPlayer.outputNode) return;
soundPlayer.outputNode.loop = true;
}

stopLooping(args, util) {
const index = this._getSoundIndex(args.SOUND, util);
if (index < 0) return false;
const sprite = util.target.sprite;

const soundId = sprite.sounds[index].soundId;
const soundPlayer = sprite.soundBank.soundPlayers[soundId];

if (!soundPlayer.outputNode) return;
soundPlayer.outputNode.loop = false;
}

isLooping(args, util) {
const index = this._getSoundIndex(args.SOUND, util);
if (index < 0) return false;
const sprite = util.target.sprite;

const soundId = sprite.sounds[index].soundId;
const soundPlayer = sprite.soundBank.soundPlayers[soundId];

if (!soundPlayer.outputNode) return false;
return soundPlayer.outputNode.loop;
}

isSoundPlaying(args, util) {
const index = this._getSoundIndex(args.SOUND, util);
if (index < 0) return false;
const sprite = util.target.sprite;

const soundId = sprite.sounds[index].soundId;
const soundPlayers = sprite.soundBank.soundPlayers;
return soundPlayers[soundId].isPlaying;
}

attributeOfSound(args, util) {
const index = this._getSoundIndex(args.SOUND, util);
if (index < 0) return 0;
const sprite = util.target.sprite;

const soundId = sprite.sounds[index].soundId;
const soundPlayer = sprite.soundBank.soundPlayers[soundId];
const soundBuffer = soundPlayer.buffer;

switch (args.ATTRIBUTE) {
case ('length'):
return Math.round(soundBuffer.duration * 100) / 100;
case ('channels'):
return soundBuffer.numberOfChannels;
case ('sample rate'):
return soundBuffer.sampleRate;
}
}

getSoundEffect(args, util) {
const target = getSpriteTargetByName(util, args.TARGET);
const effects = target.soundEffects;
if (!effects) return 0;
return effects[args.EFFECT];
}

/* Utility Functions */

_getSoundIndex(soundName, util) {
const len = util.target.sprite.sounds.length;
if (len === 0) {
return -1;
}
const index = this._getSoundIndexByName(soundName, util);
if (index !== -1) {
return index;
}
const oneIndexedIndex = parseInt(soundName, 10);
if (!isNaN(oneIndexedIndex)) {
return this._wrapClamp(oneIndexedIndex - 1, 0, len - 1);
}
return -1;
}

_getSoundIndexByName(soundName, util) {
const sounds = util.target.sprite.sounds;
for (let i = 0; i < sounds.length; i++) {
if (sounds[i].name === soundName) {
return i;
}
}
return -1;
}

_wrapClamp(n, min, max) {
const range = (max - min) + 1;
return n - (Math.floor((n - min) / range) * range);
}

_getTargets() {
const spriteNames = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's avoid the same mistake as looks+. have the top value always be "myself" with value _myself_ and just list every sprite under it, and also add the stage (_stage_)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a script that does that for skins, I'll add that now

const targets = runtime.targets;
const myself = runtime.getEditingTarget().getName();
for (let index = 1; index < targets.length; index++) {
const target = targets[index];
if (target.isOriginal) {
const targetName = target.getName();
if (targetName === myself) {
spriteNames.unshift({
text: 'myself',
value: targetName
});
} else {
spriteNames.push({
text: targetName,
value: targetName
});
}
}
}
if (spriteNames.length > 0) {
return spriteNames;
} else {
return [{
text: "",
value: 0
}]; //this should never happen but it's a failsafe
}
}
}

Scratch.extensions.register(new SoundExpanded());
})(Scratch);