2016-04-13 18:26:31 +02:00
<!DOCTYPE html>
2016-08-27 17:58:27 +02:00
< script src = "../scripts/force-https.js" > < / script >
2016-04-13 18:26:31 +02:00
< link rel = "shortcut icon" href = "../favicon.png" type = "image/png" >
< link rel = "icon" href = "../favicon.png" type = "image/png" >
< title > Ebiten example - audio< / title >
2016-08-27 17:48:15 +02:00
< link rel = "stylesheet" href = "../stylesheets/bootstrap.min.css" >
< link rel = "stylesheet" href = "../stylesheets/highlight-github.css" >
< link rel = "stylesheet" href = "../stylesheets/ebiten.css" >
2016-08-26 19:28:57 +02:00
< header class = "navbar" > < div class = "container" >
< div class = "navbar-header" >
2016-08-27 17:48:15 +02:00
< a class = "navbar-brand" href = ".." > Ebiten< / a >
2016-08-26 19:28:57 +02:00
< / div >
2016-08-26 19:37:08 +02:00
< nav class = "collapse navbar-collapse" >
< ul class = "nav navbar-nav navbar-right" >
< li > < a href = "https://github.com/hajimehoshi/ebiten" > GitHub< / a > < / li >
2016-08-27 17:58:27 +02:00
< li > < a href = "https://godoc.org/github.com/hajimehoshi/ebiten" > GoDoc< / a > < / li >
2016-08-26 19:37:08 +02:00
< li > < a href = "https://github.com/hajimehoshi/ebiten/wiki" > Wiki< / a >
< / ul >
< / nav >
2016-08-26 19:28:57 +02:00
< / header >
2016-04-13 18:26:31 +02:00
2016-08-26 19:28:57 +02:00
< main > < div class = "container" >
2016-08-26 20:18:16 +02:00
< h2 > Ebiten example - audio< / h2 >
2016-08-26 19:28:57 +02:00
< iframe src = "audio.content.html" width = "640" height = "480" > < / iframe >
2016-08-27 07:00:06 +02:00
< pre > < code class = "language-go" > // + build example
2016-08-26 16:33:36 +02:00
package main
2016-04-13 18:26:31 +02:00
import (
2016-08-26 22:36:52 +02:00
" fmt"
" image/color"
" io/ioutil"
" log"
" time"
2016-04-13 18:26:31 +02:00
2016-08-26 22:36:52 +02:00
" github.com/hajimehoshi/ebiten"
" github.com/hajimehoshi/ebiten/audio"
" github.com/hajimehoshi/ebiten/audio/vorbis"
" github.com/hajimehoshi/ebiten/audio/wav"
" github.com/hajimehoshi/ebiten/ebitenutil"
2016-04-13 18:26:31 +02:00
)
const (
2016-08-26 22:36:52 +02:00
screenWidth = 320
screenHeight = 240
2016-04-13 18:26:31 +02:00
)
var (
2016-08-26 22:36:52 +02:00
playerBarImage *ebiten.Image
playerCurrentImage *ebiten.Image
2016-04-13 18:26:31 +02:00
)
func init() {
2016-08-26 22:36:52 +02:00
var err error
playerBarImage, err = ebiten.NewImage(300, 4, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := playerBarImage.Fill(& color.RGBA{0x80, 0x80, 0x80, 0xff}); err != nil {
log.Fatal(err)
}
2016-04-13 18:26:31 +02:00
2016-08-26 22:36:52 +02:00
playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := playerCurrentImage.Fill(& color.RGBA{0xff, 0xff, 0xff, 0xff}); err != nil {
log.Fatal(err)
}
2016-04-13 18:26:31 +02:00
}
type Player struct {
2016-08-26 22:36:52 +02:00
audioPlayer *audio.Player
total time.Duration
2016-04-13 18:26:31 +02:00
}
var (
2016-08-26 22:36:52 +02:00
audioContext *audio.Context
musicPlayer *Player
seBytes []byte
musicCh = make(chan *Player)
seCh = make(chan []byte)
mouseButtonState = map[ebiten.MouseButton]int{}
keyState = map[ebiten.Key]int{}
volume128 = 128
2016-04-13 18:26:31 +02:00
)
func playerBarRect() (x, y, w, h int) {
2016-08-26 22:36:52 +02:00
w, h = playerBarImage.Size()
x = (screenWidth - w) / 2
y = screenHeight - h - 16
return
2016-04-13 18:26:31 +02:00
}
func (p *Player) updateSE() error {
2016-08-26 22:36:52 +02:00
if seBytes == nil {
return nil
}
if !ebiten.IsKeyPressed(ebiten.KeyP) {
keyState[ebiten.KeyP] = 0
return nil
}
keyState[ebiten.KeyP]+ +
if keyState[ebiten.KeyP] != 1 {
return nil
}
sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes)
if err != nil {
return err
}
return sePlayer.Play()
2016-04-13 18:26:31 +02:00
}
func (p *Player) updateVolume() error {
2016-08-26 22:36:52 +02:00
if p.audioPlayer == nil {
return nil
}
if ebiten.IsKeyPressed(ebiten.KeyZ) {
volume128--
}
if ebiten.IsKeyPressed(ebiten.KeyX) {
volume128+ +
}
if volume128 < 0 {
volume128 = 0
}
if 128 < volume128 {
volume128 = 128
}
p.audioPlayer.SetVolume(float64(volume128) / 128)
2016-04-13 18:26:31 +02:00
return nil
}
func (p *Player) updatePlayPause() error {
2016-08-26 22:36:52 +02:00
if p.audioPlayer == nil {
return nil
}
if !ebiten.IsKeyPressed(ebiten.KeyS) {
keyState[ebiten.KeyS] = 0
return nil
}
keyState[ebiten.KeyS]+ +
if keyState[ebiten.KeyS] != 1 {
return nil
}
if p.audioPlayer.IsPlaying() {
return p.audioPlayer.Pause()
}
return p.audioPlayer.Play()
2016-04-13 18:26:31 +02:00
}
func (p *Player) updateBar() error {
2016-08-26 22:36:52 +02:00
if p.audioPlayer == nil {
return nil
}
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
mouseButtonState[ebiten.MouseButtonLeft] = 0
return nil
}
mouseButtonState[ebiten.MouseButtonLeft]+ +
if mouseButtonState[ebiten.MouseButtonLeft] != 1 {
return nil
}
x, y := ebiten.CursorPosition()
bx, by, bw, bh := playerBarRect()
const padding = 4
if y < by-padding || by+ bh+ padding < = y {
return nil
}
if x < bx || bx+ bw < = x {
return nil
}
pos := time.Duration(x-bx) * p.total / time.Duration(bw)
return p.audioPlayer.Seek(pos)
2016-04-13 18:26:31 +02:00
}
func (p *Player) close() error {
2016-08-26 22:36:52 +02:00
return p.audioPlayer.Close()
2016-04-13 18:26:31 +02:00
}
func update(screen *ebiten.Image) error {
2016-08-26 22:36:52 +02:00
if musicPlayer == nil {
select {
case musicPlayer = < -musicCh:
default:
}
2016-04-13 18:26:31 +02:00
}
2016-08-26 22:36:52 +02:00
if seBytes == nil {
select {
case seBytes = < -seCh:
default:
}
2016-04-13 18:26:31 +02:00
}
2016-08-26 22:36:52 +02:00
if musicPlayer != nil {
if err := musicPlayer.updateBar(); err != nil {
return err
}
if err := musicPlayer.updatePlayPause(); err != nil {
return err
}
if err := musicPlayer.updateSE(); err != nil {
return err
}
if err := musicPlayer.updateVolume(); err != nil {
return err
}
2016-04-13 18:26:31 +02:00
}
2016-08-26 22:36:52 +02:00
op := & ebiten.DrawImageOptions{}
x, y, w, h := playerBarRect()
op.GeoM.Translate(float64(x), float64(y))
if err := screen.DrawImage(playerBarImage, op); err != nil {
return err
}
currentTimeStr := " 00:00"
if musicPlayer != nil {
c := musicPlayer.audioPlayer.Current()
2016-04-13 18:26:31 +02:00
2016-08-26 22:36:52 +02:00
// Current Time
m := (c / time.Minute) % 100
s := (c / time.Second) % 60
currentTimeStr = fmt.Sprintf(" %02d:%02d" , m, s)
2016-04-13 18:26:31 +02:00
2016-08-26 22:36:52 +02:00
// Bar
cw, ch := playerCurrentImage.Size()
cx := int(time.Duration(w)*c/musicPlayer.total) + x - cw/2
cy := y - (ch-h)/2
op := & ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(cx), float64(cy))
if err := screen.DrawImage(playerCurrentImage, op); err != nil {
return err
}
2016-08-26 16:33:36 +02:00
}
2016-04-13 18:26:31 +02:00
2016-08-26 22:36:52 +02:00
msg := fmt.Sprintf(`FPS: %0.2f
2016-04-13 18:26:31 +02:00
Press S to toggle Play/Pause
Press P to play SE
Press Z or X to change volume of the music
%s`, ebiten.CurrentFPS(), currentTimeStr)
2016-08-26 22:36:52 +02:00
if musicPlayer == nil {
msg + = " \nNow Loading..."
}
if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
if err := audioContext.Update(); err != nil {
return err
}
return nil
2016-04-13 18:26:31 +02:00
}
func main() {
2016-08-26 22:36:52 +02:00
wavF, err := ebitenutil.OpenFile(" _resources/audio/jab.wav" )
2016-04-13 18:26:31 +02:00
if err != nil {
2016-08-26 22:36:52 +02:00
log.Fatal(err)
2016-04-13 18:26:31 +02:00
}
2016-08-26 22:36:52 +02:00
oggF, err := ebitenutil.OpenFile(" _resources/audio/ragtime.ogg" )
2016-04-13 18:26:31 +02:00
if err != nil {
2016-08-26 22:36:52 +02:00
log.Fatal(err)
2016-04-13 18:26:31 +02:00
}
2016-08-26 22:36:52 +02:00
const sampleRate = 22050
const bytesPerSample = 4 // TODO: This should be defined in audio package
audioContext, err = audio.NewContext(sampleRate)
2016-04-13 18:26:31 +02:00
if err != nil {
2016-08-26 22:36:52 +02:00
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)
}()
go func() {
s, err := vorbis.Decode(audioContext, oggF)
if err != nil {
log.Fatal(err)
return
}
p, err := audio.NewPlayer(audioContext, s)
if err != nil {
log.Fatal(err)
return
}
musicCh < - & Player{
audioPlayer: p,
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
}
close(musicCh)
// TODO: Is this goroutine-safe?
if err := p.Play(); err != nil {
log.Fatal(err)
return
}
}()
if err := ebiten.Run(update, screenWidth, screenHeight, 2, " Audio (Ebiten Demo)" ); err != nil {
log.Fatal(err)
}
if musicPlayer != nil {
if err := musicPlayer.close(); err != nil {
log.Fatal(err)
}
}
2016-04-13 18:26:31 +02:00
}
< / code > < / pre >
2016-08-26 19:28:57 +02:00
< / div > < / main >
< footer > < div class = "container" >
< p > © 2013 Hajime Hoshi< / p >
2016-08-28 00:10:41 +02:00
< p > Code is licensed under < a href = "https://github.com/hajimehoshi/ebiten/blob/master/LICENSE" > the Apache License 2.0< / a > .< / p >
2016-08-26 19:28:57 +02:00
< p > The content of this page is licensed under < a href = "https://creativecommons.org/licenses/by/4.0/" > the Creative Commons Attribution 4.0 License< / a > .< / p >
< / div > < / footer >
2016-04-13 18:26:31 +02:00
2016-08-27 17:48:15 +02:00
< script src = "../scripts/highlight.pack.js" > < / script >
2016-08-27 07:00:06 +02:00
< script > hljs . initHighlightingOnLoad ( ) ; < / script >
2016-08-27 17:44:50 +02:00
< script >
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-83252440-1', 'auto');
ga('send', 'pageview');
< / script >