Add music

This commit is contained in:
Michal Moskal
2017-07-07 11:44:34 +01:00
parent 43b167957b
commit 97b6c7dc4b
13 changed files with 166 additions and 5 deletions

52
libs/music/music.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "pxt.h"
#include "ev3.h"
#define NOTE_PAUSE 20
namespace music {
byte currVolume = 100;
/**
* Set the output volume of the sound synthesizer.
* @param volume the volume 0...256, eg: 128
*/
//% weight=96
//% blockId=synth_set_volume block="set volume %volume"
//% parts="speaker" blockGap=8
//% volume.min=0 volume.max=256
//% help=music/set-volume
//% weight=1
void setVolume(int volume) {
currVolume = max(0, min(100, volume * 100 / 256));
}
/**
* Play a tone through the speaker for some amount of time.
* @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=music_play_note block="play tone|at %note=device_note|for %duration=device_beat"
//% parts="headphone" async blockGap=8
//% blockNamespace=music
void playTone(int frequency, int ms) {
if (frequency <= 0) {
StopSound();
if (ms >= 0)
sleep_ms(ms);
} else {
if (ms > 0) {
int d = max(1, ms - NOTE_PAUSE); // allow for short rest
int r = max(1, ms - d);
PlayToneEx(frequency, d, currVolume);
sleep_ms(d + r);
} else {
// ring
PlayToneEx(frequency, 60000, currVolume);
}
}
sleep_ms(1);
}
}

1
libs/music/pins.cpp Normal file
View File

@ -0,0 +1 @@

21
libs/music/pxt.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "music",
"description": "The music library",
"files": [
"README.md",
"music.cpp",
"enums.d.ts",
"shims.d.ts",
"melodies.ts",
"music.ts",
"ns.ts"
],
"testFiles": [
"test.ts"
],
"public": true,
"additionalFilePath": "../../node_modules/pxt-common-packages/libs/music",
"dependencies": {
"core": "file:../core"
}
}

28
libs/music/shims.d.ts vendored Normal file
View File

@ -0,0 +1,28 @@
// Auto-generated. Do not edit.
declare namespace music {
/**
* Set the output volume of the sound synthesizer.
* @param volume the volume 0...256, eg: 128
*/
//% weight=96
//% blockId=synth_set_volume block="set volume %volume"
//% parts="speaker" blockGap=8
//% volume.min=0 volume.max=256
//% help=music/set-volume
//% weight=1 shim=music::setVolume
function setVolume(volume: int32): void;
/**
* Play a tone through the speaker for some amount of time.
* @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=music_play_note block="play tone|at %note=device_note|for %duration=device_beat"
//% parts="headphone" async blockGap=8
//% blockNamespace=music shim=music::playTone
function playTone(frequency: int32, ms: int32): void;
}
// Auto-generated. Do not edit. Really.