2016-11-30 06:55:37 +01:00
|
|
|
#include "pxt.h"
|
2016-10-27 15:51:42 +02:00
|
|
|
|
2022-03-22 17:36:19 +01:00
|
|
|
namespace pins {
|
|
|
|
void analogSetPitchVolume(int volume);
|
|
|
|
int analogPitchVolume();
|
|
|
|
}
|
|
|
|
|
2016-10-27 15:51:42 +02:00
|
|
|
namespace music {
|
2016-10-27 17:44:38 +02:00
|
|
|
/**
|
|
|
|
* Plays a tone through ``speaker`` for the given duration.
|
|
|
|
* @param frequency pitch of the tone to play in Hertz (Hz)
|
|
|
|
* @param ms tone duration in milliseconds (ms)
|
|
|
|
*/
|
2017-12-14 19:34:04 +01:00
|
|
|
//%
|
2017-02-23 18:44:18 +01:00
|
|
|
//% parts="speaker" async useEnumVal=1
|
2017-12-14 19:34:04 +01:00
|
|
|
void speakerPlayTone(int frequency, int ms) {
|
2017-02-19 11:19:26 +01:00
|
|
|
if(frequency > 0) uBit.soundmotor.soundOn(frequency);
|
|
|
|
else uBit.soundmotor.soundOff();
|
|
|
|
if(ms > 0) {
|
|
|
|
uBit.sleep(ms);
|
|
|
|
uBit.soundmotor.soundOff();
|
|
|
|
}
|
|
|
|
|
2016-10-27 15:51:42 +02:00
|
|
|
}
|
2019-12-02 05:58:26 +01:00
|
|
|
|
2022-03-22 17:36:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default output volume of the sound synthesizer.
|
|
|
|
* @param volume the volume 0...255
|
|
|
|
*/
|
|
|
|
//% blockId=synth_set_volume block="set volume %volume"
|
|
|
|
//% volume.min=0 volume.max=255
|
|
|
|
//% volume.defl=127
|
|
|
|
//% help=music/set-volume
|
|
|
|
//% weight=70
|
|
|
|
//% group="Volume"
|
|
|
|
//% blockGap=8
|
|
|
|
//% blockHidden=true
|
|
|
|
void setVolume(int volume) {
|
|
|
|
#if MICROBIT_CODAL
|
|
|
|
uBit.audio.setVolume(max(0, min(255, volume)));
|
|
|
|
#else
|
|
|
|
pins::analogSetPitchVolume(volume);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current output volume of the sound synthesizer.
|
|
|
|
*/
|
|
|
|
//% blockId=synth_get_volume block="volume"
|
|
|
|
//% help=music/volume
|
|
|
|
//% weight=69
|
|
|
|
//% group="Volume"
|
|
|
|
//% blockGap=8
|
|
|
|
//% blockHidden=true
|
|
|
|
int volume() {
|
|
|
|
#if MICROBIT_CODAL
|
|
|
|
return uBit.audio.getVolume();
|
|
|
|
#else
|
|
|
|
return pins::analogPitchVolume();
|
|
|
|
#endif
|
|
|
|
}
|
2019-12-02 05:58:26 +01:00
|
|
|
}
|