Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
87 changes: 87 additions & 0 deletions extensions/Amp/oscillators.js
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks fine to me

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Name: Oscillators
// ID: aecOscillator
// Description: Synthensize simple sound waves.
// By: AmpElectrecuted <https://scratch.mit.edu/users/AmpElectrecuted/>
// License: MPL-2.0

(function (Scratch) {
"use strict";
let context = new AudioContext();
let oscillator = new OscillatorNode(context);
oscillator.connect(context.destination);
let isStart = false;
let isPaused = true;

class OscillatorExtension {
getInfo() {
return {
id: "aecOscillator",
name: Scratch.translate("Oscillators"),
blocks: [
{
opcode: "startFreq",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("start [WAVEFORM] wave at [FREQ] Hz"),
arguments: {
WAVEFORM: {
type: Scratch.ArgumentType.STRING,
menu: "WAVEFORM_MENU",
},
FREQ: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 440,
},
},
},
{
opcode: "stopFreq",
blockType: Scratch.BlockType.COMMAND,
text: Scratch.translate("stop wave"),
},
],
menus: {
WAVEFORM_MENU: {
acceptReporters: true,
items: [
{
text: Scratch.translate("sine"),
value: "sine",
},
{
text: Scratch.translate("square"),
value: "square",
},
{
text: Scratch.translate("sawtooth"),
value: "sawtooth",
},
{
text: Scratch.translate("triangle"),
value: "triangle",
},
],
},
},
};
}

startFreq(args) {
oscillator.type = Scratch.Cast.toString(args.WAVEFORM);
oscillator.frequency.setValueAtTime(Scratch.Cast.toNumber(args.FREQ), context.currentTime);
if (!isStart) {
oscillator.start();
isStart = true;
}
if (isPaused) {
context.resume();
isPaused = false;
}
}

stopFreq() {
context.suspend();
isPaused = true;
}
}
Scratch.extensions.register(new OscillatorExtension());
})(Scratch);
1 change: 1 addition & 0 deletions extensions/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"ar",
"encoding",
"Lily/SoundExpanded",
"Amp/oscillators",
"Lily/TempVariables2",
"Lily/MoreTimers",
"clouddata-ping",
Expand Down
40 changes: 40 additions & 0 deletions images/Amp/oscillators.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading