mirror of
https://dev.narayana.im/narayana/telegabber.git
synced 2026-08-05 12:17:06 +00:00
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package audio
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
)
|
|
|
|
// 20ms interleaved stereo sine; both channels identical
|
|
func makeSineStereo(freqHz float64) []int16 {
|
|
pcm := make([]int16, SamplesPerOpusFrame*Channels)
|
|
for i := 0; i < SamplesPerOpusFrame; i++ {
|
|
v := int16(math.Sin(2*math.Pi*freqHz*float64(i)/SampleRate) * 16000)
|
|
pcm[i*2] = v
|
|
pcm[i*2+1] = v
|
|
}
|
|
return pcm
|
|
}
|
|
|
|
func TestEncodeDecodeRoundtrip(t *testing.T) {
|
|
enc, err := NewEncoder()
|
|
if err != nil {
|
|
t.Fatalf("NewEncoder: %v", err)
|
|
}
|
|
dec, err := NewDecoder()
|
|
if err != nil {
|
|
t.Fatalf("NewDecoder: %v", err)
|
|
}
|
|
|
|
in := makeSineStereo(440)
|
|
pkt := make([]byte, MaxOpusPacketBytes)
|
|
n, err := enc.Encode(in, pkt)
|
|
if err != nil {
|
|
t.Fatalf("Encode: %v", err)
|
|
}
|
|
if n <= 0 || n > MaxOpusPacketBytes {
|
|
t.Fatalf("Encode returned %d bytes", n)
|
|
}
|
|
|
|
out := make([]int16, MaxDecodeSamplesPerChannel*Channels)
|
|
samplesPerCh, err := dec.Decode(pkt[:n], out)
|
|
if err != nil {
|
|
t.Fatalf("Decode: %v", err)
|
|
}
|
|
if samplesPerCh != SamplesPerOpusFrame {
|
|
t.Errorf("Decode produced %d samples/ch, want %d", samplesPerCh, SamplesPerOpusFrame)
|
|
}
|
|
}
|
|
|