dndmusicbot/opus/decode_test.go

80 lines
1.1 KiB
Go

package opus
import (
"context"
"log"
"os"
"sync"
"testing"
"time"
"github.com/faiface/beep"
)
var (
mu sync.Mutex
mixer beep.Mixer
frameSize int = 960
samples = make([][2]float64, frameSize)
buf []byte
maxBytes int = (frameSize * 2) * 2
done chan struct{}
)
func update() {
mu.Lock()
mixer.Stream(samples)
mu.Unlock()
for i := range samples {
for c := range samples[i] {
val := samples[i][c]
if val < -1 {
val = -1
}
if val > +1 {
val = +1
}
valInt16 := int16(val * (1<<15 - 1))
low := byte(valInt16)
high := byte(valInt16 >> 8)
buf[i*4+c*2+0] = low
buf[i*4+c*2+1] = high
}
}
//log.Printf("%+v", buf)
}
func TestMain(t *testing.T) {
buf = make([]byte, maxBytes)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*9)
defer cancel()
file, err := os.Open("test.opus")
if err != nil {
log.Fatal(err)
}
d, err := New(file)
if err != nil {
log.Fatal(err)
}
loop := beep.Loop(-1, d)
mu.Lock()
mixer.Add(loop)
mu.Unlock()
for {
select {
default:
update()
case <-ctx.Done():
return
}
}
}