Merged in looper-combined (pull request #385)

Audio looper for pi - v2

Approved-by: Saxon Milton <saxon.milton@gmail.com>
This commit is contained in:
Scott Barnard 2020-03-06 07:34:07 +00:00 committed by Saxon Milton
commit 3b0d004934
7 changed files with 334 additions and 0 deletions

View File

@ -0,0 +1,42 @@
# Install rc.local file
USER := $(shell whoami)
PATH := /usr/local/go/bin:$(PATH)
.SILENT:copy_files
.SILENT:hard_copy_files
.SILENT:build
.SILENT:clean
install: as_root copy_files build
@echo "Install complete"
install_hard: as_root hard_copy_files build
@echo "Install complete"
as_root:
ifneq ($(USER),root)
$(error Must run as superuser!)
endif
copy_files:
if [ -f /etc/rc.local ] ; then \
echo "/etc/rc.local left unmodified" ; \
else \
cp rc.local /etc; \
fi
hard_copy_files:
if [ -f /etc/rc.local ] ; then \
echo "Backed up rc.local to /etc/rc.local.bak" ; \
cp /etc/rc.local /etc/rc.local.bak ; \
fi
cp -f rc.local /etc
build:
if grep -q 'Raspberry Pi 3' '/proc/device-tree/model'; then \
echo "Compiling for Raspberry pi 3";\
go build -tags pi3;\
else \
echo "Compiling for Raspberry pi 0";\
go build -tags pi0;\
fi

View File

@ -0,0 +1,14 @@
# DESCRIPTION
looper is a process that will continually repeat playback of an audio file.
Intended hardware is raspberry pi 3 or raspberry pi zero (with audio injector
sound card hat, see http://www.audioinjector.net/rpi-zero).
# AUTHORS
Ella Pietraroia <ella@ausocean.org>
Saxon Nelson-Milton <saxon@ausocean.org>
# Pi Setup:
1) sudo make install_hard
2) systemctl enable rc-local
3) sudo systemctl start rc-local.service
4) To check that install steps were successful, restart device and confirm that sound plays.

View File

@ -0,0 +1,34 @@
// +build circleci
/*
DESCRIPTION
circleci.go defines a dummy initialisation command for running on circleci.
AUTHORS
Ella Pietraroia <ella@ausocean.org>
Scott Barnard <scott@ausocean.org>
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 main
import "bitbucket.org/ausocean/utils/logger"
const audioCmd = ""
func initCommand(log *logger.Logger) {}

View File

@ -0,0 +1,132 @@
/*
DESCRIPTION
Looper is a program that loops an audio file.
AUTHORS
Ella Pietraroia <ella@ausocean.org>
Scott Barnard <scott@ausocean.org>
Saxon Nelson-Milton <saxon@ausocean.org>
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.
*/
// Looper is a bare bones program for repeated playback of an audio file.
package main
import (
"bytes"
"flag"
"fmt"
"io"
"os/exec"
"bitbucket.org/ausocean/utils/logger"
"gopkg.in/natefinch/lumberjack.v2"
)
// Logging related constants.
const (
logPath = "/var/log/audiolooper/audiolooper.log"
logMaxSize = 500 // MB
logMaxBackup = 10
logMaxAge = 28 // days
logVerbosity = logger.Debug
logSuppress = true
)
func main() {
filePtr := flag.String("path", "", "Path to sound file we wish to play.")
flag.Parse()
// Create lumberjack logger to handle logging to file.
fileLog := &lumberjack.Logger{
Filename: logPath,
MaxSize: logMaxSize,
MaxBackups: logMaxBackup,
MaxAge: logMaxAge,
}
// Create logger that we call methods on to log.
log := logger.New(logVerbosity, fileLog, logSuppress)
// Call initialisation code that is specific to the platform (pi 0 or 3).
initCommand(log)
// Repeatedly play audio file.
var numPlays int
for {
cmd := exec.Command(audioCmd, *filePtr)
// We'd like to see what the playback software is outputting, so pipe
// stdout and stderr.
outPipe, err := cmd.StdoutPipe()
if err != nil {
log.Log(logger.Error, "failed to pipe stdout", "error", err)
}
errPipe, err := cmd.StderrPipe()
if err != nil {
log.Log(logger.Error, "failed to pipe stderr", "error", err)
}
// Start playback of the audio file.
err = cmd.Start()
if err != nil {
log.Log(logger.Error, "start failed", "error", err.Error())
continue
}
numPlays++
log.Log(logger.Debug, "playing audio", "numPlays", numPlays)
// Copy any std out to a buffer for logging.
var outBuff bytes.Buffer
go func() {
_, err = io.Copy(&outBuff, outPipe)
if err != nil {
log.Log(logger.Error, "failed to copy out pipe", "error", err)
}
}()
// 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)
}
}()
// Wait for playback to complete.
err = cmd.Wait()
if err != nil {
log.Log(logger.Error, "failed to wait for execution finish", "error", err.Error())
}
log.Log(logger.Debug, "stdout received", "stdout", string(outBuff.Bytes()))
// If there was any errors on stderr, log them.
if errBuff.Len() != 0 {
log.Log(logger.Error, "errors from stderr", "stderr", string(errBuff.Bytes()))
}
}
}
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)
}

View File

@ -0,0 +1,62 @@
// +build pi0
/*
DESCRIPTION
pi0.go defines an initialisation function for use when running on the
Raspberry Pi 0.
AUTHORS
Ella Pietraroia <ella@ausocean.org>
Scott Barnard <scott@ausocean.org>
Saxon Nelson-Milton <saxon@ausocean.org>
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 main
import (
"os/exec"
"time"
"bitbucket.org/ausocean/utils/logger"
)
const audioCmd = "play"
func initCommand(l *logger.Logger) {
const (
cardPath = "/usr/share/doc/audioInjector/asound.state.RCA.thru.test"
retryDur = 5 * time.Second
alsactl = "alsactl"
)
// Make sure utility to set up sound card, alsactl, exists.
checkPath(alsactl, l)
// Set up sound card using alsactl.
cmdInit := exec.Command(alsactl, "-f", cardPath, "restore")
err := cmdInit.Run()
for err != nil {
l.Log(logger.Warning, "alsactl run failed, retrying...", "error", err)
time.Sleep(retryDur)
err = cmdInit.Run()
}
// Make sure utility to play audio exists.
checkPath(audioCmd, l)
}

View File

@ -0,0 +1,38 @@
// +build pi3
/*
DESCRIPTION
pi3.go defines an initialisation function for use when running on the
Raspberry Pi 3.
AUTHORS
Ella Pietraroia <ella@ausocean.org>
Scott Barnard <scott@ausocean.org>
Saxon Nelson-Milton <saxon@ausocean.org>
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 main
import (
"bitbucket.org/ausocean/utils/logger"
)
const audioCmd = "omxplayer"
func initCommand(l *logger.Logger) { checkPath(audioCmd, l) }

View File

@ -0,0 +1,12 @@
#!/bin/sh -e
# /etc/rc.local
# This script launches audio looper at boot time
printf "rc.local started" # show start of execution
set -x # tell sh to display commands before execution
PLAYERPATH=/home/pi/go/src/bitbucket.org/ausocean/av/cmd/audio-player/looper
cd $PLAYERPATH
sudo ./looper -path=/home/pi/example.mp3 &
exit 0