mirror of https://bitbucket.org/ausocean/av.git
120 lines
3.0 KiB
Go
120 lines
3.0 KiB
Go
/*
|
|
DESCRIPTION
|
|
Looper is a program that loops an audio file.
|
|
|
|
AUTHORS
|
|
Ella Pietraroia <ella@ausocean.org>
|
|
Scott Barnard <scott@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.
|
|
*/
|
|
|
|
// Audio looper.
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"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
|
|
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.Fatal, "cmd.Start() for 'play' failed", "error", err.Error())
|
|
}
|
|
|
|
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.Fatal, "cmd.Run() for 'play' failed", "error", err.Error())
|
|
}
|
|
if errStdout != nil || errStderr != nil {
|
|
log.Log(logger.Fatal, "failed to capture stdout or stderr")
|
|
}
|
|
outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
|
|
log.Log(logger.Debug, "stdout received", "stdout", outStr)
|
|
log.Log(logger.Debug, "stderr received", "stderr", errStr)
|
|
}
|
|
}
|