av/cmd/audio-player/looper/main.go

138 lines
3.5 KiB
Go

/*
DESCRIPTION
Looper is a program that loops an audio file.
AUTHORS
Ella Pietraroia <ella@ausocean.org>
Scott Barnard <scott@ausocean.org>
Saxon Nelson-Milton <saxon@ausocean.org>
LICENSE
audio player is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
// Looper is a bare bones program for repeated playback of an audio file.
package main
import (
"bytes"
"flag"
"fmt"
"io"
"os"
"os/exec"
"sync"
"bitbucket.org/ausocean/utils/logger"
"gopkg.in/natefinch/lumberjack.v2"
)
func main() {
// Command line flags.
var soundFilePtr = flag.String("path", "", "Location of sound file")
flag.Parse()
// Logging constants.
const (
logPath = "/var/log/audiolooper/audiolooper.log"
logMaxSize = 500 // MB
logMaxBackup = 10
logMaxAge = 28 // days
logVerbosity = logger.Debug
logSuppress = true
)
// Create lumberjack logger to handle logging to file.
fileLog := &lumberjack.Logger{
Filename: logPath,
MaxSize: logMaxSize,
MaxBackups: logMaxBackup,
MaxAge: logMaxAge,
}
// Create logger that we call methods on to log.
log := logger.New(logVerbosity, fileLog, logSuppress)
// Pi model specific initialisation
initCommand(log)
// Infinite loop that outputs audio and gathers debug information.
var numPlays int
var fatal bool
for {
numPlays++
log.Log(logger.Debug, "playing audio", "play number", numPlays)
cmd := exec.Command(audioCmd, *soundFilePtr)
var stdoutBuf, stderrBuf bytes.Buffer
stdoutIn, err := cmd.StdoutPipe()
if err != nil {
log.Log(logger.Error, "failed to make stdout pipe", "error", err.Error())
}
stderrIn, err := cmd.StderrPipe()
if err != nil {
log.Log(logger.Error, "failed to make stderr pipe", "error", err.Error())
}
var errStdout, errStderr error
stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
stderr := io.MultiWriter(os.Stderr, &stderrBuf)
err = cmd.Start()
if err != nil {
log.Log(logger.Error, "cmd.Start() failed", "error", err.Error())
fatal = true
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
wg.Done()
}()
_, errStderr = io.Copy(stderr, stderrIn)
wg.Wait()
err = cmd.Wait()
if err != nil {
log.Log(logger.Error, "cmd.Run() failed", "error", err.Error())
fatal = true
}
if errStdout != nil || errStderr != nil {
log.Log(logger.Error, "failed to capture stdout or stderr")
fatal = true
}
outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
log.Log(logger.Debug, "stdout received", "stdout", outStr)
log.Log(logger.Debug, "stderr received", "stderr", errStr)
if fatal == true {
log.Log(logger.Fatal, "a fatal error has occured while trying to play (see above)")
}
}
}
func checkPath(cmd string, log *logger.Logger) {
path, err := exec.LookPath(cmd)
if err != nil {
log.Log(logger.Fatal, fmt.Sprintf("couldn't find %s", cmd), "error", err)
}
log.Log(logger.Debug, fmt.Sprintf("found %s", cmd), "path", path)
}