mirror of https://bitbucket.org/ausocean/av.git
adpcm: encoder and decoder structs are now exported
This commit is contained in:
parent
537d5bab73
commit
a1fe6c6deb
|
@ -27,7 +27,7 @@ LICENSE
|
|||
Reference algorithms for ADPCM compression and decompression are in part 6.
|
||||
*/
|
||||
|
||||
// Package adpcm provides encoder and decoder structs to encode and decode PCM to and from ADPCM.
|
||||
// Package adpcm provides Encoder and Decoder structs to encode and decode PCM to and from ADPCM.
|
||||
package adpcm
|
||||
|
||||
import (
|
||||
|
@ -69,8 +69,8 @@ var stepTable = []int16{
|
|||
32767,
|
||||
}
|
||||
|
||||
// encoder is used to encode to ADPCM from PCM data.
|
||||
type encoder struct {
|
||||
// Encoder is used to encode to ADPCM from PCM data.
|
||||
type Encoder struct {
|
||||
// dst is the destination for encoded data.
|
||||
dst io.Writer
|
||||
|
||||
|
@ -79,8 +79,8 @@ type encoder struct {
|
|||
index int16
|
||||
}
|
||||
|
||||
// decoder is used to decode from ADPCM to PCM data.
|
||||
type decoder struct {
|
||||
// Decoder is used to decode from ADPCM to PCM data.
|
||||
type Decoder struct {
|
||||
// dst is the output buffer that implements io.Writer and io.Bytewriter, ie. where the decoded PCM data is written to.
|
||||
dst io.Writer
|
||||
|
||||
|
@ -90,9 +90,9 @@ type decoder struct {
|
|||
step int16
|
||||
}
|
||||
|
||||
// NewEncoder retuns a new ADPCM encoder.
|
||||
func NewEncoder(dst io.Writer) *encoder {
|
||||
e := encoder{
|
||||
// NewEncoder retuns a new ADPCM Encoder.
|
||||
func NewEncoder(dst io.Writer) *Encoder {
|
||||
e := Encoder{
|
||||
dst: dst,
|
||||
}
|
||||
return &e
|
||||
|
@ -100,7 +100,7 @@ func NewEncoder(dst io.Writer) *encoder {
|
|||
|
||||
// encodeSample takes a single 16 bit PCM sample and
|
||||
// returns a byte of which the last 4 bits are an encoded ADPCM nibble.
|
||||
func (e *encoder) encodeSample(sample int16) byte {
|
||||
func (e *Encoder) encodeSample(sample int16) byte {
|
||||
// Find difference between the sample and the previous estimation.
|
||||
delta := capAdd16(sample, -e.est)
|
||||
|
||||
|
@ -144,10 +144,10 @@ func (e *encoder) encodeSample(sample int16) byte {
|
|||
return nib
|
||||
}
|
||||
|
||||
// calcHead sets the state for the encoder by running the first sample through
|
||||
// the encoder, and writing the first sample to the encoder's io.Writer (dst).
|
||||
// It returns the number of bytes written to the encoder's io.Writer (dst) along with any errors.
|
||||
func (e *encoder) calcHead(sample []byte, pad bool) (int, error) {
|
||||
// calcHead sets the state for the Encoder by running the first sample through
|
||||
// the Encoder, and writing the first sample to the Encoder's io.Writer (dst).
|
||||
// It returns the number of bytes written to the Encoder's io.Writer (dst) along with any errors.
|
||||
func (e *Encoder) calcHead(sample []byte, pad bool) (int, error) {
|
||||
// Check that we are given 1 sample.
|
||||
if len(sample) != byteDepth {
|
||||
return 0, fmt.Errorf("length of given byte array is: %v, expected: %v", len(sample), byteDepth)
|
||||
|
@ -176,10 +176,10 @@ func (e *encoder) calcHead(sample []byte, pad bool) (int, error) {
|
|||
return n, nil
|
||||
}
|
||||
|
||||
// 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.
|
||||
// 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(samples []byte) {
|
||||
func (e *Encoder) init(samples []byte) {
|
||||
int1 := int16(binary.LittleEndian.Uint16(samples[:byteDepth]))
|
||||
int2 := int16(binary.LittleEndian.Uint16(samples[byteDepth:initBytes]))
|
||||
e.est = int1
|
||||
|
@ -197,10 +197,10 @@ func (e *encoder) init(samples []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.
|
||||
// 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(b []byte) (int, error) {
|
||||
// Check that pcm has enough data to initialize decoder.
|
||||
func (e *Encoder) Write(b []byte) (int, error) {
|
||||
// Check that pcm has enough data to initialize Decoder.
|
||||
pcmLen := len(b)
|
||||
if pcmLen < initBytes {
|
||||
return 0, fmt.Errorf("length of given byte array must be >= %v", initBytes)
|
||||
|
@ -240,9 +240,9 @@ func (e *encoder) Write(b []byte) (int, error) {
|
|||
return n, nil
|
||||
}
|
||||
|
||||
// NewDecoder retuns a new ADPCM decoder.
|
||||
func NewDecoder(dst io.Writer) *decoder {
|
||||
d := decoder{
|
||||
// NewDecoder retuns a new ADPCM Decoder.
|
||||
func NewDecoder(dst io.Writer) *Decoder {
|
||||
d := Decoder{
|
||||
dst: dst,
|
||||
}
|
||||
return &d
|
||||
|
@ -250,7 +250,7 @@ func NewDecoder(dst io.Writer) *decoder {
|
|||
|
||||
// decodeSample takes a byte, the last 4 bits of which contain a single
|
||||
// 4 bit ADPCM nibble, and returns a 16 bit decoded PCM sample.
|
||||
func (d *decoder) decodeSample(nibble byte) int16 {
|
||||
func (d *Decoder) decodeSample(nibble byte) int16 {
|
||||
// Calculate difference.
|
||||
var diff int16
|
||||
if nibble&4 != 0 {
|
||||
|
@ -289,10 +289,10 @@ 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.
|
||||
// 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(b []byte) (int, error) {
|
||||
// Initialize decoder with first 4 bytes of b.
|
||||
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]
|
||||
|
|
Loading…
Reference in New Issue