ADPCM: encoder now uses bytes.Buffer so that bytes and byte arrays can be written out

This commit is contained in:
Trek H 2019-02-28 13:05:45 +10:30
parent edb0ec6de1
commit e45c67e157
1 changed files with 8 additions and 9 deletions

View File

@ -33,26 +33,26 @@ LICENSE
package adpcm
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
)
// Encoder is used to encode to ADPCM from PCM data.
// pred and index hold state that persists between calls to encodeSample and calcHead.
// dest is the output, ie. where the encoded ADPCM data is written to.
// dest is the output buffer that implements io.writer and io.bytewriter, ie. where the encoded ADPCM data is written to.
type Encoder struct {
dest io.ByteWriter
dest *bytes.Buffer
pred int16
index int16
}
// 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, ie. where the decoded PCM data is written to.
// 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 io.ByteWriter
dest *bytes.Buffer
pred int16
index int16
step int16
@ -81,7 +81,7 @@ var stepTable = []int16{
}
// NewEncoder retuns a new ADPCM encoder.
func NewEncoder(dst io.ByteWriter) *Encoder {
func NewEncoder(dst *bytes.Buffer) *Encoder {
e := Encoder{
dest: dst,
}
@ -89,7 +89,7 @@ func NewEncoder(dst io.ByteWriter) *Encoder {
}
// NewDecoder retuns a new ADPCM decoder.
func NewDecoder(dst io.ByteWriter) *Decoder {
func NewDecoder(dst *bytes.Buffer) *Decoder {
d := Decoder{
step: stepTable[0],
dest: dst,
@ -213,8 +213,7 @@ func (e *Encoder) calcHead(sample []byte) error {
intSample := int16(binary.LittleEndian.Uint16(sample))
e.encodeSample(intSample)
e.dest.WriteByte(sample[0])
e.dest.WriteByte(sample[1])
e.dest.Write(sample)
e.dest.WriteByte(byte(uint16(e.index)))
e.dest.WriteByte(byte(0x00))