dndmusicbot/opus/decode_test.go

87 lines
1.2 KiB
Go
Raw Normal View History

2022-11-27 23:42:51 +00:00
package opus
import (
"context"
"log"
"os"
"sync"
"testing"
"time"
"github.com/gopxl/beep"
2022-11-27 23:42:51 +00:00
)
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{}
f beep.Format
2022-11-27 23:42:51 +00:00
)
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)
}
f = beep.Format{
SampleRate: beep.SampleRate(48000),
NumChannels: 2,
Precision: 2,
}
2022-11-27 23:42:51 +00:00
loop := beep.Loop(-1, d)
mu.Lock()
mixer.Add(loop)
mu.Unlock()
for {
select {
default:
update()
case <-ctx.Done():
return
}
}
}