alsa: refactor DataSize function

This commit is contained in:
Trek H 2020-12-24 10:55:44 +10:30
parent 35e602748d
commit 1d67ab65a4
5 changed files with 10 additions and 13 deletions

View File

@ -156,7 +156,6 @@ func main() {
uint(ac.parameters.channels), uint(ac.parameters.channels),
uint(ac.parameters.bits), uint(ac.parameters.bits),
float64(ac.parameters.period), float64(ac.parameters.period),
0,
) )
rbLen := rbDuration / ac.period rbLen := rbDuration / ac.period
ac.rb = ring.NewBuffer(int(rbLen), cs, rbTimeout) ac.rb = ring.NewBuffer(int(rbLen), cs, rbTimeout)

View File

@ -40,5 +40,5 @@ const (
// IsValid recieves an int representing a codec and checks if it is valid. // IsValid recieves an int representing a codec and checks if it is valid.
func IsValid(codec uint8) bool { func IsValid(codec uint8) bool {
return 0 <= codec && codec < numCodecs return 0 < codec && codec <= numCodecs
} }

View File

@ -32,8 +32,6 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"bitbucket.org/ausocean/av/codec/adpcm"
"bitbucket.org/ausocean/av/codec/codecutil"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -67,12 +65,9 @@ type Buffer struct {
Data []byte Data []byte
} }
// DataSize takes audio attributes describing audio data and returns the size of that data. // DataSize takes audio attributes describing PCM audio data and returns the size of that data.
func DataSize(rate, channels, bitDepth uint, period float64, codec uint8) int { func DataSize(rate, channels, bitDepth uint, period float64) int {
s := int(float64(channels) * float64(rate) * float64(bitDepth/8) * period) s := int(float64(channels) * float64(rate) * float64(bitDepth/8) * period)
if codec == codecutil.ADPCM {
s = adpcm.EncBytes(s)
}
return s return s
} }

View File

@ -170,7 +170,7 @@ func (d *ALSA) Setup(c config.Config) error {
} }
// Create ring buffer with appropriate chunk size. // Create ring buffer with appropriate chunk size.
cs := pcm.DataSize(d.SampleRate, d.Channels, d.BitDepth, d.RecPeriod, d.Codec) cs := d.DataSize()
d.rb = ring.NewBuffer(rbLen, cs, rbTimeout) d.rb = ring.NewBuffer(rbLen, cs, rbTimeout)
// Start device in paused mode. // Start device in paused mode.
@ -470,7 +470,11 @@ func (d *ALSA) formatBuffer() pcm.Buffer {
// DataSize returns the size in bytes of the data ALSA device d will // DataSize returns the size in bytes of the data ALSA device d will
// output in the duration of a single recording period. // output in the duration of a single recording period.
func (d *ALSA) DataSize() int { func (d *ALSA) DataSize() int {
return pcm.DataSize(d.SampleRate, d.Channels, d.BitDepth, d.RecPeriod, d.Codec) s := pcm.DataSize(d.SampleRate, d.Channels, d.BitDepth, d.RecPeriod)
if d.Codec == codecutil.ADPCM {
s = adpcm.EncBytes(s)
}
return s
} }
// nearestPowerOfTwo finds and returns the nearest power of two to the given integer. // nearestPowerOfTwo finds and returns the nearest power of two to the given integer.

View File

@ -34,7 +34,6 @@ import (
"time" "time"
"bitbucket.org/ausocean/av/codec/codecutil" "bitbucket.org/ausocean/av/codec/codecutil"
"bitbucket.org/ausocean/av/codec/pcm"
"bitbucket.org/ausocean/av/device" "bitbucket.org/ausocean/av/device"
"bitbucket.org/ausocean/av/revid/config" "bitbucket.org/ausocean/av/revid/config"
"bitbucket.org/ausocean/utils/logger" "bitbucket.org/ausocean/utils/logger"
@ -66,7 +65,7 @@ func TestDevice(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
cs := pcm.DataSize(c.SampleRate, c.Channels, c.BitDepth, c.RecPeriod, c.InputCodec) cs := ai.DataSize()
lexer, err := codecutil.NewByteLexer(cs) lexer, err := codecutil.NewByteLexer(cs)
go lexer.Lex(ioutil.Discard, ai, time.Duration(c.RecPeriod*float64(time.Second))) go lexer.Lex(ioutil.Discard, ai, time.Duration(c.RecPeriod*float64(time.Second)))
time.Sleep(time.Duration(c.RecPeriod*float64(time.Second)) * time.Duration(n)) time.Sleep(time.Duration(c.RecPeriod*float64(time.Second)) * time.Duration(n))