Merged in issue-399-software-channels (pull request #502)

Software Channels

Approved-by: Trek Hopton
This commit is contained in:
David Sutton 2023-07-10 01:30:31 +00:00
commit 47b2b1576f
1 changed files with 83 additions and 74 deletions

View File

@ -35,7 +35,7 @@ import (
"io" "io"
"os/exec" "os/exec"
"strconv" "strconv"
"sync" "strings"
"time" "time"
"bitbucket.org/ausocean/av/container/mts" "bitbucket.org/ausocean/av/container/mts"
@ -78,17 +78,18 @@ const (
i2cPort = 1 i2cPort = 1
) )
// Treatment modes. // Channel modes.
const ( const (
modePaused = "Paused" modeStereo = "Stereo"
modeTreatment = "Play" modeLeft = "LeftMono"
modeCheck = "Check" modeRight = "RightMono"
modeMute = "Mute"
) )
// Variable map to send to netreceiver/vidgrind. // Variable map to send to netreceiver/vidgrind.
var varMap = map[string]string{ var varMap = map[string]string{
"mode": "enum:Paused,Play,Check", "SpeakerMode": "enum:"+strings.Join([]string{modeStereo, modeLeft, modeRight, modeMute}, ","),
"AudioFilePath": "string", "AudioFilePath": "string",
} }
func main() { func main() {
@ -131,6 +132,10 @@ func main() {
log.Fatal("could not initialise revid", "error", err) log.Fatal("could not initialise revid", "error", err)
} }
// Play the audio (audio will play even while muted).
log.Debug("Playing the audio")
go playAudio(filePtr, log)
// Start the control loop. // Start the control loop.
log.Debug("starting control loop") log.Debug("starting control loop")
run(rv, ns, filePtr, log, netLog) run(rv, ns, filePtr, log, netLog)
@ -139,12 +144,7 @@ func main() {
// run starts a control loop that runs netsender, sends logs, checks for var changes, and // 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) // if var changes, changes current mode (paused,audio playback or soundcheck)
func run(rv *revid.Revid, ns *netsender.Sender, file *string, l logging.Logger, nl *netlogger.Logger) { func run(rv *revid.Revid, ns *netsender.Sender, file *string, l logging.Logger, nl *netlogger.Logger) {
var ( var vs int
wg sync.WaitGroup
audioQuit chan struct{}
treating bool
vs int
)
for { for {
l.Debug("running netsender") l.Debug("running netsender")
@ -207,50 +207,80 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l logging.Logger,
} }
l.Debug("checking mode") l.Debug("checking mode")
switch ns.Mode() { _ = setChannels(vars["SpeakerMode"], l)
case modePaused:
stopAudio(&wg, &treating, audioQuit)
l.Info("mode is Paused, stopping revid")
rv.Stop()
case modeTreatment:
l.Debug("checking audio file path")
f := vars["AudioFilePath"]
if f != "" && *file != f {
file = &f
l.Info("updated audio file path, stopping audio", "AudioFilePath", f)
stopAudio(&wg, &treating, audioQuit)
}
if !treating {
l.Info("starting audio treatment")
rv.Stop()
audioQuit = make(chan struct{})
treating = true
wg.Add(1)
go playAudio(file, audioQuit, &wg, l)
}
case modeCheck:
stopAudio(&wg, &treating, audioQuit)
l.Info("sound checking")
err = rv.Start()
if err != nil {
l.Error("could not start revid", "error", err)
ns.SetMode(modePaused, &vs)
sleep(ns, l)
continue
}
default:
l.Warning("mode is not valid", "mode", ns.Mode())
}
l.Info("revid updated with new mode")
sleep(ns, l) sleep(ns, l)
} }
} }
// playAudio is intended to be run as a routine. It will repeatedly play an audio file until // setChannels handles the muting of one, both, or neither of the channels. It takes in SpeakerMode
// a signal is received to return. The entire audio file is played before the termination // and sets the relevant volumes.
// signal chan is checked. func setChannels(mode string, l logging.Logger) error {
func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l logging.Logger) { l.Info("mode is", "mode", mode)
// Set the volume of each channel.
vols := map[string]string{
modeStereo: "100%,100%",
modeLeft: "0%,100%",
modeRight: "100%,0%",
modeMute: "0%,0%",
}[mode]
if vols == "" {
l.Warning("invalid SpeakeMode", "SpeakerMode", mode)
return fmt.Errorf("invalid SpeakerMode: %s", mode)
}
// Create the command to change the channel volumes.
cmd := exec.Command("amixer", "sset", "Speaker", vols)
// Pipe the output to stdout and stderr.
outPipe, err := cmd.StdoutPipe()
if err != nil {
l.Error("unable to setup pipe to stdout", "error", err)
return fmt.Errorf("unable to setup pipe to stdout: %w", err)
}
errPipe, err := cmd.StderrPipe()
if err != nil {
l.Error("unable to setup pipe to stderr", "error", err)
return fmt.Errorf("unable to setup pipe to stderr: %w", err)
}
// Execute the channel setting command.
err = cmd.Start()
if err != nil {
l.Error("unable to set channel", "error", err)
return fmt.Errorf("unable to set channel: %w", err)
}
// 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)
}
l.Info("command run", "stdout", outBuff)
}()
// 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)
}
l.Error("command failed", "stderr", errBuff)
}()
if errBuff.String() != "" {
return fmt.Errorf("channel set command failed: %s", &errBuff)
}
l.Info("mode set to", "mode", mode)
return nil
}
// playAudio is intended to be run as a routine. It will continuously run even while muted.
func playAudio(file *string, l logging.Logger) {
var numPlays int var numPlays int
for { for {
cmd := exec.Command(audioCmd, *file) cmd := exec.Command(audioCmd, *file)
@ -303,30 +333,9 @@ func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l logging.L
if errBuff.Len() != 0 { if errBuff.Len() != 0 {
l.Error("errors from stderr", "stderr", string(errBuff.Bytes())) l.Error("errors from stderr", "stderr", string(errBuff.Bytes()))
} }
// Check for audio signal halt.
// TODO: work out better way to do this. Doing it this way means we have to wait for
// the audio file to finish playing.
select {
case <-quit:
wg.Done()
return
default:
}
} }
} }
// stopAudio signals to the playAudio routine to terminate and then waits for it to
// do so.
func stopAudio(wg *sync.WaitGroup, treating *bool, signal chan struct{}) {
if !*treating {
return
}
close(signal)
wg.Wait()
*treating = false
}
// sleep uses a delay to halt the program based on the monitoring period // sleep uses a delay to halt the program based on the monitoring period
// netsender parameter (mp) defined in the netsender.conf config. // netsender parameter (mp) defined in the netsender.conf config.
func sleep(ns *netsender.Sender, l logging.Logger) { func sleep(ns *netsender.Sender, l logging.Logger) {