ebiten/docs/examples/audio.html

324 lines
10 KiB
HTML
Raw Normal View History

2016-04-13 18:26:31 +02:00
<!DOCTYPE html>
2017-08-18 16:26:17 +02:00
<meta charset="utf-8">
2017-08-18 16:43:08 +02:00
<meta property="og:image" itemprop="image primaryImageOfPage" content="https://hajimehoshi.github.io/ebiten/images/examples/audio.png">
<meta name="description" content="Ebiten example - audio">
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>
2017-08-18 16:26:17 +02:00
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-09-02 18:55:07 +02:00
<script src="../scripts/googleanalytics.js"></script>
2016-08-26 19:28:57 +02:00
2017-08-17 20:00:05 +02:00
<nav class="navbar"><div class="container">
2017-08-17 18:22:19 +02:00
<nav class="d-flex flex-row" style="width: 100%;">
2017-08-17 20:00:05 +02:00
<div class="nav mr-auto"><a class="navbar-brand" href="../"><img src="../images/logo_white.svg" alt="EBITEN"></a></div>
2017-08-17 18:22:19 +02:00
<ul class="nav">
<li class="nav-item"><a class="nav-link" href="https://github.com/hajimehoshi/ebiten">GitHub</a></li>
<li class="nav-item"><a class="nav-link" href="https://godoc.org/github.com/hajimehoshi/ebiten">GoDoc</a></li>
<li class="nav-item"><a class="nav-link" href="https://github.com/hajimehoshi/ebiten/wiki">Wiki</a>
<li class="nav-item"><a class="nav-link" href="https://ebiten-playground.github.io/">Playground</a>
2016-08-26 19:37:08 +02:00
</ul>
</nav>
2017-08-17 20:00:05 +02:00
</div></nav>
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>
2017-08-18 16:26:17 +02:00
<div class="card"><pre class="card-body"><code class="language-go">// &#43;build example
2016-08-26 16:33:36 +02:00
2017-08-17 16:13:15 +02:00
// This is an example to implement an audio player.
// See examples/wav for a simpler example to play a sound file.
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
&#34;fmt&#34;
&#34;image/color&#34;
&#34;io/ioutil&#34;
&#34;log&#34;
&#34;time&#34;
2016-04-13 18:26:31 +02:00
2016-08-26 22:36:52 +02:00
&#34;github.com/hajimehoshi/ebiten&#34;
&#34;github.com/hajimehoshi/ebiten/audio&#34;
2017-07-01 10:10:55 +02:00
&#34;github.com/hajimehoshi/ebiten/audio/mp3&#34;
2016-08-26 22:36:52 +02:00
&#34;github.com/hajimehoshi/ebiten/audio/wav&#34;
&#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
2016-04-13 18:26:31 +02:00
)
const (
2016-08-26 22:36:52 +02:00
screenWidth = 320
screenHeight = 240
2017-06-09 04:43:50 +02:00
2017-07-01 10:10:55 +02:00
// This sample rate doesn&#39;t match with wav/mp3&#39;s sample rate,
2017-06-09 04:43:50 +02:00
// but decoders adjust them.
sampleRate = 48000
2016-04-13 18:26:31 +02:00
)
var (
2017-08-18 16:26:17 +02:00
playerBarColor = color.RGBA{0x80, 0x80, 0x80, 0xff}
playerCurrentColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
2016-04-13 18:26:31 +02:00
)
2017-06-09 04:43:50 +02:00
type Input struct {
mouseButtonStates map[ebiten.MouseButton]int
keyStates map[ebiten.Key]int
}
func (i *Input) update() {
2017-08-17 16:13:15 +02:00
for _, key := range []ebiten.Key{ebiten.KeyP, ebiten.KeyS, ebiten.KeyX, ebiten.KeyZ, ebiten.KeyB} {
2017-06-09 04:43:50 +02:00
if !ebiten.IsKeyPressed(key) {
i.keyStates[key] = 0
} else {
i.keyStates[key]&#43;&#43;
}
2016-08-26 22:36:52 +02:00
}
2017-06-09 04:43:50 +02:00
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
i.mouseButtonStates[ebiten.MouseButtonLeft] = 0
} else {
i.mouseButtonStates[ebiten.MouseButtonLeft]&#43;&#43;
2016-08-26 22:36:52 +02:00
}
2016-04-13 18:26:31 +02:00
}
2017-06-09 04:43:50 +02:00
func (i *Input) isKeyTriggered(key ebiten.Key) bool {
return i.keyStates[key] == 1
}
func (i *Input) isKeyPressed(key ebiten.Key) bool {
return i.keyStates[key] &gt; 0
}
func (i *Input) isMouseButtonTriggered(mouseButton ebiten.MouseButton) bool {
return i.mouseButtonStates[mouseButton] == 1
}
2016-04-13 18:26:31 +02:00
type Player struct {
2017-06-09 04:43:50 +02:00
input *Input
audioContext *audio.Context
audioPlayer *audio.Player
2017-07-01 10:10:55 +02:00
current time.Duration
2017-06-09 04:43:50 +02:00
total time.Duration
seBytes []uint8
seCh chan []uint8
volume128 int
2016-04-13 18:26:31 +02:00
}
var (
2017-06-09 04:43:50 +02:00
musicPlayer *Player
2016-04-13 18:26:31 +02:00
)
func playerBarRect() (x, y, w, h int) {
2017-08-18 16:26:17 +02:00
w, h = 300, 4
2016-08-26 22:36:52 +02:00
x = (screenWidth - w) / 2
y = screenHeight - h - 16
return
2016-04-13 18:26:31 +02:00
}
2017-06-09 04:43:50 +02:00
func NewPlayer(audioContext *audio.Context) (*Player, error) {
const bytesPerSample = 4 // TODO: This should be defined in audio package
wavF, err := ebitenutil.OpenFile(&#34;_resources/audio/jab.wav&#34;)
if err != nil {
return nil, err
2016-08-26 22:36:52 +02:00
}
2017-07-01 10:10:55 +02:00
mp3F, err := ebitenutil.OpenFile(&#34;_resources/audio/classic.mp3&#34;)
2017-06-09 04:43:50 +02:00
if err != nil {
return nil, err
2016-08-26 22:36:52 +02:00
}
2017-07-01 10:10:55 +02:00
s, err := mp3.Decode(audioContext, mp3F)
2017-06-09 04:43:50 +02:00
if err != nil {
return nil, err
2016-08-26 22:36:52 +02:00
}
2017-06-09 04:43:50 +02:00
p, err := audio.NewPlayer(audioContext, s)
2016-08-26 22:36:52 +02:00
if err != nil {
2017-06-09 04:43:50 +02:00
return nil, err
}
player := &amp;Player{
input: &amp;Input{
mouseButtonStates: map[ebiten.MouseButton]int{},
keyStates: map[ebiten.Key]int{},
},
audioContext: audioContext,
audioPlayer: p,
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
volume128: 128,
seCh: make(chan []uint8),
}
2017-07-01 10:10:55 +02:00
if player.total == 0 {
player.total = 1
}
2017-06-09 04:43:50 +02:00
player.audioPlayer.Play()
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
}
player.seCh &lt;- b
}()
return player, nil
}
func (p *Player) update() error {
p.input.update()
select {
case p.seBytes = &lt;-p.seCh:
close(p.seCh)
p.seCh = nil
default:
}
2017-07-01 10:10:55 +02:00
if p.audioPlayer.IsPlaying() {
p.current = p.audioPlayer.Current()
}
2017-06-09 04:43:50 +02:00
p.updateBar()
p.updatePlayPause()
p.updateSE()
p.updateVolume()
2017-08-17 16:13:15 +02:00
if p.input.isKeyTriggered(ebiten.KeyB) {
b := ebiten.IsRunnableInBackground()
ebiten.SetRunnableInBackground(!b)
}
2017-06-09 04:43:50 +02:00
if err := p.audioContext.Update(); err != nil {
2016-08-26 22:36:52 +02:00
return err
}
2017-06-09 04:43:50 +02:00
return nil
2016-04-13 18:26:31 +02:00
}
2017-06-09 04:43:50 +02:00
func (p *Player) updateSE() {
if p.seBytes == nil {
2017-03-04 15:27:25 +01:00
return
2016-08-26 22:36:52 +02:00
}
2017-06-09 04:43:50 +02:00
if !p.input.isKeyTriggered(ebiten.KeyP) {
return
2016-08-26 22:36:52 +02:00
}
2017-06-09 04:43:50 +02:00
sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, p.seBytes)
sePlayer.Play()
2016-04-13 18:26:31 +02:00
}
2017-06-09 04:43:50 +02:00
func (p *Player) updateVolume() {
if p.input.isKeyPressed(ebiten.KeyZ) {
p.volume128--
2016-08-26 22:36:52 +02:00
}
2017-06-09 04:43:50 +02:00
if p.input.isKeyPressed(ebiten.KeyX) {
p.volume128&#43;&#43;
2016-08-26 22:36:52 +02:00
}
2017-06-09 04:43:50 +02:00
if p.volume128 &lt; 0 {
p.volume128 = 0
2016-08-26 22:36:52 +02:00
}
2017-06-09 04:43:50 +02:00
if 128 &lt; p.volume128 {
p.volume128 = 128
2016-08-26 22:36:52 +02:00
}
2017-06-09 04:43:50 +02:00
p.audioPlayer.SetVolume(float64(p.volume128) / 128)
2016-04-13 18:26:31 +02:00
}
2017-06-09 04:43:50 +02:00
func (p *Player) updatePlayPause() {
if !p.input.isKeyTriggered(ebiten.KeyS) {
2017-03-04 15:27:25 +01:00
return
2016-08-26 22:36:52 +02:00
}
2017-06-09 04:43:50 +02:00
if p.audioPlayer.IsPlaying() {
p.audioPlayer.Pause()
2017-03-04 15:27:25 +01:00
return
}
2017-06-09 04:43:50 +02:00
p.audioPlayer.Play()
}
func (p *Player) updateBar() {
if !p.input.isMouseButtonTriggered(ebiten.MouseButtonLeft) {
2017-03-04 15:27:25 +01:00
return
2016-08-26 22:36:52 +02:00
}
2017-06-09 04:43:50 +02:00
// Start seeking.
2016-08-26 22:36:52 +02:00
x, y := ebiten.CursorPosition()
bx, by, bw, bh := playerBarRect()
const padding = 4
if y &lt; by-padding || by&#43;bh&#43;padding &lt;= y {
2017-03-04 15:27:25 +01:00
return
2016-08-26 22:36:52 +02:00
}
if x &lt; bx || bx&#43;bw &lt;= x {
2017-03-04 15:27:25 +01:00
return
2016-08-26 22:36:52 +02:00
}
pos := time.Duration(x-bx) * p.total / time.Duration(bw)
2017-07-01 10:10:55 +02:00
p.current = pos
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
}
2017-06-09 04:43:50 +02:00
func (p *Player) draw(screen *ebiten.Image) {
2017-08-18 16:26:17 +02:00
// Bar
2016-08-26 22:36:52 +02:00
x, y, w, h := playerBarRect()
2017-08-18 16:26:17 +02:00
ebitenutil.DrawRect(screen, float64(x), float64(y), float64(w), float64(h), playerBarColor)
2016-08-26 22:36:52 +02:00
currentTimeStr := &#34;00:00&#34;
2017-06-09 04:43:50 +02:00
// Current Time
2017-07-01 10:10:55 +02:00
c := p.current
2017-06-09 04:43:50 +02:00
m := (c / time.Minute) % 100
s := (c / time.Second) % 60
currentTimeStr = fmt.Sprintf(&#34;%02d:%02d&#34;, m, s)
2017-08-18 16:26:17 +02:00
// Cursor
cw, ch := 4, 10
2017-06-09 04:43:50 +02:00
cx := int(time.Duration(w)*c/p.total) &#43; x - cw/2
cy := y - (ch-h)/2
2017-08-18 16:26:17 +02:00
ebitenutil.DrawRect(screen, float64(cx), float64(cy), float64(cw), float64(ch), playerCurrentColor)
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
2017-08-17 16:13:15 +02:00
Press B to switch the run-in-background state
2016-04-13 18:26:31 +02:00
%s`, ebiten.CurrentFPS(), currentTimeStr)
2017-03-04 15:27:25 +01:00
ebitenutil.DebugPrint(screen, msg)
2017-06-09 04:43:50 +02:00
}
func update(screen *ebiten.Image) error {
if err := musicPlayer.update(); err != nil {
2016-08-26 22:36:52 +02:00
return err
}
2017-06-09 04:43:50 +02:00
if ebiten.IsRunningSlowly() {
return nil
}
musicPlayer.draw(screen)
2016-08-26 22:36:52 +02:00
return nil
2016-04-13 18:26:31 +02:00
}
func main() {
2017-06-09 04:43:50 +02:00
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)
2016-04-13 18:26:31 +02:00
}
2017-07-13 19:26:41 +02:00
2017-06-09 04:43:50 +02:00
musicPlayer, err = NewPlayer(audioContext)
2016-04-13 18:26:31 +02:00
if err != nil {
2016-08-26 22:36:52 +02:00
log.Fatal(err)
}
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)
}
}
2016-04-13 18:26:31 +02:00
}
2017-08-18 16:26:17 +02:00
</code></pre></div>
2016-04-13 18:26:31 +02:00
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>