alsa: renamed ALSA.buf to ALSA.pb since there are two buffers

Also elaborated on the difference in the comments.
This commit is contained in:
Trek H 2019-11-13 17:11:35 +10:30
parent 796a3b9a97
commit f2c9cc5881
2 changed files with 25 additions and 25 deletions

View File

@ -79,8 +79,8 @@ type audioClient struct {
// internals // internals
dev *yalsa.Device // audio input device dev *yalsa.Device // audio input device
buf pcm.Buffer // Buffer to contain the recording. pb pcm.Buffer // Buffer to contain the direct audio from ALSA.
rb *ring.Buffer // our buffer rb *ring.Buffer // Ring buffer to contain processed audio ready to be read.
ns *netsender.Sender // our NetSender ns *netsender.Sender // our NetSender
vs int // our "var sum" to track var changes vs int // our "var sum" to track var changes
} }
@ -146,12 +146,12 @@ func main() {
Channels: ab.Format.Channels, Channels: ab.Format.Channels,
Rate: ab.Format.Rate, Rate: ab.Format.Rate,
} }
ac.buf = pcm.Buffer{ ac.pb = pcm.Buffer{
Format: cf, Format: cf,
Data: ab.Data, 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 rbLen := rbDuration / ac.period
ac.rb = ring.NewBuffer(rbLen, recSize, rbTimeout) 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) log.Log(logger.Debug, "recording audio for period", "seconds", ac.period)
ac.mu.Lock() ac.mu.Lock()
err := ac.dev.Read(ac.buf.Data) err := ac.dev.Read(ac.pb.Data)
ac.mu.Unlock() ac.mu.Unlock()
if err != nil { if err != nil {
log.Log(logger.Debug, "device.Read failed", "error", err.Error()) 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. // This function also handles NetReceiver configuration requests and updating of NetReceiver vars.
func (ac *audioClient) output() { func (ac *audioClient) output() {
// Calculate the size of the output data based on wanted channels and rate. // 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) 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) 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() ac.mu.Unlock()
// If nothing needs to be changed, return the original. // If nothing needs to be changed, return the original.
if ac.buf.Format.Channels == wantChannels && ac.buf.Format.Rate == wantRate { if ac.pb.Format.Channels == wantChannels && ac.pb.Format.Rate == wantRate {
return ac.buf return ac.pb
} }
formatted := pcm.Buffer{Format: ac.buf.Format} formatted := pcm.Buffer{Format: ac.pb.Format}
bufCopied := false bufCopied := false
if ac.buf.Format.Channels != wantChannels { if ac.pb.Format.Channels != wantChannels {
// Convert channels. // Convert channels.
if ac.buf.Format.Channels == 2 && wantChannels == 1 { if ac.pb.Format.Channels == 2 && wantChannels == 1 {
if formatted, err = pcm.StereoToMono(ac.buf); err != nil { if formatted, err = pcm.StereoToMono(ac.pb); err != nil {
log.Log(logger.Warning, "channel conversion failed, audio has remained stereo", "error", err.Error()) log.Log(logger.Warning, "channel conversion failed, audio has remained stereo", "error", err.Error())
} else { } else {
formatted.Format.Channels = 1 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. // Convert rate.
if bufCopied { if bufCopied {
formatted, err = pcm.Resample(formatted, wantRate) formatted, err = pcm.Resample(formatted, wantRate)
} else { } else {
formatted, err = pcm.Resample(ac.buf, wantRate) formatted, err = pcm.Resample(ac.pb, wantRate)
} }
if err != nil { if err != nil {
log.Log(logger.Warning, "rate conversion failed, audio has remained original rate", "error", err.Error()) log.Log(logger.Warning, "rate conversion failed, audio has remained original rate", "error", err.Error())

View File

@ -68,8 +68,8 @@ type ALSA struct {
mu sync.Mutex // Provides synchronisation when changing modes concurrently. mu sync.Mutex // Provides synchronisation when changing modes concurrently.
title string // Name of audio title, or empty for the default title. title string // Name of audio title, or empty for the default title.
dev *yalsa.Device // ALSA device's Audio input device. dev *yalsa.Device // ALSA device's Audio input device.
buf pcm.Buffer // Buffer to contain the recording. pb pcm.Buffer // Buffer to contain the direct audio from ALSA.
rb *ring.Buffer // Our buffer. 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. chunkSize int // This is the number of bytes that will be stored in rb at a time.
Config // Configuration parameters for this device. Config // Configuration parameters for this device.
} }
@ -144,13 +144,13 @@ func (d *ALSA) Set(c config.Config) error {
Channels: ab.Format.Channels, Channels: ab.Format.Channels,
Rate: ab.Format.Rate, Rate: ab.Format.Rate,
} }
d.buf = pcm.Buffer{ d.pb = pcm.Buffer{
Format: cf, Format: cf,
Data: ab.Data, Data: ab.Data,
} }
// Account for channel conversion. // 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. // Account for resampling.
chunkSize = (chunkSize / float64(d.dev.BufferFormat().Rate)) * float64(d.SampleRate) chunkSize = (chunkSize / float64(d.dev.BufferFormat().Rate)) * float64(d.SampleRate)
@ -386,7 +386,7 @@ func (d *ALSA) input() {
// Read from audio device. // Read from audio device.
d.l.Log(logger.Debug, pkg+"recording audio for period", "seconds", d.RecPeriod) 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 { if err != nil {
d.l.Log(logger.Debug, pkg+"read failed", "error", err.Error()) d.l.Log(logger.Debug, pkg+"read failed", "error", err.Error())
err = d.open() // re-open err = d.open() // re-open
@ -432,22 +432,22 @@ func (d *ALSA) formatBuffer() pcm.Buffer {
var err error var err error
// If nothing needs to be changed, return the original. // If nothing needs to be changed, return the original.
if d.buf.Format.Channels == d.Channels && d.buf.Format.Rate == d.SampleRate { if d.pb.Format.Channels == d.Channels && d.pb.Format.Rate == d.SampleRate {
return d.buf return d.pb
} }
var formatted pcm.Buffer var formatted pcm.Buffer
if d.buf.Format.Channels != d.Channels { if d.pb.Format.Channels != d.Channels {
// Convert channels. // Convert channels.
// TODO(Trek): Make this work for conversions other than stereo to mono. // TODO(Trek): Make this work for conversions other than stereo to mono.
if d.buf.Format.Channels == 2 && d.Channels == 1 { if d.pb.Format.Channels == 2 && d.Channels == 1 {
formatted, err = pcm.StereoToMono(d.buf) formatted, err = pcm.StereoToMono(d.pb)
if err != nil { if err != nil {
d.l.Log(logger.Fatal, pkg+"channel conversion failed", "error", err.Error()) 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. // Convert rate.
formatted, err = pcm.Resample(formatted, d.SampleRate) formatted, err = pcm.Resample(formatted, d.SampleRate)
if err != nil { if err != nil {