From d0646f73bc2dba186c98f3d26a68c1b782c89b00 Mon Sep 17 00:00:00 2001 From: Saxon Date: Fri, 6 Mar 2020 15:49:50 +1030 Subject: [PATCH] cmd/audio-player/looper: general clean up of pi0.go --- cmd/audio-player/looper/pi0.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/cmd/audio-player/looper/pi0.go b/cmd/audio-player/looper/pi0.go index b758889c..0d4e4986 100644 --- a/cmd/audio-player/looper/pi0.go +++ b/cmd/audio-player/looper/pi0.go @@ -29,6 +29,7 @@ LICENSE package main import ( + "fmt" "os/exec" "time" @@ -38,26 +39,31 @@ import ( const audioCmd = "play" func initCommand(log *logger.Logger) { - const soundcardPath = "/usr/share/doc/audioInjector/asound.state.RCA.thru.test" - const sleepDur = 1 * time.Second + const ( + cardPath = "/usr/share/doc/audioInjector/asound.state.RCA.thru.test" + retryDur = 5 * time.Second + ) - // alsactl is a command that ensures the sound will be played through the correct soundcard. + // Make sure utility to set up sound card, alsactl, exists. path, err := exec.LookPath("alsactl") if err != nil { - log.Log(logger.Fatal, "didn't find 'alsactl' executable") + log.Log(logger.Fatal, "couldn't find alsactl", "error", err) } - log.Log(logger.Debug, "'alsactl' executable path", "path", path) + log.Log(logger.Debug, "alsactl found", "path", path) - cmdInit := exec.Command("alsactl", "-f", soundcardPath, "restore") - for cmdInit.Run() != nil { - log.Log(logger.Warning, "cmd.Run() for 'alsactl' failed", "error", err.Error()) - time.Sleep(sleepDur) + // Set up sound card using alsactl. + cmdInit := exec.Command("alsactl", "-f", cardPath, "restore") + err = cmdInit.Run() + for err != nil { + log.Log(logger.Warning, "alsactl run failed, retrying...", "error", err) + time.Sleep(retryDur) + err = cmdInit.Run() } - // Making sure that play command is on the pi. + // Make sure utility to play audio exists. path, err = exec.LookPath(audioCmd) if err != nil { - log.Log(logger.Fatal, "didn't find 'play' executable") + log.Log(logger.Fatal, "couldn't find 'play' executable", "error", err) } - log.Log(logger.Debug, "'play' executable path", "path", path) + log.Log(logger.Debug, fmt.Sprintf("%s found", audioCmd), "path", path) }