audio: Adjust buffer size for Android Chrome

This commit is contained in:
Hajime Hoshi 2017-12-17 01:20:10 +09:00
parent 7f9afd4e14
commit 7cd89bc44b

View File

@ -45,6 +45,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/audiobinding" "github.com/hajimehoshi/ebiten/internal/audiobinding"
"github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/web"
) )
type players struct { type players struct {
@ -245,9 +246,14 @@ func (c *Context) loop() {
// This is a heuristic decision of audio buffer size. // This is a heuristic decision of audio buffer size.
// On most desktops, 1/30[s] is enough but there are some known environment that is too short (e.g. Windows on Parallels). // On most desktops, 1/30[s] is enough but there are some known environment that is too short (e.g. Windows on Parallels).
// On browsers, this depends on the sample rate, but 1/15[s] should work with any sample rate. // On desktop browsers, 1/15[s] should work with any sample rate except for mobile browsers.
// On mobiles, we don't have enough data. For iOS, 1/30[s] is too short and 1/20[s] seems fine. 1/15[s] is safer. // On mobiles, we don't have enough data. For iOS, 1/30[s] is too short and 1/20[s] seems fine. 1/15[s] is safer.
bufferSize := c.sampleRate * channelNum * bytesPerSample / 15 bufferSize := c.sampleRate * channelNum * bytesPerSample / 15
if web.IsAndroidChrome() {
// On Android Chrome, it looks like 9600 * 4 is a sweet spot of the buffer size
// regardless of the sample rate. This is about 1/5[s] for 48000[Hz].
bufferSize = 9600 * channelNum * bytesPerSample
}
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, bufferSize) p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, bufferSize)
if err != nil { if err != nil {
audiobinding.SetError(err) audiobinding.SetError(err)