From f2c9aeb3a828c5859619e467a85411aa880e13a4 Mon Sep 17 00:00:00 2001 From: Wijnand Modderman-Lenstra Date: Thu, 29 Oct 2026 17:38:07 +0100 Subject: [PATCH] Monitor MPD FIFO output status for audio sources --- README.md | 12 +++++++- beatglow.conf | 4 +++ config/config.go | 1 + controller.go | 7 +++++ player/mpd/fifo.go | 29 ++++++++++++++++++ player/mpd/outputs.go | 61 ++++++++++++++++++++++++++++++++++++++ player/mpd/outputs_test.go | 33 +++++++++++++++++++++ player/mpd/player.go | 11 +++++++ player/volumio/player.go | 2 ++ 9 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 player/mpd/fifo.go create mode 100644 player/mpd/outputs.go create mode 100644 player/mpd/outputs_test.go diff --git a/README.md b/README.md index 7f1239d..a7d1de0 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,16 @@ During the shader, running various shader effects, running 44.1kHz stereo audio: ## Setup +When the audio source uses `type = "fifo"`, MPD-based players check the output +status after connecting and every 15 seconds. BeatGlow logs a warning if the +matching output is disabled or missing, and reports when it becomes enabled. +It does not enable outputs automatically. A single FIFO output is selected +automatically. If MPD has multiple FIFO outputs, set `output = "BeatGlow"` in +the `audio` block, using the name of the MPD output that writes to your FIFO. +Older MPD versions that omit the output plugin also require this name. +MPD does not expose FIFO paths through its output status, so the configured +output must write to the same path as the audio source. + Minimal requirements: - Linux Computer (Raspberry Pi 3B+) - Supported display @@ -155,4 +165,4 @@ display { } ``` -[^1]: Affiliate links, by clicking them and making an order there is a small kick back for me, which allows me to order parts for testing. There is usually no additional costs for the purchaser. \ No newline at end of file +[^1]: Affiliate links, by clicking them and making an order there is a small kick back for me, which allows me to order parts for testing. There is usually no additional costs for the purchaser. diff --git a/beatglow.conf b/beatglow.conf index eaf831e..6cf248f 100644 --- a/beatglow.conf +++ b/beatglow.conf @@ -160,6 +160,10 @@ audio { # Path to the audio stream. path = "/tmp/beatglow.fifo" + + # MPD output name (optional when MPD has exactly one FIFO output). + # Must match the name of the output writing to the path above. + # output = "BeatGlow" # Rate is the sampling rate. rate = 44100 diff --git a/config/config.go b/config/config.go index a3fbaa6..5f291c8 100644 --- a/config/config.go +++ b/config/config.go @@ -88,6 +88,7 @@ type Encoder struct { // Audio source provides PCM samples. type Audio struct { + Output string `hcl:"output,optional"` Type string `hcl:"type"` Path string `hcl:"path,optional"` Host string `hcl:"host,optional"` diff --git a/controller.go b/controller.go index f32bdd6..8d91116 100644 --- a/controller.go +++ b/controller.go @@ -30,6 +30,10 @@ type Controller struct { recorderPalette color.Palette } +type audioSourceConfigurator interface { + SetAudioSource(config.Audio) +} + func New(config *config.Config) (*Controller, error) { log := logrus.NewEntry(logrus.StandardLogger()) @@ -59,6 +63,9 @@ func New(config *config.Config) (*Controller, error) { c.teardown(log) return nil, err } + if p, ok := c.ctx.player.(audioSourceConfigurator); ok { + p.SetAudioSource(config.Audio) + } if err := c.configureInputs(log, &config.Input); err != nil { log.WithError(err).Error("fatal error setting up inputs") c.teardown(log) diff --git a/player/mpd/fifo.go b/player/mpd/fifo.go new file mode 100644 index 0000000..069a1ad --- /dev/null +++ b/player/mpd/fifo.go @@ -0,0 +1,29 @@ +package mpd + +import "github.com/BeatGlow/beatglow/config" + +// SetAudioSource configures FIFO output monitoring before Events is called. +func (p *Player) SetAudioSource(source config.Audio) { + p.audioSource = source +} + +func (p *Player) checkFIFOOutput(conn *clientConn) { + lines, err := p.sendDataCommand(conn, "outputs") + if err == nil { + err = fifoOutputState(parseOutputs(lines), p.audioSource.Output) + } + state := "enabled" + if err != nil { + state = err.Error() + } + if state == p.fifoState { + return + } + p.fifoState = state + log := conn.log().WithField("fifo", p.audioSource.Path).WithField("output", p.audioSource.Output) + if err != nil { + log.WithError(err).Warn("FIFO audio source may not receive samples") + } else { + log.Info("MPD FIFO output is enabled") + } +} diff --git a/player/mpd/outputs.go b/player/mpd/outputs.go new file mode 100644 index 0000000..8b4b091 --- /dev/null +++ b/player/mpd/outputs.go @@ -0,0 +1,61 @@ +package mpd + +import ( + "fmt" + "strings" +) + +type output struct { + name, plugin string + enabled bool +} + +func parseOutputs(lines []string) []output { + var outputs []output + for _, line := range lines { + key, value, ok := strings.Cut(line, ": ") + if !ok { + continue + } + if key == "outputid" { + outputs = append(outputs, output{}) + } + if len(outputs) == 0 { + continue + } + o := &outputs[len(outputs)-1] + switch key { + case "outputname": + o.name = value + case "plugin": + o.plugin = value + case "outputenabled": + o.enabled = value == "1" + } + } + return outputs +} + +func fifoOutputState(outputs []output, name string) error { + var matches []output + for _, o := range outputs { + if name != "" { + if o.name == name && (o.plugin == "fifo" || o.plugin == "") { + matches = append(matches, o) + } + } else if o.plugin == "fifo" { + matches = append(matches, o) + } + } + switch len(matches) { + case 0: + return fmt.Errorf("no matching MPD FIFO output; check MPD configuration and audio.output") + case 1: + if !matches[0].enabled { + return fmt.Errorf("MPD FIFO output %q is disabled", matches[0].name) + } + return nil + default: + return fmt.Errorf("multiple matching MPD FIFO outputs; set audio.output to the output name") + } +} diff --git a/player/mpd/outputs_test.go b/player/mpd/outputs_test.go new file mode 100644 index 0000000..079c42f --- /dev/null +++ b/player/mpd/outputs_test.go @@ -0,0 +1,33 @@ +package mpd + +import ( + "strings" + "testing" +) + +func TestFIFOOutputState(t *testing.T) { + for _, tt := range []struct { + name, response, output, want string + }{ + {"enabled", "outputid: 0\noutputname: DAC\nplugin: alsa\noutputenabled: 0\noutputid: 1\noutputname: Visualizer\nplugin: fifo\noutputenabled: 1", "", ""}, + {"disabled", "outputid: 2\noutputname: Visualizer\nplugin: fifo\noutputenabled: 0", "", "disabled"}, + {"missing", "outputid: 0\noutputname: DAC\nplugin: alsa\noutputenabled: 1", "", "no matching"}, + {"ambiguous", "outputid: 1\noutputname: One\nplugin: fifo\noutputenabled: 1\noutputid: 2\noutputname: Two\nplugin: fifo\noutputenabled: 0", "", "multiple"}, + {"select named disabled", "outputid: 1\noutputname: One\nplugin: fifo\noutputenabled: 1\noutputid: 2\noutputname: Two\nplugin: fifo\noutputenabled: 0", "Two", "disabled"}, + {"legacy named", "outputid: 1\noutputname: Visualizer\noutputenabled: 1", "Visualizer", ""}, + {"legacy unidentified", "outputid: 1\noutputname: Visualizer\noutputenabled: 1", "", "no matching"}, + {"wrong plugin", "outputid: 1\noutputname: Visualizer\nplugin: alsa\noutputenabled: 1", "Visualizer", "no matching"}, + {"empty", "", "", "no matching"}, + } { + t.Run(tt.name, func(t *testing.T) { + err := fifoOutputState(parseOutputs(strings.Split(tt.response, "\n")), tt.output) + if tt.want == "" { + if err != nil { + t.Fatal(err) + } + } else if err == nil || !strings.Contains(err.Error(), tt.want) { + t.Fatalf("got %v, want error containing %q", err, tt.want) + } + }) + } +} diff --git a/player/mpd/player.go b/player/mpd/player.go index ef7449f..c367045 100644 --- a/player/mpd/player.go +++ b/player/mpd/player.go @@ -33,6 +33,8 @@ type Player struct { Version Version name string + audioSource config.Audio + fifoState string addr string password string idleSubsystems []string @@ -226,6 +228,10 @@ func (p *Player) sendDataCommand(conn *clientConn, format string, args ...interf if line == "OK" { break } + if strings.HasPrefix(line, "ACK ") { + err = textproto.ProtocolError(line) + break + } lines = append(lines, line) } @@ -443,6 +449,7 @@ func (p *Player) mainLoop(conn *clientConn, events chan player.Event) { defer ticker.Stop() var err error + var outputCheck time.Time loop: for { select { @@ -469,6 +476,10 @@ loop: } case <-ticker.C: + if p.audioSource.Type == "fifo" && time.Since(outputCheck) >= 15*time.Second { + outputCheck = time.Now() + p.checkFIFOOutput(conn) + } var status map[string]string if status, err = p.sendMapCommand(conn, "status"); err != nil { log.WithError(err).Error("error retrieving status") diff --git a/player/volumio/player.go b/player/volumio/player.go index f53a55e..e20fec5 100644 --- a/player/volumio/player.go +++ b/player/volumio/player.go @@ -67,6 +67,8 @@ func (p *Player) Events() (<-chan player.Event, error) { return p.mpd.Events() } +func (p *Player) SetAudioSource(source config.Audio) { p.mpd.SetAudioSource(source) } + func (p *Player) AlbumArt(t *player.Track) (image.Image, bool) { log := p.log()