Use the new note picker field in music and EV3 extensions (#1720)

* Add note field type

* Note picker in music extension plays notes

* Use note picker in EV3 beep block
This commit is contained in:
Eric Rosenbaum 2018-11-07 11:50:15 -05:00 committed by GitHub
parent eef1885fad
commit 2cff4159bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 4 deletions

View file

@ -68,6 +68,10 @@ const ArgumentTypeMap = (() => {
shadowType: 'matrix', shadowType: 'matrix',
fieldType: 'MATRIX' fieldType: 'MATRIX'
}; };
map[ArgumentType.NOTE] = {
shadowType: 'note',
fieldType: 'NOTE'
};
return map; return map;
})(); })();

View file

@ -29,9 +29,14 @@ const ArgumentType = {
STRING: 'string', STRING: 'string',
/** /**
* String value with matirx field * String value with matrix field
*/ */
MATRIX: 'matrix' MATRIX: 'matrix',
/**
* MIDI note number with note picker (piano) field
*/
NOTE: 'note'
}; };
module.exports = ArgumentType; module.exports = ArgumentType;

View file

@ -910,6 +910,9 @@ class Scratch3Ev3Blocks {
// Create a new EV3 peripheral instance // Create a new EV3 peripheral instance
this._peripheral = new EV3(this.runtime, Scratch3Ev3Blocks.EXTENSION_ID); this._peripheral = new EV3(this.runtime, Scratch3Ev3Blocks.EXTENSION_ID);
this._playNoteForPicker = this._playNoteForPicker.bind(this);
this.runtime.on('PLAY_NOTE', this._playNoteForPicker);
} }
/** /**
@ -1089,7 +1092,7 @@ class Scratch3Ev3Blocks {
blockType: BlockType.COMMAND, blockType: BlockType.COMMAND,
arguments: { arguments: {
NOTE: { NOTE: {
type: ArgumentType.NUMBER, type: ArgumentType.NOTE,
defaultValue: 60 defaultValue: 60
}, },
TIME: { TIME: {
@ -1212,6 +1215,14 @@ class Scratch3Ev3Blocks {
return this._peripheral.brightness; return this._peripheral.brightness;
} }
_playNoteForPicker (note, category) {
if (category !== this.getInfo().name) return;
this.beep({
NOTE: note,
TIME: 0.25
});
}
beep (args) { beep (args) {
const note = MathUtil.clamp(Cast.toNumber(args.NOTE), 47, 99); // valid EV3 sounds const note = MathUtil.clamp(Cast.toNumber(args.NOTE), 47, 99); // valid EV3 sounds
let time = Cast.toNumber(args.TIME) * 1000; let time = Cast.toNumber(args.TIME) * 1000;

View file

@ -85,6 +85,9 @@ class Scratch3MusicBlocks {
this._onTargetCreated = this._onTargetCreated.bind(this); this._onTargetCreated = this._onTargetCreated.bind(this);
this.runtime.on('targetWasCreated', this._onTargetCreated); this.runtime.on('targetWasCreated', this._onTargetCreated);
this._playNoteForPicker = this._playNoteForPicker.bind(this);
this.runtime.on('PLAY_NOTE', this._playNoteForPicker);
} }
/** /**
@ -747,7 +750,7 @@ class Scratch3MusicBlocks {
}), }),
arguments: { arguments: {
NOTE: { NOTE: {
type: ArgumentType.NUMBER, type: ArgumentType.NOTE,
defaultValue: 60 defaultValue: 60
}, },
BEATS: { BEATS: {
@ -940,6 +943,15 @@ class Scratch3MusicBlocks {
} }
} }
_playNoteForPicker (noteNum, category) {
if (category !== this.getInfo().name) return;
const util = {
runtime: this.runtime,
target: this.runtime.getEditingTarget()
};
this._playNote(util, noteNum, 0.25);
}
/** /**
* Play a note using the current instrument for a duration in seconds. * Play a note using the current instrument for a duration in seconds.
* This function actually plays the sound, and handles the timing of the sound, including the * This function actually plays the sound, and handles the timing of the sound, including the