audio: At most one audio context can exist (#271)

This commit is contained in:
Hajime Hoshi 2016-10-02 22:18:44 +09:00
parent 2eac8dca8c
commit 61676616a7

View File

@ -186,12 +186,22 @@ type Context struct {
writtenBytes int
}
var (
theContext *Context
theContextLock sync.Mutex
)
// NewContext creates a new audio context with the given sample rate (e.g. 44100).
func NewContext(sampleRate int) (*Context, error) {
// TODO: Panic if one context exists.
theContextLock.Lock()
defer theContextLock.Unlock()
if theContext != nil {
return nil, errors.New("audio: context is already created")
}
c := &Context{
sampleRate: sampleRate,
}
theContext = c
c.players = &players{
players: map[*Player]struct{}{},
}