Merged in audiopath (pull request #453)

treatment: make audio file path configurable

Approved-by: Saxon Milton
This commit is contained in:
Trek Hopton 2021-02-15 04:47:29 +00:00
commit df614092d2
1 changed files with 48 additions and 40 deletions

View File

@ -82,7 +82,8 @@ const (
// Variable map to send to netreceiver/vidgrind.
var varMap = map[string]string{
"mode": "enum:Paused,Play,Check",
"mode": "enum:Paused,Play,Check",
"AudioFilePath": "string",
}
func main() {
@ -108,25 +109,25 @@ func main() {
log := logger.New(logVerbosity, io.MultiWriter(fileLog, netLog), logSuppress)
if *filePtr == "" {
log.Log(logger.Fatal, "no file path provided, check usage")
log.Fatal("no file path provided, check usage")
}
// The netsender client will handle communication with netreceiver and GPIO stuff.
log.Log(logger.Debug, "initialising netsender client")
log.Debug("initialising netsender client")
ns, err := netsender.New(log, gpio.InitPin, nil, gpio.WritePin, varMap)
if err != nil {
log.Log(logger.Fatal, "could not initialise netsender client", "error", err.Error())
log.Fatal("could not initialise netsender client", "error", err.Error())
}
// Revid will handle the recording and sending of audio for sound checking.
log.Log(logger.Debug, "initialising revid")
log.Debug("initialising revid")
rv, err := revid.New(config.Config{Logger: log}, ns)
if err != nil {
log.Log(logger.Fatal, "could not initialise revid", "error", err.Error())
log.Fatal("could not initialise revid", "error", err.Error())
}
// Start the control loop.
log.Log(logger.Debug, "starting control loop")
log.Debug("starting control loop")
run(rv, ns, filePtr, log, netLog)
}
@ -141,57 +142,64 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger,
)
for {
l.Log(logger.Debug, "running netsender")
l.Debug("running netsender")
err := ns.Run()
if err != nil {
l.Log(logger.Warning, "run failed. Retrying...", "error", err.Error())
l.Warning("run failed. Retrying...", "error", err.Error())
time.Sleep(netSendRetryTime)
continue
}
l.Log(logger.Debug, "sending logs")
l.Debug("sending logs")
err = nl.Send(ns)
if err != nil {
l.Log(logger.Warning, pkg+"Logs could not be sent", "error", err.Error())
l.Warning(pkg+"Logs could not be sent", "error", err.Error())
}
l.Log(logger.Debug, "checking varsum")
l.Debug("checking varsum")
newVs := ns.VarSum()
if vs == newVs {
sleep(ns, l)
continue
}
vs = newVs
l.Log(logger.Info, "varsum changed", "vs", vs)
l.Info("varsum changed", "vs", vs)
l.Log(logger.Debug, "getting new vars")
l.Debug("getting new vars")
vars, err := ns.Vars()
if err != nil {
l.Log(logger.Error, pkg+"netSender failed to get vars", "error", err.Error())
l.Error(pkg+"netSender failed to get vars", "error", err.Error())
time.Sleep(netSendRetryTime)
continue
}
l.Log(logger.Info, "got new vars", "vars", vars)
l.Info("got new vars", "vars", vars)
// Configure revid based on the vars.
l.Log(logger.Debug, "updating revid configuration")
l.Debug("updating revid configuration")
err = rv.Update(vars)
if err != nil {
l.Log(logger.Warning, pkg+"couldn't update revid", "error", err.Error())
l.Warning(pkg+"couldn't update revid", "error", err.Error())
sleep(ns, l)
continue
}
l.Log(logger.Info, "revid successfully reconfigured")
l.Info("revid successfully reconfigured")
l.Log(logger.Debug, "checking mode")
l.Debug("checking mode")
switch ns.Mode() {
case modePaused:
stopAudio(&wg, &treating, audioQuit)
l.Log(logger.Info, "mode is Paused, stopping revid")
l.Info("mode is Paused, stopping revid")
rv.Stop()
case modeTreatment:
l.Debug("checking audio file path")
f := vars["AudioFilePath"]
if f != "" && *file != f {
file = &f
l.Info("updated audio file path, stopping audio", "AudioFilePath", f)
stopAudio(&wg, &treating, audioQuit)
}
if !treating {
l.Log(logger.Info, "starting audio treatment")
l.Info("starting audio treatment")
rv.Stop()
audioQuit = make(chan struct{})
treating = true
@ -200,19 +208,19 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger,
}
case modeCheck:
stopAudio(&wg, &treating, audioQuit)
l.Log(logger.Info, "sound checking")
l.Info("sound checking")
err = rv.Start()
if err != nil {
l.Log(logger.Error, "could not start revid", "error", err.Error())
l.Error("could not start revid", "error", err.Error())
ns.SetMode(modePaused, &vs)
sleep(ns, l)
continue
}
default:
l.Log(logger.Warning, "mode is not valid", "mode", ns.Mode())
l.Warning("mode is not valid", "mode", ns.Mode())
}
l.Log(logger.Info, "revid updated with new mode")
l.Info("revid updated with new mode")
sleep(ns, l)
}
}
@ -228,28 +236,28 @@ func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l *logger.L
// stdout and stderr.
outPipe, err := cmd.StdoutPipe()
if err != nil {
l.Log(logger.Error, "failed to pipe stdout", "error", err)
l.Error("failed to pipe stdout", "error", err)
}
errPipe, err := cmd.StderrPipe()
if err != nil {
l.Log(logger.Error, "failed to pipe stderr", "error", err)
l.Error("failed to pipe stderr", "error", err)
}
// Start playback of the audio file.
err = cmd.Start()
if err != nil {
l.Log(logger.Error, "start failed", "error", err.Error())
l.Error("start failed", "error", err.Error())
continue
}
numPlays++
l.Log(logger.Debug, "playing audio", "numPlays", numPlays)
l.Debug("playing audio", "numPlays", numPlays)
// Copy any std out to a buffer for logging.
var outBuff bytes.Buffer
go func() {
_, err = io.Copy(&outBuff, outPipe)
if err != nil {
l.Log(logger.Error, "failed to copy out pipe", "error", err)
l.Error("failed to copy out pipe", "error", err)
}
}()
@ -258,20 +266,20 @@ func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l *logger.L
go func() {
_, err = io.Copy(&errBuff, errPipe)
if err != nil {
l.Log(logger.Error, "failed to copy error pipe", "error", err)
l.Error("failed to copy error pipe", "error", err)
}
}()
// Wait for playback to complete.
err = cmd.Wait()
if err != nil {
l.Log(logger.Error, "failed to wait for execution finish", "error", err.Error())
l.Error("failed to wait for execution finish", "error", err.Error())
}
l.Log(logger.Debug, "stdout received", "stdout", string(outBuff.Bytes()))
l.Debug("stdout received", "stdout", string(outBuff.Bytes()))
// If there was any errors on stderr, log them.
if errBuff.Len() != 0 {
l.Log(logger.Error, "errors from stderr", "stderr", string(errBuff.Bytes()))
l.Error("errors from stderr", "stderr", string(errBuff.Bytes()))
}
// Check for audio signal halt.
@ -300,14 +308,14 @@ func stopAudio(wg *sync.WaitGroup, treating *bool, signal chan struct{}) {
// sleep uses a delay to halt the program based on the monitoring period
// netsender parameter (mp) defined in the netsender.conf config.
func sleep(ns *netsender.Sender, l *logger.Logger) {
l.Log(logger.Debug, "sleeping")
l.Debug("sleeping")
t, err := strconv.Atoi(ns.Param("mp"))
if err != nil {
l.Log(logger.Error, pkg+"could not get sleep time, using default", "error", err)
l.Error(pkg+"could not get sleep time, using default", "error", err)
t = defaultSleepTime
}
time.Sleep(time.Duration(t) * time.Second)
l.Log(logger.Debug, "finished sleeping")
l.Debug("finished sleeping")
}
// checkPath wraps the use of lookPath to check the existence of executables
@ -315,7 +323,7 @@ func sleep(ns *netsender.Sender, l *logger.Logger) {
func checkPath(cmd string, l *logger.Logger) {
path, err := exec.LookPath(cmd)
if err != nil {
l.Log(logger.Fatal, fmt.Sprintf("couldn't find %s", cmd), "error", err)
l.Fatal(fmt.Sprintf("couldn't find %s", cmd), "error", err)
}
l.Log(logger.Debug, fmt.Sprintf("found %s", cmd), "path", path)
l.Debug(fmt.Sprintf("found %s", cmd), "path", path)
}