From e45c67e15706066827504bda638046f1776d33d1 Mon Sep 17 00:00:00 2001 From: Trek H Date: Thu, 28 Feb 2019 13:05:45 +1030 Subject: [PATCH] ADPCM: encoder now uses bytes.Buffer so that bytes and byte arrays can be written out --- stream/adpcm/adpcm.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/stream/adpcm/adpcm.go b/stream/adpcm/adpcm.go index de123bf7..326f6758 100644 --- a/stream/adpcm/adpcm.go +++ b/stream/adpcm/adpcm.go @@ -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))