55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package audio
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func pcm(samples int) []byte {
|
|
b := make([]byte, samples*2)
|
|
for i := 0; i < samples; i++ {
|
|
binary.LittleEndian.PutUint16(b[i*2:], uint16(int16(i)))
|
|
}
|
|
return b
|
|
}
|
|
func TestPCMBase64ChunkResample(t *testing.T) {
|
|
data := pcm(160)
|
|
if !IsPCM16Aligned(data) {
|
|
t.Fatal("aligned")
|
|
}
|
|
if IsPCM16Aligned([]byte{1}) {
|
|
t.Fatal("misaligned")
|
|
}
|
|
enc := Base64Encode(data)
|
|
dec, err := Base64Decode(enc)
|
|
if err != nil || len(dec) != len(data) {
|
|
t.Fatal(err)
|
|
}
|
|
chunks := ChunkBytes(data, 100)
|
|
if len(chunks) == 0 || len(chunks[0]) > 100 {
|
|
t.Fatal("chunks")
|
|
}
|
|
dur := ChunkPCM16ByDuration(data, 16000, 10*time.Millisecond)
|
|
if len(dur) != 1 {
|
|
t.Fatal("duration chunks")
|
|
}
|
|
up, err := ResamplePCM16MonoLinear(pcm(16000), 16000, 24000)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := len(up) / 2; got < 23990 || got > 24010 {
|
|
t.Fatalf("up samples=%d", got)
|
|
}
|
|
down, err := ResamplePCM16MonoLinear(pcm(24000), 24000, 16000)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := len(down) / 2; got < 15990 || got > 16010 {
|
|
t.Fatalf("down samples=%d", got)
|
|
}
|
|
if _, err := ResamplePCM16MonoLinear([]byte{1}, 16000, 24000); err == nil {
|
|
t.Fatal("want err")
|
|
}
|
|
}
|