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-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-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-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
|
|
|
|
stdoutIn, _ := cmd.StdoutPipe()
|
|
|
|
stderrIn, _ := cmd.StderrPipe()
|
|
|
|
|
|
|
|
var errStdout, errStderr error
|
|
|
|
stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
|
|
|
|
stderr := io.MultiWriter(os.Stderr, &stderrBuf)
|
|
|
|
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
2020-02-28 07:00:55 +03:00
|
|
|
log.Log(logger.Fatal, "cmd.Start() for 'play' failed", "error", err.Error())
|
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-02-28 07:00:55 +03:00
|
|
|
log.Log(logger.Fatal, "cmd.Run() for 'play' failed", "error", err.Error())
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
if errStdout != nil || errStderr != nil {
|
2020-02-28 07:00:55 +03:00
|
|
|
log.Log(logger.Fatal, "failed to capture stdout or stderr")
|
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-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
}
|