example/piano: Cache notes

This commit is contained in:
Hajime Hoshi 2015-02-17 11:19:30 +09:00
parent 9ec7b13f27
commit e366e1c6f3

View File

@ -49,8 +49,18 @@ func init() {
} }
} }
var (
noteLCache = map[int][]int16{}
noteRCache = map[int][]int16{}
)
func addNote(freq float64, vol float64) { func addNote(freq float64, vol float64) {
f := int(freq) f := int(freq)
if l, ok := noteLCache[f]; ok {
r := noteRCache[f]
audio.Play(-1, l, r)
return
}
length := len(pcm) * baseFreq / f length := len(pcm) * baseFreq / f
l := make([]int16, length) l := make([]int16, length)
r := make([]int16, length) r := make([]int16, length)
@ -63,6 +73,8 @@ func addNote(freq float64, vol float64) {
jj += f jj += f
j = jj / baseFreq j = jj / baseFreq
} }
noteLCache[f] = l
noteRCache[f] = r
audio.Play(-1, l, r) audio.Play(-1, l, r)
} }