diff --git a/selfdrive/common/util.h b/selfdrive/common/util.h index 3da88bb767..b75c2f4343 100644 --- a/selfdrive/common/util.h +++ b/selfdrive/common/util.h @@ -2,8 +2,11 @@ #include #include +#include #include -#include +#include +#include + #include #include #include @@ -11,9 +14,7 @@ #include #include #include -#include -#include -#include +#include #ifndef sighandler_t typedef void (*sighandler_t)(int sig); @@ -38,6 +39,19 @@ int set_core_affinity(int core); namespace util { +// ***** math helpers ***** + +// map x from [a1, a2] to [b1, b2] +template +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) { return s.compare(0, prefix.size(), prefix) == 0; } diff --git a/selfdrive/hardware/hw.h b/selfdrive/hardware/hw.h index 8115aa946c..c4086c9dde 100644 --- a/selfdrive/hardware/hw.h +++ b/selfdrive/hardware/hw.h @@ -17,6 +17,9 @@ // no-op base hw class class HardwareNone { public: + static constexpr float MAX_VOLUME = 0; + static constexpr float MIN_VOLUME = 0; + static std::string get_os_version() { return "openpilot for PC"; }; static void reboot() {}; @@ -29,6 +32,9 @@ public: class HardwareEon : public HardwareNone { public: + static constexpr float MAX_VOLUME = 1.0; + static constexpr float MIN_VOLUME = 0.5; + static std::string get_os_version() { return "NEOS " + util::read_file("/VERSION"); }; @@ -54,6 +60,9 @@ public: class HardwareTici : public HardwareNone { public: + static constexpr float MAX_VOLUME = 0.5; + static constexpr float MIN_VOLUME = 0.4; + static std::string get_os_version() { return "AGNOS " + util::read_file("/VERSION"); }; diff --git a/selfdrive/ui/qt/home.cc b/selfdrive/ui/qt/home.cc index acb333c5ab..790abe26cf 100644 --- a/selfdrive/ui/qt/home.cc +++ b/selfdrive/ui/qt/home.cc @@ -278,6 +278,10 @@ void GLWindow::timerUpdate() { 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); repaint(); watchdog_kick(); diff --git a/selfdrive/ui/qt/qt_sound.cc b/selfdrive/ui/qt/qt_sound.cc index 9d1b141950..ee04785748 100644 --- a/selfdrive/ui/qt/qt_sound.cc +++ b/selfdrive/ui/qt/qt_sound.cc @@ -11,7 +11,7 @@ QtSound::QtSound() { bool QtSound::play(AudibleAlert alert) { int loops = sound_map[alert].second> - 1 ? sound_map[alert].second : QSoundEffect::Infinite; sounds[alert].setLoopCount(loops); - sounds[alert].setVolume(0.45); + sounds[alert].setVolume(volume); sounds[alert].play(); return true; } diff --git a/selfdrive/ui/qt/qt_sound.hpp b/selfdrive/ui/qt/qt_sound.hpp index 8c7f8aa348..da84c23be1 100644 --- a/selfdrive/ui/qt/qt_sound.hpp +++ b/selfdrive/ui/qt/qt_sound.hpp @@ -9,6 +9,7 @@ public: bool play(AudibleAlert alert); void stop(); void setVolume(int volume); + float volume = 0; private: std::map sounds;