pxt-calliope/libs/core/music.ts
chmora 56904acb99 Blockly integrate pxt microbit (#355)
* added new values for Integration with field_note
2017-02-23 09:44:18 -08:00

244 lines
7.4 KiB
TypeScript

enum Note {
//% blockIdentity=music.noteFrequency enumval=262
C = 262,
//% block=C#
//% blockIdentity=music.noteFrequency enumval=277
CSharp = 277,
//% blockIdentity=music.noteFrequency enumval=294
D = 294,
//% blockIdentity=music.noteFrequency enumval=311
Eb = 311,
//% blockIdentity=music.noteFrequency enumval=330
E = 330,
//% blockIdentity=music.noteFrequency enumval=349
F = 349,
//% block=F#
//% blockIdentity=music.noteFrequency enumval=370
FSharp = 370,
//% blockIdentity=music.noteFrequency enumval=392
G = 392,
//% block=G#
//% blockIdentity=music.noteFrequency enumval=415
GSharp = 415,
//% blockIdentity=music.noteFrequency enumval=440
A = 440,
//% blockIdentity=music.noteFrequency enumval=466
Bb = 466,
//% blockIdentity=music.noteFrequency enumval=494
B = 494,
//% blockIdentity=music.noteFrequency enumval=131
C3 = 131,
//% block=C#3
//% blockIdentity=music.noteFrequency enumval=139
CSharp3 = 139,
//% blockIdentity=music.noteFrequency enumval=147
D3 = 147,
//% blockIdentity=music.noteFrequency enumval=156
Eb3 = 156,
//% blockIdentity=music.noteFrequency enumval=165
E3 = 165,
//% blockIdentity=music.noteFrequency enumval=175
F3 = 175,
//% block=F#3
//% blockIdentity=music.noteFrequency enumval=185
FSharp3 = 185,
//% blockIdentity=music.noteFrequency enumval=196
G3 = 196,
//% block=G#3
//% blockIdentity=music.noteFrequency enumval=208
GSharp3 = 208,
//% blockIdentity=music.noteFrequency enumval=220
A3 = 220,
//% blockIdentity=music.noteFrequency enumval=233
Bb3 = 233,
//% blockIdentity=music.noteFrequency enumval=247
B3 = 247,
//% blockIdentity=music.noteFrequency enumval=262
C4 = 262,
//% block=C#4
//% blockIdentity=music.noteFrequency enumval=277
CSharp4 = 277,
//% blockIdentity=music.noteFrequency enumval=294
D4 = 294,
//% blockIdentity=music.noteFrequency enumval=311
Eb4 = 311,
//% blockIdentity=music.noteFrequency enumval=330
E4 = 330,
//% blockIdentity=music.noteFrequency enumval=349
F4 = 349,
//% block=F#4
//% blockIdentity=music.noteFrequency enumval=370
FSharp4 = 370,
//% blockIdentity=music.noteFrequency enumval=392
G4 = 392,
//% block=G#4
//% blockIdentity=music.noteFrequency enumval=415
GSharp4 = 415,
//% blockIdentity=music.noteFrequency enumval=440
A4 = 440,
//% blockIdentity=music.noteFrequency enumval=466
Bb4 = 466,
//% blockIdentity=music.noteFrequency enumval=494
B4 = 494,
//% blockIdentity=music.noteFrequency enumval=523
C5 = 523,
//% block=C#5
//% blockIdentity=music.noteFrequency enumval=555
CSharp5 = 555,
//% blockIdentity=music.noteFrequency enumval=587
D5 = 587,
//% blockIdentity=music.noteFrequency enumval=622
Eb5 = 622,
//% blockIdentity=music.noteFrequency enumval=659
E5 = 659,
//% blockIdentity=music.noteFrequency enumval=698
F5 = 698,
//% block=F#5
//% blockIdentity=music.noteFrequency enumval=740
FSharp5 = 740,
//% blockIdentity=music.noteFrequency enumval=784
G5 = 784,
//% block=G#5
//% blockIdentity=music.noteFrequency enumval=831
GSharp5 = 831,
//% blockIdentity=music.noteFrequency enumval=880
A5 = 880,
//% blockIdentity=music.noteFrequency enumval=932
Bb5 = 932,
//% blockIdentity=music.noteFrequency enumval=988
B5 = 988,
}
enum BeatFraction {
//% block=1
Whole = 1,
//% block="1/2"
Half = 2,
//% block="1/4"
Quarter = 4,
//% block="1/8"
Eighth = 8,
//% block="1/16"
Sixteenth = 16,
//% block="2"
Double = 32,
//% block="4",
Breve = 64
}
/**
* Generation of music tones through pin ``P0``.
*/
//% color=#D83B01 weight=98 icon="\uf025"
namespace music {
let beatsPerMinute: number = 120;
/**
* 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 {
pins.analogPitch(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 {
pins.analogPitch(frequency, 0);
}
/**
* Rests (plays nothing) for a specified time through pin ``P0``.
* @param ms rest duration in milliseconds (ms)
*/
//% help=music/rest weight=79
//% blockId=device_rest block="rest(ms)|%duration=device_beat"
//% parts="headphone"
export function rest(ms: number): void {
playTone(0, ms);
}
/**
* Gets the frequency of a note.
* @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
export function noteFrequency(name: Note): number {
return name;
}
function init() {
if (beatsPerMinute <= 0) beatsPerMinute = 120;
}
/**
* Returns the duration of a beat in milli-seconds
*/
//% help=music/beat weight=49
//% blockId=device_beat block="%fraction|beat"
export function beat(fraction?: BeatFraction): number {
init();
if (fraction == null) fraction = BeatFraction.Whole;
let beat = 60000 / beatsPerMinute;
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;
}
}
/**
* 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.
*/
//% help=music/tempo weight=40
//% blockId=device_tempo block="tempo (bpm)" blockGap=8
export function tempo(): number {
init();
return beatsPerMinute;
}
/**
* Change the tempo by the specified amount
* @param bpm The change in beats per minute to the tempo, eg: 20
*/
//% help=music/change-tempo-by weight=39
//% blockId=device_change_tempo block="change tempo by (bpm)|%value" blockGap=8
export function changeTempoBy(bpm: number): void {
init();
setTempo(beatsPerMinute + bpm);
}
/**
* Sets the tempo to the specified amount
* @param bpm The new tempo in beats per minute, eg: 120
*/
//% help=music/set-tempo weight=38
//% blockId=device_set_tempo block="set tempo to (bpm)|%value"
export function setTempo(bpm: number): void {
init();
if (bpm > 0) {
beatsPerMinute = Math.max(1, bpm);
}
}
}