upgrading music APIs (#12)
This commit is contained in:
@@ -119,15 +119,82 @@ enum BeatFraction {
|
||||
//% block="1/8"
|
||||
Eighth = 8,
|
||||
//% block="1/16"
|
||||
Sixteenth = 16
|
||||
Sixteenth = 16,
|
||||
//% block="2"
|
||||
Double = 32,
|
||||
//% block="4",
|
||||
Breve = 64
|
||||
}
|
||||
|
||||
enum MelodyOptions {
|
||||
//% block="once""
|
||||
Once = 1,
|
||||
//% block="forever"
|
||||
Forever = 2,
|
||||
//% block="once in background"
|
||||
OnceInBackground = 4,
|
||||
//% block="forever in background"
|
||||
ForeverInBackground = 8
|
||||
}
|
||||
|
||||
enum MusicEvent {
|
||||
//% block="melody note played"
|
||||
MelodyNotePlayed = 1,
|
||||
//% block="melody started"
|
||||
MelodyStarted = 2,
|
||||
//% block="melody ended"
|
||||
MelodyEnded = 3,
|
||||
//% block="melody repeated"
|
||||
MelodyRepeated = 4,
|
||||
//% block="background melody note played"
|
||||
BackgroundMelodyNotePlayed = MelodyNotePlayed | 0xf0,
|
||||
//% block="background melody started"
|
||||
BackgroundMelodyStarted = MelodyStarted | 0xf0,
|
||||
//% block="background melody ended"
|
||||
BackgroundMelodyEnded = MelodyEnded | 0xf0,
|
||||
//% block="background melody repeated"
|
||||
BackgroundMelodyRepeated = MelodyRepeated | 0xf0,
|
||||
//% block="background melody paused"
|
||||
BackgroundMelodyPaused = 5 | 0xf0,
|
||||
//% block="background melody resumed"
|
||||
BackgroundMelodyResumed = 6 | 0xf0
|
||||
}
|
||||
|
||||
/**
|
||||
* Generation of music tones through pin ``P0``.
|
||||
* Generation of music tones.
|
||||
*/
|
||||
//% color=#DF4600 weight=98 icon="\uf025"
|
||||
namespace music {
|
||||
let beatsPerMinute: number = 120;
|
||||
let freqTable: number[] = [];
|
||||
let _playTone: (frequency: number, duration: number) => void;
|
||||
const MICROBIT_MELODY_ID = 2000;
|
||||
|
||||
/**
|
||||
* Plays a tone through pin ``P0`` for the given duration.
|
||||
* @param frequency pitch of the tone to play in Hertz (Hz)
|
||||
* @param ms tone duration in milliseconds (ms)
|
||||
*/
|
||||
//% help=music/play-tone weight=90
|
||||
//% blockId=device_play_note block="play|tone %note=device_note|for %duration=device_beat" blockGap=8
|
||||
//% parts="headphone"
|
||||
//% useEnumVal=1
|
||||
export function playTone(frequency: number, ms: number): void {
|
||||
if (_playTone) _playTone(frequency, ms);
|
||||
else speakerPlayTone(frequency, ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a tone through pin ``P0``.
|
||||
* @param frequency pitch of the tone to play in Hertz (Hz)
|
||||
*/
|
||||
//% help=music/ring-tone weight=80
|
||||
//% blockId=device_ring block="ring tone (Hz)|%note=device_note" blockGap=8
|
||||
//% parts="headphone"
|
||||
//% useEnumVal=1
|
||||
export function ringTone(frequency: number): void {
|
||||
playTone(frequency, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rests (plays nothing) for a specified time through pin ``P0``.
|
||||
@@ -135,38 +202,28 @@ namespace music {
|
||||
*/
|
||||
//% help=music/rest weight=79
|
||||
//% blockId=device_rest block="rest(ms)|%duration=device_beat"
|
||||
//% parts="speaker"
|
||||
//% parts="headphone"
|
||||
export function rest(ms: number): void {
|
||||
playTone(0, ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a tone through ``speaker``.
|
||||
* @param frequency pitch of the tone to play in Hertz (Hz)
|
||||
*/
|
||||
//% help=music/ring-tone weight=80
|
||||
//% blockId=device_ring block="ring tone (Hz)|%note=device_note" blockGap=8
|
||||
//% parts="speaker" async
|
||||
//% useEnumVal=1
|
||||
export function ringTone(frequency: number) {
|
||||
playTone(frequency, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the frequency of a note.
|
||||
* @param name the note name
|
||||
* @param name the note name, eg: Note.C
|
||||
*/
|
||||
//% weight=50 help=music/note-frequency
|
||||
//% blockId=device_note block="%note"
|
||||
//% shim=TD_ID blockHidden=true
|
||||
//% blockFieldEditor="note_editor"
|
||||
//% useEnumVal = 1
|
||||
//% shim=TD_ID
|
||||
//% note.fieldEditor="note" note.defl="262"
|
||||
//% useEnumVal=1
|
||||
export function noteFrequency(name: Note): number {
|
||||
return name;
|
||||
}
|
||||
|
||||
function init() {
|
||||
if (beatsPerMinute <= 0) beatsPerMinute = 120;
|
||||
if (freqTable.length == 0) freqTable = [31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 117, 123, 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -178,11 +235,15 @@ namespace music {
|
||||
init();
|
||||
if (fraction == null) fraction = BeatFraction.Whole;
|
||||
let beat = 60000 / beatsPerMinute;
|
||||
if (fraction == BeatFraction.Whole) return beat;
|
||||
else if (fraction == BeatFraction.Half) return beat / 2;
|
||||
else if (fraction == BeatFraction.Quarter) return beat / 4;
|
||||
else if (fraction == BeatFraction.Eighth) return beat / 8;
|
||||
else return beat / 16;
|
||||
switch (fraction) {
|
||||
case BeatFraction.Half: return beat / 2;
|
||||
case BeatFraction.Quarter: return beat / 4;
|
||||
case BeatFraction.Eighth: return beat / 8;
|
||||
case BeatFraction.Sixteenth: return beat / 16;
|
||||
case BeatFraction.Double: return beat * 2;
|
||||
case BeatFraction.Breve: return beat * 4;
|
||||
default: return beat;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,10 +273,166 @@ namespace music {
|
||||
*/
|
||||
//% help=music/set-tempo weight=38
|
||||
//% blockId=device_set_tempo block="set tempo to (bpm)|%value"
|
||||
//% bpm.min=4 bpm.max=400
|
||||
export function setTempo(bpm: number): void {
|
||||
init();
|
||||
if (bpm > 0) {
|
||||
beatsPerMinute = Math.max(1, bpm);
|
||||
}
|
||||
}
|
||||
|
||||
let currentMelody: Melody;
|
||||
let currentBackgroundMelody: Melody;
|
||||
|
||||
/**
|
||||
* Gets the melody array of a built-in melody.
|
||||
* @param name the note name, eg: Note.C
|
||||
*/
|
||||
//% weight=50 help=music/builtin-melody
|
||||
//% blockId=device_builtin_melody block="%melody"
|
||||
//% blockHidden=true
|
||||
export function builtInMelody(melody: Melodies): string[] {
|
||||
return getMelody(melody);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers code to run on various melody events
|
||||
*/
|
||||
//% blockId=melody_on_event block="music on %value"
|
||||
//% help=music/on-event weight=59
|
||||
export function onEvent(value: MusicEvent, handler: Action) {
|
||||
control.onEvent(MICROBIT_MELODY_ID, value, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts playing a melody.
|
||||
* Notes are expressed as a string of characters with this format: NOTE[octave][:duration]
|
||||
* @param melodyArray the melody array to play, eg: ['g5:1']
|
||||
* @param options melody options, once / forever, in the foreground / background
|
||||
*/
|
||||
//% help=music/begin-melody weight=60 blockGap=8
|
||||
//% blockId=device_start_melody block="start melody %melody=device_builtin_melody| repeating %options"
|
||||
//% parts="headphone"
|
||||
export function beginMelody(melodyArray: string[], options: MelodyOptions = 1) {
|
||||
init();
|
||||
if (currentMelody != undefined) {
|
||||
if (((options & MelodyOptions.OnceInBackground) == 0)
|
||||
&& ((options & MelodyOptions.ForeverInBackground) == 0)
|
||||
&& currentMelody.background) {
|
||||
currentBackgroundMelody = currentMelody;
|
||||
currentMelody = null;
|
||||
control.raiseEvent(MICROBIT_MELODY_ID, MusicEvent.BackgroundMelodyPaused);
|
||||
}
|
||||
if (currentMelody)
|
||||
control.raiseEvent(MICROBIT_MELODY_ID, currentMelody.background ? MusicEvent.BackgroundMelodyEnded : MusicEvent.MelodyEnded);
|
||||
currentMelody = new Melody(melodyArray, options);
|
||||
control.raiseEvent(MICROBIT_MELODY_ID, currentMelody.background ? MusicEvent.BackgroundMelodyStarted : MusicEvent.MelodyStarted);
|
||||
} else {
|
||||
currentMelody = new Melody(melodyArray, options);
|
||||
control.raiseEvent(MICROBIT_MELODY_ID, currentMelody.background ? MusicEvent.BackgroundMelodyStarted : MusicEvent.MelodyStarted);
|
||||
// Only start the fiber once
|
||||
control.inBackground(() => {
|
||||
while (currentMelody.hasNextNote()) {
|
||||
playNextNote(currentMelody);
|
||||
if (!currentMelody.hasNextNote() && currentBackgroundMelody) {
|
||||
// Swap the background melody back
|
||||
currentMelody = currentBackgroundMelody;
|
||||
currentBackgroundMelody = null;
|
||||
control.raiseEvent(MICROBIT_MELODY_ID, MusicEvent.MelodyEnded);
|
||||
control.raiseEvent(MICROBIT_MELODY_ID, MusicEvent.BackgroundMelodyResumed);
|
||||
}
|
||||
}
|
||||
control.raiseEvent(MICROBIT_MELODY_ID, currentMelody.background ? MusicEvent.BackgroundMelodyEnded : MusicEvent.MelodyEnded);
|
||||
currentMelody = null;
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a custom playTone function for playing melodies
|
||||
*/
|
||||
//% help=music/set-play-tone
|
||||
//% advanced=true
|
||||
export function setPlayTone(f: (frequency: number, duration: number) => void) {
|
||||
_playTone = f;
|
||||
}
|
||||
|
||||
function playNextNote(melody: Melody): void {
|
||||
// cache elements
|
||||
let currNote = melody.nextNote();
|
||||
let currentPos = melody.currentPos;
|
||||
let currentDuration = melody.currentDuration;
|
||||
let currentOctave = melody.currentOctave;
|
||||
|
||||
let note: number;
|
||||
let isrest: boolean = false;
|
||||
let beatPos: number;
|
||||
let parsingOctave: boolean = true;
|
||||
|
||||
for (let pos = 0; pos < currNote.length; pos++) {
|
||||
let noteChar = currNote.charAt(pos);
|
||||
switch (noteChar) {
|
||||
case 'c': case 'C': note = 1; break;
|
||||
case 'd': case 'D': note = 3; break;
|
||||
case 'e': case 'E': note = 5; break;
|
||||
case 'f': case 'F': note = 6; break;
|
||||
case 'g': case 'G': note = 8; break;
|
||||
case 'a': case 'A': note = 10; break;
|
||||
case 'b': case 'B': note = 12; break;
|
||||
case 'r': case 'R': isrest = true; break;
|
||||
case '#': note++; break;
|
||||
case 'b': note--; break;
|
||||
case ':': parsingOctave = false; beatPos = pos; break;
|
||||
default: if (parsingOctave) currentOctave = parseInt(noteChar);
|
||||
}
|
||||
}
|
||||
if (!parsingOctave) {
|
||||
currentDuration = parseInt(currNote.substr(beatPos + 1, currNote.length - beatPos));
|
||||
}
|
||||
let beat = (60000 / beatsPerMinute) / 4;
|
||||
if (isrest) {
|
||||
music.rest(currentDuration * beat)
|
||||
} else {
|
||||
let keyNumber = note + (12 * (currentOctave - 1));
|
||||
let frequency = keyNumber >= 0 && keyNumber < freqTable.length ? freqTable[keyNumber] : 0;
|
||||
music.playTone(frequency, currentDuration * beat);
|
||||
}
|
||||
melody.currentDuration = currentDuration;
|
||||
melody.currentOctave = currentOctave;
|
||||
const repeating = melody.repeating && currentPos == melody.melodyArray.length - 1;
|
||||
melody.currentPos = repeating ? 0 : currentPos + 1;
|
||||
|
||||
control.raiseEvent(MICROBIT_MELODY_ID, melody.background ? MusicEvent.BackgroundMelodyNotePlayed : MusicEvent.MelodyNotePlayed);
|
||||
if (repeating)
|
||||
control.raiseEvent(MICROBIT_MELODY_ID, melody.background ? MusicEvent.BackgroundMelodyRepeated : MusicEvent.MelodyRepeated);
|
||||
}
|
||||
|
||||
class Melody {
|
||||
public melodyArray: string[];
|
||||
public currentDuration: number;
|
||||
public currentOctave: number;
|
||||
public currentPos: number;
|
||||
public repeating: boolean;
|
||||
public background: boolean;
|
||||
|
||||
constructor(melodyArray: string[], options: MelodyOptions) {
|
||||
this.melodyArray = melodyArray;
|
||||
this.repeating = ((options & MelodyOptions.Forever) != 0);
|
||||
this.repeating = this.repeating ? true : ((options & MelodyOptions.ForeverInBackground) != 0)
|
||||
this.background = ((options & MelodyOptions.OnceInBackground) != 0);
|
||||
this.background = this.background ? true : ((options & MelodyOptions.ForeverInBackground) != 0);
|
||||
this.currentDuration = 4; //Default duration (Crotchet)
|
||||
this.currentOctave = 4; //Middle octave
|
||||
this.currentPos = 0;
|
||||
}
|
||||
|
||||
hasNextNote() {
|
||||
return this.repeating || this.currentPos < this.melodyArray.length;
|
||||
}
|
||||
|
||||
nextNote(): string {
|
||||
const currentNote = this.melodyArray[this.currentPos];
|
||||
return currentNote;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user