From b26cf289c3b18ece2ae5850ba916cfa2cfff3fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Mrozi=C5=84ski?= Date: Wed, 9 Sep 2020 08:53:33 +0200 Subject: [PATCH] Change sound volume to level (#986) --- libs/music/music.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/libs/music/music.cpp b/libs/music/music.cpp index 2557a65a..c3f4e513 100644 --- a/libs/music/music.cpp +++ b/libs/music/music.cpp @@ -45,7 +45,7 @@ void setVolume(int volume) { struct ToneCmd { uint8_t cmd; - uint8_t vol; + uint8_t lvl; uint16_t freq; uint16_t duration; }; @@ -55,10 +55,26 @@ static void _stopSound() { writeDev(&cmd, sizeof(cmd)); } +static uint8_t _getVolumeLevel(uint8_t volume, uint8_t levels) { + uint8_t level; + uint8_t step = (uint8_t) (100 / (levels - 1)); + if (volume < step) { + level = 0; + } else if (volume > step * (levels - 1)) { + level = levels; + } else { + level = (uint8_t) (volume / step); + } + return level; +} + static void _playTone(uint16_t frequency, uint16_t duration, uint8_t volume) { + // https://github.com/mindboards/ev3sources/blob/78ebaf5b6f8fe31cc17aa5dce0f8e4916a4fc072/lms2012/c_sound/source/c_sound.c#L471 + uint8_t level = _getVolumeLevel(volume, 13); + ToneCmd cmd; cmd.cmd = SOUND_CMD_TONE; - cmd.vol = volume; + cmd.lvl = level; cmd.freq = frequency; cmd.duration = duration; // (*SoundInstance.pSound).Busy = TRUE; @@ -122,7 +138,8 @@ void playSample(Buffer buf) { stopUser(); pthread_mutex_lock(&pumpMutex); *lmsSoundMMap = 1; // BUSY - uint8_t cmd[] = {SOUND_CMD_PLAY, (uint8_t)((currVolume / 33) + 1)}; + // https://github.com/mindboards/ev3sources/blob/78ebaf5b6f8fe31cc17aa5dce0f8e4916a4fc072/lms2012/c_sound/source/c_sound.c#L605 + uint8_t cmd[] = {SOUND_CMD_PLAY, _getVolumeLevel(currVolume, 8)}; writeDev(cmd, 2); decrRC(currentSample); currentSample = buf;