From b65675615912f41c355523d17cf28c3bf8eadb80 Mon Sep 17 00:00:00 2001 From: Mateusz Piesta Date: Tue, 9 Nov 2021 08:43:56 +0100 Subject: [PATCH] [BH-1117] Bell audio service Added Bell audio service Added volume scaling. --- image/user/db/settings_bell_002.sql | 4 - module-audio/Audio/AudioCommon.hpp | 7 +- module-audio/Audio/AudioMux.cpp | 9 +- .../board/rt1051/bellpx/BellPxAudioCodec.cpp | 5 +- .../rt1051/bellpx/bsp/audio/AW8898driver.cpp | 4 +- .../rt1051/bellpx/bsp/audio/CodecAW8898.cpp | 41 ++- .../rt1051/bellpx/bsp/audio/CodecAW8898.hpp | 26 -- products/BellHybrid/BellHybridMain.cpp | 4 +- products/BellHybrid/CMakeLists.txt | 4 +- products/BellHybrid/alarms/CMakeLists.txt | 2 +- .../alarms/include/BellAlarmHandler.hpp | 1 - .../alarms/src/actions/PlayAudioActions.cpp | 14 +- .../alarms/src/actions/PlayAudioActions.hpp | 3 +- .../ApplicationBellBackgroundSounds.cpp | 19 +- .../CMakeLists.txt | 2 +- .../data/BGSoundsCommon.hpp | 1 - .../ApplicationBellBackgroundSounds.hpp | 2 + .../presenter/BGSoundsProgressPresenter.cpp | 5 +- .../presenter/BGSoundsProgressPresenter.hpp | 9 +- .../BGSoundsTimerSelectPresenter.hpp | 3 +- .../presenter/BGSoundsVolumePresenter.cpp | 19 +- .../presenter/BGSoundsVolumePresenter.hpp | 45 +-- .../widgets/BGSoundsPlayer.cpp | 114 ++----- .../widgets/BGSoundsPlayer.hpp | 57 ++-- .../windows/BGSoundsProgressWindow.cpp | 12 +- .../windows/BGSoundsVolumeWindow.cpp | 49 +-- .../windows/BGSoundsVolumeWindow.hpp | 15 +- .../ApplicationBellBedtime.cpp | 2 +- .../ApplicationBellMain.cpp | 1 - .../CMakeLists.txt | 1 + .../windows/MeditationRunningWindow.cpp | 7 +- .../ApplicationBellPowerNap.cpp | 1 - .../presenter/PowerNapProgressPresenter.cpp | 14 +- .../presenter/PowerNapProgressPresenter.hpp | 1 - .../ApplicationBellSettings.cpp | 12 +- .../application-bell-settings/CMakeLists.txt | 1 + .../alarm_settings/AlarmSettingsModel.cpp | 13 +- .../alarm_settings/AlarmSettingsModel.hpp | 11 +- .../alarm_settings/PrewakeUpSettingsModel.cpp | 13 +- .../alarm_settings/PrewakeUpSettingsModel.hpp | 11 +- .../alarm_settings/SnoozeSettingsModel.cpp | 12 +- .../alarm_settings/SnoozeSettingsModel.hpp | 10 +- .../presenter/BedtimeSettingsPresenter.cpp | 26 +- .../presenter/BedtimeSettingsPresenter.hpp | 3 +- .../alarm_settings/AlarmSettingsPresenter.cpp | 24 +- .../alarm_settings/AlarmSettingsPresenter.hpp | 3 +- .../alarm_settings/PrewakeUpPresenter.cpp | 27 +- .../alarm_settings/PrewakeUpPresenter.hpp | 3 +- .../alarm_settings/SnoozePresenter.cpp | 25 +- .../alarm_settings/SnoozePresenter.hpp | 4 +- .../windows/BellSettingsBedtimeToneWindow.cpp | 3 + .../BellSettingsAlarmSettingsSnoozeWindow.cpp | 5 + .../BellSettingsAlarmSettingsWindow.cpp | 3 + .../BellSettingsPrewakeUpWindow.cpp | 3 + .../BellHybrid/apps/common/CMakeLists.txt | 1 + .../common/models/AbstractAudioModel.hpp | 33 +- .../common/models/AbstractSettingsModel.hpp | 44 +++ .../include/common/models/AudioModel.hpp | 22 +- .../include/common/models/BedtimeModel.hpp | 20 +- .../BellHybrid/apps/common/src/AudioModel.cpp | 161 ++++++++- .../apps/common/src/models/BedtimeModel.cpp | 18 +- .../src/popups/BedtimeNotificationWindow.cpp | 6 +- products/BellHybrid/services/CMakeLists.txt | 1 + .../BellHybrid/services/audio/CMakeLists.txt | 33 ++ .../services/audio/ServiceAudio.cpp | 307 ++++++++++++++++++ .../audio/include/audio/AudioMessage.hpp | 141 ++++++++ .../audio/include/audio/ServiceAudio.hpp | 87 +++++ .../services/db/include/db/SystemSettings.hpp | 4 - .../services/evtmgr/WorkerEvent.cpp | 1 - 69 files changed, 1119 insertions(+), 475 deletions(-) create mode 100644 products/BellHybrid/services/audio/CMakeLists.txt create mode 100644 products/BellHybrid/services/audio/ServiceAudio.cpp create mode 100644 products/BellHybrid/services/audio/include/audio/AudioMessage.hpp create mode 100644 products/BellHybrid/services/audio/include/audio/ServiceAudio.hpp diff --git a/image/user/db/settings_bell_002.sql b/image/user/db/settings_bell_002.sql index 0e250a12e22bb77c3ecf0efed79a4186dd11fc26..f411e4594ae5da84e5dcf1d537b1b9a111be50c7 100644 --- a/image/user/db/settings_bell_002.sql +++ b/image/user/db/settings_bell_002.sql @@ -35,20 +35,16 @@ INSERT OR IGNORE INTO settings_tab (path, value) VALUES ('snooze_length','10'), ('snooze_interval','1'), ('snooze_tone','Gentle Chime'), - ('snooze_volume','5'), ('prewake_up_duration', '10'), ('prewake_up_tone','Joyful Awakening'), - ('prewake_up_volume','5'), ('prewake_up_light_duration','10'), ('alarm_tone','Gentle Daybreak'), - ('alarm_volume','5'), ('alarm_light_active','1'), ('alarm_duration','10000'), ('bedtime_active','0'), ('bedtime_time','21:00'), ('bedtime_tone','Evening Horizon'), ('bedtime_duration','5'), - ('bedtime_volume','1'), ('\ServiceEink\\display_inverted_mode', '0'); diff --git a/module-audio/Audio/AudioCommon.hpp b/module-audio/Audio/AudioCommon.hpp index cf590b3e8d0cf067610191a1d1933c42eb4456ad..26819600553b1304172e981ccb5ecee0be695a86 100644 --- a/module-audio/Audio/AudioCommon.hpp +++ b/module-audio/Audio/AudioCommon.hpp @@ -66,7 +66,10 @@ namespace audio TextMessageRingtone, Meditation, Alarm, - Last = Alarm, + PreWakeUp, + Snooze, + Bedtime, + Last = Bedtime, }; /// Used to describe audio operations @@ -174,7 +177,7 @@ namespace audio std::bitset()> audioSinkState; }; - enum class RetCode + enum class RetCode : std::uint8_t { Success = 0, InvokedInIncorrectState, diff --git a/module-audio/Audio/AudioMux.cpp b/module-audio/Audio/AudioMux.cpp index a65b2f9bf0366bf7c0d25119e1fe44dc9d10e055..b065f2cd100db5d0ffd4a926b4c894f0e41fa13e 100644 --- a/module-audio/Audio/AudioMux.cpp +++ b/module-audio/Audio/AudioMux.cpp @@ -163,20 +163,17 @@ namespace audio { switch (type) { case PlaybackType::None: - [[fallthrough]]; case PlaybackType::Notifications: - [[fallthrough]]; case PlaybackType::KeypadSound: - [[fallthrough]]; case PlaybackType::TextMessageRingtone: return true; case PlaybackType::CallRingtone: - [[fallthrough]]; case PlaybackType::Meditation: - [[fallthrough]]; case PlaybackType::Alarm: - [[fallthrough]]; case PlaybackType::Multimedia: + case PlaybackType::Bedtime: + case PlaybackType::PreWakeUp: + case PlaybackType::Snooze: return false; } return false; diff --git a/module-audio/board/rt1051/bellpx/BellPxAudioCodec.cpp b/module-audio/board/rt1051/bellpx/BellPxAudioCodec.cpp index bda9b1583ecbf9293bd7147556fdbfe99c45cf1e..d0b32ff9dea5eccfedb147397a816f8867fc5240 100644 --- a/module-audio/board/rt1051/bellpx/BellPxAudioCodec.cpp +++ b/module-audio/board/rt1051/bellpx/BellPxAudioCodec.cpp @@ -3,7 +3,6 @@ #include "BellPxAudioCodec.hpp" #include "board.h" -#include "dma_config.h" #include #include "board/BoardDefinitions.hpp" @@ -104,7 +103,7 @@ namespace audio AudioDevice::RetCode BellPxAudioCodec::setOutputVolume(float vol) { currentFormat.outputVolume = vol; - CodecParams params; + CodecParamsAW8898 params; params.outVolume = vol; params.opCmd = CodecParams::Cmd::SetOutVolume; codec.Ioctrl(params); @@ -114,7 +113,7 @@ namespace audio AudioDevice::RetCode BellPxAudioCodec::setInputGain(float gain) { currentFormat.inputGain = gain; - CodecParams params; + CodecParamsAW8898 params; params.inGain = gain; params.opCmd = CodecParams::Cmd::SetInGain; codec.Ioctrl(params); diff --git a/module-bsp/board/rt1051/bellpx/bsp/audio/AW8898driver.cpp b/module-bsp/board/rt1051/bellpx/bsp/audio/AW8898driver.cpp index 4e76564c3315c3edf172c802bf0766bf2e6c8bb4..376b106026f9feaa5c9e9d60e0418fc169192a34 100644 --- a/module-bsp/board/rt1051/bellpx/bsp/audio/AW8898driver.cpp +++ b/module-bsp/board/rt1051/bellpx/bsp/audio/AW8898driver.cpp @@ -469,7 +469,7 @@ namespace bsp::audio::AW8898 return res; } - status_t GetVolume(std::uint8_t *gian) + status_t GetVolume(std::uint8_t *gain) { status_t res = kStatus_Success; std::uint16_t reg = 0; @@ -480,7 +480,7 @@ namespace bsp::audio::AW8898 return res; } - *gian = reg >> 8; + *gain = reg >> 8; return res; } diff --git a/module-bsp/board/rt1051/bellpx/bsp/audio/CodecAW8898.cpp b/module-bsp/board/rt1051/bellpx/bsp/audio/CodecAW8898.cpp index bdb06e908a72377a24793f6302e122904053ff57..ebd1df9d187ff6a07b7385cb9cc5515521b366b7 100644 --- a/module-bsp/board/rt1051/bellpx/bsp/audio/CodecAW8898.cpp +++ b/module-bsp/board/rt1051/bellpx/bsp/audio/CodecAW8898.cpp @@ -21,10 +21,23 @@ using namespace bsp::audio; namespace { - constexpr auto ReadStatusRetries = 5; - constexpr auto OneByteAddressing = 1; - constexpr auto PositiveLogic = 0; - constexpr auto defaultVolumeLevel = 3.0f; + constexpr auto ReadStatusRetries = 5; + constexpr auto OneByteAddressing = 1; + constexpr auto PositiveLogic = 0; + constexpr auto maxInVolume = 10; + constexpr auto minInVolume = 0; + + /// Higher layers operate using 0-10 range. Here we are transforming it into more usable one. + /// Anything above 9 is going to distort the speaker and values below 5 are too quiet. + constexpr float transformVolumeLvl(float value) + { + constexpr auto maxOutVolume = 9; + constexpr auto minOutVolume = 5; + constexpr auto inputRange = std::make_pair(minInVolume, maxInVolume); + constexpr auto outputRange = std::make_pair(minOutVolume, maxOutVolume); + constexpr float slope = 1.0 * (outputRange.second - outputRange.first) / (inputRange.second - inputRange.first); + return outputRange.first + slope * (value - inputRange.first); + } } // namespace CodecAW8898::CodecAW8898() @@ -83,10 +96,7 @@ CodecRetCode CodecAW8898::Start(const CodecParams ¶m) // Store param configuration currentParams = params; - auto currVol = currentParams.outVolume; - // remove this once volume control in application is working - currVol = defaultVolumeLevel; - SetOutputVolume(currVol); + SetOutputVolume(currentParams.outVolume); AW8898::ReadAllReg(); @@ -115,10 +125,8 @@ CodecRetCode CodecAW8898::Stop() CodecRetCode CodecAW8898::Ioctrl(const CodecParams ¶m) { - const CodecParamsAW8898 ¶ms = static_cast(param); - - CodecRetCode ret = CodecRetCode::Success; + CodecRetCode ret = CodecRetCode::Success; switch (params.opCmd) { case CodecParamsAW8898::Cmd::SetOutVolume: @@ -152,14 +160,13 @@ CodecRetCode CodecAW8898::Ioctrl(const CodecParams ¶m) std::uint8_t CodecAW8898::VolumeTo8Bit(const float vol) { - static constexpr auto maxVolumeLevel = 10.0f; - static constexpr auto conversionRatio = static_cast(UINT8_MAX) / maxVolumeLevel; + static constexpr auto conversionRatio = static_cast(UINT8_MAX) / maxInVolume; if (vol < 0) { return 0; } - if (vol > maxVolumeLevel) { + if (vol > maxInVolume) { return UINT8_MAX; } @@ -168,12 +175,10 @@ std::uint8_t CodecAW8898::VolumeTo8Bit(const float vol) CodecRetCode CodecAW8898::SetOutputVolume(const float vol) { - uint8_t mute = 0; - // If volume set to 0 then mute output AW8898::RunMute(vol == 0); //(PWMCTRL.HMUTE=0) - disable mute - - AW8898::SetVolume(VolumeTo8Bit(vol)); + // AW8898 has "negative" gain control - higher the absolute value, lower the volume + AW8898::SetVolume(UINT8_MAX - VolumeTo8Bit(transformVolumeLvl(vol))); currentParams.outVolume = vol; return CodecRetCode::Success; } diff --git a/module-bsp/board/rt1051/bellpx/bsp/audio/CodecAW8898.hpp b/module-bsp/board/rt1051/bellpx/bsp/audio/CodecAW8898.hpp index 9e5e50ce5b0f63fd711c11362ebae668108cfb9e..2c7fc8d098a16aba6483ed644d8830394f90a929 100644 --- a/module-bsp/board/rt1051/bellpx/bsp/audio/CodecAW8898.hpp +++ b/module-bsp/board/rt1051/bellpx/bsp/audio/CodecAW8898.hpp @@ -17,29 +17,6 @@ extern "C" class CodecParamsAW8898 : public CodecParams { public: - enum class Cmd - { - SetOutVolume, - SetInGain, - SetMute, - SetOutput, - SetInput, - Reset, - MicBiasCtrl, - None - }; - - enum class SampleRate - { - Rate8KHz = 8000, - Rate16KHz = 16000, - Rate44K1Hz = 44100, - Rate48KHz = 48000, - Rate32KHz = 32000, - Rate96KHz = 96000, - Invalid - }; - enum class MonoStereo { Left, @@ -97,10 +74,7 @@ class CodecParamsAW8898 : public CodecParams } } - Cmd opCmd = Cmd::None; - float outVolume = 0; MonoStereo monoStereo = MonoStereo::Mono; - SampleRate sampleRate = SampleRate ::Rate44K1Hz; }; class CodecAW8898 : public Codec diff --git a/products/BellHybrid/BellHybridMain.cpp b/products/BellHybrid/BellHybridMain.cpp index e867e8b1a9eaa515f7b0053424635aa0a00f6a18..ffe8c833cfa6bbc61fd6295ae4d86e72f310d583 100644 --- a/products/BellHybrid/BellHybridMain.cpp +++ b/products/BellHybrid/BellHybridMain.cpp @@ -19,11 +19,11 @@ // services #include +#include