async set volumn

pull/29389/head
deanlee 2 years ago
parent 22c89676b4
commit 7bc88c4983
  1. 5
      selfdrive/ui/soundd/sound.cc
  2. 4
      selfdrive/ui/soundd/sound.h

@ -5,6 +5,7 @@
#include <QAudio>
#include <QAudioDeviceInfo>
#include <QDebug>
#include <QtConcurrent>
#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);
}
}

@ -1,3 +1,4 @@
#include <QFuture>
#include <QMap>
#include <QSoundEffect>
#include <QString>
@ -29,6 +30,7 @@ protected:
SubMaster sm;
Alert current_alert = {};
QMap<AudibleAlert, QPair<QSoundEffect *, int>> sounds;
int current_volume = -1;
QFuture<void> future;
QMap<AudibleAlert, QPair<QSoundEffect *, int>> sounds;
};

Loading…
Cancel
Save