From eb0d65061207c2d122eb9fbf04fe54f6356272fd Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 17 Mar 2016 22:59:29 +0900 Subject: [PATCH] tmp --- examples/audio/main.go | 48 ++++++++++++++++++++++++++++++++++++-- exp/audio/audio.go | 2 +- exp/audio/vorbis/stream.go | 2 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/examples/audio/main.go b/examples/audio/main.go index f75003d1b..24d99dfef 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -16,7 +16,9 @@ package main import ( "fmt" + "image/color" "log" + "time" "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/ebitenutil" @@ -29,10 +31,32 @@ const ( screenHeight = 240 ) +var ( + playerBarImage *ebiten.Image + playerCurrentImage *ebiten.Image +) + +func init() { + var err error + playerBarImage, err = ebiten.NewImage(300, 4, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + playerBarImage.Fill(&color.RGBA{0x80, 0x80, 0x80, 0xff}) + + playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest) + if err != nil { + log.Fatal(err) + } + playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff}) +} + var ( audioContext *audio.Context audioLoadingDone chan struct{} audioLoaded bool + audioPlayer *audio.Player + total time.Duration ) func update(screen *ebiten.Image) error { @@ -44,6 +68,24 @@ func update(screen *ebiten.Image) error { default: } } + + op := &ebiten.DrawImageOptions{} + w, h := playerBarImage.Size() + x := float64(screenWidth-w) / 2 + y := float64(screenHeight - h - 16) + op.GeoM.Translate(x, y) + screen.DrawImage(playerBarImage, op) + if audioLoaded && audioPlayer.IsPlaying() { + c := audioPlayer.Current() + w, _ := playerBarImage.Size() + cw, ch := playerCurrentImage.Size() + cx := float64(w)*float64(c)/float64(total) + x - float64(cw)/2 + cy := y - float64(ch-h)/2 + op := &ebiten.DrawImageOptions{} + op.GeoM.Translate(cx, cy) + screen.DrawImage(playerCurrentImage, op) + } + msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS()) if !audioLoaded { msg += "\nNow Loading..." @@ -63,18 +105,20 @@ func main() { audioLoadingDone = make(chan struct{}) // TODO: This doesn't work synchronously on browsers because of decoding. Fix this. go func() { + var err error s, err := vorbis.Decode(audioContext, f) if err != nil { log.Fatal(err) return } - p, err := audioContext.NewPlayer(s) + total = s.Len() + audioPlayer, err = audioContext.NewPlayer(s) if err != nil { log.Fatal(err) return } close(audioLoadingDone) - p.Play() + audioPlayer.Play() }() if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil { log.Fatal(err) diff --git a/exp/audio/audio.go b/exp/audio/audio.go index 18a0a4e0a..eccd69e04 100644 --- a/exp/audio/audio.go +++ b/exp/audio/audio.go @@ -240,7 +240,7 @@ func (p *Player) Pause() error { } func (p *Player) Current() time.Duration { - return time.Duration(p.pos) * time.Second / time.Duration(p.context.sampleRate) + return time.Duration(p.pos/bytesPerSample/channelNum) * time.Second / time.Duration(p.context.sampleRate) } // TODO: Volume / SetVolume? diff --git a/exp/audio/vorbis/stream.go b/exp/audio/vorbis/stream.go index 483818eb1..bd381fb1c 100644 --- a/exp/audio/vorbis/stream.go +++ b/exp/audio/vorbis/stream.go @@ -40,5 +40,5 @@ func (s *stream) Seek(offset int64, whence int) (int64, error) { func (s *stream) Len() time.Duration { const bytesPerSample = 4 - return time.Duration(s.buf.Len() / bytesPerSample / s.sampleRate) + return time.Duration(s.buf.Len()/bytesPerSample) * time.Second / time.Duration(s.sampleRate) }