mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
audio: Give up syncing audio timer and game timer (#617)
Syncing was already incomplete (e.g. decoding takes more than one frame and delays can happen in this case). Giving up syncing audio timer and game timer should not affect the game experience so much. Instead, clock implementation will be much simpler.
This commit is contained in:
parent
5976e4bbbc
commit
faaf391619
@ -28,9 +28,6 @@
|
|||||||
// When multiple players play, mixing is automatically done.
|
// When multiple players play, mixing is automatically done.
|
||||||
// Note that too many players may cause distortion.
|
// Note that too many players may cause distortion.
|
||||||
//
|
//
|
||||||
// Ebiten's game progress tries to synchronizes with audio progress, but
|
|
||||||
// delay can happen if, e.g., decoding an audio source takes long.
|
|
||||||
//
|
|
||||||
// For the simplest example to play sound, see wav package in the examples.
|
// For the simplest example to play sound, see wav package in the examples.
|
||||||
package audio
|
package audio
|
||||||
|
|
||||||
@ -246,9 +243,6 @@ func (c *Context) loop() {
|
|||||||
}
|
}
|
||||||
defer p.Close()
|
defer p.Close()
|
||||||
|
|
||||||
bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / clock.FPS
|
|
||||||
written := int64(0)
|
|
||||||
prevWritten := int64(0)
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-suspendCh:
|
case <-suspendCh:
|
||||||
@ -259,11 +253,6 @@ func (c *Context) loop() {
|
|||||||
c.err = err
|
c.err = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
written += int64(n)
|
|
||||||
fs := written/int64(bytesPerFrame) - prevWritten/int64(bytesPerFrame)
|
|
||||||
clock.ProceedAudioTimer(fs)
|
|
||||||
prevWritten = written
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,15 +13,6 @@
|
|||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// Package clock manages game timers.
|
// Package clock manages game timers.
|
||||||
//
|
|
||||||
// There are two types of clocks internally:
|
|
||||||
//
|
|
||||||
// System clock:
|
|
||||||
// A clock offered by the OS.
|
|
||||||
//
|
|
||||||
// Audio clock:
|
|
||||||
// An audio clock that is used in the higher priority over the system clock.
|
|
||||||
// An audio clock might not exist when the audio is not used.
|
|
||||||
package clock
|
package clock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -33,8 +24,6 @@ const FPS = 60
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
frames int64
|
frames int64
|
||||||
audioTimeInFrames int64
|
|
||||||
lastAudioTimeInFrames int64
|
|
||||||
|
|
||||||
// lastSystemTime is the last system time in the previous Update.
|
// lastSystemTime is the last system time in the previous Update.
|
||||||
lastSystemTime int64
|
lastSystemTime int64
|
||||||
@ -62,13 +51,6 @@ func OnStart(f func()) {
|
|||||||
m.Unlock()
|
m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProceedAudioTimer increments the audio time by the given number of frames.
|
|
||||||
func ProceedAudioTimer(num int64) {
|
|
||||||
m.Lock()
|
|
||||||
audioTimeInFrames += num
|
|
||||||
m.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateFPS(now int64) {
|
func updateFPS(now int64) {
|
||||||
if lastFPSUpdated == 0 {
|
if lastFPSUpdated == 0 {
|
||||||
lastFPSUpdated = now
|
lastFPSUpdated = now
|
||||||
@ -110,21 +92,6 @@ func Update() int {
|
|||||||
count := 0
|
count := 0
|
||||||
syncWithSystemClock := false
|
syncWithSystemClock := false
|
||||||
|
|
||||||
if audioTimeInFrames > 0 && lastAudioTimeInFrames != audioTimeInFrames {
|
|
||||||
// If the audio clock is updated, use this.
|
|
||||||
if frames < audioTimeInFrames {
|
|
||||||
count = int(audioTimeInFrames - frames)
|
|
||||||
}
|
|
||||||
lastAudioTimeInFrames = audioTimeInFrames
|
|
||||||
|
|
||||||
// Now the current lastSystemTime value is not meaningful,
|
|
||||||
// force to sync lastSystemTime with the system timer.
|
|
||||||
syncWithSystemClock = true
|
|
||||||
} else {
|
|
||||||
// Use system clock when the audio clock is not updated yet.
|
|
||||||
// As the audio clock can be updated discountinuously,
|
|
||||||
// the system clock is still needed.
|
|
||||||
|
|
||||||
if diff > 5*int64(time.Second)/FPS {
|
if diff > 5*int64(time.Second)/FPS {
|
||||||
// The previous time is too old.
|
// The previous time is too old.
|
||||||
// Let's force to sync the game time with the system clock.
|
// Let's force to sync the game time with the system clock.
|
||||||
@ -132,7 +99,6 @@ func Update() int {
|
|||||||
} else {
|
} else {
|
||||||
count = int(diff * FPS / int64(time.Second))
|
count = int(diff * FPS / int64(time.Second))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Stabilize FPS.
|
// Stabilize FPS.
|
||||||
// Without this adjustment, count can be unstable like 0, 2, 0, 2, ...
|
// Without this adjustment, count can be unstable like 0, 2, 0, 2, ...
|
||||||
|
Loading…
Reference in New Issue
Block a user