This commit is contained in:
Hajime Hoshi 2016-03-17 22:59:29 +09:00
parent 79e1d1b1f7
commit eb0d650612
3 changed files with 48 additions and 4 deletions

View File

@ -16,7 +16,9 @@ package main
import ( import (
"fmt" "fmt"
"image/color"
"log" "log"
"time"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
@ -29,10 +31,32 @@ const (
screenHeight = 240 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 ( var (
audioContext *audio.Context audioContext *audio.Context
audioLoadingDone chan struct{} audioLoadingDone chan struct{}
audioLoaded bool audioLoaded bool
audioPlayer *audio.Player
total time.Duration
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
@ -44,6 +68,24 @@ func update(screen *ebiten.Image) error {
default: 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()) msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())
if !audioLoaded { if !audioLoaded {
msg += "\nNow Loading..." msg += "\nNow Loading..."
@ -63,18 +105,20 @@ func main() {
audioLoadingDone = make(chan struct{}) 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
} }
p, err := audioContext.NewPlayer(s) total = s.Len()
audioPlayer, err = audioContext.NewPlayer(s)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return return
} }
close(audioLoadingDone) close(audioLoadingDone)
p.Play() audioPlayer.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)

View File

@ -240,7 +240,7 @@ func (p *Player) Pause() error {
} }
func (p *Player) Current() time.Duration { 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? // TODO: Volume / SetVolume?

View File

@ -40,5 +40,5 @@ func (s *stream) Seek(offset int64, whence int) (int64, error) {
func (s *stream) Len() time.Duration { func (s *stream) Len() time.Duration {
const bytesPerSample = 4 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)
} }