2019-01-26 19:29:41 +01:00
|
|
|
// 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"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
2019-01-26 19:29:41 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type context interface {
|
|
|
|
NewPlayer() io.WriteCloser
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
var contextForTesting context
|
|
|
|
|
2019-04-27 15:30:09 +02:00
|
|
|
func newContext(sampleRate int) context {
|
2019-01-26 19:29:41 +01:00
|
|
|
if contextForTesting != nil {
|
2019-03-01 20:42:19 +01:00
|
|
|
return contextForTesting
|
2019-01-26 19:29:41 +01:00
|
|
|
}
|
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
|
|
|
|
})
|
2020-12-30 11:21:15 +01:00
|
|
|
return newOtoContext(sampleRate, ch)
|
2019-01-26 19:29:41 +01:00
|
|
|
}
|
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)
|
|
|
|
}
|