soundd: don't loop controls unresponsive sound infinitely (#22839)

* soundd: don't loop controls unresponsive sound infinitely

* update tests

* actually fix
This commit is contained in:
Adeeb Shihadeh
2021-11-09 16:58:21 -08:00
committed by GitHub
parent 2621e5301d
commit 445a5aaca4
6 changed files with 39 additions and 31 deletions
+5 -4
View File
@@ -13,9 +13,9 @@ Sound::Sound(QObject *parent) : sm({"carState", "controlsState"}) {
QObject::connect(s, &QSoundEffect::statusChanged, [=]() {
assert(s->status() != QSoundEffect::Error);
});
s->setSource(QUrl::fromLocalFile(sound_asset_path + fn));
s->setVolume(Hardware::MIN_VOLUME);
sounds[alert] = {s, loops ? QSoundEffect::Infinite : 0};
s->setSource(QUrl::fromLocalFile(sound_asset_path + fn));
sounds[alert] = {s, loops};
}
QTimer *timer = new QTimer(this);
@@ -25,8 +25,9 @@ Sound::Sound(QObject *parent) : sm({"carState", "controlsState"}) {
void Sound::update() {
sm.update(0);
// scale volume with speed
if (sm.updated("carState")) {
// scale volume with speed
float volume = util::map_val(sm["carState"].getCarState().getVEgo(), 0.f, 20.f,
Hardware::MIN_VOLUME, Hardware::MAX_VOLUME);
for (auto &[s, loops] : sounds) {
@@ -43,7 +44,7 @@ void Sound::setAlert(const Alert &alert) {
// stop sounds
for (auto &[s, loops] : sounds) {
// Only stop repeating sounds
if (s->loopsRemaining() == QSoundEffect::Infinite) {
if (s->loopsRemaining() > 1 || s->loopsRemaining() == QSoundEffect::Infinite) {
s->stop();
}
}
+10 -9
View File
@@ -5,15 +5,16 @@
#include "selfdrive/hardware/hw.h"
#include "selfdrive/ui/ui.h"
const std::tuple<AudibleAlert, QString, bool> sound_list[] = {
{AudibleAlert::CHIME_DISENGAGE, "disengaged.wav", false},
{AudibleAlert::CHIME_ENGAGE, "engaged.wav", false},
{AudibleAlert::CHIME_WARNING1, "warning_1.wav", false},
{AudibleAlert::CHIME_WARNING2, "warning_2.wav", false},
{AudibleAlert::CHIME_WARNING2_REPEAT, "warning_2.wav", true},
{AudibleAlert::CHIME_WARNING_REPEAT, "warning_repeat.wav", true},
{AudibleAlert::CHIME_ERROR, "error.wav", false},
{AudibleAlert::CHIME_PROMPT, "error.wav", false},
const std::tuple<AudibleAlert, QString, int> sound_list[] = {
// AudibleAlert, file name, loop count
{AudibleAlert::CHIME_DISENGAGE, "disengaged.wav", 0},
{AudibleAlert::CHIME_ENGAGE, "engaged.wav", 0},
{AudibleAlert::CHIME_WARNING1, "warning_1.wav", 0},
{AudibleAlert::CHIME_WARNING_REPEAT, "warning_repeat.wav", 10},
{AudibleAlert::CHIME_WARNING_REPEAT_INFINITE, "warning_repeat.wav", QSoundEffect::Infinite},
{AudibleAlert::CHIME_WARNING2_REPEAT_INFINITE, "warning_2.wav", QSoundEffect::Infinite},
{AudibleAlert::CHIME_ERROR, "error.wav", 0},
{AudibleAlert::CHIME_PROMPT, "error.wav", 0},
};
class Sound : public QObject {
+13 -7
View File
@@ -11,8 +11,6 @@ public:
for (auto i = sounds.constBegin(); i != sounds.constEnd(); ++i) {
QObject::connect(i.value().first, &QSoundEffect::playingChanged, [=, s = i.value().first, a = i.key()]() {
if (s->isPlaying()) {
bool repeat = a == AudibleAlert::CHIME_WARNING_REPEAT || a == AudibleAlert::CHIME_WARNING2_REPEAT;
REQUIRE((s->loopsRemaining() == repeat ? QSoundEffect::Infinite : 1));
sound_stats[a].first++;
} else {
sound_stats[a].second++;
@@ -40,23 +38,31 @@ void controls_thread(int loop_cnt) {
}
}
}
// send no alert sound
for (int j = 0; j < 1000 / DT_CTRL; ++j) {
MessageBuilder msg;
msg.initEvent().initControlsState();
pm.send("controlsState", msg);
QThread::msleep(DT_CTRL);
}
QThread::currentThread()->quit();
}
TEST_CASE("test sound") {
TEST_CASE("test soundd") {
QEventLoop loop;
TestSound test_sound;
const int test_loop_cnt = 2;
QThread t;
QObject::connect(&t, &QThread::started, [=]() { controls_thread(test_loop_cnt); });
QObject::connect(&t, &QThread::finished, [&]() { loop.quit(); });
t.start();
loop.exec();
for (auto [play, stop] : test_sound.sound_stats) {
for (const AudibleAlert alert : test_sound.sound_stats.keys()) {
auto [play, stop] = test_sound.sound_stats[alert];
REQUIRE(play == test_loop_cnt);
REQUIRE(stop == test_loop_cnt);
}
+2 -2
View File
@@ -20,9 +20,9 @@ SOUNDS = {
AudibleAlert.chimeError: 173,
AudibleAlert.chimePrompt: 173,
AudibleAlert.chimeWarning1: 163,
AudibleAlert.chimeWarning2: 216,
AudibleAlert.chimeWarning2Repeat: 470,
AudibleAlert.chimeWarningRepeat: 468,
AudibleAlert.chimeWarningRepeatInfinite: 468,
AudibleAlert.chimeWarning2RepeatInfinite: 470,
}
def get_total_writes():