<!DOCTYPE html>
<meta charset="utf-8">
<meta property="og:image" itemprop="image primaryImageOfPage" content="https://hajimehoshi.github.io/ebiten/images/examples/audio.png">
<meta name="description" content="Ebiten example - audio">
<link rel="shortcut icon" href="../favicon.png" type="image/png" >
<link rel="icon" href="../favicon.png" type="image/png" >
<title>Ebiten example - audio</title>

<link rel="stylesheet" href="../stylesheets/bootstrap.min.css">
<link rel="stylesheet" href="../stylesheets/highlight-github.css">
<link rel="stylesheet" href="../stylesheets/ebiten.css">
<script src="../scripts/googleanalytics.js"></script>

<nav class="navbar"><div class="container">
  <nav class="d-flex flex-row" style="width: 100%;">
    <div class="nav mr-auto"><a class="navbar-brand" href="../"><img src="../images/logo_white.svg" alt="EBITEN"></a></div>
    <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>
    </ul>
  </nav>
</div></nav>

<main><div class="container">

  <h2>Ebiten example - audio</h2>
  <iframe src="audio.content.html" width="640" height="480"></iframe>
  <div class="card"><pre class="card-body"><code class="language-go">// &#43;build example

// This is an example to implement an audio player.
// See examples/wav for a simpler example to play a sound file.

package main

import (
        &#34;fmt&#34;
        &#34;image/color&#34;
        &#34;io/ioutil&#34;
        &#34;log&#34;
        &#34;time&#34;

        &#34;github.com/hajimehoshi/ebiten&#34;
        &#34;github.com/hajimehoshi/ebiten/audio&#34;
        &#34;github.com/hajimehoshi/ebiten/audio/mp3&#34;
        &#34;github.com/hajimehoshi/ebiten/audio/wav&#34;
        &#34;github.com/hajimehoshi/ebiten/ebitenutil&#34;
        &#34;github.com/hajimehoshi/ebiten/inpututil&#34;
)

const (
        screenWidth  = 320
        screenHeight = 240

        // This sample rate doesn&#39;t match with wav/mp3&#39;s sample rate,
        // but decoders adjust them.
        sampleRate = 48000
)

var (
        playerBarColor     = color.RGBA{0x80, 0x80, 0x80, 0xff}
        playerCurrentColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
)

// Player represents the current audio state.
type Player struct {
        audioContext *audio.Context
        audioPlayer  *audio.Player
        current      time.Duration
        total        time.Duration
        seBytes      []byte
        seCh         chan []byte
        volume128    int
}

func playerBarRect() (x, y, w, h int) {
        w, h = 300, 4
        x = (screenWidth - w) / 2
        y = screenHeight - h - 16
        return
}

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
        }
        mp3F, err := ebitenutil.OpenFile(&#34;_resources/audio/classic.mp3&#34;)
        if err != nil {
                return nil, err
        }
        s, err := mp3.Decode(audioContext, mp3F)
        if err != nil {
                return nil, err
        }
        p, err := audio.NewPlayer(audioContext, s)
        if err != nil {
                return nil, err
        }
        player := &amp;Player{
                audioContext: audioContext,
                audioPlayer:  p,
                total:        time.Second * time.Duration(s.Length()) / bytesPerSample / sampleRate,
                volume128:    128,
                seCh:         make(chan []byte),
        }
        if player.total == 0 {
                player.total = 1
        }
        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 {
        select {
        case p.seBytes = &lt;-p.seCh:
                close(p.seCh)
                p.seCh = nil
        default:
        }

        if p.audioPlayer.IsPlaying() {
                p.current = p.audioPlayer.Current()
        }
        p.seekBarIfNeeded()
        p.switchPlayStateIfNeeded()
        p.playSEIfNeeded()
        p.updateVolumeIfNeeded()

        if inpututil.IsKeyJustPressed(ebiten.KeyB) {
                b := ebiten.IsRunnableInBackground()
                ebiten.SetRunnableInBackground(!b)
        }
        return nil
}

func (p *Player) playSEIfNeeded() {
        if p.seBytes == nil {
                // Bytes for the SE is not loaded yet.
                return
        }

        if !inpututil.IsKeyJustPressed(ebiten.KeyP) {
                return
        }
        sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, p.seBytes)
        sePlayer.Play()
}

func (p *Player) updateVolumeIfNeeded() {
        if ebiten.IsKeyPressed(ebiten.KeyZ) {
                p.volume128--
        }
        if ebiten.IsKeyPressed(ebiten.KeyX) {
                p.volume128&#43;&#43;
        }
        if p.volume128 &lt; 0 {
                p.volume128 = 0
        }
        if 128 &lt; p.volume128 {
                p.volume128 = 128
        }
        p.audioPlayer.SetVolume(float64(p.volume128) / 128)
}

func (p *Player) switchPlayStateIfNeeded() {
        if !inpututil.IsKeyJustPressed(ebiten.KeyS) {
                return
        }
        if p.audioPlayer.IsPlaying() {
                p.audioPlayer.Pause()
                return
        }
        p.audioPlayer.Play()
}

func (p *Player) seekBarIfNeeded() {
        if !inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
                return
        }

        // Calculate the next seeking position from the current cursor position.
        x, y := ebiten.CursorPosition()
        bx, by, bw, bh := playerBarRect()
        const padding = 4
        if y &lt; by-padding || by&#43;bh&#43;padding &lt;= y {
                return
        }
        if x &lt; bx || bx&#43;bw &lt;= x {
                return
        }
        pos := time.Duration(x-bx) * p.total / time.Duration(bw)
        p.current = pos
        p.audioPlayer.Seek(pos)
}

func (p *Player) close() error {
        return p.audioPlayer.Close()
}

func (p *Player) draw(screen *ebiten.Image) {
        // Draw the bar.
        x, y, w, h := playerBarRect()
        ebitenutil.DrawRect(screen, float64(x), float64(y), float64(w), float64(h), playerBarColor)

        // Draw the cursor on the bar.
        c := p.current
        cw, ch := 4, 10
        cx := int(time.Duration(w)*c/p.total) &#43; x - cw/2
        cy := y - (ch-h)/2
        ebitenutil.DrawRect(screen, float64(cx), float64(cy), float64(cw), float64(ch), playerCurrentColor)

        // Compose the curren time text.
        m := (c / time.Minute) % 100
        s := (c / time.Second) % 60
        currentTimeStr := fmt.Sprintf(&#34;%02d:%02d&#34;, m, s)

        // Draw the debug message.
        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
Press B to switch the run-in-background state
Current Time: %s`, ebiten.CurrentFPS(), currentTimeStr)
        ebitenutil.DebugPrint(screen, msg)
}

var (
        musicPlayer *Player
)

func update(screen *ebiten.Image) error {
        if err := musicPlayer.update(); err != nil {
                return err
        }

        if ebiten.IsRunningSlowly() {
                return nil
        }

        musicPlayer.draw(screen)
        return nil
}

func main() {
        audioContext, err := audio.NewContext(sampleRate)
        if err != nil {
                log.Fatal(err)
        }

        musicPlayer, err = NewPlayer(audioContext)
        if err != nil {
                log.Fatal(err)
        }
        if err := ebiten.Run(update, screenWidth, screenHeight, 2, &#34;Audio (Ebiten Demo)&#34;); err != nil {
                log.Fatal(err)
        }
        if err := musicPlayer.close(); err != nil {
                log.Fatal(err)
        }
}
</code></pre></div>

</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">the 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>

<script src="../scripts/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>