From 7f9afd4e14c89c95d15b301d9775502028edb26b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 17 Dec 2017 00:41:35 +0900 Subject: [PATCH] audio: Unify buffer size 1/15[s] experimentally This would mitigate glitches on Windows (#451) --- audio/audio.go | 7 ++++++- audio/buffersize.go | 22 ---------------------- audio/buffersize_ios.go | 22 ---------------------- audio/buffersize_js.go | 40 ---------------------------------------- 4 files changed, 6 insertions(+), 85 deletions(-) delete mode 100644 audio/buffersize.go delete mode 100644 audio/buffersize_ios.go delete mode 100644 audio/buffersize_js.go diff --git a/audio/audio.go b/audio/audio.go index 0a9d2ccb0..25c50a8f3 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -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 diff --git a/audio/buffersize.go b/audio/buffersize.go deleted file mode 100644 index 9024789f5..000000000 --- a/audio/buffersize.go +++ /dev/null @@ -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 -} diff --git a/audio/buffersize_ios.go b/audio/buffersize_ios.go deleted file mode 100644 index 5c60e20b3..000000000 --- a/audio/buffersize_ios.go +++ /dev/null @@ -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 -} diff --git a/audio/buffersize_js.go b/audio/buffersize_js.go deleted file mode 100644 index 5a34bd2cc..000000000 --- a/audio/buffersize_js.go +++ /dev/null @@ -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 -}