examples/audio: Copy SE buffer only once

This commit is contained in:
Hajime Hoshi 2016-03-27 19:23:55 +09:00
parent 27911b8156
commit 457581c8ad

View File

@ -18,7 +18,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"image/color" "image/color"
"io" "io/ioutil"
"log" "log"
"time" "time"
@ -63,6 +63,7 @@ var (
audioContext *audio.Context audioContext *audio.Context
musicPlayer *Player musicPlayer *Player
seStream *wav.Stream seStream *wav.Stream
seBuffer []byte
musicCh = make(chan *Player) musicCh = make(chan *Player)
seCh = make(chan *wav.Stream) seCh = make(chan *wav.Stream)
mouseButtonState = map[ebiten.MouseButton]int{} mouseButtonState = map[ebiten.MouseButton]int{}
@ -88,15 +89,14 @@ func (p *Player) updateSE() error {
if keyState[ebiten.KeyP] != 1 { if keyState[ebiten.KeyP] != 1 {
return nil return nil
} }
// Clone the stream if seBuffer == nil {
b := &bytes.Buffer{} b, err := ioutil.ReadAll(seStream)
if _, err := seStream.Seek(0, 0); err != nil { if err != nil {
return err return err
} }
if _, err := io.Copy(b, seStream); err != nil { seBuffer = b
return err
} }
sePlayer, err := audioContext.NewPlayer(bytes.NewReader(b.Bytes())) sePlayer, err := audioContext.NewPlayer(bytes.NewReader(seBuffer))
if err != nil { if err != nil {
return err return err
} }