audio: Unify buffer size 1/15[s] experimentally

This would mitigate glitches on Windows (#451)
This commit is contained in:
Hajime Hoshi 2017-12-17 00:41:35 +09:00
parent d1f9312242
commit 7f9afd4e14
4 changed files with 6 additions and 85 deletions

View File

@ -243,7 +243,12 @@ func (c *Context) loop() {
// e.g. a variable for JVM on Android might not be set.
<-initCh
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, c.bufferSize())
// 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 browsers, this depends on the sample rate, but 1/15[s] should work with any sample rate.
// 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
p, err := oto.NewPlayer(c.sampleRate, channelNum, bytesPerSample, bufferSize)
if err != nil {
audiobinding.SetError(err)
return

View File

@ -1,22 +0,0 @@
// 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 / 20
}

View File

@ -1,22 +0,0 @@
// 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
}

View File

@ -1,40 +0,0 @@
// 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 := 10
if !web.IsMobileBrowser() {
// TODO: More general calculation
switch c.sampleRate {
case 44100, 88200:
n = 30
case 22050:
// #434
n = 15
case 24000, 32000, 48000:
n = 20
default:
n = 15
}
}
return c.sampleRate * channelNum * bytesPerSample / n
}