Monitor MPD FIFO output status for audio sources

This commit is contained in:
maze committed 2026-10-29 17:38:07 +01:00
1 parent 1b2285fc91
commit f2c9aeb3a8
9 files changed
+158

No files matched your search

+10
View File
@@ -59,6 +59,16 @@ During the shader, running various shader effects, running 44.1kHz stereo audio:
## Setup ## 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: Minimal requirements:
- Linux Computer (Raspberry Pi 3B+) - Linux Computer (Raspberry Pi 3B+)
- Supported display - Supported display
+4
View File
@@ -161,6 +161,10 @@ audio {
# Path to the audio stream. # Path to the audio stream.
path = "/tmp/beatglow.fifo" 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 is the sampling rate.
rate = 44100 rate = 44100
+1
View File
@@ -88,6 +88,7 @@ type Encoder struct {
// Audio source provides PCM samples. // Audio source provides PCM samples.
type Audio struct { type Audio struct {
Output string `hcl:"output,optional"`
Type string `hcl:"type"` Type string `hcl:"type"`
Path string `hcl:"path,optional"` Path string `hcl:"path,optional"`
Host string `hcl:"host,optional"` Host string `hcl:"host,optional"`
+7
View File
@@ -30,6 +30,10 @@ type Controller struct {
recorderPalette color.Palette recorderPalette color.Palette
} }
type audioSourceConfigurator interface {
SetAudioSource(config.Audio)
}
func New(config *config.Config) (*Controller, error) { func New(config *config.Config) (*Controller, error) {
log := logrus.NewEntry(logrus.StandardLogger()) log := logrus.NewEntry(logrus.StandardLogger())
@@ -59,6 +63,9 @@ func New(config *config.Config) (*Controller, error) {
c.teardown(log) c.teardown(log)
return nil, err return nil, err
} }
if p, ok := c.ctx.player.(audioSourceConfigurator); ok {
p.SetAudioSource(config.Audio)
}
if err := c.configureInputs(log, &config.Input); err != nil { if err := c.configureInputs(log, &config.Input); err != nil {
log.WithError(err).Error("fatal error setting up inputs") log.WithError(err).Error("fatal error setting up inputs")
c.teardown(log) c.teardown(log)
+29
View File
@@ -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")
}
}
+61
View File
@@ -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")
}
}
+33
View File
@@ -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)
}
})
}
}
+11
View File
@@ -33,6 +33,8 @@ type Player struct {
Version Version Version Version
name string name string
audioSource config.Audio
fifoState string
addr string addr string
password string password string
idleSubsystems []string idleSubsystems []string
@@ -226,6 +228,10 @@ func (p *Player) sendDataCommand(conn *clientConn, format string, args ...interf
if line == "OK" { if line == "OK" {
break break
} }
if strings.HasPrefix(line, "ACK ") {
err = textproto.ProtocolError(line)
break
}
lines = append(lines, line) lines = append(lines, line)
} }
@@ -443,6 +449,7 @@ func (p *Player) mainLoop(conn *clientConn, events chan player.Event) {
defer ticker.Stop() defer ticker.Stop()
var err error var err error
var outputCheck time.Time
loop: loop:
for { for {
select { select {
@@ -469,6 +476,10 @@ loop:
} }
case <-ticker.C: 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 var status map[string]string
if status, err = p.sendMapCommand(conn, "status"); err != nil { if status, err = p.sendMapCommand(conn, "status"); err != nil {
log.WithError(err).Error("error retrieving status") log.WithError(err).Error("error retrieving status")
+2
View File
@@ -67,6 +67,8 @@ func (p *Player) Events() (<-chan player.Event, error) {
return p.mpd.Events() 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) { func (p *Player) AlbumArt(t *player.Track) (image.Image, bool) {
log := p.log() log := p.log()