Qt UI: scale volume with speed (#20441)

* Qt UI: scale volume with speed

* for all hw

* way too loud for tici

* set volume

* volume

* swap
old-commit-hash: f1da1f9cb2
commatwo_master
Adeeb Shihadeh 4 years ago committed by GitHub
parent 555e4630ff
commit 2298d8f8f9
  1. 22
      selfdrive/common/util.h
  2. 9
      selfdrive/hardware/hw.h
  3. 4
      selfdrive/ui/qt/home.cc
  4. 2
      selfdrive/ui/qt/qt_sound.cc
  5. 1
      selfdrive/ui/qt/qt_sound.hpp

@ -2,8 +2,11 @@
#include <cstdio> #include <cstdio>
#include <csignal> #include <csignal>
#include <cassert>
#include <cstring> #include <cstring>
#include <cstdlib> #include <unistd.h>
#include <fcntl.h>
#include <string> #include <string>
#include <memory> #include <memory>
#include <atomic> #include <atomic>
@ -11,9 +14,7 @@
#include <fstream> #include <fstream>
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include <cassert> #include <algorithm>
#include <unistd.h>
#include <fcntl.h>
#ifndef sighandler_t #ifndef sighandler_t
typedef void (*sighandler_t)(int sig); typedef void (*sighandler_t)(int sig);
@ -38,6 +39,19 @@ int set_core_affinity(int core);
namespace util { namespace util {
// ***** math helpers *****
// map x from [a1, a2] to [b1, b2]
template<typename T>
T map_val(T x, T a1, T a2, T b1, T b2) {
x = std::clamp(x, a1, a2);
T ra = a2 - a1;
T rb = b2 - b1;
return (x - a1)*rb / ra + b1;
}
// ***** string helpers *****
inline bool starts_with(const std::string &s, const std::string &prefix) { inline bool starts_with(const std::string &s, const std::string &prefix) {
return s.compare(0, prefix.size(), prefix) == 0; return s.compare(0, prefix.size(), prefix) == 0;
} }

@ -17,6 +17,9 @@
// no-op base hw class // no-op base hw class
class HardwareNone { class HardwareNone {
public: public:
static constexpr float MAX_VOLUME = 0;
static constexpr float MIN_VOLUME = 0;
static std::string get_os_version() { return "openpilot for PC"; }; static std::string get_os_version() { return "openpilot for PC"; };
static void reboot() {}; static void reboot() {};
@ -29,6 +32,9 @@ public:
class HardwareEon : public HardwareNone { class HardwareEon : public HardwareNone {
public: public:
static constexpr float MAX_VOLUME = 1.0;
static constexpr float MIN_VOLUME = 0.5;
static std::string get_os_version() { static std::string get_os_version() {
return "NEOS " + util::read_file("/VERSION"); return "NEOS " + util::read_file("/VERSION");
}; };
@ -54,6 +60,9 @@ public:
class HardwareTici : public HardwareNone { class HardwareTici : public HardwareNone {
public: public:
static constexpr float MAX_VOLUME = 0.5;
static constexpr float MIN_VOLUME = 0.4;
static std::string get_os_version() { static std::string get_os_version() {
return "AGNOS " + util::read_file("/VERSION"); return "AGNOS " + util::read_file("/VERSION");
}; };

@ -278,6 +278,10 @@ void GLWindow::timerUpdate() {
handle_display_state(&ui_state, false); handle_display_state(&ui_state, false);
// scale volume with speed
sound.volume = util::map_val(ui_state.scene.car_state.getVEgo(), 0.f, 20.f,
Hardware::MIN_VOLUME, Hardware::MAX_VOLUME);
ui_update(&ui_state); ui_update(&ui_state);
repaint(); repaint();
watchdog_kick(); watchdog_kick();

@ -11,7 +11,7 @@ QtSound::QtSound() {
bool QtSound::play(AudibleAlert alert) { bool QtSound::play(AudibleAlert alert) {
int loops = sound_map[alert].second> - 1 ? sound_map[alert].second : QSoundEffect::Infinite; int loops = sound_map[alert].second> - 1 ? sound_map[alert].second : QSoundEffect::Infinite;
sounds[alert].setLoopCount(loops); sounds[alert].setLoopCount(loops);
sounds[alert].setVolume(0.45); sounds[alert].setVolume(volume);
sounds[alert].play(); sounds[alert].play();
return true; return true;
} }

@ -9,6 +9,7 @@ public:
bool play(AudibleAlert alert); bool play(AudibleAlert alert);
void stop(); void stop();
void setVolume(int volume); void setVolume(int volume);
float volume = 0;
private: private:
std::map<AudibleAlert, QSoundEffect> sounds; std::map<AudibleAlert, QSoundEffect> sounds;

Loading…
Cancel
Save