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 3ebf928ffe
commit 16df9e78cb
1 changed files with 8 additions and 9 deletions

View File

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