diff --git a/libs/music/docs/reference/music.md b/libs/music/docs/reference/music.md index b722fe1c..142e2a34 100644 --- a/libs/music/docs/reference/music.md +++ b/libs/music/docs/reference/music.md @@ -12,4 +12,5 @@ music.setTempo(120) music.noteFrequency(Note.C) music.beat() music.setVolume(50) -``` \ No newline at end of file +music.volume() +``` diff --git a/libs/music/music.cpp b/libs/music/music.cpp index c3f4e513..13d03e14 100644 --- a/libs/music/music.cpp +++ b/libs/music/music.cpp @@ -13,7 +13,22 @@ namespace music { -uint8_t currVolume = 50; +int _readSystemVolume() { + char ParBuf[8]; + int volume; + + int fd = open("../sys/settings/Volume.rtf", O_RDONLY); + read(fd, ParBuf, sizeof(ParBuf)); + close(fd); + if (sscanf(ParBuf,"%d",&volume) > 0) { + if ((volume >= 0) && (volume <= 100)) { + return volume; + } + } + return 50; +} + +uint8_t currVolume = _readSystemVolume(); uint8_t *lmsSoundMMap; int writeDev(void *data, int size) { @@ -37,6 +52,18 @@ void setVolume(int volume) { currVolume = max(0, min(100, volume)); } +/** +* Return the output volume of the sound synthesizer. +*/ +//% weight=96 +//% blockId=synth_get_volume block="volume" +//% parts="speaker" blockGap=8 +//% help=music/volume +//% weight=1 +int volume() { + return currVolume; +} + #define SOUND_CMD_BREAK 0 #define SOUND_CMD_TONE 1 #define SOUND_CMD_PLAY 2 @@ -218,4 +245,4 @@ Buffer buffer(Sound snd) { void play(Sound snd) { music::playSample(snd); } -} \ No newline at end of file +} diff --git a/libs/music/shims.d.ts b/libs/music/shims.d.ts index 2e9fb0e4..26e4a7c4 100644 --- a/libs/music/shims.d.ts +++ b/libs/music/shims.d.ts @@ -13,6 +13,16 @@ declare namespace music { //% weight=1 shim=music::setVolume function setVolume(volume: int32): void; + /** + * Return the output volume of the sound synthesizer. + */ + //% weight=96 + //% blockId=synth_get_volume block="volume" + //% parts="speaker" blockGap=8 + //% help=music/volume + //% weight=1 shim=music::volume + function volume(): int32; + /** * Play a tone through the speaker for some amount of time. * @param frequency pitch of the tone to play in Hertz (Hz), eg: Note.C diff --git a/libs/music/test.ts b/libs/music/test.ts index c701b46f..60960599 100644 --- a/libs/music/test.ts +++ b/libs/music/test.ts @@ -5,3 +5,4 @@ music.playTone(1440, 500) pause(500) music.playTone(2440, 500) pause(500) +music.volume() diff --git a/sim/state/sounds.ts b/sim/state/sounds.ts index 82ee4233..f2e6eb28 100644 --- a/sim/state/sounds.ts +++ b/sim/state/sounds.ts @@ -7,6 +7,14 @@ namespace pxsim.music { export function stopAllSounds() { SoundMethods.stop() } + + pxsim.music.setVolume = (volume: number): void => { + pxsim.getAudioState().volume = volume; + }; + + export function volume() { + return pxsim.getAudioState().volume; + } } namespace pxsim.SoundMethods {