mirror of https://bitbucket.org/ausocean/av.git
ADPCM: Improved comment consistancy.
This commit is contained in:
parent
d9baabdf02
commit
6cfd7c5104
|
@ -46,14 +46,14 @@ func main() {
|
|||
flag.StringVar(&outPath, "out", "decoded.pcm", "file path of output data")
|
||||
flag.Parse()
|
||||
|
||||
// read adpcm
|
||||
// Read adpcm.
|
||||
comp, err := ioutil.ReadFile(inPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("Read", len(comp), "bytes from file", inPath)
|
||||
|
||||
// decode adpcm
|
||||
// Decode adpcm.
|
||||
inBSize := 256
|
||||
numBlocks := len(comp) / inBSize
|
||||
outBSize := 2 + (inBSize-4)*4 // 2 bytes are copied, 2 are used as block header info, the remaining bytes are decompressed 1:4
|
||||
|
@ -67,7 +67,7 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
// save pcm to file
|
||||
// Save pcm to file.
|
||||
err = ioutil.WriteFile(outPath, decoded.Bytes(), 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
|
@ -46,17 +46,17 @@ func main() {
|
|||
flag.StringVar(&adpcmPath, "out", "encoded.adpcm", "file path of output")
|
||||
flag.Parse()
|
||||
|
||||
//read pcm
|
||||
// Read pcm.
|
||||
pcm, err := ioutil.ReadFile(inPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println("Read", len(pcm), "bytes from file", inPath)
|
||||
|
||||
//encode adpcm
|
||||
// Encode adpcm.
|
||||
inBSize := 1010
|
||||
numBlocks := len(pcm) / inBSize
|
||||
outBSize := int(float64(inBSize)/4 + 3.5) // compression is 4:1 and 3.5 bytes of info are added to each block
|
||||
outBSize := int(float64(inBSize)/4 + 3.5) // Compression is 4:1 and 3.5 bytes of info are added to each block.
|
||||
comp := bytes.NewBuffer(make([]byte, 0, outBSize*numBlocks))
|
||||
enc := adpcm.NewEncoder(comp)
|
||||
for i := 0; i < numBlocks; i++ {
|
||||
|
@ -67,7 +67,7 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
// save adpcm to file
|
||||
// Save adpcm to file.
|
||||
err = ioutil.WriteFile(adpcmPath, comp.Bytes(), 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
|
@ -57,13 +57,13 @@ type Decoder struct {
|
|||
step int16
|
||||
}
|
||||
|
||||
// table of index changes (see spec)
|
||||
// Table of index changes (see spec).
|
||||
var indexTable = []int16{
|
||||
-1, -1, -1, -1, 2, 4, 6, 8,
|
||||
-1, -1, -1, -1, 2, 4, 6, 8,
|
||||
}
|
||||
|
||||
// quantize step size table (see spec)
|
||||
// Quantize step size table (see spec).
|
||||
var stepTable = []int16{
|
||||
7, 8, 9, 10, 11, 12, 13, 14,
|
||||
16, 17, 19, 21, 23, 25, 28, 31,
|
||||
|
@ -97,12 +97,12 @@ func NewDecoder(dst *bytes.Buffer) *Decoder {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
// find difference of actual sample from encoder's prediction
|
||||
// Find difference of actual sample from encoder's prediction.
|
||||
delta := sample - e.pred
|
||||
|
||||
// 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
|
||||
if delta < 0 {
|
||||
nib = 8
|
||||
|
@ -123,7 +123,7 @@ func (e *Encoder) encodeSample(sample int16) byte {
|
|||
step >>= 1
|
||||
}
|
||||
|
||||
// adjust predicted sample based on calculated difference
|
||||
// Adjust predicted sample based on calculated difference.
|
||||
if nib&8 != 0 {
|
||||
e.pred -= diff
|
||||
} else {
|
||||
|
@ -131,7 +131,7 @@ func (e *Encoder) encodeSample(sample int16) byte {
|
|||
}
|
||||
|
||||
e.index += indexTable[nib&7]
|
||||
// check for underflow and overflow
|
||||
// Check for underflow and overflow.
|
||||
if e.index < 0 {
|
||||
e.index = 0
|
||||
} else if e.index > int16(len(stepTable)-1) {
|
||||
|
@ -142,9 +142,9 @@ func (e *Encoder) encodeSample(sample int16) byte {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
// calculate difference
|
||||
// Calculate difference.
|
||||
var diff int16
|
||||
if nibble&4 != 0 {
|
||||
diff += d.step
|
||||
|
@ -157,34 +157,34 @@ func (d *Decoder) decodeSample(nibble byte) int16 {
|
|||
}
|
||||
diff += d.step >> 3
|
||||
|
||||
// account for sign bit
|
||||
// Account for sign bit.
|
||||
if nibble&8 != 0 {
|
||||
diff = -diff
|
||||
}
|
||||
|
||||
// adjust predicted sample based on calculated difference
|
||||
// Adjust predicted sample based on calculated difference.
|
||||
d.pred += diff
|
||||
|
||||
// adjust index into step size lookup table using nibble
|
||||
// Adjust index into step size lookup table using nibble.
|
||||
d.index += indexTable[nibble]
|
||||
|
||||
// check for overflow and underflow
|
||||
// Check for overflow and underflow.
|
||||
if d.index < 0 {
|
||||
d.index = 0
|
||||
} else if d.index > int16(len(stepTable)-1) {
|
||||
d.index = int16(len(stepTable) - 1)
|
||||
}
|
||||
|
||||
// find new quantizer step size
|
||||
// Find new quantizer step size.
|
||||
d.step = stepTable[d.index]
|
||||
|
||||
return d.pred
|
||||
}
|
||||
|
||||
// calcHead sets the state for the encoder by running the first sample through
|
||||
// the encoder, and writing the first sample
|
||||
// the encoder, and writing the first sample.
|
||||
func (e *Encoder) calcHead(sample []byte) error {
|
||||
// check that we are given 1 16-bit sample (2 bytes)
|
||||
// Check that we are given 1 16-bit sample (2 bytes).
|
||||
sampSize := 2
|
||||
if len(sample) != sampSize {
|
||||
return fmt.Errorf("length of given byte array is: %v, expected: %v", len(sample), sampSize)
|
||||
|
@ -202,10 +202,10 @@ func (e *Encoder) calcHead(sample []byte) error {
|
|||
|
||||
// EncodeBlock takes a slice of 1010 bytes (505 16-bit PCM samples).
|
||||
// It outputs encoded (compressed) bytes (each byte containing two ADPCM nibbles) to the encoder's dest writer.
|
||||
// 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
|
||||
// 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) error {
|
||||
bSize := 1010
|
||||
if len(block) != bSize {
|
||||
|
@ -234,14 +234,14 @@ func (d *Decoder) DecodeBlock(block []byte) error {
|
|||
return fmt.Errorf("unsupported block size. Given: %v, expected: %v", len(block), bSize)
|
||||
}
|
||||
|
||||
// initialize decoder with first 4 bytes of the block
|
||||
// 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]
|
||||
d.dest.Write(block[0:2])
|
||||
|
||||
// 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
|
||||
// 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.
|
||||
for i := 4; i < bSize; i++ {
|
||||
twoNibs := block[i]
|
||||
nib2 := byte(twoNibs >> 4)
|
||||
|
|
|
@ -37,13 +37,13 @@ import (
|
|||
// TestEncodeBlock will read PCM data, encode it in blocks and generate ADPCM
|
||||
// then compare the result with expected ADPCM.
|
||||
func TestEncodeBlock(t *testing.T) {
|
||||
//read input pcm
|
||||
// Read input pcm.
|
||||
pcm, err := ioutil.ReadFile("../../../test/test-data/av/input/raw-voice.pcm")
|
||||
if err != nil {
|
||||
t.Errorf("Unable to read input PCM file: %v", err)
|
||||
}
|
||||
|
||||
//encode adpcm
|
||||
// Encode adpcm.
|
||||
inBSize := 1010
|
||||
numBlocks := len(pcm) / inBSize
|
||||
outBSize := int(float64(inBSize)/4 + 3.5) // compression is 4:1 and 3.5 bytes of info are added to each block
|
||||
|
@ -57,7 +57,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")
|
||||
if err != nil {
|
||||
t.Errorf("Unable to read expected ADPCM file: %v", err)
|
||||
|
@ -71,13 +71,13 @@ func TestEncodeBlock(t *testing.T) {
|
|||
// TestDecodeBlock will read encoded ADPCM, decode it in blocks and then compare the
|
||||
// resulting PCM with the expected decoded PCM.
|
||||
func TestDecodeBlock(t *testing.T) {
|
||||
//read adpcm
|
||||
// Read adpcm.
|
||||
comp, err := ioutil.ReadFile("../../../test/test-data/av/input/encoded-voice.adpcm")
|
||||
if err != nil {
|
||||
t.Errorf("Unable to read input ADPCM file: %v", err)
|
||||
}
|
||||
|
||||
//decode adpcm
|
||||
// Decode adpcm.
|
||||
inBSize := 256
|
||||
numBlocks := len(comp) / inBSize
|
||||
outBSize := 2 + (inBSize-4)*4 // 2 bytes are copied, 2 are used as block header info, the remaining bytes are decompressed 1:4
|
||||
|
@ -91,7 +91,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")
|
||||
if err != nil {
|
||||
t.Errorf("Unable to read expected PCM file: %v", err)
|
||||
|
|
Loading…
Reference in New Issue