2020-02-25 09:46:16 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2020-02-27 06:59:45 +03:00
|
|
|
test-audio/looper0/main.go
|
2020-02-25 09:46:16 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Ella Pietraroia <ella@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2020-02-27 06:59:45 +03:00
|
|
|
revid is 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-02-27 06:59:45 +03:00
|
|
|
// Audio looper for pi0 model
|
2020-02-25 09:46:16 +03:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-02-27 06:59:45 +03:00
|
|
|
soundFile := "/home/pi/48khz.wav"
|
2020-02-26 05:00:34 +03:00
|
|
|
soundcardPath := "/usr/share/doc/audioInjector/asound.state.RCA.thru.test"
|
|
|
|
logFile := "audio.log"
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-02-27 06:59:45 +03:00
|
|
|
// Making log file.
|
2020-02-26 05:00:34 +03:00
|
|
|
_, err := os.Stat(logFile)
|
2020-02-25 09:46:16 +03:00
|
|
|
if !os.IsNotExist(err) {
|
2020-02-26 05:00:34 +03:00
|
|
|
err := os.Remove(logFile)
|
2020-02-25 09:46:16 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("fatal: error clearing file: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 06:59:45 +03:00
|
|
|
f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE, 0666)
|
2020-02-25 09:46:16 +03:00
|
|
|
if err != nil {
|
2020-02-26 05:00:34 +03:00
|
|
|
log.Fatalf("fatal: error opening file %s: %v", logFile, err)
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
log.SetOutput(f)
|
|
|
|
|
2020-02-27 06:59:45 +03:00
|
|
|
path, err := exec.LookPath("alsactl") // alsactl is a command that ensures the sound will be played through the correct soundcard
|
2020-02-25 09:46:16 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("fatal: didn't find 'alsactl' executable\n")
|
|
|
|
} else {
|
2020-02-27 06:59:45 +03:00
|
|
|
log.Printf("debug: 'alsactl' executable is in '%s'\n", path)
|
2020-02-26 08:35:03 +03:00
|
|
|
}
|
|
|
|
|
2020-02-25 09:46:16 +03:00
|
|
|
for {
|
2020-02-26 05:00:34 +03:00
|
|
|
cmdInit := exec.Command("alsactl", "-f", soundcardPath, "restore")
|
2020-02-25 09:46:16 +03:00
|
|
|
err := cmdInit.Run()
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2020-02-26 05:00:34 +03:00
|
|
|
log.Printf("fatal(ish): cmd.Run() for 'alsactl' failed with '%s'\n", err)
|
2020-02-25 09:46:16 +03:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
|
2020-02-27 06:59:45 +03:00
|
|
|
// Making sure that play command is on the pi.
|
2020-02-28 01:55:52 +03:00
|
|
|
path, err = exec.LookPath("play")
|
2020-02-25 09:46:16 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("fatal: didn't find 'play' executable\n")
|
|
|
|
} else {
|
2020-02-27 06:59:45 +03:00
|
|
|
log.Printf("debug: 'play' executable is in '%s'\n", path)
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
|
2020-02-27 06:59:45 +03:00
|
|
|
// Infinite loop that outputs audio and gathers debug information.
|
2020-02-25 09:46:16 +03:00
|
|
|
numPlays := 0
|
|
|
|
for {
|
|
|
|
numPlays++
|
2020-02-27 06:59:45 +03:00
|
|
|
log.Printf("debug: play number: %d\n", numPlays)
|
2020-02-25 09:46:16 +03:00
|
|
|
|
2020-02-26 05:00:34 +03:00
|
|
|
cmd := exec.Command("play", soundFile)
|
2020-02-25 09:46:16 +03:00
|
|
|
|
|
|
|
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 {
|
2020-02-26 05:00:34 +03:00
|
|
|
log.Fatalf("fatal: cmd.Start() for 'play' failed with '%s'\n", err)
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-02-26 05:00:34 +03:00
|
|
|
log.Fatalf("fatal: cmd.Run() for 'play' failed with %s\n", err)
|
2020-02-25 09:46:16 +03:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|