From c030493eb648e99700ae0872205bc0956538c0d2 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Sun, 28 Mar 2021 20:45:47 -0700 Subject: [PATCH] sound cleanup (#20491) * fix high CPU usage when playing repeated sounds * fix build * fix cpu usage * same behavior for now Co-authored-by: Comma Device --- selfdrive/ui/SConscript | 2 +- selfdrive/ui/qt/home.hpp | 4 ++-- selfdrive/ui/qt/qt_sound.hpp | 16 ---------------- selfdrive/ui/qt/{qt_sound.cc => sound.cc} | 13 ++++--------- selfdrive/ui/{ => qt}/sound.hpp | 13 +++++++++---- selfdrive/ui/ui.hpp | 2 +- 6 files changed, 17 insertions(+), 33 deletions(-) delete mode 100644 selfdrive/ui/qt/qt_sound.hpp rename selfdrive/ui/qt/{qt_sound.cc => sound.cc} (71%) rename selfdrive/ui/{ => qt}/sound.hpp (84%) diff --git a/selfdrive/ui/SConscript b/selfdrive/ui/SConscript index a0507ef598..ebef246ab6 100644 --- a/selfdrive/ui/SConscript +++ b/selfdrive/ui/SConscript @@ -16,7 +16,7 @@ if arch == "Darwin": qt_env['FRAMEWORKS'] += ['OpenCL'] widgets_src = ["qt/widgets/input.cc", "qt/widgets/drive_stats.cc", - "qt/widgets/ssh_keys.cc", "qt/widgets/toggle.cc", "qt/widgets/controls.cc", "qt/qt_sound.cc", + "qt/widgets/ssh_keys.cc", "qt/widgets/toggle.cc", "qt/widgets/controls.cc", "qt/sound.cc", "qt/widgets/offroad_alerts.cc", "qt/widgets/setup.cc", "qt/widgets/keyboard.cc", "#phonelibs/qrcode/QrCode.cc"] if arch != 'aarch64': diff --git a/selfdrive/ui/qt/home.hpp b/selfdrive/ui/qt/home.hpp index f941c928aa..f5cf74cb0b 100644 --- a/selfdrive/ui/qt/home.hpp +++ b/selfdrive/ui/qt/home.hpp @@ -9,7 +9,7 @@ #include #include -#include "qt_sound.hpp" +#include "sound.hpp" #include "ui/ui.hpp" #include "widgets/offroad_alerts.hpp" @@ -38,7 +38,7 @@ private: QTimer* timer; QTimer* backlight_timer; - QtSound sound; + Sound sound; bool onroad = true; double prev_draw_t = 0; diff --git a/selfdrive/ui/qt/qt_sound.hpp b/selfdrive/ui/qt/qt_sound.hpp deleted file mode 100644 index da84c23be1..0000000000 --- a/selfdrive/ui/qt/qt_sound.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include "sound.hpp" - -class QtSound : public Sound { -public: - QtSound(); - bool play(AudibleAlert alert); - void stop(); - void setVolume(int volume); - float volume = 0; - -private: - std::map sounds; -}; diff --git a/selfdrive/ui/qt/qt_sound.cc b/selfdrive/ui/qt/sound.cc similarity index 71% rename from selfdrive/ui/qt/qt_sound.cc rename to selfdrive/ui/qt/sound.cc index ee04785748..641ffb22a5 100644 --- a/selfdrive/ui/qt/qt_sound.cc +++ b/selfdrive/ui/qt/sound.cc @@ -1,22 +1,21 @@ #include -#include "qt_sound.hpp" +#include "sound.hpp" -QtSound::QtSound() { +Sound::Sound() { for (auto &kv : sound_map) { auto path = QUrl::fromLocalFile(kv.second.first); sounds[kv.first].setSource(path); } } -bool QtSound::play(AudibleAlert alert) { +void Sound::play(AudibleAlert alert) { int loops = sound_map[alert].second> - 1 ? sound_map[alert].second : QSoundEffect::Infinite; sounds[alert].setLoopCount(loops); sounds[alert].setVolume(volume); sounds[alert].play(); - return true; } -void QtSound::stop() { +void Sound::stop() { for (auto &kv : sounds) { // Only stop repeating sounds if (sound_map[kv.first].second != 0) { @@ -24,7 +23,3 @@ void QtSound::stop() { } } } - -void QtSound::setVolume(int volume) { - // TODO: implement this -} diff --git a/selfdrive/ui/sound.hpp b/selfdrive/ui/qt/sound.hpp similarity index 84% rename from selfdrive/ui/sound.hpp rename to selfdrive/ui/qt/sound.hpp index ab83a0180d..cee1ea51ae 100644 --- a/selfdrive/ui/sound.hpp +++ b/selfdrive/ui/qt/sound.hpp @@ -1,5 +1,7 @@ #pragma once + #include +#include #include "cereal/gen/cpp/log.capnp.h" typedef cereal::CarControl::HUDControl::AudibleAlert AudibleAlert; @@ -18,8 +20,11 @@ static std::map> sound_map { class Sound { public: - virtual ~Sound() {} - virtual bool play(AudibleAlert alert) = 0; - virtual void stop() = 0; - virtual void setVolume(int volume) = 0; + Sound(); + void play(AudibleAlert alert); + void stop(); + float volume = 0; + +private: + std::map sounds; }; diff --git a/selfdrive/ui/ui.hpp b/selfdrive/ui/ui.hpp index e5e8790b1a..aec9a22563 100644 --- a/selfdrive/ui/ui.hpp +++ b/selfdrive/ui/ui.hpp @@ -25,7 +25,7 @@ #include "common/params.h" #include "common/glutil.h" #include "common/transformations/orientation.hpp" -#include "sound.hpp" +#include "qt/sound.hpp" #include "visionipc.h" #include "visionipc_client.h"