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 10:25:13 +03:00
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
2020-02-25 09:46:16 +03:00
|
|
|
|
|
|
|
LICENSE
|
2020-03-06 10:25:13 +03:00
|
|
|
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-04-23 06:43:37 +03:00
|
|
|
// Package 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-03-09 07:46:17 +03:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2020-02-28 07:00:55 +03:00
|
|
|
|
2020-03-09 07:46:17 +03:00
|
|
|
"bitbucket.org/ausocean/iot/pi/netlogger"
|
|
|
|
"bitbucket.org/ausocean/iot/pi/netsender"
|
|
|
|
"bitbucket.org/ausocean/iot/pi/sds"
|
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-03-09 07:46:17 +03:00
|
|
|
// Netsender related consts.
|
|
|
|
const (
|
|
|
|
netSendRetryTime = 5 * time.Second
|
|
|
|
defaultSleepTime = 60 // Seconds
|
|
|
|
)
|
|
|
|
|
|
|
|
// Looper modes.
|
|
|
|
const (
|
|
|
|
modeNormal = "Normal"
|
|
|
|
modePaused = "Paused"
|
|
|
|
)
|
|
|
|
|
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-03-09 07:46:17 +03:00
|
|
|
// Create a netlogger to deal with logging to cloud.
|
|
|
|
nl := netlogger.New()
|
|
|
|
|
|
|
|
// Create logger that we call methods on to l.
|
|
|
|
l := logger.New(logVerbosity, io.MultiWriter(fileLog, nl), 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-03-09 07:46:17 +03:00
|
|
|
initCommand(l)
|
|
|
|
|
|
|
|
// Create netsender client.
|
|
|
|
ns, err := netsender.New(l, nil, readPin(), nil)
|
|
|
|
if err != nil {
|
|
|
|
l.Log(logger.Fatal, "could not initialise netsender client", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This routine will deal with things that need to happen with the netsender client.
|
|
|
|
go run(ns, l, nl)
|
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-09 07:46:17 +03:00
|
|
|
l.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-09 07:46:17 +03:00
|
|
|
l.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-09 07:46:17 +03:00
|
|
|
l.Log(logger.Error, "start failed", "error", err.Error())
|
2020-03-06 09:48:32 +03:00
|
|
|
continue
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
2020-03-06 09:48:32 +03:00
|
|
|
numPlays++
|
2020-03-09 07:46:17 +03:00
|
|
|
l.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 {
|
2020-03-09 07:46:17 +03:00
|
|
|
l.Log(logger.Error, "failed to copy out pipe", "error", err)
|
2020-03-06 09:48:32 +03:00
|
|
|
}
|
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 {
|
2020-03-09 07:46:17 +03:00
|
|
|
l.Log(logger.Error, "failed to copy error pipe", "error", err)
|
2020-03-06 09:48:32 +03:00
|
|
|
}
|
|
|
|
}()
|
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-09 07:46:17 +03:00
|
|
|
l.Log(logger.Error, "failed to wait for execution finish", "error", err.Error())
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
2020-03-09 07:46:17 +03:00
|
|
|
l.Log(logger.Debug, "stdout received", "stdout", string(outBuff.Bytes()))
|
2020-03-02 11:02:28 +03:00
|
|
|
|
2020-03-09 07:46:17 +03:00
|
|
|
// If there was any errors on stderr, l them.
|
2020-03-06 09:48:32 +03:00
|
|
|
if errBuff.Len() != 0 {
|
2020-03-09 07:46:17 +03:00
|
|
|
l.Log(logger.Error, "errors from stderr", "stderr", string(errBuff.Bytes()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run is a routine to deal with netsender related tasks.
|
|
|
|
func run(ns *netsender.Sender, l *logger.Logger, nl *netlogger.Logger) {
|
|
|
|
var vs int
|
|
|
|
for {
|
|
|
|
err := ns.Run()
|
|
|
|
if err != nil {
|
|
|
|
l.Log(logger.Warning, "Run Failed. Retrying...", "error", err)
|
|
|
|
time.Sleep(netSendRetryTime)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = nl.Send(ns)
|
|
|
|
if err != nil {
|
|
|
|
l.Log(logger.Warning, "Logs could not be sent", "error", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If var sum hasn't changed we skip rest of loop.
|
|
|
|
newVs := ns.VarSum()
|
|
|
|
if vs == newVs {
|
|
|
|
sleep(ns, l)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vs = newVs
|
|
|
|
|
|
|
|
vars, err := ns.Vars()
|
|
|
|
if err != nil {
|
|
|
|
l.Log(logger.Error, "netSender failed to get vars", "error", err)
|
|
|
|
time.Sleep(netSendRetryTime)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure looper based on vars.
|
|
|
|
err = update(vars)
|
|
|
|
if err != nil {
|
|
|
|
l.Log(logger.Warning, "couldn't update with new vars", "error", err)
|
|
|
|
sleep(ns, l)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: consider handling of any modes ? We'd likely have paused and
|
|
|
|
// normal for the audio looper.
|
|
|
|
switch ns.Mode() {
|
|
|
|
case modePaused:
|
|
|
|
case modeNormal:
|
2020-03-02 11:02:28 +03:00
|
|
|
}
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
}
|
2020-03-06 08:38:32 +03:00
|
|
|
|
2020-03-09 07:46:17 +03:00
|
|
|
// checkPath wraps the use of lookPath to check the existence of executables
|
|
|
|
// that will be used by the audio looper.
|
|
|
|
func checkPath(cmd string, l *logger.Logger) {
|
2020-03-06 08:38:32 +03:00
|
|
|
path, err := exec.LookPath(cmd)
|
|
|
|
if err != nil {
|
2020-03-09 07:46:17 +03:00
|
|
|
l.Log(logger.Fatal, fmt.Sprintf("couldn't find %s", cmd), "error", err)
|
|
|
|
}
|
|
|
|
l.Log(logger.Debug, fmt.Sprintf("found %s", cmd), "path", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sleep uses a delay to halt the program based on the monitoring period
|
|
|
|
// netsender parameter (mp) defined in the netsender.conf config.
|
|
|
|
func sleep(ns *netsender.Sender, l *logger.Logger) {
|
|
|
|
t, err := strconv.Atoi(ns.Param("mp"))
|
|
|
|
if err != nil {
|
|
|
|
l.Log(logger.Error, "could not get sleep time, using default", "error", err)
|
|
|
|
t = defaultSleepTime
|
2020-03-06 08:38:32 +03:00
|
|
|
}
|
2020-03-09 07:46:17 +03:00
|
|
|
time.Sleep(time.Duration(t) * time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// readPin provides a callback function of consistent signature for use by
|
|
|
|
// netsender to retrieve software defined pin values.
|
|
|
|
func readPin() func(pin *netsender.Pin) error {
|
|
|
|
return func(pin *netsender.Pin) error {
|
|
|
|
switch {
|
|
|
|
case pin.Name == "X23":
|
|
|
|
pin.Value = -1
|
|
|
|
case pin.Name[0] == 'X':
|
|
|
|
return sds.ReadSystem(pin)
|
|
|
|
default:
|
|
|
|
pin.Value = -1
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update is currently a stub, but might used to update looper related params
|
|
|
|
// in future.
|
|
|
|
func update(v map[string]string) error {
|
|
|
|
return nil
|
2020-03-06 08:38:32 +03:00
|
|
|
}
|