2020-08-14 09:10:22 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
treatment is a netsender client intended to provide audio playback control,
|
2020-08-14 10:56:47 +03:00
|
|
|
and speaker health checking by reversing signal and recording using revid.
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
2020-08-14 10:56:47 +03:00
|
|
|
Trek Hopton <trek@ausocean.org>
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
LICENSE
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package treatment is a program for playing and recording audio through a common
|
|
|
|
// speaker unit.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
2023-06-01 03:00:55 +03:00
|
|
|
"strings"
|
2020-08-14 09:10:22 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/container/mts"
|
|
|
|
"bitbucket.org/ausocean/av/container/mts/meta"
|
|
|
|
"bitbucket.org/ausocean/av/revid"
|
|
|
|
"bitbucket.org/ausocean/av/revid/config"
|
|
|
|
"bitbucket.org/ausocean/iot/pi/gpio"
|
|
|
|
"bitbucket.org/ausocean/iot/pi/netlogger"
|
|
|
|
"bitbucket.org/ausocean/iot/pi/netsender"
|
2022-05-27 09:12:52 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logging"
|
2021-03-03 14:12:01 +03:00
|
|
|
"github.com/kidoman/embd"
|
2020-08-21 14:51:13 +03:00
|
|
|
_ "github.com/kidoman/embd/host/rpi"
|
2020-08-14 09:10:22 +03:00
|
|
|
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Copyright information prefixed to all metadata.
|
|
|
|
const (
|
|
|
|
metaPreambleKey = "copyright"
|
|
|
|
metaPreambleData = "ausocean.org/license/content2020"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logging configuration.
|
|
|
|
const (
|
|
|
|
logPath = "/var/log/netsender/netsender.log"
|
|
|
|
logMaxSize = 500 // MB
|
|
|
|
logMaxBackup = 10
|
|
|
|
logMaxAge = 28 // days
|
2022-05-27 09:12:52 +03:00
|
|
|
logVerbosity = logging.Info
|
2020-08-14 09:10:22 +03:00
|
|
|
logSuppress = true
|
|
|
|
)
|
|
|
|
|
|
|
|
// Misc constants.
|
|
|
|
const (
|
|
|
|
netSendRetryTime = 5 * time.Second
|
|
|
|
defaultSleepTime = 60 // Seconds
|
|
|
|
pkg = "rv: "
|
2021-03-03 14:12:01 +03:00
|
|
|
minAmpVolume = 0
|
|
|
|
maxAmpVolume = 63
|
|
|
|
volAddr = 0x4B
|
2021-03-04 04:20:05 +03:00
|
|
|
i2cPort = 1
|
2020-08-14 09:10:22 +03:00
|
|
|
)
|
|
|
|
|
2023-05-19 05:35:31 +03:00
|
|
|
// Channel modes.
|
2020-08-14 09:10:22 +03:00
|
|
|
const (
|
2023-05-19 05:35:31 +03:00
|
|
|
modeStereo = "Stereo"
|
2024-04-17 10:17:57 +03:00
|
|
|
modeLeft = "LeftMono"
|
|
|
|
modeRight = "RightMono"
|
|
|
|
modeMute = "Mute"
|
2020-08-14 09:10:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Variable map to send to netreceiver/vidgrind.
|
|
|
|
var varMap = map[string]string{
|
2024-04-17 10:17:57 +03:00
|
|
|
"SpeakerMode": "enum:" + strings.Join([]string{modeStereo, modeLeft, modeRight, modeMute}, ","),
|
|
|
|
"AudioFilePath": "string",
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
2024-04-17 10:17:57 +03:00
|
|
|
const audioCmd = "aplay"
|
|
|
|
|
|
|
|
func initCommand(l logging.Logger) { checkPath(audioCmd, l) }
|
|
|
|
|
2020-08-14 09:10:22 +03:00
|
|
|
func main() {
|
|
|
|
mts.Meta = meta.NewWith([][2]string{{metaPreambleKey, metaPreambleData}})
|
|
|
|
|
|
|
|
// Set up the player command with audio file path.
|
2020-10-19 12:02:54 +03:00
|
|
|
filePtr := flag.String("path", "/home/pi/audio.wav", "Path to sound file we wish to play.")
|
2020-08-14 09:10:22 +03:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
// Create lumberjack logger to handle logging to file.
|
|
|
|
fileLog := &lumberjack.Logger{
|
|
|
|
Filename: logPath,
|
|
|
|
MaxSize: logMaxSize,
|
|
|
|
MaxBackups: logMaxBackup,
|
|
|
|
MaxAge: logMaxAge,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create netlogger to handle logging to cloud.
|
|
|
|
netLog := netlogger.New()
|
|
|
|
|
|
|
|
// Create logger that we call methods on to log, which in turn writes to the
|
|
|
|
// lumberjack and netloggers.
|
2022-05-27 09:12:52 +03:00
|
|
|
log := logging.New(logVerbosity, io.MultiWriter(fileLog, netLog), logSuppress)
|
2020-08-14 09:10:22 +03:00
|
|
|
|
2020-08-19 06:13:56 +03:00
|
|
|
if *filePtr == "" {
|
2021-02-15 03:33:37 +03:00
|
|
|
log.Fatal("no file path provided, check usage")
|
2020-08-19 06:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-08-14 09:10:22 +03:00
|
|
|
// The netsender client will handle communication with netreceiver and GPIO stuff.
|
2021-02-15 03:33:37 +03:00
|
|
|
log.Debug("initialising netsender client")
|
2022-05-25 08:31:24 +03:00
|
|
|
ns, err := netsender.New(log, gpio.InitPin, nil, gpio.WritePin, netsender.WithVarTypes(varMap))
|
2020-08-14 09:10:22 +03:00
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
log.Fatal("could not initialise netsender client", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Revid will handle the recording and sending of audio for sound checking.
|
2021-02-15 03:33:37 +03:00
|
|
|
log.Debug("initialising revid")
|
2020-09-14 04:52:11 +03:00
|
|
|
rv, err := revid.New(config.Config{Logger: log}, ns)
|
2020-08-14 09:10:22 +03:00
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
log.Fatal("could not initialise revid", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 03:00:55 +03:00
|
|
|
// Play the audio (audio will play even while muted).
|
2023-05-19 05:35:31 +03:00
|
|
|
log.Debug("Playing the audio")
|
|
|
|
go playAudio(filePtr, log)
|
|
|
|
|
2020-08-14 09:10:22 +03:00
|
|
|
// Start the control loop.
|
2021-02-15 03:33:37 +03:00
|
|
|
log.Debug("starting control loop")
|
2020-08-21 14:51:13 +03:00
|
|
|
run(rv, ns, filePtr, log, netLog)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// run starts a control loop that runs netsender, sends logs, checks for var changes, and
|
|
|
|
// if var changes, changes current mode (paused,audio playback or soundcheck)
|
2022-05-27 09:12:52 +03:00
|
|
|
func run(rv *revid.Revid, ns *netsender.Sender, file *string, l logging.Logger, nl *netlogger.Logger) {
|
2023-06-01 03:00:55 +03:00
|
|
|
var vs int
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
for {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("running netsender")
|
2020-08-14 09:10:22 +03:00
|
|
|
err := ns.Run()
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Warning("run failed. Retrying...", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
time.Sleep(netSendRetryTime)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("sending logs")
|
2020-08-14 09:10:22 +03:00
|
|
|
err = nl.Send(ns)
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Warning(pkg+"Logs could not be sent", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("checking varsum")
|
2020-08-14 09:10:22 +03:00
|
|
|
newVs := ns.VarSum()
|
|
|
|
if vs == newVs {
|
|
|
|
sleep(ns, l)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vs = newVs
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("varsum changed", "vs", vs)
|
2020-08-14 09:10:22 +03:00
|
|
|
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("getting new vars")
|
2020-08-14 09:10:22 +03:00
|
|
|
vars, err := ns.Vars()
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error(pkg+"netSender failed to get vars", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
time.Sleep(netSendRetryTime)
|
|
|
|
continue
|
|
|
|
}
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("got new vars", "vars", vars)
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
// Configure revid based on the vars.
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("updating revid configuration")
|
2020-08-14 09:10:22 +03:00
|
|
|
err = rv.Update(vars)
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Warning(pkg+"couldn't update revid", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
sleep(ns, l)
|
|
|
|
continue
|
|
|
|
}
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Info("revid successfully reconfigured")
|
2020-08-14 09:10:22 +03:00
|
|
|
|
2021-03-03 14:12:01 +03:00
|
|
|
l.Debug("checking amplifier volume")
|
|
|
|
v := vars["AmpVolume"]
|
|
|
|
if v != "" {
|
|
|
|
vol, err := strconv.ParseInt(v, 10, 8)
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error(pkg+"failed to parse amplifier volume", "error", err)
|
2021-03-03 14:12:01 +03:00
|
|
|
} else if vol < minAmpVolume || vol > maxAmpVolume {
|
|
|
|
l.Error(fmt.Sprintf("%s invalid amplifier volume, must be between %v and %v", pkg, minAmpVolume, maxAmpVolume), "volume", vol)
|
|
|
|
} else {
|
2021-03-04 04:20:05 +03:00
|
|
|
bus := embd.NewI2CBus(i2cPort)
|
2021-03-03 14:12:01 +03:00
|
|
|
err := bus.WriteByte(volAddr, byte(vol))
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error(pkg+"failed to write amplifier volume", "error", err)
|
2021-03-03 14:12:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("checking mode")
|
2023-06-01 03:00:55 +03:00
|
|
|
_ = setChannels(vars["SpeakerMode"], l)
|
2020-08-14 09:10:22 +03:00
|
|
|
sleep(ns, l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 03:00:55 +03:00
|
|
|
// setChannels handles the muting of one, both, or neither of the channels. It takes in SpeakerMode
|
2023-05-19 05:35:31 +03:00
|
|
|
// and sets the relevant volumes.
|
2024-04-17 10:17:57 +03:00
|
|
|
func setChannels(mode string, l logging.Logger) error {
|
2023-05-19 05:35:31 +03:00
|
|
|
l.Info("mode is", "mode", mode)
|
|
|
|
|
|
|
|
// Set the volume of each channel.
|
2023-06-01 03:00:55 +03:00
|
|
|
vols := map[string]string{
|
|
|
|
modeStereo: "100%,100%",
|
2024-04-17 10:17:57 +03:00
|
|
|
modeLeft: "0%,100%",
|
|
|
|
modeRight: "100%,0%",
|
|
|
|
modeMute: "0%,0%",
|
2023-06-01 03:00:55 +03:00
|
|
|
}[mode]
|
|
|
|
if vols == "" {
|
|
|
|
l.Warning("invalid SpeakeMode", "SpeakerMode", mode)
|
|
|
|
return fmt.Errorf("invalid SpeakerMode: %s", mode)
|
2023-05-19 05:35:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the command to change the channel volumes.
|
|
|
|
cmd := exec.Command("amixer", "sset", "Speaker", vols)
|
2024-04-17 10:17:57 +03:00
|
|
|
|
2023-05-19 05:35:31 +03:00
|
|
|
// Pipe the output to stdout and stderr.
|
|
|
|
outPipe, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2023-06-01 03:00:55 +03:00
|
|
|
l.Error("unable to setup pipe to stdout", "error", err)
|
|
|
|
return fmt.Errorf("unable to setup pipe to stdout: %w", err)
|
2023-05-19 05:35:31 +03:00
|
|
|
}
|
|
|
|
errPipe, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
2023-06-01 03:00:55 +03:00
|
|
|
l.Error("unable to setup pipe to stderr", "error", err)
|
|
|
|
return fmt.Errorf("unable to setup pipe to stderr: %w", err)
|
2023-05-19 05:35:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the channel setting command.
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
2023-06-01 03:00:55 +03:00
|
|
|
l.Error("unable to set channel", "error", err)
|
|
|
|
return fmt.Errorf("unable to set channel: %w", err)
|
2023-05-19 05:35:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy any std out to a buffer for logging.
|
|
|
|
var outBuff bytes.Buffer
|
|
|
|
go func() {
|
|
|
|
_, err = io.Copy(&outBuff, outPipe)
|
|
|
|
if err != nil {
|
|
|
|
l.Error("failed to copy out pipe", "error", err)
|
|
|
|
}
|
2023-06-01 03:00:55 +03:00
|
|
|
l.Info("command run", "stdout", outBuff)
|
2023-05-19 05:35:31 +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 {
|
|
|
|
l.Error("failed to copy error pipe", "error", err)
|
|
|
|
}
|
2023-06-01 03:00:55 +03:00
|
|
|
l.Error("command failed", "stderr", errBuff)
|
2023-05-19 05:35:31 +03:00
|
|
|
}()
|
2023-06-01 03:00:55 +03:00
|
|
|
if errBuff.String() != "" {
|
|
|
|
return fmt.Errorf("channel set command failed: %s", &errBuff)
|
|
|
|
}
|
|
|
|
|
2023-05-19 05:35:31 +03:00
|
|
|
l.Info("mode set to", "mode", mode)
|
2023-06-01 03:00:55 +03:00
|
|
|
return nil
|
2023-05-19 05:35:31 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 03:00:55 +03:00
|
|
|
// playAudio is intended to be run as a routine. It will continuously run even while muted.
|
2023-05-19 05:35:31 +03:00
|
|
|
func playAudio(file *string, l logging.Logger) {
|
2020-08-14 09:10:22 +03:00
|
|
|
var numPlays int
|
|
|
|
for {
|
2020-08-21 14:51:13 +03:00
|
|
|
cmd := exec.Command(audioCmd, *file)
|
2020-08-14 09:10:22 +03:00
|
|
|
// We'd like to see what the playback software is outputting, so pipe
|
|
|
|
// stdout and stderr.
|
|
|
|
outPipe, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error("failed to pipe stdout", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
errPipe, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error("failed to pipe stderr", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start playback of the audio file.
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error("start failed", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
numPlays++
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("playing audio", "numPlays", numPlays)
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
// Copy any std out to a buffer for logging.
|
|
|
|
var outBuff bytes.Buffer
|
|
|
|
go func() {
|
|
|
|
_, err = io.Copy(&outBuff, outPipe)
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error("failed to copy out pipe", "error", err)
|
2020-08-14 09:10:22 +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 {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error("failed to copy error pipe", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for playback to complete.
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
2021-03-04 04:20:05 +03:00
|
|
|
l.Error("failed to wait for execution finish", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("stdout received", "stdout", string(outBuff.Bytes()))
|
2020-08-14 09:10:22 +03:00
|
|
|
|
|
|
|
// If there was any errors on stderr, log them.
|
|
|
|
if errBuff.Len() != 0 {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error("errors from stderr", "stderr", string(errBuff.Bytes()))
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
2020-08-21 14:51:13 +03:00
|
|
|
}
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// sleep uses a delay to halt the program based on the monitoring period
|
|
|
|
// netsender parameter (mp) defined in the netsender.conf config.
|
2022-05-27 09:12:52 +03:00
|
|
|
func sleep(ns *netsender.Sender, l logging.Logger) {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("sleeping")
|
2020-08-14 09:10:22 +03:00
|
|
|
t, err := strconv.Atoi(ns.Param("mp"))
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Error(pkg+"could not get sleep time, using default", "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
t = defaultSleepTime
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(t) * time.Second)
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug("finished sleeping")
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkPath wraps the use of lookPath to check the existence of executables
|
|
|
|
// that will be used by the audio looper.
|
2022-05-27 09:12:52 +03:00
|
|
|
func checkPath(cmd string, l logging.Logger) {
|
2020-08-14 09:10:22 +03:00
|
|
|
path, err := exec.LookPath(cmd)
|
|
|
|
if err != nil {
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Fatal(fmt.Sprintf("couldn't find %s", cmd), "error", err)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|
2021-02-15 03:33:37 +03:00
|
|
|
l.Debug(fmt.Sprintf("found %s", cmd), "path", path)
|
2020-08-14 09:10:22 +03:00
|
|
|
}
|