2016-03-24 16:26:55 +01:00
|
|
|
enum Note {
|
2016-09-22 23:36:37 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
C = 262,
|
|
|
|
//% block=C#
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
CSharp = 277,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
D = 294,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
Eb = 311,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
E = 330,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
F = 349,
|
|
|
|
//% block=F#
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
FSharp = 370,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
G = 392,
|
|
|
|
//% block=G#
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
GSharp = 415,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
A = 440,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
Bb = 466,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
B = 494,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
C3 = 131,
|
|
|
|
//% block=C#3
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
CSharp3 = 139,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
D3 = 147,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
Eb3 = 156,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
E3 = 165,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
F3 = 175,
|
|
|
|
//% block=F#3
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
FSharp3 = 185,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
G3 = 196,
|
|
|
|
//% block=G#3
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
GSharp3 = 208,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
A3 = 220,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
Bb3 = 233,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
B3 = 247,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
C4 = 262,
|
|
|
|
//% block=C#4
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
CSharp4 = 277,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
D4 = 294,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
Eb4 = 311,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
E4 = 330,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
F4 = 349,
|
2016-04-12 04:44:39 +02:00
|
|
|
//% block=F#4
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
FSharp4 = 370,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
G4 = 392,
|
2016-04-12 04:44:39 +02:00
|
|
|
//% block=G#4
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
GSharp4 = 415,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
A4 = 440,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
Bb4 = 466,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
B4 = 494,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
C5 = 523,
|
|
|
|
//% block=C#5
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
CSharp5 = 555,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
D5 = 587,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
Eb5 = 622,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
E5 = 659,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
F5 = 698,
|
|
|
|
//% block=F#5
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
FSharp5 = 740,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
G5 = 784,
|
|
|
|
//% block=G#5
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
GSharp5 = 831,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
A5 = 880,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
Bb5 = 932,
|
2016-08-31 20:22:07 +02:00
|
|
|
//% blockIdentity=music.noteFrequency
|
2016-04-01 18:17:41 +02:00
|
|
|
B5 = 989,
|
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-03-10 23:01:04 +01:00
|
|
|
Sixteenth = 16
|
|
|
|
}
|
|
|
|
|
2016-04-06 00:59:25 +02:00
|
|
|
/**
|
|
|
|
* Generation of music tones through pin ``P0``.
|
|
|
|
*/
|
2016-10-28 01:15:44 +02:00
|
|
|
//% color=#DF4600 weight=98
|
2016-03-10 23:01:04 +01:00
|
|
|
namespace music {
|
2016-05-17 01:24:44 +02:00
|
|
|
let beatsPerMinute: number = 120;
|
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-10-25 08:17:16 +02:00
|
|
|
//% parts="speaker"
|
2016-03-10 23:01:04 +01:00
|
|
|
export function rest(ms: number): void {
|
|
|
|
playTone(0, ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the frequency of a note.
|
2016-05-20 14:40:54 +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
|
2016-03-10 23:01:04 +01:00
|
|
|
//% blockId=device_note block="%note"
|
2016-08-31 20:22:07 +02:00
|
|
|
//% shim=TD_ID
|
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;
|
2016-03-14 05:35:31 +01:00
|
|
|
let beat = 60000 / beatsPerMinute;
|
|
|
|
if (fraction == BeatFraction.Whole) return beat;
|
|
|
|
else if (fraction == BeatFraction.Half) return beat / 2;
|
2016-10-26 20:52:40 +02:00
|
|
|
else if (fraction == BeatFraction.Quarter) return beat / 4;
|
2016-03-14 05:35:31 +01:00
|
|
|
else if (fraction == BeatFraction.Eighth) return beat / 8;
|
|
|
|
else return beat / 16;
|
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"
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|