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
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)
}