mirror of https://bitbucket.org/ausocean/av.git
WIP
This commit is contained in:
parent
ee6046cab0
commit
b7267169c3
|
@ -90,7 +90,7 @@ const (
|
|||
logMaxBackup = 10
|
||||
logMaxAge = 28 // days
|
||||
logVerbosity = logging.Info
|
||||
logSuppress = true
|
||||
logSuppress = false
|
||||
)
|
||||
|
||||
// Revid modes.
|
||||
|
|
|
@ -31,6 +31,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -336,9 +337,14 @@ func (d *ALSA) open() error {
|
|||
bytesPerSecond := rate * channels * (bitdepth / 8)
|
||||
wantPeriodSize := int(float64(bytesPerSecond) * wantPeriod)
|
||||
nearWantPeriodSize := nearestPowerOfTwo(wantPeriodSize)
|
||||
periodSize, err := d.dev.NegotiatePeriodSize(nearWantPeriodSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d.l.Debug("alsa device period size set", "periodsize", periodSize)
|
||||
|
||||
// At least two period sizes should fit within the buffer.
|
||||
bufSize, err := d.dev.NegotiateBufferSize(nearWantPeriodSize * 2)
|
||||
bufSize, err := d.dev.NegotiateBufferSize(periodSize * 4)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -361,6 +367,9 @@ func (d *ALSA) input() {
|
|||
|
||||
// Read audio in 1 minute sections.
|
||||
go chunkingRead(d, ch)
|
||||
// Sleep for 1 minute before the first attempt to read.
|
||||
d.l.Debug("Sleeping before read", "sleep time", "1 minute")
|
||||
time.Sleep(time.Minute)
|
||||
|
||||
for {
|
||||
// Check mode.
|
||||
|
@ -384,18 +393,11 @@ func (d *ALSA) input() {
|
|||
return
|
||||
}
|
||||
|
||||
// Read from audio device.
|
||||
// Read audio chunk from channel.
|
||||
d.l.Debug("recording audio for period", "seconds", d.RecPeriod)
|
||||
err := d.dev.Read(d.pb.Data)
|
||||
if err != nil {
|
||||
d.l.Debug("read failed", "error", err.Error())
|
||||
err = d.open() // re-open
|
||||
if err != nil {
|
||||
d.l.Fatal("reopening device failed", "error", err.Error())
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
d.pb.Data = <-ch
|
||||
d.l.Debug("read audio from channel", "length read", len(d.pb.Data))
|
||||
d.l.Debug("first bytes of audio", "bytes [0:32]", fmt.Sprintf("%x", d.pb.Data[0:4]))
|
||||
|
||||
// Process audio.
|
||||
d.l.Debug("processing audio")
|
||||
|
@ -418,13 +420,37 @@ func (d *ALSA) input() {
|
|||
// audio is then chunked into the recording period set by d.RecPeriod and sent over
|
||||
// the channel.
|
||||
func chunkingRead(d *ALSA, ch chan []byte) {
|
||||
d.l.Debug("Datasize of recperiod", "datasize", d.DataSize())
|
||||
for {
|
||||
buf := d.dev.NewBufferDuration(time.Minute)
|
||||
// Read audio in 1 minute sections.
|
||||
d.dev.Read(d.pb.Data)
|
||||
d.l.Debug("Reading audio for 1 minute")
|
||||
err := d.dev.Read(buf.Data)
|
||||
if err != nil {
|
||||
d.l.Debug("read failed", "error", err.Error())
|
||||
err = d.open() // re-open
|
||||
if err != nil {
|
||||
d.l.Fatal("reopening device failed", "error", err.Error())
|
||||
return
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
file, err := os.Create("formatted.pcm")
|
||||
if err != nil {
|
||||
d.l.Error("unable to create output file", "error", err)
|
||||
continue
|
||||
}
|
||||
_, err = file.Write(buf.Data)
|
||||
if err != nil {
|
||||
d.l.Error("unable to write formatted audio to file", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Chunk the audio into length of RecPeriod.
|
||||
bytesPerRecPeriod := d.dev.BytesPerFrame() * d.dev.BufferFormat().Rate * int(d.RecPeriod)
|
||||
for i := 0; i < len(d.pb.Data); i += bytesPerRecPeriod {
|
||||
ch <- d.pb.Data[i:i+bytesPerRecPeriod-1]
|
||||
for i := 0; i < len(buf.Data); i += d.DataSize() {
|
||||
ch <- buf.Data[i:(i + d.DataSize())]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -470,6 +496,7 @@ func (d *ALSA) formatBuffer() pcm.Buffer {
|
|||
|
||||
// If nothing needs to be changed, return the original.
|
||||
if d.pb.Format.Channels == d.Channels && d.pb.Format.Rate == d.SampleRate {
|
||||
d.l.Debug("doing nothing in formatBuffer")
|
||||
return d.pb
|
||||
}
|
||||
var formatted pcm.Buffer
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
[Unit]
|
||||
Description=Netsender Client for Playing and Recording Sound
|
||||
requires=alsa-utils
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
|
|
|
@ -383,6 +383,8 @@ func (s *mtsSender) Write(d []byte) (int, error) {
|
|||
}
|
||||
}
|
||||
s.buf = s.buf[:0]
|
||||
} else {
|
||||
s.log.Debug("didn't meet initial conditions...")
|
||||
}
|
||||
return len(d), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue