-
Notifications
You must be signed in to change notification settings - Fork 114
Music to numbers #260
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
Open
orengdog
wants to merge
6
commits into
PenguinMod:main
Choose a base branch
from
orengdog:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Music to numbers #260
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3959454
Create Music to numbers
orengdog 3fe783c
Music to numbers.js
orengdog b33077a
Delete static/extensions/Music to numbers
orengdog 54cd8a0
Hello world!
orengdog 40699ec
Add files via upload
orengdog ba9e330
Delete static/images/orengdog/Hello world!
orengdog 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 hidden or 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,323 @@ | ||
(async function(Scratch) { | ||
const variables = { | ||
volume: 1.0, | ||
pitchShift: 0, | ||
leftRightValue: 0, | ||
selectedEffect: "none", | ||
isPlaying: false, | ||
instrument: "sine", | ||
instrumentID: 0, | ||
instrumentName: "without instrument", | ||
pauseDuration: 0.5 | ||
}; | ||
|
||
class MusicGenerator { | ||
constructor() { | ||
this.audioContext = new (window.AudioContext || window.webkitAudioContext)(); | ||
this.digitToFrequency = { | ||
"0": 261.63, // C4 | ||
"1": 293.66, // D4 | ||
"2": 329.63, // E4 | ||
"3": 349.23, // F4 | ||
"4": 392.00, // G4 | ||
"5": 440.00, // A4 | ||
"6": 493.88, // B4 | ||
"7": 523.25, // C5 | ||
"8": 587.33, // D5 | ||
"9": 659.25 // E5 | ||
}; | ||
this.activeOscillators = []; | ||
} | ||
|
||
getFrequencyWithEffect(frequency) { | ||
if (variables.selectedEffect === "pitch") { | ||
return frequency * Math.pow(2, variables.pitchShift / 12); | ||
} | ||
return frequency; | ||
} | ||
|
||
playContinuousSequence(sequence, noteDuration = 0.3) { | ||
let currentTime = this.audioContext.currentTime; | ||
variables.isPlaying = true; | ||
|
||
sequence.split('').forEach((char) => { | ||
if (this.digitToFrequency[char] !== undefined) { | ||
const frequency = this.getFrequencyWithEffect(this.digitToFrequency[char]); | ||
this.playNoteAtTime(frequency, currentTime, noteDuration); | ||
currentTime += noteDuration; | ||
} else if (char === ' ' || char === '_' || char === '.') { | ||
currentTime += variables.pauseDuration; | ||
} | ||
}); | ||
|
||
setTimeout(() => { variables.isPlaying = false; }, (currentTime - this.audioContext.currentTime) * 1000); | ||
} | ||
|
||
playNoteAtTime(frequency, startTime, duration) { | ||
const oscillator = this.audioContext.createOscillator(); | ||
oscillator.type = variables.instrument; | ||
oscillator.frequency.setValueAtTime(frequency, startTime); | ||
|
||
const gainNode = this.audioContext.createGain(); | ||
gainNode.gain.setValueAtTime(variables.volume * 0.1, startTime); | ||
|
||
let panNode; | ||
if (variables.selectedEffect === "left-right") { | ||
panNode = this.audioContext.createStereoPanner(); | ||
panNode.pan.setValueAtTime(variables.leftRightValue, startTime); | ||
oscillator.connect(panNode); | ||
panNode.connect(gainNode); | ||
} else { | ||
oscillator.connect(gainNode); | ||
} | ||
|
||
gainNode.connect(this.audioContext.destination); | ||
oscillator.start(startTime); | ||
oscillator.stop(startTime + duration); | ||
this.activeOscillators.push(oscillator); | ||
} | ||
|
||
stopAllMusic() { | ||
this.activeOscillators.forEach(oscillator => oscillator.stop()); | ||
this.activeOscillators = []; | ||
variables.isPlaying = false; | ||
} | ||
} | ||
|
||
const musicGen = new MusicGenerator(); | ||
|
||
const extension = { | ||
getInfo() { | ||
return { | ||
blockIconURI: "data:image/png;base64,iVBORw0KGgoAAAANS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TJZLY3ksRC84ejLf4/awMIs+vNl1kAAAAASUVORK5CYII=", | ||
id: "musicDigitGenerator", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add your name to the ID |
||
name: "Music to Numbers", | ||
color1: "#0088ff", | ||
color2: "#0063ba", | ||
blocks: [ | ||
{ | ||
opcode: 'playDigitSequence', | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: 'play note sequence for [sequence]', | ||
arguments: { | ||
sequence: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: '1 2 3 . 4 _ 5' | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'setVolume', | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: 'set volume to [volume] (1-100)', | ||
arguments: { | ||
volume: { | ||
type: Scratch.ArgumentType.NUMBER, | ||
defaultValue: 100 | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'setEffect', | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: 'set [effectType] effect value to [value]', | ||
arguments: { | ||
effectType: { | ||
type: Scratch.ArgumentType.STRING, | ||
menu: 'effectTypes', | ||
defaultValue: 'pitch' | ||
}, | ||
value: { | ||
type: Scratch.ArgumentType.NUMBER, | ||
defaultValue: 0 | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'changeEffect', | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: 'change [effectType] effect by [amount]', | ||
arguments: { | ||
effectType: { | ||
type: Scratch.ArgumentType.STRING, | ||
menu: 'effectTypes', | ||
defaultValue: 'pitch' | ||
}, | ||
amount: { | ||
type: Scratch.ArgumentType.NUMBER, | ||
defaultValue: 1 | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'getCurrentEffect', | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: 'current [effectType] effect', | ||
arguments: { | ||
effectType: { | ||
type: Scratch.ArgumentType.STRING, | ||
menu: 'effectTypes', | ||
defaultValue: 'pitch' | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'setPauseDuration', | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: 'set pause to [duration] seconds', | ||
arguments: { | ||
duration: { | ||
type: Scratch.ArgumentType.NUMBER, | ||
defaultValue: 0.5 | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'changePauseDuration', | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: 'change pause by [amount] seconds', | ||
arguments: { | ||
amount: { | ||
type: Scratch.ArgumentType.NUMBER, | ||
defaultValue: 0.1 | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'getCurrentPause', | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: 'current pause duration', | ||
}, | ||
{ | ||
opcode: 'setInstrument', | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: 'set instrument to [instrument]', | ||
arguments: { | ||
instrument: { | ||
type: Scratch.ArgumentType.STRING, | ||
menu: 'instruments', | ||
defaultValue: 'without instrument' | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'getCurrentInstrument', | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: 'current instrument [returnType]', | ||
arguments: { | ||
returnType: { | ||
type: Scratch.ArgumentType.STRING, | ||
menu: 'returnTypes', | ||
defaultValue: 'name' | ||
} | ||
} | ||
}, | ||
{ | ||
opcode: 'isMusicPlaying', | ||
blockType: Scratch.BlockType.BOOLEAN, | ||
text: 'music playing now?' | ||
}, | ||
{ | ||
opcode: 'stopMusic', | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: 'stop music' | ||
} | ||
], | ||
menus: { | ||
effectTypes: { | ||
acceptReporters: true, | ||
items: ['pitch', 'left-right'] | ||
}, | ||
instruments: { | ||
acceptReporters: true, | ||
items: ['without instrument', 'piano', 'organ'] | ||
}, | ||
returnTypes: { | ||
acceptReporters: true, | ||
items: ['name', 'number'] | ||
} | ||
} | ||
}; | ||
}, | ||
|
||
playDigitSequence(args) { | ||
const sequence = args.sequence; | ||
musicGen.playContinuousSequence(sequence); | ||
}, | ||
|
||
setVolume(args) { | ||
variables.volume = Math.max(0, Math.min(1, args.volume / 100)); | ||
}, | ||
|
||
setEffect(args) { | ||
variables.selectedEffect = args.effectType; | ||
if (args.effectType === "pitch") { | ||
variables.pitchShift = args.value; | ||
} else if (args.effectType === "left-right") { | ||
variables.leftRightValue = Math.max(-1, Math.min(1, args.value)); | ||
} | ||
}, | ||
|
||
changeEffect(args) { | ||
if (args.effectType === "pitch") { | ||
variables.pitchShift += args.amount; | ||
} else if (args.effectType === "left-right") { | ||
variables.leftRightValue = Math.max(-1, Math.min(1, variables.leftRightValue + args.amount)); | ||
} | ||
}, | ||
|
||
getCurrentEffect(args) { | ||
if (args.effectType === "pitch") { | ||
return variables.pitchShift; | ||
} else if (args.effectType === "left-right") { | ||
return variables.leftRightValue; | ||
} | ||
return 0; | ||
}, | ||
|
||
setPauseDuration(args) { | ||
variables.pauseDuration = Math.max(0, args.duration); | ||
}, | ||
|
||
changePauseDuration(args) { | ||
variables.pauseDuration = Math.max(0, variables.pauseDuration + args.amount); | ||
}, | ||
|
||
getCurrentPause() { | ||
return variables.pauseDuration; | ||
}, | ||
|
||
getCurrentInstrument(args) { | ||
return args.returnType === "name" ? variables.instrumentName : variables.instrumentID; | ||
}, | ||
|
||
setInstrument(args) { | ||
switch (args.instrument) { | ||
case 'piano': | ||
variables.instrument = 'triangle'; | ||
variables.instrumentID = 1; | ||
variables.instrumentName = 'piano'; | ||
break; | ||
case 'organ': | ||
variables.instrument = 'square'; | ||
variables.instrumentID = 2; | ||
variables.instrumentName = 'organ'; | ||
break; | ||
default: | ||
variables.instrument = 'sine'; | ||
variables.instrumentID = 0; | ||
variables.instrumentName = 'without instrument'; | ||
} | ||
}, | ||
|
||
isMusicPlaying() { | ||
return variables.isPlaying; | ||
}, | ||
|
||
stopMusic() { | ||
musicGen.stopAllMusic(); | ||
} | ||
}; | ||
|
||
Scratch.extensions.register(extension); | ||
})(Scratch); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
make sure to register this audioContext to PM.
you'll need to connect stuff that originally connects to
audioContext.destination
to a main gain node that is connected toaudioContext.destination
, you already make gain nodes for playing notes so you can just make a main gain node hereyou can then register the context with this function:
make sure to replace EXTENSION_ID with your extension's ID.
this will make the pause button and volume slider work with the extension, and future addons or extensions will be able to modify it if needed