package audio

import (
	"sync"

	"github.com/ebitengine/oto/v3"
	"go.uber.org/zap"
)

var (
	otoCtx  *oto.Context
	otoOnce sync.Once
)

const (
	UniversalSampleRate = 44100
	DefaultChannelCount = 2
)

func initContext() (*oto.Context, error) {
	var initErr error
	otoOnce.Do(func() {
		op := &oto.NewContextOptions{}
		op.SampleRate = UniversalSampleRate
		op.ChannelCount = DefaultChannelCount
		op.Format = oto.FormatSignedInt16LE

		var ready <-chan struct{}
		otoCtx, ready, initErr = oto.NewContext(op)
		if initErr != nil {
			return
		}
		<-ready
		zap.S().Infof("oto/v3 音频系统就绪 (%d Hz)", UniversalSampleRate)
	})
	return otoCtx, initErr
}
