examples/audio: Refactoring: Unify channel usages (#302)

This commit is contained in:
Hajime Hoshi 2017-06-04 01:34:36 +09:00
parent 579491afbd
commit 035e4d321a

View File

@ -90,13 +90,13 @@ type Player struct {
audioPlayer *audio.Player audioPlayer *audio.Player
total time.Duration total time.Duration
seekedCh chan error seekedCh chan error
seBytes []uint8
seCh chan []uint8
volume128 int volume128 int
} }
var ( var (
musicPlayer *Player musicPlayer *Player
seBytes []byte
seCh = make(chan []byte)
) )
func playerBarRect() (x, y, w, h int) { func playerBarRect() (x, y, w, h int) {
@ -108,6 +108,10 @@ func playerBarRect() (x, y, w, h int) {
func NewPlayer(audioContext *audio.Context) (*Player, error) { func NewPlayer(audioContext *audio.Context) (*Player, error) {
const bytesPerSample = 4 // TODO: This should be defined in audio package const bytesPerSample = 4 // TODO: This should be defined in audio package
wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
if err != nil {
return nil, err
}
oggF, err := ebitenutil.OpenFile("_resources/audio/game.ogg") oggF, err := ebitenutil.OpenFile("_resources/audio/game.ogg")
if err != nil { if err != nil {
return nil, err return nil, err
@ -131,21 +135,31 @@ func NewPlayer(audioContext *audio.Context) (*Player, error) {
audioPlayer: p, audioPlayer: p,
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate, total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
volume128: 128, volume128: 128,
seCh: make(chan []uint8),
} }
player.audioPlayer.Play() player.audioPlayer.Play()
go func() {
s, err := wav.Decode(audioContext, wavF)
if err != nil {
log.Fatal(err)
return
}
b, err := ioutil.ReadAll(s)
if err != nil {
log.Fatal(err)
return
}
player.seCh <- b
}()
return player, nil return player, nil
} }
func (p *Player) update() error { func (p *Player) update() error {
p.input.update() p.input.update()
p.updateBar()
p.updatePlayPause()
p.updateSE()
p.updateVolume()
if err := p.audioContext.Update(); err != nil {
return err
}
select { select {
case p.seBytes = <-p.seCh:
close(p.seCh)
p.seCh = nil
case err := <-p.seekedCh: case err := <-p.seekedCh:
if err != nil { if err != nil {
return err return err
@ -154,17 +168,24 @@ func (p *Player) update() error {
p.seekedCh = nil p.seekedCh = nil
default: default:
} }
p.updateBar()
p.updatePlayPause()
p.updateSE()
p.updateVolume()
if err := p.audioContext.Update(); err != nil {
return err
}
return nil return nil
} }
func (p *Player) updateSE() { func (p *Player) updateSE() {
if seBytes == nil { if p.seBytes == nil {
return return
} }
if !p.input.isKeyTriggered(ebiten.KeyP) { if !p.input.isKeyTriggered(ebiten.KeyP) {
return return
} }
sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, seBytes) sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, p.seBytes)
sePlayer.Play() sePlayer.Play()
} }
@ -202,6 +223,7 @@ func (p *Player) updateBar() {
if !p.input.isMouseButtonTriggered(ebiten.MouseButtonLeft) { if !p.input.isMouseButtonTriggered(ebiten.MouseButtonLeft) {
return return
} }
// Start seeking.
x, y := ebiten.CursorPosition() x, y := ebiten.CursorPosition()
bx, by, bw, bh := playerBarRect() bx, by, bw, bh := playerBarRect()
const padding = 4 const padding = 4
@ -255,12 +277,6 @@ Press Z or X to change volume of the music
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
if seBytes == nil {
select {
case seBytes = <-seCh:
default:
}
}
if err := musicPlayer.update(); err != nil { if err := musicPlayer.update(); err != nil {
return err return err
} }
@ -272,28 +288,10 @@ func update(screen *ebiten.Image) error {
} }
func main() { func main() {
wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
if err != nil {
log.Fatal(err)
}
audioContext, err := audio.NewContext(sampleRate) audioContext, err := audio.NewContext(sampleRate)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
go func() {
s, err := wav.Decode(audioContext, wavF)
if err != nil {
log.Fatal(err)
return
}
b, err := ioutil.ReadAll(s)
if err != nil {
log.Fatal(err)
return
}
seCh <- b
close(seCh)
}()
musicPlayer, err = NewPlayer(audioContext) musicPlayer, err = NewPlayer(audioContext)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)