audio: Use samller window for resampling on browsers

This commit is contained in:
Hajime Hoshi 2017-09-29 02:05:14 +09:00
parent 2c6f6e605e
commit b4e3e9375c

View File

@ -19,6 +19,7 @@ import (
"math" "math"
"github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/internal/web"
) )
func sinc(x float64) float64 { func sinc(x float64) float64 {
@ -126,16 +127,19 @@ func (r *Resampling) src(i int) (float64, float64, error) {
} }
func (r *Resampling) at(t int64) (float64, float64, error) { func (r *Resampling) at(t int64) (float64, float64, error) {
const windowSize = 8 windowSize := 8
if web.IsBrowser() {
windowSize = 4
}
tInSrc := float64(t) * float64(r.from) / float64(r.to) tInSrc := float64(t) * float64(r.from) / float64(r.to)
startN := int64(tInSrc) - windowSize startN := int64(tInSrc) - int64(windowSize)
if startN < 0 { if startN < 0 {
startN = 0 startN = 0
} }
if r.size/4 <= startN { if r.size/4 <= startN {
startN = r.size/4 - 1 startN = r.size/4 - 1
} }
endN := int64(tInSrc) + windowSize + 1 endN := int64(tInSrc) + int64(windowSize) + 1
if r.size/4 <= endN { if r.size/4 <= endN {
endN = r.size/4 - 1 endN = r.size/4 - 1
} }
@ -146,7 +150,7 @@ func (r *Resampling) at(t int64) (float64, float64, error) {
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
w := 0.5 + 0.5*math.Cos(2*math.Pi*(tInSrc-float64(n))/(windowSize*2+1)) w := 0.5 + 0.5*math.Cos(2*math.Pi*(tInSrc-float64(n))/(float64(windowSize)*2+1))
s := sinc(math.Pi*(tInSrc-float64(n))) * w s := sinc(math.Pi*(tInSrc-float64(n))) * w
lv += srcL * s lv += srcL * s
rv += srcR * s rv += srcR * s