example/audio: More punches

This commit is contained in:
Hajime Hoshi 2016-06-27 03:11:10 +09:00
parent e822aae4a2
commit 0ed95031c5

View File

@ -15,9 +15,9 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"image/color" "image/color"
"io/ioutil"
"log" "log"
"time" "time"
@ -61,10 +61,9 @@ type Player struct {
var ( var (
audioContext *audio.Context audioContext *audio.Context
musicPlayer *Player musicPlayer *Player
seStream *wav.Stream seBytes []byte
sePlayer *audio.Player
musicCh = make(chan *Player) musicCh = make(chan *Player)
seCh = make(chan *wav.Stream) seCh = make(chan []byte)
mouseButtonState = map[ebiten.MouseButton]int{} mouseButtonState = map[ebiten.MouseButton]int{}
keyState = map[ebiten.Key]int{} keyState = map[ebiten.Key]int{}
volume128 = 128 volume128 = 128
@ -77,16 +76,8 @@ func playerBarRect() (x, y, w, h int) {
return return
} }
type SEStream struct {
*bytes.Reader
}
func (s *SEStream) Close() error {
return nil
}
func (p *Player) updateSE() error { func (p *Player) updateSE() error {
if seStream == nil { if seBytes == nil {
return nil return nil
} }
if !ebiten.IsKeyPressed(ebiten.KeyP) { if !ebiten.IsKeyPressed(ebiten.KeyP) {
@ -97,17 +88,8 @@ func (p *Player) updateSE() error {
if keyState[ebiten.KeyP] != 1 { if keyState[ebiten.KeyP] != 1 {
return nil return nil
} }
if sePlayer == nil { sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes)
var err error if err != nil {
sePlayer, err = audio.NewPlayer(audioContext, seStream)
if err != nil {
return err
}
}
if sePlayer.IsPlaying() {
return nil
}
if err := sePlayer.Rewind(); err != nil {
return err return err
} }
return sePlayer.Play() return sePlayer.Play()
@ -187,9 +169,9 @@ func update(screen *ebiten.Image) error {
default: default:
} }
} }
if seStream == nil { if seBytes == nil {
select { select {
case seStream = <-seCh: case seBytes = <-seCh:
default: default:
} }
} }
@ -266,7 +248,12 @@ func main() {
log.Fatal(err) log.Fatal(err)
return return
} }
seCh <- s b, err := ioutil.ReadAll(s)
if err != nil {
log.Fatal(err)
return
}
seCh <- b
close(seCh) close(seCh)
}() }()
go func() { go func() {