This commit is contained in:
legop3
2026-03-17 03:11:04 -04:00
parent aaa93847e1
commit 84eaab3c9c
13 changed files with 107 additions and 79 deletions
+39 -3
View File
@@ -1,5 +1,11 @@
package roverd
import (
"fmt"
"math"
"os/exec"
)
type AudioLevels struct {
HornGain float64
TTSGain float64
@@ -34,9 +40,7 @@ func (c *WSClient) setAudioLevels(next AudioLevels) {
c.audioMu.Lock()
c.audioLevels = normalized
c.audioMu.Unlock()
if c.horn != nil {
c.horn.SetGlobalGain(normalized.HornGain)
}
c.applyAudioLevelsToMixer(normalized)
}
func (c *WSClient) handleAudioLevels(payload *audioLevelsPayload) error {
@@ -56,3 +60,35 @@ func (c *WSClient) handleAudioLevels(payload *audioLevelsPayload) error {
c.setAudioLevels(levels)
return nil
}
func (c *WSClient) applyAudioLevelsToMixer(levels AudioLevels) {
c.applyMixerGain("HornMaster", levels.HornGain)
c.applyMixerGain("TTSMaster", levels.TTSGain)
c.applyMixerGain("ForwardMaster", levels.ForwardGain)
}
func (c *WSClient) applyMixerGain(control string, gain float64) {
normalized := clampAudioGain(gain)
if normalized <= 0 {
out, err := exec.Command("amixer", "-q", "-c", "0", "sset", control, "0%").CombinedOutput()
if err != nil {
c.log.Printf("audio-levels: amixer mute %s failed: %v (%s)", control, err, string(out))
}
return
}
// Convert linear gain to dB, matching softvol max_dB=12.0 in /etc/asound.conf.
db := 20.0 * math.Log10(normalized)
if db > 12.0 {
db = 12.0
}
if db < -60.0 {
db = -60.0
}
dbArg := fmt.Sprintf("%.2fdB", db)
out, err := exec.Command("amixer", "-q", "-c", "0", "sset", control, dbArg).CombinedOutput()
if err != nil {
c.log.Printf("audio-levels: amixer set %s=%s failed: %v (%s)", control, dbArg, err, string(out))
}
}