ebiten/_docs/public/examples/audio.html

322 lines
8.4 KiB
HTML
Raw Normal View History

2016-04-13 18:26:31 +02:00
<!DOCTYPE html>
<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-26 19:28:57 +02:00
<link rel="stylesheet" href="/stylesheets/bootstrap.min.css">
<link rel="stylesheet" href="/stylesheets/ebiten.css">
<header class="navbar"><div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">Ebiten</a>
</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>
<li><a href="http://godoc.org/github.com/hajimehoshi/ebiten">GoDoc</a></li>
<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">
<h1>Ebiten example - audio</h1>
<iframe src="audio.content.html" width="640" height="480"></iframe>
<pre><code>// &#43;build example
2016-08-26 16:33:36 +02:00
package main
2016-04-13 18:26:31 +02:00
import (
&#34;fmt&#34;
&#34;image/color&#34;
2016-08-01 17:36:43 +02:00
&#34;io/ioutil&#34;
2016-04-13 18:26:31 +02:00
&#34;log&#34;
&#34;time&#34;
&#34;github.com/hajimehoshi/ebiten&#34;
2016-04-21 16:24:53 +02:00
&#34;github.com/hajimehoshi/ebiten/audio&#34;
&#34;github.com/hajimehoshi/ebiten/audio/vorbis&#34;
&#34;github.com/hajimehoshi/ebiten/audio/wav&#34;
2016-04-13 18:26:31 +02:00
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
)
const (
screenWidth = 320
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)
}
2016-08-26 16:33:36 +02:00
if err := playerBarImage.Fill(&amp;color.RGBA{0x80, 0x80, 0x80, 0xff}); err != nil {
log.Fatal(err)
}
2016-04-13 18:26:31 +02:00
playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
2016-08-26 16:33:36 +02:00
if err := playerCurrentImage.Fill(&amp;color.RGBA{0xff, 0xff, 0xff, 0xff}); err != nil {
log.Fatal(err)
}
2016-04-13 18:26:31 +02:00
}
type Player struct {
audioPlayer *audio.Player
total time.Duration
}
var (
audioContext *audio.Context
musicPlayer *Player
2016-08-01 17:36:43 +02:00
seBytes []byte
2016-04-13 18:26:31 +02:00
musicCh = make(chan *Player)
2016-08-01 17:36:43 +02:00
seCh = make(chan []byte)
2016-04-13 18:26:31 +02:00
mouseButtonState = map[ebiten.MouseButton]int{}
keyState = map[ebiten.Key]int{}
volume128 = 128
)
func playerBarRect() (x, y, w, h int) {
w, h = playerBarImage.Size()
x = (screenWidth - w) / 2
y = screenHeight - h - 16
return
}
func (p *Player) updateSE() error {
2016-08-01 17:36:43 +02:00
if seBytes == nil {
2016-04-13 18:26:31 +02:00
return nil
}
if !ebiten.IsKeyPressed(ebiten.KeyP) {
keyState[ebiten.KeyP] = 0
return nil
}
keyState[ebiten.KeyP]&#43;&#43;
if keyState[ebiten.KeyP] != 1 {
return nil
}
2016-08-01 17:36:43 +02:00
sePlayer, err := audio.NewPlayerFromBytes(audioContext, seBytes)
if err != nil {
2016-04-13 18:26:31 +02:00
return err
}
return sePlayer.Play()
}
func (p *Player) updateVolume() error {
if p.audioPlayer == nil {
return nil
}
if ebiten.IsKeyPressed(ebiten.KeyZ) {
volume128--
}
if ebiten.IsKeyPressed(ebiten.KeyX) {
volume128&#43;&#43;
}
if volume128 &lt; 0 {
volume128 = 0
}
if 128 &lt; volume128 {
volume128 = 128
}
p.audioPlayer.SetVolume(float64(volume128) / 128)
return nil
}
func (p *Player) updatePlayPause() error {
if p.audioPlayer == nil {
return nil
}
if !ebiten.IsKeyPressed(ebiten.KeyS) {
keyState[ebiten.KeyS] = 0
return nil
}
keyState[ebiten.KeyS]&#43;&#43;
if keyState[ebiten.KeyS] != 1 {
return nil
}
if p.audioPlayer.IsPlaying() {
return p.audioPlayer.Pause()
}
return p.audioPlayer.Play()
}
func (p *Player) updateBar() error {
if p.audioPlayer == nil {
return nil
}
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
mouseButtonState[ebiten.MouseButtonLeft] = 0
return nil
}
mouseButtonState[ebiten.MouseButtonLeft]&#43;&#43;
if mouseButtonState[ebiten.MouseButtonLeft] != 1 {
return nil
}
x, y := ebiten.CursorPosition()
bx, by, bw, bh := playerBarRect()
2016-04-21 16:24:53 +02:00
const padding = 4
if y &lt; by-padding || by&#43;bh&#43;padding &lt;= y {
2016-04-13 18:26:31 +02:00
return nil
}
if x &lt; bx || bx&#43;bw &lt;= x {
return nil
}
pos := time.Duration(x-bx) * p.total / time.Duration(bw)
return p.audioPlayer.Seek(pos)
}
func (p *Player) close() error {
return p.audioPlayer.Close()
}
func update(screen *ebiten.Image) error {
if musicPlayer == nil {
select {
case musicPlayer = &lt;-musicCh:
default:
}
}
2016-08-01 17:36:43 +02:00
if seBytes == nil {
2016-04-13 18:26:31 +02:00
select {
2016-08-01 17:36:43 +02:00
case seBytes = &lt;-seCh:
2016-04-13 18:26:31 +02:00
default:
}
}
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
}
}
op := &amp;ebiten.DrawImageOptions{}
x, y, w, h := playerBarRect()
op.GeoM.Translate(float64(x), float64(y))
2016-08-26 16:33:36 +02:00
if err := screen.DrawImage(playerBarImage, op); err != nil {
return err
}
2016-04-13 18:26:31 +02:00
currentTimeStr := &#34;00:00&#34;
if musicPlayer != nil {
c := musicPlayer.audioPlayer.Current()
// Current Time
m := (c / time.Minute) % 100
s := (c / time.Second) % 60
currentTimeStr = fmt.Sprintf(&#34;%02d:%02d&#34;, m, s)
// Bar
cw, ch := playerCurrentImage.Size()
cx := int(time.Duration(w)*c/musicPlayer.total) &#43; x - cw/2
cy := y - (ch-h)/2
op := &amp;ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(cx), float64(cy))
2016-08-26 16:33:36 +02:00
if err := screen.DrawImage(playerCurrentImage, op); err != nil {
return err
}
2016-04-13 18:26:31 +02:00
}
msg := fmt.Sprintf(`FPS: %0.2f
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)
if musicPlayer == nil {
msg &#43;= &#34;\nNow Loading...&#34;
}
2016-08-26 16:33:36 +02:00
if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
2016-04-21 18:16:58 +02:00
if err := audioContext.Update(); err != nil {
return err
}
2016-04-13 18:26:31 +02:00
return nil
}
func main() {
wavF, err := ebitenutil.OpenFile(&#34;_resources/audio/jab.wav&#34;)
if err != nil {
log.Fatal(err)
}
oggF, err := ebitenutil.OpenFile(&#34;_resources/audio/ragtime.ogg&#34;)
if err != nil {
log.Fatal(err)
}
const sampleRate = 22050
const bytesPerSample = 4 // TODO: This should be defined in audio package
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
go func() {
s, err := wav.Decode(audioContext, wavF)
if err != nil {
log.Fatal(err)
return
}
2016-08-01 17:36:43 +02:00
b, err := ioutil.ReadAll(s)
if err != nil {
log.Fatal(err)
return
}
seCh &lt;- b
2016-04-13 18:26:31 +02:00
close(seCh)
}()
go func() {
s, err := vorbis.Decode(audioContext, oggF)
if err != nil {
log.Fatal(err)
return
}
2016-04-21 16:24:53 +02:00
p, err := audio.NewPlayer(audioContext, s)
2016-04-13 18:26:31 +02:00
if err != nil {
log.Fatal(err)
return
}
musicCh &lt;- &amp;Player{
audioPlayer: p,
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
}
close(musicCh)
// TODO: Is this goroutine-safe?
2016-08-26 16:33:36 +02:00
if err := p.Play(); err != nil {
log.Fatal(err)
return
}
2016-04-13 18:26:31 +02:00
}()
if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Audio (Ebiten Demo)&#34;); err != nil {
log.Fatal(err)
}
if musicPlayer != nil {
if err := musicPlayer.close(); err != nil {
log.Fatal(err)
}
}
}
</code></pre>
2016-08-26 19:28:57 +02:00
</div></main>
<footer><div class="container">
<p>© 2013 Hajime Hoshi</p>
<p>Code is licensed under <a href="https://github.com/hajimehoshi/ebiten/blob/master/LICENSE">Apache License 2.0</a>.</p>
<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