-
Notifications
You must be signed in to change notification settings - Fork 238
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
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 c37b1f8
Actually, this is better.
LilyMakesThings 38e6d36
Update SoundExpanded.js
LilyMakesThings dfc0ff4
Update SoundExpanded.js
LilyMakesThings 53106a4
shut up, eslint
LilyMakesThings 5759061
Update SoundExpanded.js
LilyMakesThings 7a22532
Update SoundExpanded.js
LilyMakesThings 4328bf6
Update SoundExpanded.js
LilyMakesThings f606f04
Update SoundExpanded.js
LilyMakesThings c342af5
Update and rename SoundExpanded.js to SoundPlus.js
LilyMakesThings 9afb799
Update SoundPlus.js
LilyMakesThings 216041c
Update SoundExpanded.js
LilyMakesThings 76392f4
Merge branch 'master' into SoundExpanded
LilyMakesThings 4c2f787
Update and rename SoundPlus.js to SoundExpanded.js
LilyMakesThings af59276
Update SoundExpanded.js
LilyMakesThings 419e9d7
Fix weird discrepencies
LilyMakesThings 3cb8008
Update SoundExpanded.js
LilyMakesThings 85f1222
Update SoundExpanded.js
LilyMakesThings d9ca249
Update SoundExpanded.js
LilyMakesThings e510f3c
Update SoundExpanded.js
LilyMakesThings 6a68c2d
Update SoundExpanded.js
LilyMakesThings 6381e68
Merge branch 'TurboWarp:master' into SoundExpanded
LilyMakesThings b9014d6
Update SoundExpanded.js
LilyMakesThings ef21660
unused variable
GarboMuffin b17c2b3
Update SoundExpanded.js
LilyMakesThings 0233680
Update SoundExpanded.js
LilyMakesThings 09a45ae
Add to homepage
GarboMuffin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; | ||
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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_
)There was a problem hiding this comment.
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