mirror of https://bitbucket.org/ausocean/av.git
105 lines
2.5 KiB
Go
105 lines
2.5 KiB
Go
/*
|
|
DESCRIPTION
|
|
Looper is a program that loops an audio file.
|
|
|
|
AUTHORS
|
|
Ella Pietraroia <ella@ausocean.org>
|
|
Scott Barnard <scott@ausocean.org>
|
|
|
|
LICENSE
|
|
audio player is 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.
|
|
*/
|
|
|
|
// Audio looper.
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"sync"
|
|
)
|
|
|
|
func main() {
|
|
const soundFile = "/home/pi/48khz.wav"
|
|
const logFile = "audio.log"
|
|
|
|
// Making log file.
|
|
_, err := os.Stat(logFile)
|
|
if !os.IsNotExist(err) {
|
|
err := os.Remove(logFile)
|
|
if err != nil {
|
|
log.Fatalf("fatal: error clearing file: %v", err)
|
|
}
|
|
}
|
|
|
|
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE, 0666)
|
|
if err != nil {
|
|
log.Fatalf("fatal: error opening file %s: %v", logFile, err)
|
|
}
|
|
defer f.Close()
|
|
log.SetOutput(f)
|
|
|
|
// Pi model specific initialisation
|
|
initCommand()
|
|
|
|
// Infinite loop that outputs audio and gathers debug information.
|
|
var numPlays int
|
|
for {
|
|
numPlays++
|
|
log.Printf("debug: play number: %d\n", numPlays)
|
|
|
|
cmd := exec.Command(audioCmd, soundFile)
|
|
|
|
var stdoutBuf, stderrBuf bytes.Buffer
|
|
stdoutIn, _ := cmd.StdoutPipe()
|
|
stderrIn, _ := cmd.StderrPipe()
|
|
|
|
var errStdout, errStderr error
|
|
stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
|
|
stderr := io.MultiWriter(os.Stderr, &stderrBuf)
|
|
|
|
err := cmd.Start()
|
|
if err != nil {
|
|
log.Fatalf("fatal: cmd.Start() for 'play' failed with '%s'\n", err)
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
_, errStdout = io.Copy(stdout, stdoutIn)
|
|
wg.Done()
|
|
}()
|
|
|
|
_, errStderr = io.Copy(stderr, stderrIn)
|
|
wg.Wait()
|
|
|
|
err = cmd.Wait()
|
|
if err != nil {
|
|
log.Fatalf("fatal: cmd.Run() for 'play' failed with %s\n", err)
|
|
}
|
|
if errStdout != nil || errStderr != nil {
|
|
log.Fatal("fatal: failed to capture stdout or stderr\n")
|
|
}
|
|
outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
|
|
log.Printf("\nout:\n%s\nerr:\n%s\n____________________________________________________\n\n", outStr, errStr)
|
|
}
|
|
|
|
}
|