Add volume changing logic

This commit is contained in:
NeilClack
2026-05-14 09:57:13 -05:00
parent 1f9da7ee78
commit 3ea32e0eaa
6 changed files with 104 additions and 1 deletions
+10
View File
@@ -247,6 +247,16 @@ func SetAllSinkVolumes(level float64) error {
return nil
}
// ConvertVolume converts a desired volume percentage (0%, 25%, 33%, 90%, etc) to
// the float64 value needed to pass to SetAllSinkVolumes 0..1.0
// Percentages should be passed as integers. 25% -> 25
func ConvertVolume(volPerc int) float64 {
if volPerc == 0 {
return 0.0
}
return float64(volPerc) / 100.0
}
// parseSinks parses the output of `wpctl status` and extracts audio sinks.
func parseSinks(data []byte) ([]Sink, error) {
var sinks []Sink
+34
View File
@@ -0,0 +1,34 @@
package mixer_test
import (
"math"
"testing"
"rhemawavetech.com/rhemawave/internal/mixer"
)
func TestConvertVolume(t *testing.T) {
tests := []struct {
name string
input int
expected float64
}{
{"0 percent", 0, 0.0},
{"25 percent", 25, 0.25},
{"33 percent", 33, 0.33},
{"50 percent", 50, 0.50},
{"90 percent", 90, 0.90},
{"100 percent", 100, 1.0},
}
const epsilon = 1e-9
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := mixer.ConvertVolume(tt.input)
if math.Abs(got-tt.expected) > epsilon {
t.Errorf("ConvertVolume(%d) = %v, want %v", tt.input, got, tt.expected)
}
})
}
}