2016-03-24 16:26:55 +01:00
|
|
|
enum Note {
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=262
|
2016-04-01 18:17:41 +02:00
|
|
|
C = 262,
|
|
|
|
//% block=C#
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=277
|
2016-04-01 18:17:41 +02:00
|
|
|
CSharp = 277,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=294
|
2016-04-01 18:17:41 +02:00
|
|
|
D = 294,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=311
|
2016-04-01 18:17:41 +02:00
|
|
|
Eb = 311,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=330
|
2016-04-01 18:17:41 +02:00
|
|
|
E = 330,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=349
|
2016-04-01 18:17:41 +02:00
|
|
|
F = 349,
|
|
|
|
//% block=F#
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=370
|
2016-04-01 18:17:41 +02:00
|
|
|
FSharp = 370,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=392
|
2016-04-01 18:17:41 +02:00
|
|
|
G = 392,
|
|
|
|
//% block=G#
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=415
|
2016-04-01 18:17:41 +02:00
|
|
|
GSharp = 415,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=440
|
2016-04-01 18:17:41 +02:00
|
|
|
A = 440,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=466
|
2016-04-01 18:17:41 +02:00
|
|
|
Bb = 466,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=494
|
2016-04-01 18:17:41 +02:00
|
|
|
B = 494,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=131
|
2016-04-01 18:17:41 +02:00
|
|
|
C3 = 131,
|
|
|
|
//% block=C#3
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=139
|
2016-04-01 18:17:41 +02:00
|
|
|
CSharp3 = 139,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=147
|
2016-04-01 18:17:41 +02:00
|
|
|
D3 = 147,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=156
|
2016-04-01 18:17:41 +02:00
|
|
|
Eb3 = 156,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=165
|
2016-04-01 18:17:41 +02:00
|
|
|
E3 = 165,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=175
|
2016-04-01 18:17:41 +02:00
|
|
|
F3 = 175,
|
|
|
|
//% block=F#3
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=185
|
2016-04-01 18:17:41 +02:00
|
|
|
FSharp3 = 185,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=196
|
2016-04-01 18:17:41 +02:00
|
|
|
G3 = 196,
|
|
|
|
//% block=G#3
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=208
|
2016-04-01 18:17:41 +02:00
|
|
|
GSharp3 = 208,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=220
|
2016-04-01 18:17:41 +02:00
|
|
|
A3 = 220,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=233
|
2016-04-01 18:17:41 +02:00
|
|
|
Bb3 = 233,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=247
|
2016-04-01 18:17:41 +02:00
|
|
|
B3 = 247,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=262
|
2016-04-01 18:17:41 +02:00
|
|
|
C4 = 262,
|
|
|
|
//% block=C#4
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=277
|
2016-04-01 18:17:41 +02:00
|
|
|
CSharp4 = 277,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=294
|
2016-04-01 18:17:41 +02:00
|
|
|
D4 = 294,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=311
|
2016-04-01 18:17:41 +02:00
|
|
|
Eb4 = 311,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=330
|
2016-04-01 18:17:41 +02:00
|
|
|
E4 = 330,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=349
|
2016-04-01 18:17:41 +02:00
|
|
|
F4 = 349,
|
2016-04-12 04:44:39 +02:00
|
|
|
//% block=F#4
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=370
|
2016-04-01 18:17:41 +02:00
|
|
|
FSharp4 = 370,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=392
|
2016-04-01 18:17:41 +02:00
|
|
|
G4 = 392,
|
2016-04-12 04:44:39 +02:00
|
|
|
//% block=G#4
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=415
|
2016-04-01 18:17:41 +02:00
|
|
|
GSharp4 = 415,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=440
|
2016-04-01 18:17:41 +02:00
|
|
|
A4 = 440,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=466
|
2016-04-01 18:17:41 +02:00
|
|
|
Bb4 = 466,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=494
|
2016-04-01 18:17:41 +02:00
|
|
|
B4 = 494,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=523
|
2016-04-01 18:17:41 +02:00
|
|
|
C5 = 523,
|
|
|
|
//% block=C#5
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=555
|
2016-04-01 18:17:41 +02:00
|
|
|
CSharp5 = 555,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=587
|
2016-04-01 18:17:41 +02:00
|
|
|
D5 = 587,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=622
|
2016-04-01 18:17:41 +02:00
|
|
|
Eb5 = 622,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=659
|
2016-04-01 18:17:41 +02:00
|
|
|
E5 = 659,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=698
|
2016-04-01 18:17:41 +02:00
|
|
|
F5 = 698,
|
|
|
|
//% block=F#5
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=740
|
2016-04-01 18:17:41 +02:00
|
|
|
FSharp5 = 740,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=784
|
2016-04-01 18:17:41 +02:00
|
|
|
G5 = 784,
|
|
|
|
//% block=G#5
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=831
|
2016-04-01 18:17:41 +02:00
|
|
|
GSharp5 = 831,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=880
|
2016-04-01 18:17:41 +02:00
|
|
|
A5 = 880,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=932
|
2016-04-01 18:17:41 +02:00
|
|
|
Bb5 = 932,
|
2017-02-23 18:44:18 +01:00
|
|
|
//% blockIdentity=music.noteFrequency enumval=988
|
|
|
|
B5 = 988,
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
enum BeatFraction {
|
2016-04-01 18:17:41 +02:00
|
|
|
//% block=1
|
2016-03-10 23:01:04 +01:00
|
|
|
Whole = 1,
|
2016-04-01 18:17:41 +02:00
|
|
|
//% block="1/2"
|
2016-03-10 23:01:04 +01:00
|
|
|
Half = 2,
|
2016-04-01 18:17:41 +02:00
|
|
|
//% block="1/4"
|
2016-09-24 07:27:57 +02:00
|
|
|
Quarter = 4,
|
2016-04-01 18:17:41 +02:00
|
|
|
//% block="1/8"
|
2016-03-14 05:35:31 +01:00
|
|
|
Eighth = 8,
|
2016-04-01 18:17:41 +02:00
|
|
|
//% block="1/16"
|
2016-12-01 01:06:17 +01:00
|
|
|
Sixteenth = 16,
|
|
|
|
//% block="2"
|
|
|
|
Double = 32,
|
|
|
|
//% block="4",
|
|
|
|
Breve = 64
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
2017-03-01 00:23:32 +01:00
|
|
|
enum MelodyOptions {
|
2018-12-02 16:56:47 +01:00
|
|
|
//% block="once"
|
2017-03-01 00:23:32 +01:00
|
|
|
Once = 1,
|
|
|
|
//% block="forever"
|
|
|
|
Forever = 2,
|
|
|
|
//% block="once in background"
|
|
|
|
OnceInBackground = 4,
|
|
|
|
//% block="forever in background"
|
|
|
|
ForeverInBackground = 8
|
|
|
|
}
|
|
|
|
|
2018-12-02 16:56:47 +01:00
|
|
|
enum MelodyStopOptions {
|
|
|
|
//% block="all"
|
|
|
|
All = MelodyOptions.Once | MelodyOptions.OnceInBackground,
|
|
|
|
//% block="foreground"
|
|
|
|
Foreground = MelodyOptions.Once,
|
|
|
|
//% block="background"
|
|
|
|
Background = MelodyOptions.OnceInBackground
|
|
|
|
}
|
|
|
|
|
2017-04-25 19:14:34 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:59:25 +02:00
|
|
|
/**
|
2017-04-25 19:14:34 +02:00
|
|
|
* Generation of music tones.
|
2016-04-06 00:59:25 +02:00
|
|
|
*/
|
2018-10-01 23:39:51 +02:00
|
|
|
//% color=#E63022 weight=106 icon="\uf025"
|
2016-03-10 23:01:04 +01:00
|
|
|
namespace music {
|
2016-05-17 01:24:44 +02:00
|
|
|
let beatsPerMinute: number = 120;
|
2019-04-11 00:31:50 +02:00
|
|
|
//% whenUsed
|
|
|
|
const freqs = hex`
|
|
|
|
1f00210023002500270029002c002e003100340037003a003e004100450049004e00520057005c00620068006e00
|
|
|
|
75007b0083008b0093009c00a500af00b900c400d000dc00e900f70006011501260137014a015d01720188019f01
|
|
|
|
b801d201ee010b022a024b026e029302ba02e40210033f037003a403dc03170455049704dd0427057505c8052006
|
|
|
|
7d06e0064907b8072d08a9082d09b9094d0aea0a900b400cfa0cc00d910e6f0f5a1053115b1272139a14d4152017
|
|
|
|
8018f519801b231dde1e`;
|
2017-04-25 19:14:34 +02:00
|
|
|
let _playTone: (frequency: number, duration: number) => void;
|
|
|
|
const MICROBIT_MELODY_ID = 2000;
|
2016-03-10 23:01:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays a tone through pin ``P0`` for the given duration.
|
2018-04-21 19:25:43 +02:00
|
|
|
* @param frequency pitch of the tone to play in Hertz (Hz), eg: Note.C
|
2016-05-20 14:40:54 +02:00
|
|
|
* @param ms tone duration in milliseconds (ms)
|
2016-03-10 23:01:04 +01:00
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=music/play-tone weight=90
|
2017-01-05 16:01:50 +01:00
|
|
|
//% blockId=device_play_note block="play|tone %note=device_note|for %duration=device_beat" blockGap=8
|
2016-09-16 08:50:52 +02:00
|
|
|
//% parts="headphone"
|
2017-04-27 02:13:17 +02:00
|
|
|
//% useEnumVal=1
|
2016-03-10 23:01:04 +01:00
|
|
|
export function playTone(frequency: number, ms: number): void {
|
2017-04-25 19:14:34 +02:00
|
|
|
if (_playTone) _playTone(frequency, ms);
|
|
|
|
else pins.analogPitch(frequency, ms);
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plays a tone through pin ``P0``.
|
2018-04-21 19:25:43 +02:00
|
|
|
* @param frequency pitch of the tone to play in Hertz (Hz), eg: Note.C
|
2016-03-10 23:01:04 +01:00
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=music/ring-tone weight=80
|
2017-01-05 16:01:50 +01:00
|
|
|
//% blockId=device_ring block="ring tone (Hz)|%note=device_note" blockGap=8
|
2016-09-16 08:50:52 +02:00
|
|
|
//% parts="headphone"
|
2017-04-27 02:13:17 +02:00
|
|
|
//% useEnumVal=1
|
2016-03-10 23:01:04 +01:00
|
|
|
export function ringTone(frequency: number): void {
|
2017-04-25 19:14:34 +02:00
|
|
|
playTone(frequency, 0);
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rests (plays nothing) for a specified time through pin ``P0``.
|
2016-05-20 14:40:54 +02:00
|
|
|
* @param ms rest duration in milliseconds (ms)
|
2016-03-10 23:01:04 +01:00
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=music/rest weight=79
|
2016-03-10 23:01:04 +01:00
|
|
|
//% blockId=device_rest block="rest(ms)|%duration=device_beat"
|
2016-09-16 08:50:52 +02:00
|
|
|
//% parts="headphone"
|
2016-03-10 23:01:04 +01:00
|
|
|
export function rest(ms: number): void {
|
|
|
|
playTone(0, ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the frequency of a note.
|
2018-08-08 23:51:00 +02:00
|
|
|
* @param name the note name
|
2016-03-10 23:01:04 +01:00
|
|
|
*/
|
2016-04-02 07:00:42 +02:00
|
|
|
//% weight=50 help=music/note-frequency
|
2018-08-08 23:51:00 +02:00
|
|
|
//% blockId=device_note block="%name"
|
2018-04-21 19:25:43 +02:00
|
|
|
//% shim=TD_ID color="#FFFFFF" colorSecondary="#FFFFFF"
|
2018-08-08 23:51:00 +02:00
|
|
|
//% name.fieldEditor="note" name.defl="262"
|
|
|
|
//% name.fieldOptions.decompileLiterals=true
|
2017-04-27 02:13:17 +02:00
|
|
|
//% useEnumVal=1
|
2016-03-24 16:26:55 +01:00
|
|
|
export function noteFrequency(name: Note): number {
|
2016-03-10 23:01:04 +01:00
|
|
|
return name;
|
|
|
|
}
|
2016-05-20 14:40:54 +02:00
|
|
|
|
2016-03-14 16:32:02 +01:00
|
|
|
function init() {
|
2016-05-20 14:40:54 +02:00
|
|
|
if (beatsPerMinute <= 0) beatsPerMinute = 120;
|
2016-03-14 16:32:02 +01:00
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the duration of a beat in milli-seconds
|
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=music/beat weight=49
|
2016-03-10 23:01:04 +01:00
|
|
|
//% blockId=device_beat block="%fraction|beat"
|
2016-04-13 02:57:16 +02:00
|
|
|
export function beat(fraction?: BeatFraction): number {
|
2016-03-14 16:32:02 +01:00
|
|
|
init();
|
2016-04-13 02:57:16 +02:00
|
|
|
if (fraction == null) fraction = BeatFraction.Whole;
|
2019-04-11 00:31:50 +02:00
|
|
|
let beat = Math.idiv(60000, beatsPerMinute);
|
2016-12-01 01:06:17 +01:00
|
|
|
switch (fraction) {
|
2019-04-11 00:31:50 +02:00
|
|
|
case BeatFraction.Half: return beat >> 1;
|
|
|
|
case BeatFraction.Quarter: return beat >> 2;
|
|
|
|
case BeatFraction.Eighth: return beat >> 3;
|
|
|
|
case BeatFraction.Sixteenth: return beat >> 4;
|
|
|
|
case BeatFraction.Double: return beat << 1;
|
|
|
|
case BeatFraction.Breve: return beat << 2;
|
2016-12-01 01:06:17 +01:00
|
|
|
default: return beat;
|
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the tempo in beats per minute. Tempo is the speed (bpm = beats per minute) at which notes play. The larger the tempo value, the faster the notes will play.
|
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=music/tempo weight=40
|
2016-03-10 23:01:04 +01:00
|
|
|
//% blockId=device_tempo block="tempo (bpm)" blockGap=8
|
|
|
|
export function tempo(): number {
|
2016-03-14 16:32:02 +01:00
|
|
|
init();
|
2016-03-10 23:01:04 +01:00
|
|
|
return beatsPerMinute;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the tempo by the specified amount
|
|
|
|
* @param bpm The change in beats per minute to the tempo, eg: 20
|
|
|
|
*/
|
2016-10-21 17:48:43 +02:00
|
|
|
//% help=music/change-tempo-by weight=39
|
2016-03-10 23:01:04 +01:00
|
|
|
//% blockId=device_change_tempo block="change tempo by (bpm)|%value" blockGap=8
|
|
|
|
export function changeTempoBy(bpm: number): void {
|
2016-08-30 20:17:15 +02:00
|
|
|
init();
|
2016-08-18 15:00:43 +02:00
|
|
|
setTempo(beatsPerMinute + bpm);
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the tempo to the specified amount
|
|
|
|
* @param bpm The new tempo in beats per minute, eg: 120
|
|
|
|
*/
|
2016-06-10 06:41:41 +02:00
|
|
|
//% help=music/set-tempo weight=38
|
2016-03-10 23:01:04 +01:00
|
|
|
//% blockId=device_set_tempo block="set tempo to (bpm)|%value"
|
2017-03-04 08:39:42 +01:00
|
|
|
//% bpm.min=4 bpm.max=400
|
2016-03-10 23:01:04 +01:00
|
|
|
export function setTempo(bpm: number): void {
|
2016-03-14 16:32:02 +01:00
|
|
|
init();
|
2016-03-10 23:01:04 +01:00
|
|
|
if (bpm > 0) {
|
|
|
|
beatsPerMinute = Math.max(1, bpm);
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 00:23:32 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-25 19:14:34 +02:00
|
|
|
* Registers code to run on various melody events
|
|
|
|
*/
|
|
|
|
//% blockId=melody_on_event block="music on %value"
|
2018-09-27 01:27:32 +02:00
|
|
|
//% help=music/on-event weight=59 blockGap=32
|
2018-05-14 20:05:47 +02:00
|
|
|
export function onEvent(value: MusicEvent, handler: () => void) {
|
2017-04-25 19:14:34 +02:00
|
|
|
control.onEvent(MICROBIT_MELODY_ID, value, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts playing a melody.
|
2017-03-01 00:23:32 +01:00
|
|
|
* Notes are expressed as a string of characters with this format: NOTE[octave][:duration]
|
2018-08-14 19:18:23 +02:00
|
|
|
* @param melodyArray the melody array to play
|
2017-03-01 00:23:32 +01:00
|
|
|
* @param options melody options, once / forever, in the foreground / background
|
|
|
|
*/
|
2018-09-27 01:27:32 +02:00
|
|
|
//% help=music/begin-melody weight=60 blockGap=16
|
2017-04-25 19:14:34 +02:00
|
|
|
//% blockId=device_start_melody block="start melody %melody=device_builtin_melody| repeating %options"
|
2017-03-01 00:23:32 +01:00
|
|
|
//% parts="headphone"
|
2017-08-23 20:38:47 +02:00
|
|
|
export function beginMelody(melodyArray: string[], options: MelodyOptions = 1) {
|
2017-03-01 00:23:32 +01:00
|
|
|
init();
|
|
|
|
if (currentMelody != undefined) {
|
|
|
|
if (((options & MelodyOptions.OnceInBackground) == 0)
|
|
|
|
&& ((options & MelodyOptions.ForeverInBackground) == 0)
|
2017-04-25 19:14:34 +02:00
|
|
|
&& currentMelody.background) {
|
2017-03-01 00:23:32 +01:00
|
|
|
currentBackgroundMelody = currentMelody;
|
2017-04-25 19:14:34 +02:00
|
|
|
currentMelody = null;
|
|
|
|
control.raiseEvent(MICROBIT_MELODY_ID, MusicEvent.BackgroundMelodyPaused);
|
2017-03-01 00:23:32 +01:00
|
|
|
}
|
2017-04-25 19:14:34 +02:00
|
|
|
if (currentMelody)
|
|
|
|
control.raiseEvent(MICROBIT_MELODY_ID, currentMelody.background ? MusicEvent.BackgroundMelodyEnded : MusicEvent.MelodyEnded);
|
2017-03-01 00:23:32 +01:00
|
|
|
currentMelody = new Melody(melodyArray, options);
|
2017-04-25 19:14:34 +02:00
|
|
|
control.raiseEvent(MICROBIT_MELODY_ID, currentMelody.background ? MusicEvent.BackgroundMelodyStarted : MusicEvent.MelodyStarted);
|
2017-03-01 00:23:32 +01:00
|
|
|
} else {
|
|
|
|
currentMelody = new Melody(melodyArray, options);
|
2017-04-25 19:14:34 +02:00
|
|
|
control.raiseEvent(MICROBIT_MELODY_ID, currentMelody.background ? MusicEvent.BackgroundMelodyStarted : MusicEvent.MelodyStarted);
|
2017-03-01 00:23:32 +01:00
|
|
|
// 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;
|
2017-04-25 19:14:34 +02:00
|
|
|
control.raiseEvent(MICROBIT_MELODY_ID, MusicEvent.MelodyEnded);
|
|
|
|
control.raiseEvent(MICROBIT_MELODY_ID, MusicEvent.BackgroundMelodyResumed);
|
2017-03-01 00:23:32 +01:00
|
|
|
}
|
|
|
|
}
|
2017-04-25 19:14:34 +02:00
|
|
|
control.raiseEvent(MICROBIT_MELODY_ID, currentMelody.background ? MusicEvent.BackgroundMelodyEnded : MusicEvent.MelodyEnded);
|
2017-06-12 00:49:16 +02:00
|
|
|
currentMelody = null;
|
2017-03-01 00:23:32 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-02 16:56:47 +01:00
|
|
|
/**
|
|
|
|
* Stops the melodies
|
|
|
|
* @param options which melody to stop
|
|
|
|
*/
|
|
|
|
//% help=music/stop-melody weight=59 blockGap=16
|
|
|
|
//% blockId=device_stop_melody block="stop melody $options"
|
|
|
|
//% parts="headphone"
|
|
|
|
export function stopMelody(options: MelodyStopOptions) {
|
|
|
|
if (options & MelodyStopOptions.Foreground)
|
|
|
|
beginMelody([], MelodyOptions.Once);
|
|
|
|
if (options & MelodyStopOptions.Background)
|
|
|
|
beginMelody([], MelodyOptions.OnceInBackground);
|
|
|
|
}
|
|
|
|
|
2017-04-25 19:14:34 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2017-03-01 00:23:32 +01:00
|
|
|
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) {
|
2017-05-12 09:05:03 +02:00
|
|
|
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;
|
2017-03-01 00:23:32 +01:00
|
|
|
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));
|
|
|
|
}
|
2019-04-11 00:31:50 +02:00
|
|
|
let beat = Math.idiv(60000, beatsPerMinute) >> 2;
|
2017-03-01 00:23:32 +01:00
|
|
|
if (isrest) {
|
|
|
|
music.rest(currentDuration * beat)
|
|
|
|
} else {
|
|
|
|
let keyNumber = note + (12 * (currentOctave - 1));
|
2019-04-11 00:31:50 +02:00
|
|
|
let frequency = freqs.getNumber(NumberFormat.UInt16LE, keyNumber * 2) || 0;
|
2017-03-01 00:23:32 +01:00
|
|
|
music.playTone(frequency, currentDuration * beat);
|
|
|
|
}
|
|
|
|
melody.currentDuration = currentDuration;
|
|
|
|
melody.currentOctave = currentOctave;
|
2017-04-25 19:14:34 +02:00
|
|
|
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);
|
2017-03-01 00:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|