adpcm: naming

This commit is contained in:
Trek H 2019-05-17 00:58:40 +09:30
parent a6d6a22b82
commit 5e4a2fb866
1 changed files with 26 additions and 26 deletions

View File

@ -38,13 +38,13 @@ import (
) )
const ( const (
byteDepth = 2 // TODO(Trek): make configurable. byteDepth = 2 // TODO(Trek): make configurable.
initSamps = 2 initSamps = 2
initBytes = initSamps * byteDepth initBytes = initSamps * byteDepth
headBytes = 4 headBytes = 4
sampsPerEnc = 2 samplesPerEnc = 2
bytesPerEnc = sampsPerEnc * byteDepth bytesPerEnc = samplesPerEnc * byteDepth
compFact = 4 compFact = 4
) )
// Table of index changes (see spec). // Table of index changes (see spec).
@ -179,9 +179,9 @@ func (e *encoder) calcHead(sample []byte, pad bool) (int, error) {
// init initializes the encoder's estimation to the first uncompressed sample and the index to // init initializes the encoder's estimation to the first uncompressed sample and the index to
// point to a suitable quantizer step size. // point to a suitable quantizer step size.
// The suitable step size is the closest step size in the stepTable to half the absolute difference of the first two samples. // The suitable step size is the closest step size in the stepTable to half the absolute difference of the first two samples.
func (e *encoder) init(samps []byte) { func (e *encoder) init(samples []byte) {
int1 := int16(binary.LittleEndian.Uint16(samps[:byteDepth])) int1 := int16(binary.LittleEndian.Uint16(samples[:byteDepth]))
int2 := int16(binary.LittleEndian.Uint16(samps[byteDepth:initBytes])) int2 := int16(binary.LittleEndian.Uint16(samples[byteDepth:initBytes]))
e.est = int1 e.est = int1
halfDiff := math.Abs(math.Abs(float64(int1)) - math.Abs(float64(int2))/2.0) halfDiff := math.Abs(math.Abs(float64(int1)) - math.Abs(float64(int2))/2.0)
@ -199,9 +199,9 @@ func (e *encoder) init(samps []byte) {
// Write takes a slice of bytes of arbitrary length representing pcm and encodes it into adpcm. // Write takes a slice of bytes of arbitrary length representing pcm and encodes it into adpcm.
// It writes its output to the encoder's dst. // It writes its output to the encoder's dst.
// The number of bytes written out is returned along with any error that occured. // The number of bytes written out is returned along with any error that occured.
func (e *encoder) Write(inPcm []byte) (int, error) { func (e *encoder) Write(b []byte) (int, error) {
// Check that pcm has enough data to initialize decoder. // Check that pcm has enough data to initialize decoder.
pcmLen := len(inPcm) pcmLen := len(b)
if pcmLen < initBytes { if pcmLen < initBytes {
return 0, fmt.Errorf("length of given byte array must be >= %v", initBytes) return 0, fmt.Errorf("length of given byte array must be >= %v", initBytes)
} }
@ -212,15 +212,15 @@ func (e *encoder) Write(inPcm []byte) (int, error) {
pad = true pad = true
} }
e.init(inPcm[:initBytes]) e.init(b[:initBytes])
n, err := e.calcHead(inPcm[:byteDepth], pad) n, err := e.calcHead(b[:byteDepth], pad)
if err != nil { if err != nil {
return n, err return n, err
} }
// Skip the first sample and start at the end of the first two samples, then every two samples encode them into a byte of adpcm. // Skip the first sample and start at the end of the first two samples, then every two samples encode them into a byte of adpcm.
for i := byteDepth; i+bytesPerEnc-1 < pcmLen; i += bytesPerEnc { for i := byteDepth; i+bytesPerEnc-1 < pcmLen; i += bytesPerEnc {
nib1 := e.encodeSample(int16(binary.LittleEndian.Uint16(inPcm[i : i+byteDepth]))) nib1 := e.encodeSample(int16(binary.LittleEndian.Uint16(b[i : i+byteDepth])))
nib2 := e.encodeSample(int16(binary.LittleEndian.Uint16(inPcm[i+byteDepth : i+bytesPerEnc]))) nib2 := e.encodeSample(int16(binary.LittleEndian.Uint16(b[i+byteDepth : i+bytesPerEnc])))
_n, err := e.dst.Write([]byte{byte((nib2 << 4) | nib1)}) _n, err := e.dst.Write([]byte{byte((nib2 << 4) | nib1)})
n += _n n += _n
if err != nil { if err != nil {
@ -230,7 +230,7 @@ func (e *encoder) Write(inPcm []byte) (int, error) {
// If we've reached the end of the pcm data and there's a sample left over, // If we've reached the end of the pcm data and there's a sample left over,
// compress it to a nibble and leave the first half of the byte padded with 0s. // compress it to a nibble and leave the first half of the byte padded with 0s.
if pad { if pad {
nib := e.encodeSample(int16(binary.LittleEndian.Uint16(inPcm[pcmLen-byteDepth : pcmLen]))) nib := e.encodeSample(int16(binary.LittleEndian.Uint16(b[pcmLen-byteDepth : pcmLen])))
_n, err := e.dst.Write([]byte{nib}) _n, err := e.dst.Write([]byte{nib})
n += _n n += _n
if err != nil { if err != nil {
@ -291,12 +291,12 @@ func (d *decoder) decodeSample(nibble byte) int16 {
// Write takes a slice of bytes of arbitrary length representing adpcm and decodes it into pcm. // Write takes a slice of bytes of arbitrary length representing adpcm and decodes it into pcm.
// It writes its output to the decoder's dst. // It writes its output to the decoder's dst.
// The number of bytes written out is returned along with any error that occured. // The number of bytes written out is returned along with any error that occured.
func (d *decoder) Write(chunk []byte) (int, error) { func (d *decoder) Write(b []byte) (int, error) {
// Initialize decoder with first 4 bytes of the chunk. // Initialize decoder with first 4 bytes of b.
d.est = int16(binary.LittleEndian.Uint16(chunk[:byteDepth])) d.est = int16(binary.LittleEndian.Uint16(b[:byteDepth]))
d.index = int16(chunk[byteDepth]) d.index = int16(b[byteDepth])
d.step = stepTable[d.index] d.step = stepTable[d.index]
n, err := d.dst.Write(chunk[:byteDepth]) n, err := d.dst.Write(b[:byteDepth])
if err != nil { if err != nil {
return n, err return n, err
} }
@ -304,8 +304,8 @@ func (d *decoder) Write(chunk []byte) (int, error) {
// For each byte, seperate it into two nibbles (each nibble is a compressed sample), // For each byte, seperate it into two nibbles (each nibble is a compressed sample),
// then decode each nibble and output the resulting 16-bit samples. // then decode each nibble and output the resulting 16-bit samples.
// If padding flag is true (Adpcm[3]), only decode up until the last byte, then decode that separately. // If padding flag is true (Adpcm[3]), only decode up until the last byte, then decode that separately.
for i := headBytes; i < len(chunk)-int(chunk[3]); i++ { for i := headBytes; i < len(b)-int(b[3]); i++ {
twoNibs := chunk[i] twoNibs := b[i]
nib2 := byte(twoNibs >> 4) nib2 := byte(twoNibs >> 4)
nib1 := byte((nib2 << 4) ^ twoNibs) nib1 := byte((nib2 << 4) ^ twoNibs)
@ -325,8 +325,8 @@ func (d *decoder) Write(chunk []byte) (int, error) {
return n, err return n, err
} }
} }
if chunk[3] == 0x01 { if b[3] == 0x01 {
padNib := chunk[len(chunk)-1] padNib := b[len(b)-1]
samp := make([]byte, byteDepth) samp := make([]byte, byteDepth)
binary.LittleEndian.PutUint16(samp, uint16(d.decodeSample(padNib))) binary.LittleEndian.PutUint16(samp, uint16(d.decodeSample(padNib)))
_n, err := d.dst.Write(samp) _n, err := d.dst.Write(samp)