ebiten/audio/context.go

75 lines
1.6 KiB
Go
Raw Normal View History

// Copyright 2019 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package audio
import (
"io"
"sync"
2019-04-28 12:35:59 +02:00
"github.com/hajimehoshi/ebiten/internal/hooks"
)
type context interface {
NewPlayer() io.WriteCloser
io.Closer
}
var contextForTesting context
2019-04-27 15:30:09 +02:00
func newContext(sampleRate int) context {
if contextForTesting != nil {
return contextForTesting
}
2019-04-27 15:30:09 +02:00
ch := make(chan struct{})
var once sync.Once
hooks.AppendHookOnBeforeUpdate(func() error {
once.Do(func() {
close(ch)
})
return nil
})
return newContextImpl(sampleRate, ch)
}
2019-04-28 12:35:59 +02:00
type hook interface {
OnSuspendAudio(f func())
OnResumeAudio(f func())
AppendHookOnBeforeUpdate(f func() error)
}
var hookForTesting hook
func getHook() hook {
if hookForTesting != nil {
return hookForTesting
}
return &hookImpl{}
}
type hookImpl struct{}
func (h *hookImpl) OnSuspendAudio(f func()) {
hooks.OnSuspendAudio(f)
}
func (h *hookImpl) OnResumeAudio(f func()) {
hooks.OnResumeAudio(f)
}
func (h *hookImpl) AppendHookOnBeforeUpdate(f func() error) {
hooks.AppendHookOnBeforeUpdate(f)
}