mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 02:42:02 +01:00
audio/internal/convert: add a lazy-load getter for cosTable
(#2404)
Closes #2286
This commit is contained in:
parent
fc2f999ebf
commit
18123a6336
@ -17,21 +17,32 @@ package convert
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cosTable = [65536]float64{}
|
var (
|
||||||
|
// cosTable contains values of cosine applied to the range [0, π/2).
|
||||||
|
// It must be initialised the first time it is referenced
|
||||||
|
// in a function via its lazy load wrapper getCosTable().
|
||||||
|
cosTable []float64
|
||||||
|
cosTableOnce sync.Once
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func getCosTable() []float64 {
|
||||||
for i := range cosTable {
|
cosTableOnce.Do(func() {
|
||||||
cosTable[i] = math.Cos(float64(i) * math.Pi / 2 / float64(len(cosTable)))
|
cosTable = make([]float64, 65536)
|
||||||
}
|
for i := range cosTable {
|
||||||
|
cosTable[i] = math.Cos(float64(i) * math.Pi / 2 / float64(len(cosTable)))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return cosTable
|
||||||
}
|
}
|
||||||
|
|
||||||
func fastCos01(x float64) float64 {
|
func fastCos01(x float64) float64 {
|
||||||
if x < 0 {
|
if x < 0 {
|
||||||
x = -x
|
x = -x
|
||||||
}
|
}
|
||||||
i := int(4 * float64(len(cosTable)) * x)
|
i := int(4 * float64(len(getCosTable())) * x)
|
||||||
if 4*len(cosTable) < i {
|
if 4*len(cosTable) < i {
|
||||||
i %= 4 * len(cosTable)
|
i %= 4 * len(cosTable)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user