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 <device@comma.ai>
pull/20475/head
Adeeb Shihadeh 4 years ago committed by GitHub
parent 4942e94f6c
commit c030493eb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      selfdrive/ui/SConscript
  2. 4
      selfdrive/ui/qt/home.hpp
  3. 16
      selfdrive/ui/qt/qt_sound.hpp
  4. 13
      selfdrive/ui/qt/sound.cc
  5. 13
      selfdrive/ui/qt/sound.hpp
  6. 2
      selfdrive/ui/ui.hpp

@ -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':

@ -9,7 +9,7 @@
#include <QTimer>
#include <QWidget>
#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;

@ -1,16 +0,0 @@
#pragma once
#include <QSoundEffect>
#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<AudibleAlert, QSoundEffect> sounds;
};

@ -1,22 +1,21 @@
#include <QUrl>
#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
}

@ -1,5 +1,7 @@
#pragma once
#include <map>
#include <QSoundEffect>
#include "cereal/gen/cpp/log.capnp.h"
typedef cereal::CarControl::HUDControl::AudibleAlert AudibleAlert;
@ -18,8 +20,11 @@ static std::map<AudibleAlert, std::pair<const char *, int>> 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<AudibleAlert, QSoundEffect> sounds;
};

@ -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"

Loading…
Cancel
Save