From 7bc88c498353fff158b0de3d1923aa2c0f004a37 Mon Sep 17 00:00:00 2001 From: deanlee Date: Mon, 14 Aug 2023 23:37:55 +0800 Subject: [PATCH] async set volumn --- selfdrive/ui/soundd/sound.cc | 5 +++-- selfdrive/ui/soundd/sound.h | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/selfdrive/ui/soundd/sound.cc b/selfdrive/ui/soundd/sound.cc index ae06e3e903..fe2f77879e 100644 --- a/selfdrive/ui/soundd/sound.cc +++ b/selfdrive/ui/soundd/sound.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include "cereal/messaging/messaging.h" #include "common/util.h" @@ -33,12 +34,12 @@ void Sound::update() { sm.update(0); // scale volume using ambient noise level - if (sm.updated("microphone")) { + if (sm.updated("microphone") && !future.isRunning()) { float volume = util::map_val(sm["microphone"].getMicrophone().getFilteredSoundPressureWeightedDb(), 30.f, 60.f, 0.f, 1.f); volume = QAudio::convertVolume(volume, QAudio::LogarithmicVolumeScale, QAudio::LinearVolumeScale); // set volume on changes if (std::exchange(current_volume, std::nearbyint(volume * 10)) != current_volume) { - Hardware::set_volume(volume); + future = QtConcurrent::run(Hardware::set_volume, volume); } } diff --git a/selfdrive/ui/soundd/sound.h b/selfdrive/ui/soundd/sound.h index 9cabd1c6bd..77711770f1 100644 --- a/selfdrive/ui/soundd/sound.h +++ b/selfdrive/ui/soundd/sound.h @@ -1,3 +1,4 @@ +#include #include #include #include @@ -29,6 +30,7 @@ protected: SubMaster sm; Alert current_alert = {}; - QMap> sounds; int current_volume = -1; + QFuture future; + QMap> sounds; };