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>
This commit is contained in:
Adeeb Shihadeh
2021-03-28 20:45:47 -07:00
committed by GitHub
parent 4942e94f6c
commit c030493eb6
6 changed files with 17 additions and 33 deletions
+1 -1
View File
@@ -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':
+2 -2
View File
@@ -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;
-16
View File
@@ -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;
};
+1 -1
View File
@@ -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"