mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
audio: Refactoring: Remove dependency on clock package
This commit is contained in:
parent
28c96d256a
commit
a6b3f761f8
@ -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,10 +121,25 @@ func (c *Context) loop() {
|
|||||||
hooks.OnResumeAudio(func() {
|
hooks.OnResumeAudio(func() {
|
||||||
resumeCh <- struct{}{}
|
resumeCh <- struct{}{}
|
||||||
})
|
})
|
||||||
clock.OnStart(func() {
|
|
||||||
|
initCh := make(chan struct{})
|
||||||
|
var once sync.Once
|
||||||
|
hooks.AppendHookOnBeforeUpdate(func() error {
|
||||||
|
once.Do(func() {
|
||||||
close(initCh)
|
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.
|
||||||
// Accessing oto.Player functions requires the environment to be already initialized,
|
// Accessing oto.Player functions requires the environment to be already initialized,
|
||||||
// but if Ebiten is used for a shared library, the timing when init functions are called
|
// but if Ebiten is used for a shared library, the timing when init functions are called
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user