From 75a0a758468d54b16bd644ba0f1de5f1e5f2b73c Mon Sep 17 00:00:00 2001 From: Tigran Soghbatyan Date: Wed, 17 Nov 2021 13:35:33 +0100 Subject: [PATCH] [BH-1195] Fix audio playback while volume change Prevent audio restart on volume setting change --- .../ApplicationBellBackgroundSounds.cpp | 8 ++--- .../ApplicationBellBackgroundSounds.hpp | 1 + .../widgets/BGSoundsPlayer.cpp | 12 ++++---- .../widgets/BGSoundsPlayer.hpp | 2 -- .../ApplicationBellBedtime.cpp | 2 +- .../ApplicationBellPowerNap.cpp | 10 ++++--- .../ApplicationBellPowerNap.hpp | 4 +++ .../presenter/PowerNapProgressPresenter.cpp | 8 ++--- .../presenter/PowerNapProgressPresenter.hpp | 4 +-- .../ApplicationBellSettings.cpp | 21 +++++++------- .../ApplicationBellSettings.hpp | 4 +++ .../presenter/BedtimeSettingsPresenter.cpp | 18 +++++++----- .../presenter/BedtimeSettingsPresenter.hpp | 4 +-- .../alarm_settings/AlarmSettingsPresenter.cpp | 10 +++---- .../alarm_settings/AlarmSettingsPresenter.hpp | 4 +-- .../alarm_settings/PrewakeUpPresenter.cpp | 18 +++++++----- .../alarm_settings/PrewakeUpPresenter.hpp | 4 +-- .../alarm_settings/SnoozePresenter.cpp | 18 +++++++----- .../alarm_settings/SnoozePresenter.hpp | 4 +-- .../common/models/AbstractAudioModel.hpp | 13 +++++---- .../include/common/models/AudioModel.hpp | 5 ++++ .../include/common/models/BedtimeModel.hpp | 5 ++-- .../BellHybrid/apps/common/src/AudioModel.cpp | 29 ++++++++++++++++++- 23 files changed, 125 insertions(+), 83 deletions(-) diff --git a/products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp b/products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp index 56cc8dbbf20572ce52b571642ee5386ca4d625dd..337249b04a0ece008b56a40584b33714336fa0df 100644 --- a/products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp +++ b/products/BellHybrid/apps/application-bell-background-sounds/ApplicationBellBackgroundSounds.cpp @@ -27,14 +27,10 @@ namespace app StartInBackground startInBackground, uint32_t stackDepth) : Application(std::move(name), std::move(parent), statusIndicators, startInBackground, stackDepth), - audioModel{std::make_unique(this)}, player{ - std::make_unique(*audioModel)} + audioModel{std::make_unique(this)} { + player = std::make_unique(*audioModel); bus.channels.push_back(sys::BusChannel::ServiceAudioNotifications); - connect(typeid(service::AudioEOFNotification), [&](sys::Message *msg) -> sys::MessagePointer { - auto notification = static_cast(msg); - return player->handle(notification); - }); } ApplicationBellBackgroundSounds::~ApplicationBellBackgroundSounds() = default; diff --git a/products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp b/products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp index 6062f7cb0ac219c1870bacaaff66bcc13ced6fb2..0ab0e5732062e860c8eb3dfe265a59c864d710b3 100644 --- a/products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp +++ b/products/BellHybrid/apps/application-bell-background-sounds/include/application-bell-background-sounds/ApplicationBellBackgroundSounds.hpp @@ -22,6 +22,7 @@ namespace app class ApplicationBellBackgroundSounds : public Application { + private: std::unique_ptr audioModel; std::unique_ptr player; diff --git a/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.cpp b/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.cpp index dde46ef59f6d1bc5fee84a5c4e0a0abad4beba42..c999fc19cf6a6c724b4a51ae836193f9f8a0755b 100644 --- a/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.cpp +++ b/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.cpp @@ -6,13 +6,6 @@ namespace app::bgSounds { - auto BGSoundsPlayer::handle(service::AudioEOFNotification *msg) -> std::shared_ptr - { - if (playbackMode == PlaybackMode::Looped) { - audioModel.play(recentFilePath, AbstractAudioModel::PlaybackType::Multimedia, {}); - } - return sys::msgHandled(); - } AbstractBGSoundsPlayer::PlaybackMode BGSoundsPlayer::getCurrentMode() const noexcept { return playbackMode; @@ -26,6 +19,11 @@ namespace app::bgSounds recentFilePath = filePath; playbackMode = mode; audioModel.play(filePath, AbstractAudioModel::PlaybackType::Multimedia, std::move(callback)); + audioModel.setPlaybackFinishedCb([&]() { + if (playbackMode == PlaybackMode::Looped) { + audioModel.play(recentFilePath, AbstractAudioModel::PlaybackType::Multimedia, {}); + } + }); } void BGSoundsPlayer::stop(AbstractAudioModel::OnStateChangeCallback &&callback) { diff --git a/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.hpp b/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.hpp index 317e349139d96b38766a80f724fdd574bb469483..1af846295add9516e83c849944b5fa14bf8f6d3c 100644 --- a/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.hpp +++ b/products/BellHybrid/apps/application-bell-background-sounds/widgets/BGSoundsPlayer.hpp @@ -41,8 +41,6 @@ namespace app::bgSounds public: explicit BGSoundsPlayer(AbstractAudioModel &audioModel); - auto handle(service::AudioEOFNotification *msg) -> std::shared_ptr; - private: void start(const std::string &filePath, PlaybackMode mode, diff --git a/products/BellHybrid/apps/application-bell-bedtime/ApplicationBellBedtime.cpp b/products/BellHybrid/apps/application-bell-bedtime/ApplicationBellBedtime.cpp index b0615bddd7616cf766e6666b05580dc35fb9dc49..05b4f3ef06d02d6c50e80f786af4a41200fff792 100644 --- a/products/BellHybrid/apps/application-bell-bedtime/ApplicationBellBedtime.cpp +++ b/products/BellHybrid/apps/application-bell-bedtime/ApplicationBellBedtime.cpp @@ -34,7 +34,7 @@ namespace app { windowsFactory.attach(gui::name::window::main_window, [](ApplicationCommon *app, const std::string &) { auto audioModel = std::make_unique(app); - auto bedtimeModel = std::make_unique(app, std::move(audioModel)); + auto bedtimeModel = std::make_unique(app, *audioModel); auto provider = std::make_shared(std::move(bedtimeModel)); auto presenter = std::make_unique(provider); return std::make_unique(app, std::move(presenter)); diff --git a/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp b/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp index 7560fc528fb4012cf1df68316dbdc0523c4ecc28..82d202e9911f60138c582ed16af688b71a149333 100644 --- a/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp +++ b/products/BellHybrid/apps/application-bell-powernap/ApplicationBellPowerNap.cpp @@ -18,8 +18,11 @@ namespace app StatusIndicators statusIndicators, StartInBackground startInBackground, uint32_t stackDepth) - : Application(std::move(name), std::move(parent), statusIndicators, startInBackground, stackDepth) - {} + : Application(std::move(name), std::move(parent), statusIndicators, startInBackground, stackDepth), + audioModel{std::make_unique(this)} + { + bus.channels.push_back(sys::BusChannel::ServiceAudioNotifications); + } ApplicationBellPowerNap::~ApplicationBellPowerNap() = default; sys::ReturnCodes ApplicationBellPowerNap::InitHandler() @@ -42,10 +45,9 @@ namespace app windowsFactory.attach( gui::window::name::powernapProgress, [this](ApplicationCommon *app, const std::string &name) { auto timeModel = std::make_unique(); - auto audioModel = std::make_unique(app); auto soundsRepository = std::make_unique(alarms::paths::getAlarmDir()); auto presenter = std::make_unique( - app, settings.get(), std::move(soundsRepository), std::move(audioModel), std::move(timeModel)); + app, settings.get(), std::move(soundsRepository), *audioModel, std::move(timeModel)); return std::make_unique(app, std::move(presenter)); }); windowsFactory.attach(gui::window::name::powernapSessionEnded, diff --git a/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp b/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp index 56c9717974cdc5cc382dbf634fe1f6afa0fca7f4..54d012ac6f4f009ece11a54775ca84d01001ec81 100644 --- a/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp +++ b/products/BellHybrid/apps/application-bell-powernap/include/application-bell-powernap/ApplicationBellPowerNap.hpp @@ -4,6 +4,7 @@ #pragma once #include +#include namespace gui::window::name { @@ -21,6 +22,9 @@ namespace app class ApplicationBellPowerNap : public Application { + private: + std::unique_ptr audioModel; + public: ApplicationBellPowerNap(std::string name = applicationBellPowerNapName, std::string parent = "", diff --git a/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp b/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp index 7a92c98b144eb1f567ae201035ecc63a3ef0092a..38b4f2ffa42f8024199cde24c380201740c95fb6 100644 --- a/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp +++ b/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.cpp @@ -25,10 +25,10 @@ namespace app::powernap PowerNapProgressPresenter::PowerNapProgressPresenter(app::ApplicationCommon *app, settings::Settings *settings, std::unique_ptr soundsRepository, - std::unique_ptr audioModel, + AbstractAudioModel &audioModel, std::unique_ptr timeModel) : app{app}, settings{settings}, soundsRepository{std::move(soundsRepository)}, - audioModel{std::move(audioModel)}, timeModel{std::move(timeModel)}, + audioModel{audioModel}, timeModel{std::move(timeModel)}, napAlarmTimer{sys::TimerFactory::createSingleShotTimer( app, powernapAlarmTimerName, powernapAlarmTimeout, [this](sys::Timer &) { onNapAlarmFinished(); })} @@ -60,13 +60,13 @@ namespace app::powernap const auto filePath = soundsRepository->titleToPath( settings->getValue(bell::settings::Alarm::tone, settings::SettingsScope::Global)); - audioModel->play(filePath.value_or(""), AbstractAudioModel::PlaybackType::Alarm, {}); + audioModel.play(filePath.value_or(""), AbstractAudioModel::PlaybackType::Alarm, {}); napAlarmTimer.start(); napFinished = true; } void PowerNapProgressPresenter::onNapAlarmFinished() { - audioModel->stop({}); + audioModel.stop({}); getView()->napEnded(); } diff --git a/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.hpp b/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.hpp index c8ae5bd7c82c79a59de59e9dd342b027e6e77978..0a8b62a2b487978be9f5af65954c765f621f44db 100644 --- a/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.hpp +++ b/products/BellHybrid/apps/application-bell-powernap/presenter/PowerNapProgressPresenter.hpp @@ -54,7 +54,7 @@ namespace app::powernap app::ApplicationCommon *app{}; settings::Settings *settings{}; std::unique_ptr soundsRepository; - std::unique_ptr audioModel; + AbstractAudioModel &audioModel; std::unique_ptr timer; std::unique_ptr timeModel; sys::TimerHandle napAlarmTimer; @@ -73,7 +73,7 @@ namespace app::powernap PowerNapProgressPresenter(app::ApplicationCommon *app, settings::Settings *settings, std::unique_ptr soundsRepository, - std::unique_ptr audioModel, + AbstractAudioModel &audioModel, std::unique_ptr timeModel); }; } // namespace app::powernap diff --git a/products/BellHybrid/apps/application-bell-settings/ApplicationBellSettings.cpp b/products/BellHybrid/apps/application-bell-settings/ApplicationBellSettings.cpp index 7f822c43fc38fe3116749c695dd6511412401b23..6b1b588cc412fba81cd1e2e7e5d5b9cb7a088c47 100644 --- a/products/BellHybrid/apps/application-bell-settings/ApplicationBellSettings.cpp +++ b/products/BellHybrid/apps/application-bell-settings/ApplicationBellSettings.cpp @@ -48,8 +48,11 @@ namespace app StatusIndicators statusIndicators, StartInBackground startInBackground, uint32_t stackDepth) - : Application(std::move(name), std::move(parent), statusIndicators, startInBackground, stackDepth) - {} + : Application(std::move(name), std::move(parent), statusIndicators, startInBackground, stackDepth), + audioModel{std::make_unique(this)} + { + bus.channels.push_back(sys::BusChannel::ServiceAudioNotifications); + } sys::ReturnCodes ApplicationBellSettings::InitHandler() { @@ -113,14 +116,13 @@ namespace app windowsFactory.attach( gui::window::name::bellSettingsBedtimeTone, [this](ApplicationCommon *app, const std::string &name) { - auto audioModel = std::make_unique(app); - auto bedtimeModel = std::make_shared(app, std::move(audioModel)); + auto bedtimeModel = std::make_shared(app, *audioModel); auto soundsRepository = std::make_unique(alarms::paths::getBedtimeReminderChimesDir()); auto provider = std::make_shared( bedtimeModel, soundsRepository->getSongTitles()); auto presenter = std::make_unique( - provider, bedtimeModel, std::move(audioModel), std::move(soundsRepository)); + provider, bedtimeModel, *audioModel, std::move(soundsRepository)); return std::make_unique(app, std::move(presenter)); }); @@ -144,7 +146,6 @@ namespace app windowsFactory.attach( gui::BellSettingsPrewakeUpWindow::name, [this](ApplicationCommon *app, const std::string &name) { - auto audioModel = std::make_unique(this); auto chimeDurationModel = std::make_unique(this); auto chimeToneModel = std::make_unique(this); auto chimeVolumeModel = std::make_unique(*audioModel); @@ -158,7 +159,7 @@ namespace app auto provider = std::make_shared( *prewakeUpSettingsModel, soundsRepository->getSongTitles()); auto presenter = std::make_unique( - provider, std::move(prewakeUpSettingsModel), std::move(audioModel), std::move(soundsRepository)); + provider, std::move(prewakeUpSettingsModel), *audioModel, std::move(soundsRepository)); return std::make_unique(app, std::move(presenter)); }); @@ -169,7 +170,6 @@ namespace app }); windowsFactory.attach( gui::BellSettingsAlarmSettingsSnoozeWindow::name, [this](ApplicationCommon *app, const std::string &) { - auto audioModel = std::make_unique(this); auto snoozeOnOffModel = std::make_unique(this); auto snoozeLengthModel = std::make_unique(this); auto snoozeChimeIntervalModel = std::make_unique(this); @@ -185,12 +185,11 @@ namespace app auto provider = std::make_shared( *snoozeSettingsModel, soundsRepository->getSongTitles()); auto presenter = std::make_unique( - provider, std::move(snoozeSettingsModel), std::move(audioModel), std::move(soundsRepository)); + provider, std::move(snoozeSettingsModel), *audioModel, std::move(soundsRepository)); return std::make_unique(app, std::move(presenter)); }); windowsFactory.attach( gui::BellSettingsAlarmSettingsWindow::name, [this](ApplicationCommon *app, const std::string &) { - auto audioModel = std::make_unique(this); auto alarmToneModel = std::make_unique(this); auto alarmVolumeModel = std::make_unique(*audioModel); auto alarmLightOnOffModel = std::make_unique(this); @@ -200,7 +199,7 @@ namespace app auto provider = std::make_shared( *alarmSettingsModel, soundsRepository->getSongTitles()); auto presenter = std::make_unique( - provider, std::move(alarmSettingsModel), std::move(audioModel), std::move(soundsRepository)); + provider, std::move(alarmSettingsModel), *audioModel, std::move(soundsRepository)); return std::make_unique(app, std::move(presenter)); }); diff --git a/products/BellHybrid/apps/application-bell-settings/include/application-bell-settings/ApplicationBellSettings.hpp b/products/BellHybrid/apps/application-bell-settings/include/application-bell-settings/ApplicationBellSettings.hpp index 5d74e468d47d00168a013c61e8234aa69afeadc5..60158c3392d5140394d33b300e62a770f876bf3d 100644 --- a/products/BellHybrid/apps/application-bell-settings/include/application-bell-settings/ApplicationBellSettings.hpp +++ b/products/BellHybrid/apps/application-bell-settings/include/application-bell-settings/ApplicationBellSettings.hpp @@ -4,6 +4,7 @@ #pragma once #include +#include #include namespace gui::window::name @@ -26,6 +27,9 @@ namespace app class ApplicationBellSettings : public Application { + private: + std::unique_ptr audioModel; + public: ApplicationBellSettings(std::string name = applicationBellSettingsName, std::string parent = "", diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.cpp b/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.cpp index 42ce047b6a14a7196c0264e1566d87c756574a02..8914feb4bfb74beb89608c28bfd8982babdb6c54 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.cpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.cpp @@ -7,16 +7,16 @@ namespace app::bell_settings { BedtimeSettingsPresenter::BedtimeSettingsPresenter(std::shared_ptr provider, std::shared_ptr model, - std::unique_ptr audioModel, + AbstractAudioModel &audioModel, std::unique_ptr soundsRepository) : provider(std::move(provider)), - model(std::move(model)), audioModel{std::move(audioModel)}, soundsRepository{std::move(soundsRepository)} + model(std::move(model)), audioModel{audioModel}, soundsRepository{std::move(soundsRepository)} { auto playSound = [this](const UTF8 &val) { currentSoundPath = val; - this->audioModel->play(this->soundsRepository->titleToPath(currentSoundPath).value_or(""), - AbstractAudioModel::PlaybackType::Bedtime, - {}); + this->audioModel.play(this->soundsRepository->titleToPath(currentSoundPath).value_or(""), + AbstractAudioModel::PlaybackType::Bedtime, + {}); }; this->provider->onExit = [this]() { getView()->exit(); }; @@ -28,8 +28,10 @@ namespace app::bell_settings this->provider->onVolumeEnter = playSound; this->provider->onVolumeExit = [this](const auto &) { this->stopSound(); }; this->provider->onVolumeChange = [this, playSound](const auto &val) { - this->audioModel->setVolume(val, AbstractAudioModel::PlaybackType::Bedtime, {}); - playSound(currentSoundPath); + this->audioModel.setVolume(val, AbstractAudioModel::PlaybackType::Bedtime, {}); + if (this->audioModel.hasPlaybackFinished()) { + playSound(currentSoundPath); + } }; } @@ -59,7 +61,7 @@ namespace app::bell_settings void BedtimeSettingsPresenter::stopSound() { - this->audioModel->stop({}); + this->audioModel.stop({}); } void BedtimeSettingsPresenter::exitWithoutSave() { diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.hpp b/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.hpp index 52307d96a5668f3aa6a6f8ebead9f7ee0621140e..56fd6ed9b14512f5b84d4c648574cc0199704fa2 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.hpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/BedtimeSettingsPresenter.hpp @@ -44,7 +44,7 @@ namespace app::bell_settings public: BedtimeSettingsPresenter(std::shared_ptr provider, std::shared_ptr model, - std::unique_ptr audioModel, + AbstractAudioModel &audioModel, std::unique_ptr soundsRepository); auto getPagesProvider() const -> std::shared_ptr override; @@ -58,7 +58,7 @@ namespace app::bell_settings std::shared_ptr provider; std::shared_ptr model; - std::unique_ptr audioModel; + AbstractAudioModel &audioModel; std::unique_ptr soundsRepository; UTF8 currentSoundPath; }; diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.cpp b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.cpp index e67776b3e8200aa870d7b74df433ea1a98996fe0..898ec5fd2d59fb03e229dbcbff3accf5858ed3a4 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.cpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.cpp @@ -8,14 +8,14 @@ namespace app::bell_settings { AlarmSettingsPresenter::AlarmSettingsPresenter(std::shared_ptr provider, std::unique_ptr model, - std::unique_ptr audioModel, + AbstractAudioModel &audioModel, std::unique_ptr soundsRepository) : provider(provider), - model(std::move(model)), audioModel{std::move(audioModel)}, soundsRepository{std::move(soundsRepository)} + model(std::move(model)), audioModel{audioModel}, soundsRepository{std::move(soundsRepository)} { auto playSound = [this](const UTF8 &val) { - this->audioModel->play( + this->audioModel.play( this->soundsRepository->titleToPath(val).value_or(""), AbstractAudioModel::PlaybackType::Alarm, {}); }; @@ -28,7 +28,7 @@ namespace app::bell_settings this->provider->onVolumeEnter = playSound; this->provider->onVolumeExit = [this](const auto &) { stopSound(); }; this->provider->onVolumeChange = [this](const auto &val) { - this->audioModel->setVolume(val, AbstractAudioModel::PlaybackType::Alarm, {}); + this->audioModel.setVolume(val, AbstractAudioModel::PlaybackType::Alarm, {}); }; } @@ -57,7 +57,7 @@ namespace app::bell_settings } void AlarmSettingsPresenter::stopSound() { - this->audioModel->stop({}); + this->audioModel.stop({}); } void AlarmSettingsPresenter::exitWithoutSave() { diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.hpp b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.hpp index 8970382e7c52ee63c9b166d020990a9fea3d5f8e..d64bcc61ad922c9e6d75d34a0311f85faf97d1cb 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.hpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/AlarmSettingsPresenter.hpp @@ -46,7 +46,7 @@ namespace app::bell_settings public: AlarmSettingsPresenter(std::shared_ptr provider, std::unique_ptr model, - std::unique_ptr audioModel, + AbstractAudioModel &audioModel, std::unique_ptr soundsRepository); auto getPagesProvider() const -> std::shared_ptr override; @@ -60,7 +60,7 @@ namespace app::bell_settings std::shared_ptr provider; std::unique_ptr model; - std::unique_ptr audioModel; + AbstractAudioModel &audioModel; std::unique_ptr soundsRepository; }; } // namespace app::bell_settings diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.cpp b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.cpp index 757e55b8f1055c497ab43b9b26a376e2010ecf4e..b7f5ac96e3db96ac2033b8c182b61a6fe4164d89 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.cpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.cpp @@ -8,16 +8,16 @@ namespace app::bell_settings { PrewakeUpWindowPresenter::PrewakeUpWindowPresenter(std::shared_ptr provider, std::unique_ptr model, - std::unique_ptr audioModel, + AbstractAudioModel &audioModel, std::unique_ptr soundsRepository) : provider(std::move(provider)), - model(std::move(model)), audioModel{std::move(audioModel)}, soundsRepository{std::move(soundsRepository)} + model(std::move(model)), audioModel{audioModel}, soundsRepository{std::move(soundsRepository)} { auto playSound = [this](const UTF8 &val) { currentSoundPath = val; - this->audioModel->play(this->soundsRepository->titleToPath(currentSoundPath).value_or(""), - AbstractAudioModel::PlaybackType::PreWakeup, - {}); + this->audioModel.play(this->soundsRepository->titleToPath(currentSoundPath).value_or(""), + AbstractAudioModel::PlaybackType::PreWakeup, + {}); }; this->provider->onExit = [this]() { getView()->exit(); }; @@ -29,8 +29,10 @@ namespace app::bell_settings this->provider->onVolumeEnter = playSound; this->provider->onVolumeExit = [this](const auto &) { this->stopSound(); }; this->provider->onVolumeChange = [this, playSound](const auto &val) { - this->audioModel->setVolume(val, AbstractAudioModel::PlaybackType::PreWakeup, {}); - playSound(currentSoundPath); + this->audioModel.setVolume(val, AbstractAudioModel::PlaybackType::PreWakeup, {}); + if (this->audioModel.hasPlaybackFinished()) { + playSound(currentSoundPath); + } }; } @@ -59,7 +61,7 @@ namespace app::bell_settings } void PrewakeUpWindowPresenter::stopSound() { - this->audioModel->stop({}); + this->audioModel.stop({}); } void PrewakeUpWindowPresenter::exitWithoutSave() { diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.hpp b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.hpp index b03186283085ea7248aa22fb0be20373e9f082c9..be32ab0db97c2caf640601c68a858f321d4de53e 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.hpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/PrewakeUpPresenter.hpp @@ -46,7 +46,7 @@ namespace app::bell_settings public: PrewakeUpWindowPresenter(std::shared_ptr provider, std::unique_ptr model, - std::unique_ptr audioModel, + AbstractAudioModel &audioModel, std::unique_ptr soundsRepository); auto getPagesProvider() const -> std::shared_ptr override; @@ -60,7 +60,7 @@ namespace app::bell_settings std::shared_ptr provider; std::unique_ptr model; - std::unique_ptr audioModel; + AbstractAudioModel &audioModel; std::unique_ptr soundsRepository; UTF8 currentSoundPath; }; diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.cpp b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.cpp index eda797ac1a2a24db4bb198219e7d6cf3c46952fb..c81a678e98930ceb4bc96f9f15c1e94a68a63b20 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.cpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.cpp @@ -9,16 +9,16 @@ namespace app::bell_settings SnoozePresenter::SnoozePresenter(std::shared_ptr provider, std::unique_ptr snoozeSettingsModel, - std::unique_ptr audioModel, + AbstractAudioModel &audioModel, std::unique_ptr soundsRepository) - : provider{provider}, snoozeSettingsModel{std::move(snoozeSettingsModel)}, audioModel{std::move(audioModel)}, + : provider{provider}, snoozeSettingsModel{std::move(snoozeSettingsModel)}, audioModel{audioModel}, soundsRepository{std::move(soundsRepository)} { auto playSound = [this](const UTF8 &val) { currentSoundPath = val; - this->audioModel->play(this->soundsRepository->titleToPath(currentSoundPath).value_or(""), - AbstractAudioModel::PlaybackType::Snooze, - {}); + this->audioModel.play(this->soundsRepository->titleToPath(currentSoundPath).value_or(""), + AbstractAudioModel::PlaybackType::Snooze, + {}); }; this->provider->onExit = [this]() { getView()->exit(); }; @@ -30,8 +30,10 @@ namespace app::bell_settings this->provider->onVolumeEnter = playSound; this->provider->onVolumeExit = [this](const auto &) { stopSound(); }; this->provider->onVolumeChange = [this, playSound](const auto &val) { - this->audioModel->setVolume(val, AbstractAudioModel::PlaybackType::Snooze, {}); - playSound(currentSoundPath); + this->audioModel.setVolume(val, AbstractAudioModel::PlaybackType::Snooze, {}); + if (this->audioModel.hasPlaybackFinished()) { + playSound(currentSoundPath); + } }; } @@ -56,7 +58,7 @@ namespace app::bell_settings } void SnoozePresenter::stopSound() { - audioModel->stop({}); + audioModel.stop({}); } void SnoozePresenter::eraseProviderData() { diff --git a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.hpp b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.hpp index fa7d8f85fd88617605fc89134b6ac25a258eefed..b7642bc66dfe1a4808906569b0a20bffe6264343 100644 --- a/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.hpp +++ b/products/BellHybrid/apps/application-bell-settings/presenter/alarm_settings/SnoozePresenter.hpp @@ -41,7 +41,7 @@ namespace app::bell_settings public: SnoozePresenter(std::shared_ptr provider, std::unique_ptr snoozeSettingsModel, - std::unique_ptr audioModel, + AbstractAudioModel &audioModel, std::unique_ptr soundsRepository); auto getPagesProvider() const -> std::shared_ptr override; void saveData() override; @@ -54,7 +54,7 @@ namespace app::bell_settings std::shared_ptr provider; std::unique_ptr snoozeSettingsModel; - std::unique_ptr audioModel; + AbstractAudioModel &audioModel; std::unique_ptr soundsRepository; UTF8 currentSoundPath; }; diff --git a/products/BellHybrid/apps/common/include/common/models/AbstractAudioModel.hpp b/products/BellHybrid/apps/common/include/common/models/AbstractAudioModel.hpp index e1ad255d65aff4648766fa43df37b74564027a45..c0d5f54630d757af239613a1afa29f6e8b242d0c 100644 --- a/products/BellHybrid/apps/common/include/common/models/AbstractAudioModel.hpp +++ b/products/BellHybrid/apps/common/include/common/models/AbstractAudioModel.hpp @@ -15,11 +15,12 @@ namespace app { public: /// 0-10 range - static constexpr auto minVolume = 1; - static constexpr auto maxVolume = 10; - using Volume = std::uint32_t; - using OnStateChangeCallback = std::function; - using OnGetValueCallback = std::function; + static constexpr auto minVolume = 1; + static constexpr auto maxVolume = 10; + using Volume = std::uint32_t; + using OnStateChangeCallback = std::function; + using OnGetValueCallback = std::function; + using OnPlaybackFinishedCallback = std::function; enum class PlaybackType { @@ -38,6 +39,8 @@ namespace app virtual void stop(OnStateChangeCallback &&callback) = 0; virtual void pause(OnStateChangeCallback &&callback) = 0; virtual void resume(OnStateChangeCallback &&callback) = 0; + virtual void setPlaybackFinishedCb(OnPlaybackFinishedCallback &&callback) = 0; + virtual bool hasPlaybackFinished() = 0; }; } // namespace app diff --git a/products/BellHybrid/apps/common/include/common/models/AudioModel.hpp b/products/BellHybrid/apps/common/include/common/models/AudioModel.hpp index 31888d9fef58b2a6b5e48a3653c6f623158d38f1..57b3bd131b235d1153a2328d1d69ea93e4da2689 100644 --- a/products/BellHybrid/apps/common/include/common/models/AudioModel.hpp +++ b/products/BellHybrid/apps/common/include/common/models/AudioModel.hpp @@ -12,6 +12,7 @@ namespace app { public: explicit AudioModel(ApplicationCommon *app); + virtual ~AudioModel(); void setVolume(Volume volume, PlaybackType playbackType, OnStateChangeCallback &&callback) override; std::optional getVolume(PlaybackType playbackType) override; @@ -20,8 +21,12 @@ namespace app void stop(OnStateChangeCallback &&callback) override; void pause(OnStateChangeCallback &&callback) override; void resume(OnStateChangeCallback &&callback) override; + void setPlaybackFinishedCb(OnPlaybackFinishedCallback &&callback) override; + bool hasPlaybackFinished() override; private: + std::function playbackFinishedCallback{nullptr}; + bool playbackFinishedFlag{false}; ApplicationCommon *app{}; }; } // namespace app diff --git a/products/BellHybrid/apps/common/include/common/models/BedtimeModel.hpp b/products/BellHybrid/apps/common/include/common/models/BedtimeModel.hpp index 847567c442c5000ed670273d93e410dd2c15d28b..bc536f2ea3a3aaf09e60cfdd9a3636a350505cb7 100644 --- a/products/BellHybrid/apps/common/include/common/models/BedtimeModel.hpp +++ b/products/BellHybrid/apps/common/include/common/models/BedtimeModel.hpp @@ -65,12 +65,12 @@ namespace app::bell_bedtime public: BedtimeModel() = delete; - explicit BedtimeModel(ApplicationCommon *app, std::unique_ptr &&audioModel) + explicit BedtimeModel(ApplicationCommon *app, AbstractAudioModel &audioModel) { bedtimeOnOff = std::make_unique(app); bedtimeTime = std::make_unique(app); bedtimeTone = std::make_unique(app); - bedtimeVolume = std::make_unique(*audioModel); + bedtimeVolume = std::make_unique(audioModel); } gui::AbstractSettingsModel &getBedtimeOnOff() override { @@ -92,7 +92,6 @@ namespace app::bell_bedtime } private: - std::unique_ptr audioModel; std::unique_ptr> bedtimeOnOff; std::unique_ptr> bedtimeTime; std::unique_ptr> bedtimeTone; diff --git a/products/BellHybrid/apps/common/src/AudioModel.cpp b/products/BellHybrid/apps/common/src/AudioModel.cpp index 926ef7dcd1e21413ec8361d21735a21e5bd902cd..1595001b3c3c5a44e9dd59d7837bf6004ef51e06 100644 --- a/products/BellHybrid/apps/common/src/AudioModel.cpp +++ b/products/BellHybrid/apps/common/src/AudioModel.cpp @@ -55,9 +55,25 @@ namespace app { AudioModel::AudioModel(ApplicationCommon *app) : app::AsyncCallbackReceiver{app}, app{app} - {} + { + app->connect(typeid(service::AudioEOFNotification), [&](sys::Message *msg) -> sys::MessagePointer { + playbackFinishedFlag = true; + if (playbackFinishedCallback) { + playbackFinishedCallback(); + } + + return sys::msgHandled(); + }); + } + + AudioModel::~AudioModel() + { + app->disconnect(typeid(service::AudioEOFNotification)); + } + void AudioModel::play(const std::string &filePath, PlaybackType type, OnStateChangeCallback &&callback) { + playbackFinishedFlag = false; auto msg = std::make_unique(filePath, convertPlaybackType(type)); auto task = app::AsyncRequest::createFromMessage(std::move(msg), service::audioServiceName); auto cb = [_callback = callback](auto response) { @@ -176,4 +192,15 @@ namespace app return {}; } + + void AudioModel::setPlaybackFinishedCb(OnPlaybackFinishedCallback &&callback) + { + playbackFinishedCallback = callback; + } + + bool AudioModel::hasPlaybackFinished() + { + return playbackFinishedFlag; + } + } // namespace app