From 35d98f1a73b3fa8399bc8eb5321e651fa6400e46 Mon Sep 17 00:00:00 2001 From: Trek H Date: Mon, 11 Feb 2019 15:58:33 +1030 Subject: [PATCH] ADPCM: removed unneeded files and renamed files --- cmd/adpcm/codec.go | 265 ------------------ cmd/adpcmgo/cmd/decode/decode.go | 70 ----- .../cmd/decode_sample/decode_sample.go | 30 -- cmd/adpcmgo/cmd/encode/encode.go | 47 ---- .../cmd/encode_decode/encode_decode.go | 111 -------- .../cmd/encode_sample/encode_sample.go | 30 -- cmd/adpcmgo/codec.go | 175 ------------ 7 files changed, 728 deletions(-) delete mode 100644 cmd/adpcm/codec.go delete mode 100644 cmd/adpcmgo/cmd/decode/decode.go delete mode 100644 cmd/adpcmgo/cmd/decode_sample/decode_sample.go delete mode 100644 cmd/adpcmgo/cmd/encode/encode.go delete mode 100644 cmd/adpcmgo/cmd/encode_decode/encode_decode.go delete mode 100644 cmd/adpcmgo/cmd/encode_sample/encode_sample.go delete mode 100644 cmd/adpcmgo/codec.go diff --git a/cmd/adpcm/codec.go b/cmd/adpcm/codec.go deleted file mode 100644 index b5042f83..00000000 --- a/cmd/adpcm/codec.go +++ /dev/null @@ -1,265 +0,0 @@ -package adpcm - -import ( - "encoding/binary" - "fmt" - "math" -) - -// table of index changes -var indexTable = []int16{ - -1, -1, -1, -1, 2, 4, 6, 8, - -1, -1, -1, -1, 2, 4, 6, 8, -} - -// quantize step size table -var stepTable = []int16{ - 7, 8, 9, 10, 11, 12, 13, 14, - 16, 17, 19, 21, 23, 25, 28, 31, - 34, 37, 41, 45, 50, 55, 60, 66, - 73, 80, 88, 97, 107, 118, 130, 143, - 157, 173, 190, 209, 230, 253, 279, 307, - 337, 371, 408, 449, 494, 544, 598, 658, - 724, 796, 876, 963, 1060, 1166, 1282, 1411, - 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, - 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, - 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, - 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, - 32767, -} - -var ( - encPred int16 - encIndex int16 - decPred int16 - decIndex int16 - decStep int16 = 7 - - pCount int - pNum = 8 -) - -// EncodeSample takes a single 16 bit PCM sample and -// returns a byte of which the last 4 bits are an encoded ADPCM nibble -func EncodeSample(sample int16) byte { - - // find difference from predicted sample - delta := sample - encPred - - // set sign bit and find absolute value of difference - var nibble byte - if delta < 0 { - nibble = 8 - delta = -delta - } - - step := stepTable[encIndex] - diff := step >> 3 - var mask byte = 4 - - // quantize delta down to four bits - for i := 0; i < 3; i++ { - if delta > step { - nibble |= mask - delta -= step - diff += step - } - mask >>= 1 - step >>= 1 - } - - // adjust predicted sample based on calculated difference - if nibble&8 != 0 { - encPred -= diff - } else { - encPred += diff - } - - // check for underflow and overflow - if encPred < math.MinInt16 { - encPred = math.MinInt16 - } else if encPred > math.MaxInt16 { - encPred = math.MaxInt16 - } - - encIndex += indexTable[nibble&7] - - // check for underflow and overflow - if encIndex < 0 { - encIndex = 0 - } else if encIndex > int16(len(stepTable)-1) { - encIndex = int16(len(stepTable) - 1) - } - - return nibble -} - -// 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 -func DecodeSample(nibble byte) int16 { - - // compute predicted sample estimate (decPred): - - // calculate difference - var diff int16 - if nibble&4 != 0 { - diff += decStep - } - if nibble&2 != 0 { - diff += decStep >> 1 - } - if nibble&1 != 0 { - diff += decStep >> 2 - } - diff += decStep >> 3 - // account for sign bit - if nibble&8 != 0 { - diff = -diff - } - // adjust predicted sample based on calculated difference - decPred += diff - - // check for overflow and underflow - if decPred > math.MaxInt16 { - decPred = math.MaxInt16 - } else if decPred < math.MinInt16 { - decPred = math.MinInt16 - } - - // compute new step size: - - // adjust index into step size lookup table using nibble - decIndex += indexTable[nibble] - // check for overflow and underflow - if decIndex < 0 { - decIndex = 0 - } else if decIndex > int16(len(stepTable)-1) { - decIndex = int16(len(stepTable) - 1) - } - // find new quantizer step size - decStep = stepTable[decIndex] - - return decPred -} - -func calcHead(sample []byte) ([]byte, error) { - if len(sample) != 2 { - return nil, fmt.Errorf("length of given byte array is: %v, expected: 2", len(sample)) - } - - intSample := int16(binary.LittleEndian.Uint16(sample)) - - // if pCount < pNum { - // fmt.Println("calcHead enc", intSample) - // fmt.Println(encIndex, encPred) - // } - - EncodeSample(intSample) - - head := make([]byte, 2) - - head[0] = sample[0] - head[1] = sample[1] - - // if pCount < pNum { - // fmt.Println("after calcHead enc") - // fmt.Println(encIndex, encPred) - // } - - head = append(head, byte(uint16(encIndex))) - head = append(head, byte(0x00)) - return head, nil -} - -func EncodeBlock(block []byte) ([]byte, error) { - - if len(block) != 1010 { - return nil, fmt.Errorf("unsupported block size. Given: %v, expected: 1010, ie. 505 16-bit PCM samples", len(block)) - } - - result, err := calcHead(block[0:2]) - if err != nil { - return nil, err - } - - for i := 2; i < len(block); i++ { - if (i+1)%4 == 0 { - - // var intsample int16 - // if pCount < pNum { - // fmt.Println(block[2], block[3]) - // fmt.Println("first enc", block[i-1:i+1]) - // fmt.Println(encIndex, encPred) - // } - sample2 := EncodeSample(int16(binary.LittleEndian.Uint16(block[i-1 : i+1]))) - - // if pCount < pNum { - // intsample = int16(binary.LittleEndian.Uint16(block[i+1 : i+3])) - // fmt.Println("second enc", int16(intsample)) - // fmt.Println(encIndex, encPred) - // } - sample := EncodeSample(int16(binary.LittleEndian.Uint16(block[i+1 : i+3]))) - - // if pCount < pNum { - // fmt.Println("after enc") - // fmt.Println(encIndex, encPred) - // fmt.Println() - // } - - result = append(result, byte((sample<<4)|sample2)) - - pCount++ - - } - } - - return result, nil -} - -func DecodeBlock(block []byte) ([]byte, error) { - - if len(block) != 256 { - return nil, fmt.Errorf("unsupported block size. Given: %v, expected: 256", len(block)) - } - - if pCount < pNum { - fmt.Println("pre dec", block[0:8]) - fmt.Println(decIndex, decPred, decStep) - } - - var result []byte - decPred = int16(binary.LittleEndian.Uint16(block[0:2])) - decIndex = int16(block[2]) - decStep = stepTable[decIndex] - result = append(result, block[0:2]...) - - for i := 4; i < len(block); i++ { - originalSample := block[i] - secondSample := byte(originalSample >> 4) - firstSample := byte((secondSample << 4) ^ originalSample) - - if pCount < pNum { - fmt.Println("first dec", firstSample, originalSample, len(block)) - fmt.Println(decIndex, decPred, decStep) - } - - firstBytes := make([]byte, 2) - binary.LittleEndian.PutUint16(firstBytes, uint16(DecodeSample(firstSample))) - result = append(result, firstBytes...) - - if pCount < pNum { - fmt.Println("second dec", secondSample) - fmt.Println(decIndex, decPred, decStep) - fmt.Println() - } - - secondBytes := make([]byte, 2) - binary.LittleEndian.PutUint16(secondBytes, uint16(DecodeSample(secondSample))) - result = append(result, secondBytes...) - - pCount++ - - } - - return result, nil -} diff --git a/cmd/adpcmgo/cmd/decode/decode.go b/cmd/adpcmgo/cmd/decode/decode.go deleted file mode 100644 index 087f3a3a..00000000 --- a/cmd/adpcmgo/cmd/decode/decode.go +++ /dev/null @@ -1,70 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "io/ioutil" - "os" - - "github.com/bovarysme/adpcm" - "github.com/go-audio/audio" - "github.com/go-audio/wav" -) - -func main() { - var inPath string - var nChannels int - var rate int - var bps int - flag.StringVar(&inPath, "path", "out.adpcm", "file path of input data") - flag.IntVar(&nChannels, "channels", 1, "number of channels in adpcm data") - flag.IntVar(&rate, "rate", 8000, "number of channels in adpcm data") - flag.IntVar(&bps, "bps", 16, "number of channels in adpcm data") - flag.Parse() - //read adpcm - comp, err := ioutil.ReadFile(inPath) - if err != nil { - panic(err) - } - fmt.Println("Read ADPCM from " + inPath + ". First 8 bytes: ") - for i := 0; i < len(comp) && i < 16; i++ { - fmt.Print(comp[i], ", ") - } - fmt.Println() - //decode adpcm - var decoded []int - deco := adpcm.NewDecoder(nChannels) - deco.Decode(comp, &decoded) - - //encode wav and save to file - finalWavPath := "decoded.wav" - of, err := os.Create(finalWavPath) - if err != nil { - panic(err) - } - defer of.Close() - - sampleBytes := bps / 8 - - // normal uncompressed WAV format - wavformat := 1 - - enc := wav.NewEncoder(of, rate, bps, nChannels, wavformat) - - format := &audio.Format{ - NumChannels: nChannels, - SampleRate: rate, - } - - intBuf := &audio.IntBuffer{Data: decoded, Format: format, SourceBitDepth: sampleBytes * 8} - - if err := enc.Write(intBuf); err != nil { - panic(err) - } - - if err := enc.Close(); err != nil { - panic(err) - } - fmt.Printf("Saved audio to %s\n", finalWavPath) - -} diff --git a/cmd/adpcmgo/cmd/decode_sample/decode_sample.go b/cmd/adpcmgo/cmd/decode_sample/decode_sample.go deleted file mode 100644 index 9a487058..00000000 --- a/cmd/adpcmgo/cmd/decode_sample/decode_sample.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "fmt" - - "bitbucket.org/ausocean/av/cmd/adpcmgo" -) - -func main() { - - var input byte = 4 - sample := adpcmgo.DecodeSample(input) - fmt.Println(sample) - - input = 5 - sample = adpcmgo.DecodeSample(input) - fmt.Println(sample) - - input = 6 - sample = adpcmgo.DecodeSample(input) - fmt.Println(sample) - - input = 16 - sample = adpcmgo.DecodeSample(input) - fmt.Println(sample) - - input = 235 - sample = adpcmgo.DecodeSample(input) - fmt.Println(sample) -} diff --git a/cmd/adpcmgo/cmd/encode/encode.go b/cmd/adpcmgo/cmd/encode/encode.go deleted file mode 100644 index 73a7e38d..00000000 --- a/cmd/adpcmgo/cmd/encode/encode.go +++ /dev/null @@ -1,47 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "io/ioutil" - - "bitbucket.org/ausocean/av/cmd/adpcmgo" -) - -func main() { - var inPath string - flag.StringVar(&inPath, "path", "test.pcm", "file path of input data") - flag.Parse() - //read pcm - pcm, err := ioutil.ReadFile(inPath) - if err != nil { - panic(err) - } - - //encode adpcm - var comp []byte - start := 0 - for i := 0; i < len(pcm); i++ { - if i%1010 == 1009 { - block := pcm[start : i+1] - encBlock, err := adpcmgo.EncodeBlock(block) - if err != nil { - //todo: use correct logging of error - panic(err) - } - comp = append(comp, encBlock...) - start = i + 1 - } - } - // save adpcm to file - adpcmPath := "out.adpcm" - err = ioutil.WriteFile(adpcmPath, comp, 0644) - if err != nil { - panic(err) - } - fmt.Println("Saved ADPCM to " + adpcmPath + ". First 8 bytes: ") - for i := 0; i < len(comp) && i < 16; i++ { - fmt.Print(comp[i], ", ") - } - fmt.Println() -} diff --git a/cmd/adpcmgo/cmd/encode_decode/encode_decode.go b/cmd/adpcmgo/cmd/encode_decode/encode_decode.go deleted file mode 100644 index 79c886f9..00000000 --- a/cmd/adpcmgo/cmd/encode_decode/encode_decode.go +++ /dev/null @@ -1,111 +0,0 @@ -package main - -import ( - "encoding/binary" - "flag" - "fmt" - "io/ioutil" - "os" - - "github.com/bovarysme/adpcm" - "github.com/go-audio/audio" - "github.com/go-audio/wav" - "github.com/pkg/errors" -) - -// This program is for compare data size of pcm and adpcm, it takes a wav file and compresses and encodes it to adpcm, -// the adpcm byte array is stored as a file and then the adpcm is decoded and the result saved as final.wav - -func main() { - //open wav - var wavPath string - flag.StringVar(&wavPath, "path", "before_compression.wav", "file path of input data") - flag.Parse() - f, err := os.Open(wavPath) - if err != nil { - panic(err) - } - defer f.Close() - //decode wav - dec := wav.NewDecoder(f) - if !dec.IsValidFile() { - panic(errors.Errorf("invalid WAV file %q", wavPath)) - } - sampleRate, nchannels, bps := int(dec.SampleRate), int(dec.NumChans), int(dec.BitDepth) - buf, err := dec.FullPCMBuffer() - if err != nil { - panic(err) - } - fmt.Println("Decoded wav. First 8 samples: ") - for i := 0; i < len(buf.Data) && i < 8; i++ { - fmt.Print(buf.Data[i], ", ") - } - fmt.Println() - // save pcm to file - var pcmBytes []byte - for _, sample := range buf.Data { - bs := make([]byte, 2) - binary.LittleEndian.PutUint16(bs, uint16(sample)) - pcmBytes = append(pcmBytes, bs[0], bs[1]) - } - pcmPath := "out.pcm" - err = ioutil.WriteFile(pcmPath, pcmBytes, 0644) - if err != nil { - panic(err) - } - fmt.Println("Saved raw PCM to " + pcmPath + ". First 8 bytes: ") - for i := 0; i < len(pcmBytes) && i < 8; i++ { - fmt.Print(pcmBytes[i], ", ") - } - fmt.Println() - //encode adpcm - var comp []byte - adpcm.Encode(buf.Data, &comp) - // save adpcm to file - adpcmPath := "out.adpcm" - err = ioutil.WriteFile(adpcmPath, comp, 0644) - if err != nil { - panic(err) - } - fmt.Println("Saved ADPCM to " + adpcmPath + ". First 8 bytes: ") - for i := 0; i < len(comp) && i < 16; i++ { - fmt.Print(comp[i], ", ") - } - fmt.Println() - //decode adpcm - var decoded []int - deco := adpcm.NewDecoder(nchannels) - deco.Decode(comp, &decoded) - - //encode wav and save to file - finalWavPath := "final.wav" - of, err := os.Create(finalWavPath) - if err != nil { - panic(err) - } - defer of.Close() - - sampleBytes := bps / 8 - - // normal uncompressed WAV format - wavformat := 1 - - enc := wav.NewEncoder(of, sampleRate, bps, nchannels, wavformat) - - format := &audio.Format{ - NumChannels: nchannels, - SampleRate: sampleRate, - } - - intBuf := &audio.IntBuffer{Data: decoded, Format: format, SourceBitDepth: sampleBytes * 8} - - if err := enc.Write(intBuf); err != nil { - panic(err) - } - - if err := enc.Close(); err != nil { - panic(err) - } - fmt.Printf("Saved audio to %s\n", finalWavPath) - -} diff --git a/cmd/adpcmgo/cmd/encode_sample/encode_sample.go b/cmd/adpcmgo/cmd/encode_sample/encode_sample.go deleted file mode 100644 index 4dca508e..00000000 --- a/cmd/adpcmgo/cmd/encode_sample/encode_sample.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "fmt" - - "bitbucket.org/ausocean/av/cmd/adpcmgo" -) - -func main() { - - input := -370 - nibble := adpcmgo.EncodeSample(input) - fmt.Println(nibble) - - input = 8 - nibble = adpcmgo.EncodeSample(input) - fmt.Println(nibble) - - input = 1 - nibble = adpcmgo.EncodeSample(input) - fmt.Println(nibble) - - input = 31793 - nibble = adpcmgo.EncodeSample(input) - fmt.Println(nibble) - - input = -7386 - nibble = adpcmgo.EncodeSample(input) - fmt.Println(nibble) -} diff --git a/cmd/adpcmgo/codec.go b/cmd/adpcmgo/codec.go deleted file mode 100644 index e79d981f..00000000 --- a/cmd/adpcmgo/codec.go +++ /dev/null @@ -1,175 +0,0 @@ -package adpcmgo - -import ( - "encoding/binary" - "fmt" - "math" -) - -// table of index changes -var indexTable = []int16{ - -1, -1, -1, -1, 2, 4, 6, 8, - -1, -1, -1, -1, 2, 4, 6, 8, -} - -// quantize step size table -var stepTable = []int16{ - 7, 8, 9, 10, 11, 12, 13, 14, - 16, 17, 19, 21, 23, 25, 28, 31, - 34, 37, 41, 45, 50, 55, 60, 66, - 73, 80, 88, 97, 107, 118, 130, 143, - 157, 173, 190, 209, 230, 253, 279, 307, - 337, 371, 408, 449, 494, 544, 598, 658, - 724, 796, 876, 963, 1060, 1166, 1282, 1411, - 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, - 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, - 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, - 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, - 32767, -} - -var ( - encPred int16 - encIndex int16 - decPred int16 - decIndex int16 - decStep int16 = 7 -) - -// EncodeSample takes a single 16 bit PCM sample and -// returns a byte of which the last 4 bits are an encoded ADPCM nibble -func EncodeSample(sample int16) byte { - - // find difference from predicted sample - delta := sample - encPred - - // set sign bit and find absolute value of difference - var nibble byte - if delta < 0 { - nibble = 8 - delta = -delta - } - - step := stepTable[encIndex] - diff := step >> 3 - var mask byte = 4 - - // quantize delta down to four bits - for i := 0; i < 3; i++ { - if delta > step { - nibble |= mask - delta -= step - diff += step - } - mask >>= 1 - step >>= 1 - } - - // adjust predicted sample based on calculated difference - if nibble&8 != 0 { - encPred -= diff - } else { - encPred += diff - } - - // check for underflow and overflow - if encPred < math.MinInt16 { - encPred = math.MinInt16 - } else if encPred > math.MaxInt16 { - encPred = math.MaxInt16 - } - - encIndex += indexTable[nibble&7] - - // check for underflow and overflow - if encIndex < 0 { - encIndex = 0 - } else if encIndex > int16(len(stepTable)-1) { - encIndex = int16(len(stepTable) - 1) - } - - return nibble -} - -// 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 -func DecodeSample(nibble byte) int16 { - - // compute predicted sample estimate (decPred): - - // calculate difference - var diff int16 - if nibble&4 != 0 { - diff += decStep - } - if nibble&2 != 0 { - diff += decStep >> 1 - } - if nibble&1 != 0 { - diff += decStep >> 2 - } - diff += decStep >> 3 - // account for sign bit - if nibble&8 != 0 { - diff = -diff - } - // adjust predicted sample based on calculated difference - decPred += diff - - // check for overflow and underflow - if decPred > math.MaxInt16 { - decPred = math.MaxInt16 - } else if decPred < math.MinInt16 { - decPred = math.MinInt16 - } - - // compute new step size: - - // adjust index into step size lookup table using nibble - decIndex += indexTable[nibble] - // check for overflow and underflow - if decIndex < 0 { - decIndex = 0 - } else if decIndex > int16(len(stepTable)-1) { - decIndex = int16(len(stepTable) - 1) - } - // find new quantizer step size - decStep = stepTable[decIndex] - - return decPred -} - -func calcHead(sample []byte) []byte { - fmt.Println(sample[0], sample[1]) - intSample := int16(binary.LittleEndian.Uint16(sample)) - - fmt.Println("before enc", intSample) - - EncodeSample(intSample) - head := sample - - fmt.Println(encIndex) - - head = append(head, byte(uint16(encIndex))) - head = append(head, byte(0x00)) - return head -} - -func EncodeBlock(block []byte) ([]byte, error) { - if len(block) != 1010 { - return nil, fmt.Errorf("unsupported block size. Given: %v, expected: 1010, ie. 505 16-bit PCM samples", len(block)) - } - - result := calcHead(block[0:2]) - - for i := 2; i < len(block); i++ { - if (i+1)%4 == 0 { - sample2 := EncodeSample(int16(binary.LittleEndian.Uint16(block[i-1 : i+1]))) - sample := EncodeSample(int16(binary.LittleEndian.Uint16(block[i+1 : i+3]))) - result = append(result, byte((sample<<4)|sample2)) - - } - } - - return result, nil -}