Read default volume from device settings at start (#987)
This commit is contained in:
parent
40aaf0fb18
commit
a157943bf7
@ -12,4 +12,5 @@ music.setTempo(120)
|
|||||||
music.noteFrequency(Note.C)
|
music.noteFrequency(Note.C)
|
||||||
music.beat()
|
music.beat()
|
||||||
music.setVolume(50)
|
music.setVolume(50)
|
||||||
```
|
music.volume()
|
||||||
|
```
|
||||||
|
@ -13,7 +13,22 @@
|
|||||||
|
|
||||||
namespace music {
|
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;
|
uint8_t *lmsSoundMMap;
|
||||||
|
|
||||||
int writeDev(void *data, int size) {
|
int writeDev(void *data, int size) {
|
||||||
@ -37,6 +52,18 @@ void setVolume(int volume) {
|
|||||||
currVolume = max(0, min(100, 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_BREAK 0
|
||||||
#define SOUND_CMD_TONE 1
|
#define SOUND_CMD_TONE 1
|
||||||
#define SOUND_CMD_PLAY 2
|
#define SOUND_CMD_PLAY 2
|
||||||
@ -218,4 +245,4 @@ Buffer buffer(Sound snd) {
|
|||||||
void play(Sound snd) {
|
void play(Sound snd) {
|
||||||
music::playSample(snd);
|
music::playSample(snd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
10
libs/music/shims.d.ts
vendored
10
libs/music/shims.d.ts
vendored
@ -13,6 +13,16 @@ declare namespace music {
|
|||||||
//% weight=1 shim=music::setVolume
|
//% weight=1 shim=music::setVolume
|
||||||
function setVolume(volume: int32): void;
|
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.
|
* 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
|
* @param frequency pitch of the tone to play in Hertz (Hz), eg: Note.C
|
||||||
|
@ -5,3 +5,4 @@ music.playTone(1440, 500)
|
|||||||
pause(500)
|
pause(500)
|
||||||
music.playTone(2440, 500)
|
music.playTone(2440, 500)
|
||||||
pause(500)
|
pause(500)
|
||||||
|
music.volume()
|
||||||
|
@ -7,6 +7,14 @@ namespace pxsim.music {
|
|||||||
export function stopAllSounds() {
|
export function stopAllSounds() {
|
||||||
SoundMethods.stop()
|
SoundMethods.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pxsim.music.setVolume = (volume: number): void => {
|
||||||
|
pxsim.getAudioState().volume = volume;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function volume() {
|
||||||
|
return pxsim.getAudioState().volume;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace pxsim.SoundMethods {
|
namespace pxsim.SoundMethods {
|
||||||
|
Loading…
Reference in New Issue
Block a user