mirror of https://bitbucket.org/ausocean/av.git
adpcm: naming
This commit is contained in:
parent
a6d6a22b82
commit
5e4a2fb866
|
@ -38,13 +38,13 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
byteDepth = 2 // TODO(Trek): make configurable.
|
||||
initSamps = 2
|
||||
initBytes = initSamps * byteDepth
|
||||
headBytes = 4
|
||||
sampsPerEnc = 2
|
||||
bytesPerEnc = sampsPerEnc * byteDepth
|
||||
compFact = 4
|
||||
byteDepth = 2 // TODO(Trek): make configurable.
|
||||
initSamps = 2
|
||||
initBytes = initSamps * byteDepth
|
||||
headBytes = 4
|
||||
samplesPerEnc = 2
|
||||
bytesPerEnc = samplesPerEnc * byteDepth
|
||||
compFact = 4
|
||||
)
|
||||
|
||||
// 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
|
||||
// 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.
|
||||
func (e *encoder) init(samps []byte) {
|
||||
int1 := int16(binary.LittleEndian.Uint16(samps[:byteDepth]))
|
||||
int2 := int16(binary.LittleEndian.Uint16(samps[byteDepth:initBytes]))
|
||||
func (e *encoder) init(samples []byte) {
|
||||
int1 := int16(binary.LittleEndian.Uint16(samples[:byteDepth]))
|
||||
int2 := int16(binary.LittleEndian.Uint16(samples[byteDepth:initBytes]))
|
||||
e.est = int1
|
||||
|
||||
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.
|
||||
// It writes its output to the encoder's dst.
|
||||
// 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.
|
||||
pcmLen := len(inPcm)
|
||||
pcmLen := len(b)
|
||||
if pcmLen < 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
|
||||
}
|
||||
|
||||
e.init(inPcm[:initBytes])
|
||||
n, err := e.calcHead(inPcm[:byteDepth], pad)
|
||||
e.init(b[:initBytes])
|
||||
n, err := e.calcHead(b[:byteDepth], pad)
|
||||
if err != nil {
|
||||
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.
|
||||
for i := byteDepth; i+bytesPerEnc-1 < pcmLen; i += bytesPerEnc {
|
||||
nib1 := e.encodeSample(int16(binary.LittleEndian.Uint16(inPcm[i : i+byteDepth])))
|
||||
nib2 := e.encodeSample(int16(binary.LittleEndian.Uint16(inPcm[i+byteDepth : i+bytesPerEnc])))
|
||||
nib1 := e.encodeSample(int16(binary.LittleEndian.Uint16(b[i : i+byteDepth])))
|
||||
nib2 := e.encodeSample(int16(binary.LittleEndian.Uint16(b[i+byteDepth : i+bytesPerEnc])))
|
||||
_n, err := e.dst.Write([]byte{byte((nib2 << 4) | nib1)})
|
||||
n += _n
|
||||
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,
|
||||
// compress it to a nibble and leave the first half of the byte padded with 0s.
|
||||
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 += _n
|
||||
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.
|
||||
// It writes its output to the decoder's dst.
|
||||
// The number of bytes written out is returned along with any error that occured.
|
||||
func (d *decoder) Write(chunk []byte) (int, error) {
|
||||
// Initialize decoder with first 4 bytes of the chunk.
|
||||
d.est = int16(binary.LittleEndian.Uint16(chunk[:byteDepth]))
|
||||
d.index = int16(chunk[byteDepth])
|
||||
func (d *decoder) Write(b []byte) (int, error) {
|
||||
// Initialize decoder with first 4 bytes of b.
|
||||
d.est = int16(binary.LittleEndian.Uint16(b[:byteDepth]))
|
||||
d.index = int16(b[byteDepth])
|
||||
d.step = stepTable[d.index]
|
||||
n, err := d.dst.Write(chunk[:byteDepth])
|
||||
n, err := d.dst.Write(b[:byteDepth])
|
||||
if err != nil {
|
||||
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),
|
||||
// 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.
|
||||
for i := headBytes; i < len(chunk)-int(chunk[3]); i++ {
|
||||
twoNibs := chunk[i]
|
||||
for i := headBytes; i < len(b)-int(b[3]); i++ {
|
||||
twoNibs := b[i]
|
||||
nib2 := byte(twoNibs >> 4)
|
||||
nib1 := byte((nib2 << 4) ^ twoNibs)
|
||||
|
||||
|
@ -325,8 +325,8 @@ func (d *decoder) Write(chunk []byte) (int, error) {
|
|||
return n, err
|
||||
}
|
||||
}
|
||||
if chunk[3] == 0x01 {
|
||||
padNib := chunk[len(chunk)-1]
|
||||
if b[3] == 0x01 {
|
||||
padNib := b[len(b)-1]
|
||||
samp := make([]byte, byteDepth)
|
||||
binary.LittleEndian.PutUint16(samp, uint16(d.decodeSample(padNib)))
|
||||
_n, err := d.dst.Write(samp)
|
||||
|
|
Loading…
Reference in New Issue