mirror of https://bitbucket.org/ausocean/av.git
Merge branch 'master' into comment-config
This commit is contained in:
commit
947147b9fd
|
@ -2,9 +2,6 @@
|
||||||
NAME
|
NAME
|
||||||
adpcm.go
|
adpcm.go
|
||||||
|
|
||||||
DESCRIPTION
|
|
||||||
adpcm.go contains functions for encoding/compressing pcm into adpcm and decoding/decompressing back to pcm.
|
|
||||||
|
|
||||||
AUTHOR
|
AUTHOR
|
||||||
Trek Hopton <trek@ausocean.org>
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
@ -30,40 +27,25 @@ LICENSE
|
||||||
Reference algorithms for ADPCM compression and decompression are in part 6.
|
Reference algorithms for ADPCM compression and decompression are in part 6.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Package adpcm provides functions to transcode between PCM and ADPCM.
|
||||||
package adpcm
|
package adpcm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
// encoder is used to encode to ADPCM from PCM data.
|
const (
|
||||||
// pred and index hold state that persists between calls to encodeSample and calcHead.
|
byteDepth = 2 // We are working with 16-bit samples. TODO(Trek): make configurable.
|
||||||
// dest is the output buffer that implements io.writer and io.bytewriter, ie. where the encoded ADPCM data is written to.
|
initSamps = 2 // Number of samples used to initialise the encoder.
|
||||||
type encoder struct {
|
initBytes = initSamps * byteDepth
|
||||||
dest *bytes.Buffer
|
headBytes = 4 // Number of bytes in the header of ADPCM.
|
||||||
pred int16
|
samplesPerEnc = 2 // Number of sample encoded at a time eg. 2 16-bit samples get encoded into 1 byte.
|
||||||
index int16
|
bytesPerEnc = samplesPerEnc * byteDepth
|
||||||
}
|
compFact = 4 // In general ADPCM compresses by a factor of 4.
|
||||||
|
)
|
||||||
// decoder is used to decode from ADPCM to PCM data.
|
|
||||||
// pred, index, and step hold state that persists between calls to decodeSample.
|
|
||||||
// dest is the output buffer that implements io.writer and io.bytewriter, ie. where the decoded PCM data is written to.
|
|
||||||
type decoder struct {
|
|
||||||
dest *bytes.Buffer
|
|
||||||
pred int16
|
|
||||||
index int16
|
|
||||||
step int16
|
|
||||||
}
|
|
||||||
|
|
||||||
// PcmBS is the size of the blocks that an encoder uses.
|
|
||||||
// 'encodeBlock' will encode PcmBS bytes at a time and the output will be AdpcmBS bytes long.
|
|
||||||
const PcmBS = 1010
|
|
||||||
|
|
||||||
// AdpcmBS is the size of the blocks that a decoder uses.
|
|
||||||
// 'decodeBlock' will decode AdpcmBS bytes at a time and the output will be PcmBS bytes long.
|
|
||||||
const AdpcmBS = 256
|
|
||||||
|
|
||||||
// Table of index changes (see spec).
|
// Table of index changes (see spec).
|
||||||
var indexTable = []int16{
|
var indexTable = []int16{
|
||||||
|
@ -87,28 +69,35 @@ var stepTable = []int16{
|
||||||
32767,
|
32767,
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEncoder retuns a new ADPCM encoder.
|
// Encoder is used to encode to ADPCM from PCM data.
|
||||||
func NewEncoder(dst *bytes.Buffer) *encoder {
|
type Encoder struct {
|
||||||
e := encoder{
|
// dst is the destination for ADPCM-encoded data.
|
||||||
dest: dst,
|
dst io.Writer
|
||||||
}
|
|
||||||
return &e
|
est int16 // Estimation of sample based on quantised ADPCM nibble.
|
||||||
|
idx int16 // Index to step used for estimation.
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDecoder retuns a new ADPCM decoder.
|
// Decoder is used to decode from ADPCM to PCM data.
|
||||||
func NewDecoder(dst *bytes.Buffer) *decoder {
|
type Decoder struct {
|
||||||
d := decoder{
|
// dst is the destination for PCM-encoded data.
|
||||||
step: stepTable[0],
|
dst io.Writer
|
||||||
dest: dst,
|
|
||||||
}
|
est int16 // Estimation of sample based on quantised ADPCM nibble.
|
||||||
return &d
|
idx int16 // Index to step used for estimation.
|
||||||
|
step int16
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewEncoder retuns a new ADPCM Encoder.
|
||||||
|
func NewEncoder(dst io.Writer) *Encoder {
|
||||||
|
return &Encoder{dst: dst}
|
||||||
}
|
}
|
||||||
|
|
||||||
// encodeSample takes a single 16 bit PCM sample and
|
// encodeSample takes a single 16 bit PCM sample and
|
||||||
// returns a byte of which the last 4 bits are an encoded ADPCM nibble.
|
// 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 of actual sample from encoder's prediction.
|
// Find difference between the sample and the previous estimation.
|
||||||
delta := sample - e.pred
|
delta := capAdd16(sample, -e.est)
|
||||||
|
|
||||||
// Create and set sign bit for nibble and find absolute value of difference.
|
// Create and set sign bit for nibble and find absolute value of difference.
|
||||||
var nib byte
|
var nib byte
|
||||||
|
@ -117,217 +106,250 @@ func (e *encoder) encodeSample(sample int16) byte {
|
||||||
delta = -delta
|
delta = -delta
|
||||||
}
|
}
|
||||||
|
|
||||||
step := stepTable[e.index]
|
step := stepTable[e.idx]
|
||||||
diff := step >> 3
|
diff := step >> 3
|
||||||
var mask byte = 4
|
var mask byte = 4
|
||||||
|
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
if delta > step {
|
if delta > step {
|
||||||
nib |= mask
|
nib |= mask
|
||||||
delta -= step
|
delta = capAdd16(delta, -step)
|
||||||
diff += step
|
diff = capAdd16(diff, step)
|
||||||
}
|
}
|
||||||
mask >>= 1
|
mask >>= 1
|
||||||
step >>= 1
|
step >>= 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adjust predicted sample based on calculated difference.
|
|
||||||
if nib&8 != 0 {
|
if nib&8 != 0 {
|
||||||
e.pred -= diff
|
diff = -diff
|
||||||
} else {
|
|
||||||
e.pred += diff
|
|
||||||
}
|
}
|
||||||
|
|
||||||
e.index += indexTable[nib&7]
|
// Adjust estimated sample based on calculated difference.
|
||||||
|
e.est = capAdd16(e.est, diff)
|
||||||
|
|
||||||
|
e.idx += indexTable[nib&7]
|
||||||
|
|
||||||
// Check for underflow and overflow.
|
// Check for underflow and overflow.
|
||||||
if e.index < 0 {
|
if e.idx < 0 {
|
||||||
e.index = 0
|
e.idx = 0
|
||||||
} else if e.index > int16(len(stepTable)-1) {
|
} else if e.idx > int16(len(stepTable)-1) {
|
||||||
e.index = int16(len(stepTable) - 1)
|
e.idx = int16(len(stepTable) - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nib
|
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 destination and the first error encountered.
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := e.dst.Write(sample)
|
||||||
|
if err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_n, err := e.dst.Write([]byte{byte(int16(e.idx))})
|
||||||
|
if err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
n += _n
|
||||||
|
|
||||||
|
if pad {
|
||||||
|
_n, err = e.dst.Write([]byte{0x01})
|
||||||
|
} else {
|
||||||
|
_n, err = e.dst.Write([]byte{0x00})
|
||||||
|
}
|
||||||
|
n += _n
|
||||||
|
if err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
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)
|
||||||
|
closest := math.Abs(float64(stepTable[0]) - halfDiff)
|
||||||
|
var cInd int16
|
||||||
|
for i, step := range stepTable {
|
||||||
|
if math.Abs(float64(step)-halfDiff) < closest {
|
||||||
|
closest = math.Abs(float64(step) - halfDiff)
|
||||||
|
cInd = int16(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.idx = cInd
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine if there will be a byte that won't contain two full nibbles and will need padding.
|
||||||
|
pad := false
|
||||||
|
if (pcmLen-byteDepth)%bytesPerEnc != 0 {
|
||||||
|
pad = true
|
||||||
|
}
|
||||||
|
|
||||||
|
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(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 {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 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(b[pcmLen-byteDepth : pcmLen])))
|
||||||
|
_n, err := e.dst.Write([]byte{nib})
|
||||||
|
n += _n
|
||||||
|
if err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDecoder retuns a new ADPCM Decoder.
|
||||||
|
func NewDecoder(dst io.Writer) *Decoder {
|
||||||
|
return &Decoder{dst: dst}
|
||||||
|
}
|
||||||
|
|
||||||
// decodeSample takes a byte, the last 4 bits of which contain a single
|
// 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.
|
// 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.
|
// Calculate difference.
|
||||||
var diff int16
|
var diff int16
|
||||||
if nibble&4 != 0 {
|
if nibble&4 != 0 {
|
||||||
diff += d.step
|
diff = capAdd16(diff, d.step)
|
||||||
}
|
}
|
||||||
if nibble&2 != 0 {
|
if nibble&2 != 0 {
|
||||||
diff += d.step >> 1
|
diff = capAdd16(diff, d.step>>1)
|
||||||
}
|
}
|
||||||
if nibble&1 != 0 {
|
if nibble&1 != 0 {
|
||||||
diff += d.step >> 2
|
diff = capAdd16(diff, d.step>>2)
|
||||||
}
|
}
|
||||||
diff += d.step >> 3
|
diff = capAdd16(diff, d.step>>3)
|
||||||
|
|
||||||
// Account for sign bit.
|
// Account for sign bit.
|
||||||
if nibble&8 != 0 {
|
if nibble&8 != 0 {
|
||||||
diff = -diff
|
diff = -diff
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adjust predicted sample based on calculated difference.
|
// Adjust estimated sample based on calculated difference.
|
||||||
d.pred += diff
|
d.est = capAdd16(d.est, diff)
|
||||||
|
|
||||||
// Adjust index into step size lookup table using nibble.
|
// Adjust index into step size lookup table using nibble.
|
||||||
d.index += indexTable[nibble]
|
d.idx += indexTable[nibble]
|
||||||
|
|
||||||
// Check for overflow and underflow.
|
// Check for overflow and underflow.
|
||||||
if d.index < 0 {
|
if d.idx < 0 {
|
||||||
d.index = 0
|
d.idx = 0
|
||||||
} else if d.index > int16(len(stepTable)-1) {
|
} else if d.idx > int16(len(stepTable)-1) {
|
||||||
d.index = int16(len(stepTable) - 1)
|
d.idx = int16(len(stepTable) - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find new quantizer step size.
|
// Find new quantizer step size.
|
||||||
d.step = stepTable[d.index]
|
d.step = stepTable[d.idx]
|
||||||
|
|
||||||
return d.pred
|
return d.est
|
||||||
}
|
}
|
||||||
|
|
||||||
// calcHead sets the state for the encoder by running the first sample through
|
// Write takes a slice of bytes of arbitrary length representing adpcm and decodes it into pcm.
|
||||||
// the encoder, and writing the first sample to the encoder's io.Writer (dest).
|
// It writes its output to the Decoder's dst.
|
||||||
// It returns the number of bytes written to the encoder's io.Writer (dest) along with any errors.
|
// The number of bytes written out is returned along with any error that occured.
|
||||||
func (e *encoder) calcHead(sample []byte) (int, error) {
|
func (d *Decoder) Write(b []byte) (int, error) {
|
||||||
// Check that we are given 1 16-bit sample (2 bytes).
|
// Initialize Decoder with first 4 bytes of b.
|
||||||
const sampSize = 2
|
d.est = int16(binary.LittleEndian.Uint16(b[:byteDepth]))
|
||||||
if len(sample) != sampSize {
|
d.idx = int16(b[byteDepth])
|
||||||
return 0, fmt.Errorf("length of given byte array is: %v, expected: %v", len(sample), sampSize)
|
d.step = stepTable[d.idx]
|
||||||
}
|
n, err := d.dst.Write(b[:byteDepth])
|
||||||
|
|
||||||
intSample := int16(binary.LittleEndian.Uint16(sample))
|
|
||||||
e.encodeSample(intSample)
|
|
||||||
|
|
||||||
n, err := e.dest.Write(sample)
|
|
||||||
if err != nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = e.dest.WriteByte(byte(uint16(e.index)))
|
|
||||||
if err != nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
n++
|
|
||||||
|
|
||||||
err = e.dest.WriteByte(byte(0x00))
|
|
||||||
if err != nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
n++
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// encodeBlock takes a slice of 1010 bytes (505 16-bit PCM samples).
|
|
||||||
// It writes encoded (compressed) bytes (each byte containing two ADPCM nibbles) to the encoder's io.Writer (dest).
|
|
||||||
// The number of bytes written is returned along with any errors.
|
|
||||||
// Note: nibbles are output in little endian order, eg. n1n0 n3n2 n5n4...
|
|
||||||
// Note: first 4 bytes are for initializing the decoder before decoding a block.
|
|
||||||
// - First two bytes contain the first 16-bit sample uncompressed.
|
|
||||||
// - Third byte is the decoder's starting index for the block, the fourth is padding and ignored.
|
|
||||||
func (e *encoder) encodeBlock(block []byte) (int, error) {
|
|
||||||
if len(block) != PcmBS {
|
|
||||||
return 0, fmt.Errorf("unsupported block size. Given: %v, expected: %v, ie. 505 16-bit PCM samples", len(block), PcmBS)
|
|
||||||
}
|
|
||||||
|
|
||||||
n, err := e.calcHead(block[0:2])
|
|
||||||
if err != nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 3; i < PcmBS; i += 4 {
|
|
||||||
nib1 := e.encodeSample(int16(binary.LittleEndian.Uint16(block[i-1 : i+1])))
|
|
||||||
nib2 := e.encodeSample(int16(binary.LittleEndian.Uint16(block[i+1 : i+3])))
|
|
||||||
err = e.dest.WriteByte(byte((nib2 << 4) | nib1))
|
|
||||||
if err != nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
n++
|
|
||||||
}
|
|
||||||
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// decodeBlock takes a slice of 256 bytes, each byte after the first 4 should contain two ADPCM encoded nibbles.
|
|
||||||
// It writes the resulting decoded (decompressed) 16-bit PCM samples to the decoder's io.Writer (dest).
|
|
||||||
// The number of bytes written is returned along with any errors.
|
|
||||||
func (d *decoder) decodeBlock(block []byte) (int, error) {
|
|
||||||
if len(block) != AdpcmBS {
|
|
||||||
return 0, fmt.Errorf("unsupported block size. Given: %v, expected: %v", len(block), AdpcmBS)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize decoder with first 4 bytes of the block.
|
|
||||||
d.pred = int16(binary.LittleEndian.Uint16(block[0:2]))
|
|
||||||
d.index = int16(block[2])
|
|
||||||
d.step = stepTable[d.index]
|
|
||||||
n, err := d.dest.Write(block[0:2])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
for i := 4; i < AdpcmBS; i++ {
|
// If padding flag is true (Adpcm[3]), only decode up until the last byte, then decode that separately.
|
||||||
twoNibs := block[i]
|
for i := headBytes; i < len(b)-int(b[3]); i++ {
|
||||||
|
twoNibs := b[i]
|
||||||
nib2 := byte(twoNibs >> 4)
|
nib2 := byte(twoNibs >> 4)
|
||||||
nib1 := byte((nib2 << 4) ^ twoNibs)
|
nib1 := byte((nib2 << 4) ^ twoNibs)
|
||||||
|
|
||||||
firstBytes := make([]byte, 2)
|
firstBytes := make([]byte, byteDepth)
|
||||||
binary.LittleEndian.PutUint16(firstBytes, uint16(d.decodeSample(nib1)))
|
binary.LittleEndian.PutUint16(firstBytes, uint16(d.decodeSample(nib1)))
|
||||||
_n, err := d.dest.Write(firstBytes)
|
_n, err := d.dst.Write(firstBytes)
|
||||||
n += _n
|
n += _n
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
secondBytes := make([]byte, 2)
|
secondBytes := make([]byte, byteDepth)
|
||||||
binary.LittleEndian.PutUint16(secondBytes, uint16(d.decodeSample(nib2)))
|
binary.LittleEndian.PutUint16(secondBytes, uint16(d.decodeSample(nib2)))
|
||||||
_n, err = d.dest.Write(secondBytes)
|
_n, err = d.dst.Write(secondBytes)
|
||||||
n += _n
|
n += _n
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if b[3] == 0x01 {
|
||||||
return n, nil
|
padNib := b[len(b)-1]
|
||||||
}
|
samp := make([]byte, byteDepth)
|
||||||
|
binary.LittleEndian.PutUint16(samp, uint16(d.decodeSample(padNib)))
|
||||||
// Write takes a slice of bytes of arbitrary length representing pcm and encodes in into adpcm.
|
_n, err := d.dst.Write(samp)
|
||||||
// It writes its output to the encoder's dest.
|
|
||||||
// The number of bytes written out is returned along with any error that occured.
|
|
||||||
func (e *encoder) Write(inPcm []byte) (int, error) {
|
|
||||||
numBlocks := len(inPcm) / PcmBS
|
|
||||||
n := 0
|
|
||||||
for i := 0; i < numBlocks; i++ {
|
|
||||||
block := inPcm[PcmBS*i : PcmBS*(i+1)]
|
|
||||||
_n, err := e.encodeBlock(block)
|
|
||||||
n += _n
|
n += _n
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write takes a slice of bytes of arbitrary length representing adpcm and decodes in into pcm.
|
// capAdd16 adds two int16s together and caps at max/min int16 instead of overflowing
|
||||||
// It writes its output to the decoder's dest.
|
func capAdd16(a, b int16) int16 {
|
||||||
// The number of bytes written out is returned along with any error that occured.
|
c := int32(a) + int32(b)
|
||||||
func (d *decoder) Write(inAdpcm []byte) (int, error) {
|
switch {
|
||||||
numBlocks := len(inAdpcm) / AdpcmBS
|
case c < math.MinInt16:
|
||||||
n := 0
|
return math.MinInt16
|
||||||
for i := 0; i < numBlocks; i++ {
|
case c > math.MaxInt16:
|
||||||
block := inAdpcm[AdpcmBS*i : AdpcmBS*(i+1)]
|
return math.MaxInt16
|
||||||
_n, err := d.decodeBlock(block)
|
default:
|
||||||
n += _n
|
return int16(c)
|
||||||
if err != nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return n, nil
|
|
||||||
|
// EncBytes will return the number of adpcm bytes that will be generated when encoding the given amount of pcm bytes (n).
|
||||||
|
func EncBytes(n int) int {
|
||||||
|
// For 'n' pcm bytes, 1 sample is left uncompressed, the rest is compressed by a factor of 4
|
||||||
|
// and a start index and padding-flag byte are added.
|
||||||
|
// Also if there are an even number of samples, there will be half a byte of padding added to the last byte.
|
||||||
|
if n%bytesPerEnc == 0 {
|
||||||
|
return (n-byteDepth)/compFact + headBytes + 1
|
||||||
|
}
|
||||||
|
return (n-byteDepth)/compFact + headBytes
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,14 +37,13 @@ import (
|
||||||
// then compare the result with expected ADPCM.
|
// then compare the result with expected ADPCM.
|
||||||
func TestEncodeBlock(t *testing.T) {
|
func TestEncodeBlock(t *testing.T) {
|
||||||
// Read input pcm.
|
// Read input pcm.
|
||||||
pcm, err := ioutil.ReadFile("../../../test/test-data/av/input/raw-voice.pcm")
|
pcm, err := ioutil.ReadFile("../../../test/test-data/av/input/original_8kHz_adpcm_test.pcm")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to read input PCM file: %v", err)
|
t.Errorf("Unable to read input PCM file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode adpcm.
|
// Encode adpcm.
|
||||||
numBlocks := len(pcm) / PcmBS
|
comp := bytes.NewBuffer(make([]byte, 0, EncBytes(len(pcm))))
|
||||||
comp := bytes.NewBuffer(make([]byte, 0, AdpcmBS*numBlocks))
|
|
||||||
enc := NewEncoder(comp)
|
enc := NewEncoder(comp)
|
||||||
_, err = enc.Write(pcm)
|
_, err = enc.Write(pcm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -52,7 +51,7 @@ func TestEncodeBlock(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read expected adpcm file.
|
// Read expected adpcm file.
|
||||||
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/encoded-voice.adpcm")
|
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/encoded_8kHz_adpcm_test.adpcm")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to read expected ADPCM file: %v", err)
|
t.Errorf("Unable to read expected ADPCM file: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -66,14 +65,13 @@ func TestEncodeBlock(t *testing.T) {
|
||||||
// resulting PCM with the expected decoded PCM.
|
// resulting PCM with the expected decoded PCM.
|
||||||
func TestDecodeBlock(t *testing.T) {
|
func TestDecodeBlock(t *testing.T) {
|
||||||
// Read adpcm.
|
// Read adpcm.
|
||||||
comp, err := ioutil.ReadFile("../../../test/test-data/av/input/encoded-voice.adpcm")
|
comp, err := ioutil.ReadFile("../../../test/test-data/av/input/encoded_8kHz_adpcm_test.adpcm")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to read input ADPCM file: %v", err)
|
t.Errorf("Unable to read input ADPCM file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode adpcm.
|
// Decode adpcm.
|
||||||
numBlocks := len(comp) / AdpcmBS
|
decoded := bytes.NewBuffer(make([]byte, 0, len(comp)*4))
|
||||||
decoded := bytes.NewBuffer(make([]byte, 0, PcmBS*numBlocks))
|
|
||||||
dec := NewDecoder(decoded)
|
dec := NewDecoder(decoded)
|
||||||
_, err = dec.Write(comp)
|
_, err = dec.Write(comp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -81,7 +79,7 @@ func TestDecodeBlock(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read expected pcm file.
|
// Read expected pcm file.
|
||||||
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/decoded-voice.pcm")
|
exp, err := ioutil.ReadFile("../../../test/test-data/av/output/decoded_8kHz_adpcm_test.pcm")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unable to read expected PCM file: %v", err)
|
t.Errorf("Unable to read expected PCM file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,8 @@ LICENSE
|
||||||
You should have received a copy of the GNU General Public License in gpl.txt.
|
You should have received a copy of the GNU General Public License in gpl.txt.
|
||||||
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// Package pcm provides functions for processing and converting pcm audio.
|
||||||
package pcm
|
package pcm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -2,9 +2,6 @@
|
||||||
NAME
|
NAME
|
||||||
decode-pcm.go
|
decode-pcm.go
|
||||||
|
|
||||||
DESCRIPTION
|
|
||||||
decode-pcm.go is a program for decoding/decompressing an adpcm file to a pcm file.
|
|
||||||
|
|
||||||
AUTHOR
|
AUTHOR
|
||||||
Trek Hopton <trek@ausocean.org>
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
@ -25,6 +22,7 @@ LICENSE
|
||||||
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// decode-pcm is a command-line program for decoding/decompressing an adpcm file to a pcm file.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -54,8 +52,7 @@ func main() {
|
||||||
fmt.Println("Read", len(comp), "bytes from file", inPath)
|
fmt.Println("Read", len(comp), "bytes from file", inPath)
|
||||||
|
|
||||||
// Decode adpcm.
|
// Decode adpcm.
|
||||||
numBlocks := len(comp) / adpcm.AdpcmBS
|
decoded := bytes.NewBuffer(make([]byte, 0, len(comp)*4))
|
||||||
decoded := bytes.NewBuffer(make([]byte, 0, adpcm.PcmBS*numBlocks))
|
|
||||||
dec := adpcm.NewDecoder(decoded)
|
dec := adpcm.NewDecoder(decoded)
|
||||||
_, err = dec.Write(comp)
|
_, err = dec.Write(comp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,9 +2,6 @@
|
||||||
NAME
|
NAME
|
||||||
encode-pcm.go
|
encode-pcm.go
|
||||||
|
|
||||||
DESCRIPTION
|
|
||||||
encode-pcm.go is a program for encoding/compressing a pcm file to an adpcm file.
|
|
||||||
|
|
||||||
AUTHOR
|
AUTHOR
|
||||||
Trek Hopton <trek@ausocean.org>
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
@ -25,6 +22,7 @@ LICENSE
|
||||||
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// encode-pcm is a command-line program for encoding/compressing a pcm file to an adpcm file.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -54,8 +52,7 @@ func main() {
|
||||||
fmt.Println("Read", len(pcm), "bytes from file", inPath)
|
fmt.Println("Read", len(pcm), "bytes from file", inPath)
|
||||||
|
|
||||||
// Encode adpcm.
|
// Encode adpcm.
|
||||||
numBlocks := len(pcm) / adpcm.PcmBS
|
comp := bytes.NewBuffer(make([]byte, 0, adpcm.EncBytes(len(pcm))))
|
||||||
comp := bytes.NewBuffer(make([]byte, 0, adpcm.AdpcmBS*numBlocks))
|
|
||||||
enc := adpcm.NewEncoder(comp)
|
enc := adpcm.NewEncoder(comp)
|
||||||
_, err = enc.Write(pcm)
|
_, err = enc.Write(pcm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,9 +2,6 @@
|
||||||
NAME
|
NAME
|
||||||
resample.go
|
resample.go
|
||||||
|
|
||||||
DESCRIPTION
|
|
||||||
resample.go is a program for resampling a pcm file.
|
|
||||||
|
|
||||||
AUTHOR
|
AUTHOR
|
||||||
Trek Hopton <trek@ausocean.org>
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
@ -24,6 +21,8 @@ LICENSE
|
||||||
You should have received a copy of the GNU General Public License in gpl.txt.
|
You should have received a copy of the GNU General Public License in gpl.txt.
|
||||||
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// resample is a command-line program for resampling a pcm file.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -2,9 +2,6 @@
|
||||||
NAME
|
NAME
|
||||||
stereo-to-mono.go
|
stereo-to-mono.go
|
||||||
|
|
||||||
DESCRIPTION
|
|
||||||
stereo-to-mono.go is a program for converting a mono pcm file to a stereo pcm file.
|
|
||||||
|
|
||||||
AUTHOR
|
AUTHOR
|
||||||
Trek Hopton <trek@ausocean.org>
|
Trek Hopton <trek@ausocean.org>
|
||||||
|
|
||||||
|
@ -24,6 +21,8 @@ LICENSE
|
||||||
You should have received a copy of the GNU General Public License in gpl.txt.
|
You should have received a copy of the GNU General Public License in gpl.txt.
|
||||||
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
If not, see [GNU licenses](http://www.gnu.org/licenses).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// stereo-to-mono is a command-line program for converting a mono pcm file to a stereo pcm file.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
Loading…
Reference in New Issue