examples/audio: Refactoring: Introduce Player struct

This commit is contained in:
Hajime Hoshi 2016-03-20 04:02:51 +09:00
parent 4a4621a126
commit 11c9ab3fbf

View File

@ -51,12 +51,15 @@ func init() {
playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff}) playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff})
} }
type Player struct {
audioPlayer *audio.Player
total time.Duration
}
var ( var (
audioContext *audio.Context audioContext *audio.Context
audioLoadingDone chan struct{} player *Player
audioLoaded bool playerCh = make(chan *Player)
audioPlayer *audio.Player
total time.Duration
mouseButtonState = map[ebiten.MouseButton]int{} mouseButtonState = map[ebiten.MouseButton]int{}
) )
@ -67,8 +70,8 @@ func playerBarRect() (x, y, w, h int) {
return return
} }
func updatePlayerBar() error { func (p *Player) updateBar() error {
if !audioLoaded { if p.audioPlayer == nil {
return nil return nil
} }
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
@ -87,22 +90,22 @@ func updatePlayerBar() error {
if x < bx || bx+bw <= x { if x < bx || bx+bw <= x {
return nil return nil
} }
p := time.Duration(x-bx) * total / time.Duration(bw) pos := time.Duration(x-bx) * p.total / time.Duration(bw)
return audioPlayer.Seek(p) return p.audioPlayer.Seek(pos)
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
audioContext.Update() audioContext.Update()
if !audioLoaded { if player == nil {
select { select {
case <-audioLoadingDone: case player = <-playerCh:
audioLoaded = true
default: default:
} }
} }
if player != nil {
if err := updatePlayerBar(); err != nil { if err := player.updateBar(); err != nil {
return err return err
}
} }
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
@ -110,8 +113,8 @@ func update(screen *ebiten.Image) error {
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(playerBarImage, op) screen.DrawImage(playerBarImage, op)
currentTimeStr := "" currentTimeStr := ""
if audioLoaded && audioPlayer.IsPlaying() { if player != nil && player.audioPlayer.IsPlaying() {
c := audioPlayer.Current() c := player.audioPlayer.Current()
// Current Time // Current Time
m := (c / time.Minute) % 100 m := (c / time.Minute) % 100
@ -120,7 +123,7 @@ func update(screen *ebiten.Image) error {
// Bar // Bar
cw, ch := playerCurrentImage.Size() cw, ch := playerCurrentImage.Size()
cx := int(time.Duration(w)*c/total) + x - cw/2 cx := int(time.Duration(w)*c/player.total) + x - cw/2
cy := y - (ch-h)/2 cy := y - (ch-h)/2
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(cx), float64(cy)) op.GeoM.Translate(float64(cx), float64(cy))
@ -129,7 +132,7 @@ func update(screen *ebiten.Image) error {
msg := fmt.Sprintf(`FPS: %0.2f msg := fmt.Sprintf(`FPS: %0.2f
%s`, ebiten.CurrentFPS(), currentTimeStr) %s`, ebiten.CurrentFPS(), currentTimeStr)
if !audioLoaded { if player == nil {
msg += "\nNow Loading..." msg += "\nNow Loading..."
} }
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
@ -144,23 +147,24 @@ func main() {
} }
// TODO: sampleRate should be obtained from the ogg file. // TODO: sampleRate should be obtained from the ogg file.
audioContext = audio.NewContext(22050) audioContext = audio.NewContext(22050)
audioLoadingDone = make(chan struct{})
// TODO: This doesn't work synchronously on browsers because of decoding. Fix this. // TODO: This doesn't work synchronously on browsers because of decoding. Fix this.
go func() { go func() {
var err error
s, err := vorbis.Decode(audioContext, f) s, err := vorbis.Decode(audioContext, f)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return return
} }
total = s.Len() p, err := audioContext.NewPlayer(s)
audioPlayer, err = audioContext.NewPlayer(s)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return return
} }
close(audioLoadingDone) playerCh <- &Player{
audioPlayer.Play() audioPlayer: p,
total: s.Len(),
}
close(playerCh)
p.Play()
}() }()
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil { if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)