From c442c34afe6c193ee044bc59e1212c5d18215372 Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Tue, 29 Nov 2022 19:53:44 -0800 Subject: [PATCH] implement Hardware::set_volume using pactl --- selfdrive/ui/soundd/sound.cc | 4 +++- system/hardware/base.h | 10 +++++++++- system/hardware/pc/hardware.h | 5 ++++- system/hardware/tici/hardware.h | 3 +++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/selfdrive/ui/soundd/sound.cc b/selfdrive/ui/soundd/sound.cc index fe7bfe780036ee..841bea3b8b630c 100644 --- a/selfdrive/ui/soundd/sound.cc +++ b/selfdrive/ui/soundd/sound.cc @@ -48,7 +48,9 @@ void Sound::update() { // scale volume with speed if (sm.updated("carState")) { - Hardware::set_volume(util::map_val(sm["carState"].getCarState().getVEgo(), 11.f, 20.f, 0.f, 1.f)); + float volume = util::map_val(sm["carState"].getCarState().getVEgo(), 11.f, 20.f, 0.f, 1.f); + volume = QAudio::convertVolume(volume, QAudio::LogarithmicVolumeScale, QAudio::LinearVolumeScale); + Hardware::set_volume(volume); } setAlert(Alert::get(sm, started_frame)); diff --git a/system/hardware/base.h b/system/hardware/base.h index 33f360130cfb8a..14708906faf5df 100644 --- a/system/hardware/base.h +++ b/system/hardware/base.h @@ -1,8 +1,11 @@ #pragma once +#include #include #include + #include "cereal/messaging/messaging.h" +#include "common/util.h" // no-op base hw class class HardwareNone { @@ -20,7 +23,7 @@ class HardwareNone { static void poweroff() {} static void set_brightness(int percent) {} static void set_display_power(bool on) {} - static void set_volume(float percent) {} + static void set_volume(float volume) {} static bool get_ssh_enabled() { return false; } static void set_ssh_enabled(bool enabled) {} @@ -28,4 +31,9 @@ class HardwareNone { static bool PC() { return false; } static bool TICI() { return false; } static bool AGNOS() { return false; } + +protected: + static float map_volume(float volume) { + return util::map_val(volume, 0.f, 1.f, MIN_VOLUME, MAX_VOLUME); + } }; diff --git a/system/hardware/pc/hardware.h b/system/hardware/pc/hardware.h index 68f965ee2f3bed..0ad2fabbb4582c 100644 --- a/system/hardware/pc/hardware.h +++ b/system/hardware/pc/hardware.h @@ -1,6 +1,5 @@ #pragma once -#include "common/util.h" #include "system/hardware/base.h" class HardwarePC : public HardwareNone { @@ -11,4 +10,8 @@ class HardwarePC : public HardwareNone { static bool PC() { return true; } static bool TICI() { return util::getenv("TICI", 0) == 1; } static bool AGNOS() { return util::getenv("TICI", 0) == 1; } + + static void set_volume(float volume) { + std::system(std::format("pactl set-sink-volume 1 %.3f &", map_volume(volume)).c_str()); + } }; diff --git a/system/hardware/tici/hardware.h b/system/hardware/tici/hardware.h index dcccb9f3d1f6fb..6e3dbed2c525e2 100644 --- a/system/hardware/tici/hardware.h +++ b/system/hardware/tici/hardware.h @@ -38,6 +38,9 @@ class HardwareTici : public HardwareNone { bl_power_control.close(); } }; + static void set_volume(float volume) { + std::system(std::format("pactl set-sink-volume 1 %.3f &", map_volume(volume)).c_str()); + } static bool get_ssh_enabled() { return Params().getBool("SshEnabled"); }; static void set_ssh_enabled(bool enabled) { Params().putBool("SshEnabled", enabled); };