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-03-06 08:41:27 +03:00
|
|
|
// Looper is a bare bones program for repeated playback of an audio file.
|
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/exec"
|
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
|
|
|
)
|
|
|
|
|
2020-03-06 09:48:32 +03:00
|
|
|
// Logging related constants.
|
|
|
|
const (
|
|
|
|
logPath = "/var/log/audiolooper/audiolooper.log"
|
|
|
|
logMaxSize = 500 // MB
|
|
|
|
logMaxBackup = 10
|
|
|
|
logMaxAge = 28 // days
|
|
|
|
logVerbosity = logger.Debug
|
|
|
|
logSuppress = true
|
|
|
|
)
|
|
|
|
|
2020-02-25 09:46:16 +03:00
|
|
|
func main() {
|
2020-03-06 09:48:32 +03:00
|
|
|
filePtr := flag.String("path", "", "Path to sound file we wish to play.")
|
2020-02-28 08:58:02 +03:00
|
|
|
flag.Parse()
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-02-28 07:00:55 +03:00
|
|
|
// 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-03-06 09:48:32 +03:00
|
|
|
// Call initialisation code that is specific to the platform (pi 0 or 3).
|
2020-02-28 07:00:55 +03:00
|
|
|
initCommand(log)
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-03-06 09:48:32 +03:00
|
|
|
// Repeatedly play audio file.
|
2020-02-28 06:27:58 +03:00
|
|
|
var numPlays int
|
2020-02-25 09:46:16 +03:00
|
|
|
for {
|
2020-03-06 09:48:32 +03:00
|
|
|
cmd := exec.Command(audioCmd, *filePtr)
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-03-06 09:48:32 +03:00
|
|
|
// 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 {
|
2020-03-06 09:48:32 +03:00
|
|
|
log.Log(logger.Error, "failed to pipe stdout", "error", err)
|
2020-03-02 05:03:01 +03:00
|
|
|
}
|
2020-03-06 09:48:32 +03:00
|
|
|
errPipe, err := cmd.StderrPipe()
|
2020-03-02 05:03:01 +03:00
|
|
|
if err != nil {
|
2020-03-06 09:48:32 +03:00
|
|
|
log.Log(logger.Error, "failed to pipe stderr", "error", err)
|
2020-03-02 05:03:01 +03:00
|
|
|
}
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-03-06 09:48:32 +03:00
|
|
|
// Start playback of the audio file.
|
2020-03-02 05:03:01 +03:00
|
|
|
err = cmd.Start()
|
2020-02-25 09:46:16 +03:00
|
|
|
if err != nil {
|
2020-03-06 09:48:32 +03:00
|
|
|
log.Log(logger.Error, "start failed", "error", err.Error())
|
|
|
|
continue
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
2020-03-06 09:48:32 +03:00
|
|
|
numPlays++
|
|
|
|
log.Log(logger.Debug, "playing audio", "numPlays", numPlays)
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-03-06 09:48:32 +03:00
|
|
|
// Copy any std out to a buffer for logging.
|
|
|
|
var outBuff bytes.Buffer
|
2020-02-25 09:46:16 +03:00
|
|
|
go func() {
|
2020-03-06 09:48:32 +03:00
|
|
|
_, err = io.Copy(&outBuff, outPipe)
|
|
|
|
if err != nil {
|
|
|
|
log.Log(logger.Error, "failed to copy out pipe", "error", err)
|
|
|
|
}
|
2020-02-25 09:46:16 +03:00
|
|
|
}()
|
|
|
|
|
2020-03-06 09:48:32 +03:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}()
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-03-06 09:48:32 +03:00
|
|
|
// Wait for playback to complete.
|
2020-02-25 09:46:16 +03:00
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
2020-03-06 09:48:32 +03:00
|
|
|
log.Log(logger.Error, "failed to wait for execution finish", "error", err.Error())
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
2020-03-06 09:48:32 +03:00
|
|
|
log.Log(logger.Debug, "stdout received", "stdout", string(outBuff.Bytes()))
|
2020-03-02 11:02:28 +03:00
|
|
|
|
2020-03-06 09:48:32 +03:00
|
|
|
// If there was any errors on stderr, log them.
|
|
|
|
if errBuff.Len() != 0 {
|
|
|
|
log.Log(logger.Error, "errors from stderr", "stderr", string(errBuff.Bytes()))
|
2020-03-02 11:02:28 +03:00
|
|
|
}
|
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)
|
|
|
|
}
|