cmd/audio-player/looper: general clean up of pi0.go

This commit is contained in:
Saxon 2020-03-06 15:49:50 +10:30
parent a8dc1f78df
commit d0646f73bc
1 changed files with 18 additions and 12 deletions

View File

@ -29,6 +29,7 @@ LICENSE
package main package main
import ( import (
"fmt"
"os/exec" "os/exec"
"time" "time"
@ -38,26 +39,31 @@ import (
const audioCmd = "play" const audioCmd = "play"
func initCommand(log *logger.Logger) { func initCommand(log *logger.Logger) {
const soundcardPath = "/usr/share/doc/audioInjector/asound.state.RCA.thru.test" const (
const sleepDur = 1 * time.Second 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") path, err := exec.LookPath("alsactl")
if err != nil { 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") // Set up sound card using alsactl.
for cmdInit.Run() != nil { cmdInit := exec.Command("alsactl", "-f", cardPath, "restore")
log.Log(logger.Warning, "cmd.Run() for 'alsactl' failed", "error", err.Error()) err = cmdInit.Run()
time.Sleep(sleepDur) 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) path, err = exec.LookPath(audioCmd)
if err != nil { 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)
} }