From f2c9cc588195f38fd5acbf8b699072cb412219bd Mon Sep 17 00:00:00 2001 From: Trek H Date: Wed, 13 Nov 2019 17:11:35 +1030 Subject: [PATCH] alsa: renamed ALSA.buf to ALSA.pb since there are two buffers Also elaborated on the difference in the comments. --- cmd/audio-netsender/main.go | 28 ++++++++++++++-------------- device/alsa/alsa.go | 22 +++++++++++----------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/cmd/audio-netsender/main.go b/cmd/audio-netsender/main.go index 142c4441..ade701aa 100644 --- a/cmd/audio-netsender/main.go +++ b/cmd/audio-netsender/main.go @@ -79,8 +79,8 @@ type audioClient struct { // internals dev *yalsa.Device // audio input device - buf pcm.Buffer // Buffer to contain the recording. - rb *ring.Buffer // our buffer + pb pcm.Buffer // Buffer to contain the direct audio from ALSA. + rb *ring.Buffer // Ring buffer to contain processed audio ready to be read. ns *netsender.Sender // our NetSender vs int // our "var sum" to track var changes } @@ -146,12 +146,12 @@ func main() { Channels: ab.Format.Channels, Rate: ab.Format.Rate, } - ac.buf = pcm.Buffer{ + ac.pb = pcm.Buffer{ Format: cf, Data: ab.Data, } - recSize := (((len(ac.buf.Data) / ac.dev.BufferFormat().Channels) * ac.channels) / ac.dev.BufferFormat().Rate) * ac.rate + recSize := (((len(ac.pb.Data) / ac.dev.BufferFormat().Channels) * ac.channels) / ac.dev.BufferFormat().Rate) * ac.rate rbLen := rbDuration / ac.period ac.rb = ring.NewBuffer(rbLen, recSize, rbTimeout) @@ -344,7 +344,7 @@ func (ac *audioClient) input() { } log.Log(logger.Debug, "recording audio for period", "seconds", ac.period) ac.mu.Lock() - err := ac.dev.Read(ac.buf.Data) + err := ac.dev.Read(ac.pb.Data) ac.mu.Unlock() if err != nil { log.Log(logger.Debug, "device.Read failed", "error", err.Error()) @@ -386,7 +386,7 @@ func (ac *audioClient) input() { // This function also handles NetReceiver configuration requests and updating of NetReceiver vars. func (ac *audioClient) output() { // Calculate the size of the output data based on wanted channels and rate. - outLen := (((len(ac.buf.Data) / ac.buf.Format.Channels) * ac.channels) / ac.buf.Format.Rate) * ac.rate + outLen := (((len(ac.pb.Data) / ac.pb.Format.Channels) * ac.channels) / ac.pb.Format.Rate) * ac.rate buf := make([]byte, outLen) mime := "audio/x-wav;codec=pcm;rate=" + strconv.Itoa(ac.rate) + ";channels=" + strconv.Itoa(ac.channels) + ";bits=" + strconv.Itoa(ac.bits) @@ -533,17 +533,17 @@ func (ac *audioClient) formatBuffer() pcm.Buffer { ac.mu.Unlock() // If nothing needs to be changed, return the original. - if ac.buf.Format.Channels == wantChannels && ac.buf.Format.Rate == wantRate { - return ac.buf + if ac.pb.Format.Channels == wantChannels && ac.pb.Format.Rate == wantRate { + return ac.pb } - formatted := pcm.Buffer{Format: ac.buf.Format} + formatted := pcm.Buffer{Format: ac.pb.Format} bufCopied := false - if ac.buf.Format.Channels != wantChannels { + if ac.pb.Format.Channels != wantChannels { // Convert channels. - if ac.buf.Format.Channels == 2 && wantChannels == 1 { - if formatted, err = pcm.StereoToMono(ac.buf); err != nil { + if ac.pb.Format.Channels == 2 && wantChannels == 1 { + if formatted, err = pcm.StereoToMono(ac.pb); err != nil { log.Log(logger.Warning, "channel conversion failed, audio has remained stereo", "error", err.Error()) } else { formatted.Format.Channels = 1 @@ -552,13 +552,13 @@ func (ac *audioClient) formatBuffer() pcm.Buffer { } } - if ac.buf.Format.Rate != wantRate { + if ac.pb.Format.Rate != wantRate { // Convert rate. if bufCopied { formatted, err = pcm.Resample(formatted, wantRate) } else { - formatted, err = pcm.Resample(ac.buf, wantRate) + formatted, err = pcm.Resample(ac.pb, wantRate) } if err != nil { log.Log(logger.Warning, "rate conversion failed, audio has remained original rate", "error", err.Error()) diff --git a/device/alsa/alsa.go b/device/alsa/alsa.go index 47f614bc..f04af950 100644 --- a/device/alsa/alsa.go +++ b/device/alsa/alsa.go @@ -68,8 +68,8 @@ type ALSA struct { mu sync.Mutex // Provides synchronisation when changing modes concurrently. title string // Name of audio title, or empty for the default title. dev *yalsa.Device // ALSA device's Audio input device. - buf pcm.Buffer // Buffer to contain the recording. - rb *ring.Buffer // Our buffer. + pb pcm.Buffer // Buffer to contain the direct audio from ALSA. + rb *ring.Buffer // Ring buffer to contain processed audio ready to be read. chunkSize int // This is the number of bytes that will be stored in rb at a time. Config // Configuration parameters for this device. } @@ -144,13 +144,13 @@ func (d *ALSA) Set(c config.Config) error { Channels: ab.Format.Channels, Rate: ab.Format.Rate, } - d.buf = pcm.Buffer{ + d.pb = pcm.Buffer{ Format: cf, Data: ab.Data, } // Account for channel conversion. - chunkSize := float64(len(d.buf.Data) / d.dev.BufferFormat().Channels * d.Channels) + chunkSize := float64(len(d.pb.Data) / d.dev.BufferFormat().Channels * d.Channels) // Account for resampling. chunkSize = (chunkSize / float64(d.dev.BufferFormat().Rate)) * float64(d.SampleRate) @@ -386,7 +386,7 @@ func (d *ALSA) input() { // Read from audio device. d.l.Log(logger.Debug, pkg+"recording audio for period", "seconds", d.RecPeriod) - err := d.dev.Read(d.buf.Data) + err := d.dev.Read(d.pb.Data) if err != nil { d.l.Log(logger.Debug, pkg+"read failed", "error", err.Error()) err = d.open() // re-open @@ -432,22 +432,22 @@ func (d *ALSA) formatBuffer() pcm.Buffer { var err error // If nothing needs to be changed, return the original. - if d.buf.Format.Channels == d.Channels && d.buf.Format.Rate == d.SampleRate { - return d.buf + if d.pb.Format.Channels == d.Channels && d.pb.Format.Rate == d.SampleRate { + return d.pb } var formatted pcm.Buffer - if d.buf.Format.Channels != d.Channels { + if d.pb.Format.Channels != d.Channels { // Convert channels. // TODO(Trek): Make this work for conversions other than stereo to mono. - if d.buf.Format.Channels == 2 && d.Channels == 1 { - formatted, err = pcm.StereoToMono(d.buf) + if d.pb.Format.Channels == 2 && d.Channels == 1 { + formatted, err = pcm.StereoToMono(d.pb) if err != nil { d.l.Log(logger.Fatal, pkg+"channel conversion failed", "error", err.Error()) } } } - if d.buf.Format.Rate != d.SampleRate { + if d.pb.Format.Rate != d.SampleRate { // Convert rate. formatted, err = pcm.Resample(formatted, d.SampleRate) if err != nil {