audio: Refactoring: Remove dependency on clock package

This commit is contained in:
Hajime Hoshi 2019-01-10 00:44:53 +09:00
parent 28c96d256a
commit a6b3f761f8
2 changed files with 17 additions and 35 deletions

View File

@ -42,7 +42,6 @@ import (
"github.com/hajimehoshi/oto" "github.com/hajimehoshi/oto"
"github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/hooks" "github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/web" "github.com/hajimehoshi/ebiten/internal/web"
) )
@ -67,20 +66,6 @@ var (
theContextLock sync.Mutex theContextLock sync.Mutex
) )
func init() {
hooks.AppendHookOnBeforeUpdate(func() error {
var err error
theContextLock.Lock()
if theContext != nil {
theContext.m.Lock()
err = theContext.err
theContext.m.Unlock()
}
theContextLock.Unlock()
return err
})
}
// NewContext creates a new audio context with the given sample rate. // NewContext creates a new audio context with the given sample rate.
// //
// The sample rate is also used for decoding MP3 with audio/mp3 package // The sample rate is also used for decoding MP3 with audio/mp3 package
@ -128,8 +113,6 @@ func newDriver(sampleRate int) (io.WriteCloser, error) {
} }
func (c *Context) loop() { func (c *Context) loop() {
initCh := make(chan struct{})
suspendCh := make(chan struct{}, 1) suspendCh := make(chan struct{}, 1)
resumeCh := make(chan struct{}, 1) resumeCh := make(chan struct{}, 1)
hooks.OnSuspendAudio(func() { hooks.OnSuspendAudio(func() {
@ -138,8 +121,23 @@ func (c *Context) loop() {
hooks.OnResumeAudio(func() { hooks.OnResumeAudio(func() {
resumeCh <- struct{}{} resumeCh <- struct{}{}
}) })
clock.OnStart(func() {
close(initCh) initCh := make(chan struct{})
var once sync.Once
hooks.AppendHookOnBeforeUpdate(func() error {
once.Do(func() {
close(initCh)
})
var err error
theContextLock.Lock()
if theContext != nil {
theContext.m.Lock()
err = theContext.err
theContext.m.Unlock()
}
theContextLock.Unlock()
return err
}) })
// Initialize oto.Player lazily to enable calling NewContext in an 'init' function. // Initialize oto.Player lazily to enable calling NewContext in an 'init' function.

View File

@ -30,9 +30,6 @@ var (
fpsCount = 0 fpsCount = 0
tpsCount = 0 tpsCount = 0
onStartCalled bool
onStart func()
m sync.Mutex m sync.Mutex
) )
@ -50,12 +47,6 @@ func CurrentTPS() float64 {
return v return v
} }
func OnStart(f func()) {
m.Lock()
onStart = f
m.Unlock()
}
func calcCountFromTPS(tps int64, now int64) int { func calcCountFromTPS(tps int64, now int64) int {
if tps == 0 { if tps == 0 {
return 0 return 0
@ -132,13 +123,6 @@ func Update(tps int) int {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
if !onStartCalled {
if onStart != nil {
onStart()
onStartCalled = true
}
}
n := now() n := now()
c := 0 c := 0
if tps == UncappedTPS { if tps == UncappedTPS {