2020-02-25 09:46:16 +03:00
|
|
|
/*
|
2020-02-28 06:27:58 +03:00
|
|
|
DESCRIPTION
|
|
|
|
Looper is a program that loops an audio file.
|
2020-02-25 09:46:16 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Ella Pietraroia <ella@ausocean.org>
|
2020-02-28 06:27:58 +03:00
|
|
|
Scott Barnard <scott@ausocean.org>
|
2020-03-06 08:38:32 +03:00
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
2020-02-25 09:46:16 +03:00
|
|
|
|
|
|
|
LICENSE
|
2020-02-28 05:55:07 +03:00
|
|
|
audio player is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
|
2020-02-25 09:46:16 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-02-28 05:55:07 +03:00
|
|
|
// Audio looper.
|
2020-02-25 09:46:16 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-02-28 08:58:02 +03:00
|
|
|
"flag"
|
2020-03-06 08:38:32 +03:00
|
|
|
"fmt"
|
2020-02-25 09:46:16 +03:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
2020-02-28 07:00:55 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
2020-02-25 09:46:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-02-28 08:58:02 +03:00
|
|
|
// Command line flags.
|
|
|
|
var soundFilePtr = flag.String("path", "", "Location of sound file")
|
|
|
|
flag.Parse()
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-02-28 07:00:55 +03:00
|
|
|
// 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,
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
|
2020-02-28 07:00:55 +03:00
|
|
|
// Create logger that we call methods on to log.
|
|
|
|
log := logger.New(logVerbosity, fileLog, logSuppress)
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-02-28 05:55:07 +03:00
|
|
|
// Pi model specific initialisation
|
2020-02-28 07:00:55 +03:00
|
|
|
initCommand(log)
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-02-27 06:59:45 +03:00
|
|
|
// Infinite loop that outputs audio and gathers debug information.
|
2020-02-28 06:27:58 +03:00
|
|
|
var numPlays int
|
2020-03-02 11:02:28 +03:00
|
|
|
var fatal bool
|
2020-02-25 09:46:16 +03:00
|
|
|
for {
|
|
|
|
numPlays++
|
2020-02-28 07:00:55 +03:00
|
|
|
log.Log(logger.Debug, "playing audio", "play number", numPlays)
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-02-28 08:58:02 +03:00
|
|
|
cmd := exec.Command(audioCmd, *soundFilePtr)
|
2020-02-25 09:46:16 +03:00
|
|
|
|
|
|
|
var stdoutBuf, stderrBuf bytes.Buffer
|
2020-03-02 05:03:01 +03:00
|
|
|
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())
|
|
|
|
}
|
2020-02-25 09:46:16 +03:00
|
|
|
|
|
|
|
var errStdout, errStderr error
|
|
|
|
stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
|
|
|
|
stderr := io.MultiWriter(os.Stderr, &stderrBuf)
|
|
|
|
|
2020-03-02 05:03:01 +03:00
|
|
|
err = cmd.Start()
|
2020-02-25 09:46:16 +03:00
|
|
|
if err != nil {
|
2020-03-02 11:02:28 +03:00
|
|
|
log.Log(logger.Error, "cmd.Start() failed", "error", err.Error())
|
|
|
|
fatal = true
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-03-02 11:02:28 +03:00
|
|
|
log.Log(logger.Error, "cmd.Run() failed", "error", err.Error())
|
|
|
|
fatal = true
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
if errStdout != nil || errStderr != nil {
|
2020-03-02 11:02:28 +03:00
|
|
|
log.Log(logger.Error, "failed to capture stdout or stderr")
|
|
|
|
fatal = true
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
|
2020-02-28 07:00:55 +03:00
|
|
|
log.Log(logger.Debug, "stdout received", "stdout", outStr)
|
|
|
|
log.Log(logger.Debug, "stderr received", "stderr", errStr)
|
2020-03-02 11:02:28 +03:00
|
|
|
|
|
|
|
if fatal == true {
|
|
|
|
log.Log(logger.Fatal, "a fatal error has occured while trying to play (see above)")
|
|
|
|
}
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
}
|
2020-03-06 08:38:32 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|