From e5b3e15e61cbe8de420a1aafa002aba9088f3937 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Wed, 11 Nov 2026 15:16:46 -0500 Subject: [PATCH] Attempt to honor PULSE_SERVER. --- README.md | 6 ++++++ audio_driver_test.go | 29 +++++++++++++++++++++++++++++ main.go | 24 +++++++++++++++--------- 3 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 audio_driver_test.go diff --git a/README.md b/README.md index 545e193..21d74d4 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,12 @@ Barnard uses OpenAL Soft for audio. The default backend order is determined by t - Config file: add `audiodriver = "pipewire"` to your `~/.barnard.toml` - Environment: `ALSOFT_DRIVERS=pipewire ./barnard` (takes precedence over config) +If `PULSE_SERVER` is set, Barnard selects OpenAL Soft's `pulse` backend so both +playback and capture use that server. `--audio-driver` and `ALSOFT_DRIVERS` take +precedence over `PULSE_SERVER`; otherwise `PULSE_SERVER` takes precedence over +the configured `audiodriver`. For example, in Termux, set `PULSE_SERVER` to the +address of the PulseAudio server before starting Barnard. + If PipeWire or PulseAudio support is missing, install OpenAL Soft with the corresponding backend enabled (e.g., `libopenal1` or `openal-soft` packages built with PipeWire). After changing drivers, rerun with `--list_devices` to confirm the desired devices appear. Leaving `audiodriver` empty uses the OpenAL Soft default ordering from the installed library. diff --git a/audio_driver_test.go b/audio_driver_test.go new file mode 100644 index 0000000..5291116 --- /dev/null +++ b/audio_driver_test.go @@ -0,0 +1,29 @@ +package main + +import "testing" + +func TestSelectAudioDriver(t *testing.T) { + tests := []struct { + name string + flagDriver string + envDriver string + pulseServer string + configDriver string + want string + }{ + {name: "defaults", want: ""}, + {name: "configured driver", configDriver: " alsa ", want: "alsa"}, + {name: "Pulse server", pulseServer: "tcp:127.0.0.1:4713", want: "pulse"}, + {name: "Pulse server over configured driver", pulseServer: "unix:/tmp/pulse.sock", configDriver: "pipewire", want: "pulse"}, + {name: "explicit environment over Pulse server", envDriver: "alsa", pulseServer: "tcp:127.0.0.1:4713", want: "alsa"}, + {name: "flag over environment", flagDriver: " pipewire ", envDriver: "alsa", pulseServer: "tcp:127.0.0.1:4713", want: "pipewire"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := select_audio_driver(test.flagDriver, test.envDriver, test.pulseServer, test.configDriver) + if got != test.want { + t.Errorf("select_audio_driver() = %q, want %q", got, test.want) + } + }) + } +} diff --git a/main.go b/main.go index 1f608e6..5ecdb87 100644 --- a/main.go +++ b/main.go @@ -70,6 +70,19 @@ func expandNotification(template string, event []string) string { ).Replace(template) } +func select_audio_driver(flagDriver, envDriver, pulseServer, configDriver string) string { + if driver := strings.TrimSpace(flagDriver); driver != "" { + return driver + } + if envDriver != "" { + return envDriver + } + if pulseServer != "" { + return "pulse" + } + return strings.TrimSpace(configDriver) +} + func main() { // Command line flags server := flag.String("server", "localhost:64738", "the server to connect to") @@ -173,15 +186,8 @@ func main() { certificate = userConfig.GetCertificate() } - driver := strings.TrimSpace(*audioDriver) - if driver == "" { - // Environment variable takes precedence over config - if envDriver := os.Getenv("ALSOFT_DRIVERS"); envDriver != "" { - driver = envDriver - } else { - driver = strings.TrimSpace(userConfig.GetAudioDriver()) - } - } + driver := select_audio_driver(*audioDriver, os.Getenv("ALSOFT_DRIVERS"), + os.Getenv("PULSE_SERVER"), userConfig.GetAudioDriver()) if driver != "" { os.Setenv("ALSOFT_DRIVERS", driver) }