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

133 lines
3.5 KiB
Go
Raw Normal View History

/*
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
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/exec"
"bitbucket.org/ausocean/utils/logger"
"gopkg.in/natefinch/lumberjack.v2"
)
// Logging related constants.
const (
logPath = "/var/log/audiolooper/audiolooper.log"
logMaxSize = 500 // MB
logMaxBackup = 10
logMaxAge = 28 // days
logVerbosity = logger.Debug
logSuppress = true
)
func main() {
filePtr := flag.String("path", "", "Path to sound file we wish to play.")
flag.Parse()
// 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)
// Call initialisation code that is specific to the platform (pi 0 or 3).
initCommand(log)
// Repeatedly play audio file.
var numPlays int
for {
cmd := exec.Command(audioCmd, *filePtr)
// We'd like to see what the playback software is outputting, so pipe
// stdout and stderr.
outPipe, err := cmd.StdoutPipe()
2020-03-02 05:03:01 +03:00
if err != nil {
log.Log(logger.Error, "failed to pipe stdout", "error", err)
2020-03-02 05:03:01 +03:00
}
errPipe, err := cmd.StderrPipe()
2020-03-02 05:03:01 +03:00
if err != nil {
log.Log(logger.Error, "failed to pipe stderr", "error", err)
2020-03-02 05:03:01 +03:00
}
// Start playback of the audio file.
2020-03-02 05:03:01 +03:00
err = cmd.Start()
if err != nil {
log.Log(logger.Error, "start failed", "error", err.Error())
continue
}
numPlays++
log.Log(logger.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 {
log.Log(logger.Error, "failed to copy out pipe", "error", err)
}
}()
// Copy any std error to a buffer for logging.
var errBuff bytes.Buffer
go func() {
_, err = io.Copy(&errBuff, errPipe)
if err != nil {
log.Log(logger.Error, "failed to copy error pipe", "error", err)
}
}()
// Wait for playback to complete.
err = cmd.Wait()
if err != nil {
log.Log(logger.Error, "failed to wait for execution finish", "error", err.Error())
}
log.Log(logger.Debug, "stdout received", "stdout", string(outBuff.Bytes()))
// If there was any errors on stderr, log them.
if errBuff.Len() != 0 {
log.Log(logger.Error, "errors from stderr", "stderr", string(errBuff.Bytes()))
}
}
}
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)
}