audio: Adjust the buffer size

This commit is contained in:
Hajime Hoshi 2017-07-12 03:15:01 +09:00
parent 3454b32cb6
commit c3202b8e58
6 changed files with 122 additions and 27 deletions

View File

@ -250,10 +250,7 @@ func (c *Context) loop() {
// e.g. a variable for JVM on Android might not be set.
<-c.initCh
// The buffer size is 1/15 sec.
// It looks like 1/20 sec is too short for Android.
s := c.sampleRate * channelNum * bytesPerSample / 15
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, s)
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, c.bufferSize())
if err != nil {
c.errCh <- err
return

22
audio/buffersize.go Normal file
View File

@ -0,0 +1,22 @@
// Copyright 2017 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.
// +build !js
// +build !ios
package audio
func (c *Context) bufferSize() int {
return c.sampleRate * channelNum * bytesPerSample / 30
}

22
audio/buffersize_ios.go Normal file
View File

@ -0,0 +1,22 @@
// Copyright 2017 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.
// +build ios
package audio
func (c *Context) bufferSize() int {
// TODO: Fix after oto uses HAL on Darwin.
return c.sampleRate * channelNum * bytesPerSample / 20
}

29
audio/buffersize_js.go Normal file
View File

@ -0,0 +1,29 @@
// Copyright 2017 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.
// +build js
package audio
import (
"github.com/hajimehoshi/ebiten/internal/web"
)
func (c *Context) bufferSize() int {
n := 30
if web.IsMobileBrowser() {
n = 10
}
return c.sampleRate * channelNum * bytesPerSample / n
}

View File

@ -17,32 +17,11 @@
package restorable
import (
"strings"
"github.com/gopherjs/gopherjs/js"
"github.com/hajimehoshi/ebiten/internal/web"
)
func isIOS() bool {
ua := js.Global.Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "iPhone") {
return false
}
return true
}
func isAndroidChrome() bool {
ua := js.Global.Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "Android") {
return false
}
if !strings.Contains(ua, "Chrome") {
return false
}
return true
}
func init() {
if isIOS() || isAndroidChrome() {
if web.IsMobileBrowser() {
restoringEnabled = false
}
}

46
internal/web/mobile.go Normal file
View File

@ -0,0 +1,46 @@
// Copyright 2017 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.
// +build js
package web
import (
"strings"
"github.com/gopherjs/gopherjs/js"
)
func isIOS() bool {
ua := js.Global.Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "iPhone") {
return false
}
return true
}
func isAndroidChrome() bool {
ua := js.Global.Get("navigator").Get("userAgent").String()
if !strings.Contains(ua, "Android") {
return false
}
if !strings.Contains(ua, "Chrome") {
return false
}
return true
}
func IsMobileBrowser() bool {
return isIOS() || isAndroidChrome()
}