2016-03-07 20:20:59 +01:00
|
|
|
// Copyright 2016 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-08-25 17:40:39 +02:00
|
|
|
// +build example
|
|
|
|
|
2016-03-07 20:20:59 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-17 14:59:29 +01:00
|
|
|
"image/color"
|
2016-06-26 20:11:10 +02:00
|
|
|
"io/ioutil"
|
2016-03-07 20:20:59 +01:00
|
|
|
"log"
|
2016-03-17 14:59:29 +01:00
|
|
|
"time"
|
2016-03-07 20:20:59 +01:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2016-04-18 18:24:44 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/audio"
|
|
|
|
"github.com/hajimehoshi/ebiten/audio/vorbis"
|
|
|
|
"github.com/hajimehoshi/ebiten/audio/wav"
|
2016-03-07 20:20:59 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 320
|
|
|
|
screenHeight = 240
|
2017-06-03 16:23:02 +02:00
|
|
|
|
|
|
|
// This sample rate doesn't match with wav/ogg's sample rate,
|
|
|
|
// but decoders adjust them.
|
|
|
|
sampleRate = 48000
|
2016-03-07 20:20:59 +01:00
|
|
|
)
|
|
|
|
|
2016-03-17 14:59:29 +01:00
|
|
|
var (
|
|
|
|
playerBarImage *ebiten.Image
|
|
|
|
playerCurrentImage *ebiten.Image
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-06-02 17:51:15 +02:00
|
|
|
playerBarImage, _ = ebiten.NewImage(300, 4, ebiten.FilterNearest)
|
|
|
|
playerBarImage.Fill(&color.RGBA{0x80, 0x80, 0x80, 0xff})
|
2016-03-17 14:59:29 +01:00
|
|
|
|
2017-06-02 17:51:15 +02:00
|
|
|
playerCurrentImage, _ = ebiten.NewImage(4, 10, ebiten.FilterNearest)
|
|
|
|
playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff})
|
2016-03-17 14:59:29 +01:00
|
|
|
}
|
|
|
|
|
2017-06-03 16:53:44 +02:00
|
|
|
type Input struct {
|
|
|
|
mouseButtonStates map[ebiten.MouseButton]int
|
|
|
|
keyStates map[ebiten.Key]int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) update() {
|
|
|
|
for _, key := range []ebiten.Key{ebiten.KeyP, ebiten.KeyS, ebiten.KeyX, ebiten.KeyZ} {
|
|
|
|
if !ebiten.IsKeyPressed(key) {
|
|
|
|
i.keyStates[key] = 0
|
|
|
|
} else {
|
|
|
|
i.keyStates[key]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
|
|
|
|
i.mouseButtonStates[ebiten.MouseButtonLeft] = 0
|
|
|
|
} else {
|
|
|
|
i.mouseButtonStates[ebiten.MouseButtonLeft]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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] > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Input) isMouseButtonTriggered(mouseButton ebiten.MouseButton) bool {
|
|
|
|
return i.mouseButtonStates[mouseButton] == 1
|
|
|
|
}
|
|
|
|
|
2016-03-19 20:02:51 +01:00
|
|
|
type Player struct {
|
2017-06-03 16:53:44 +02:00
|
|
|
input *Input
|
|
|
|
audioContext *audio.Context
|
|
|
|
audioPlayer *audio.Player
|
|
|
|
total time.Duration
|
|
|
|
seekedCh chan error
|
2017-06-03 18:34:36 +02:00
|
|
|
seBytes []uint8
|
|
|
|
seCh chan []uint8
|
2017-06-03 16:53:44 +02:00
|
|
|
volume128 int
|
2017-06-04 10:12:31 +02:00
|
|
|
previousPos time.Duration
|
2016-03-19 20:02:51 +01:00
|
|
|
}
|
|
|
|
|
2016-03-08 18:45:56 +01:00
|
|
|
var (
|
2017-06-03 16:37:00 +02:00
|
|
|
musicPlayer *Player
|
2016-03-08 18:45:56 +01:00
|
|
|
)
|
2016-03-07 20:20:59 +01:00
|
|
|
|
2016-03-19 17:40:10 +01:00
|
|
|
func playerBarRect() (x, y, w, h int) {
|
|
|
|
w, h = playerBarImage.Size()
|
|
|
|
x = (screenWidth - w) / 2
|
|
|
|
y = screenHeight - h - 16
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-03 16:23:02 +02:00
|
|
|
func NewPlayer(audioContext *audio.Context) (*Player, error) {
|
|
|
|
const bytesPerSample = 4 // TODO: This should be defined in audio package
|
2017-06-03 18:34:36 +02:00
|
|
|
wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-03 16:23:02 +02:00
|
|
|
oggF, err := ebitenutil.OpenFile("_resources/audio/game.ogg")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s, err := vorbis.Decode(audioContext, oggF)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
p, err := audio.NewPlayer(audioContext, s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
player := &Player{
|
2017-06-03 16:53:44 +02:00
|
|
|
input: &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,
|
2017-06-03 18:34:36 +02:00
|
|
|
seCh: make(chan []uint8),
|
2017-06-03 16:23:02 +02:00
|
|
|
}
|
|
|
|
player.audioPlayer.Play()
|
2017-06-03 18:34:36 +02:00
|
|
|
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 <- b
|
|
|
|
}()
|
2017-06-03 16:23:02 +02:00
|
|
|
return player, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) update() error {
|
2017-06-03 16:53:44 +02:00
|
|
|
p.input.update()
|
2017-06-03 16:23:02 +02:00
|
|
|
select {
|
2017-06-03 18:34:36 +02:00
|
|
|
case p.seBytes = <-p.seCh:
|
|
|
|
close(p.seCh)
|
|
|
|
p.seCh = nil
|
2017-06-03 16:23:02 +02:00
|
|
|
case err := <-p.seekedCh:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
close(p.seekedCh)
|
|
|
|
p.seekedCh = nil
|
|
|
|
default:
|
|
|
|
}
|
2017-06-03 18:34:36 +02:00
|
|
|
p.updateBar()
|
|
|
|
p.updatePlayPause()
|
|
|
|
p.updateSE()
|
|
|
|
p.updateVolume()
|
|
|
|
if err := p.audioContext.Update(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-03 16:23:02 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-02 18:40:22 +02:00
|
|
|
func (p *Player) updateSE() {
|
2017-06-03 18:34:36 +02:00
|
|
|
if p.seBytes == nil {
|
2017-06-02 18:40:22 +02:00
|
|
|
return
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2017-06-03 16:53:44 +02:00
|
|
|
if !p.input.isKeyTriggered(ebiten.KeyP) {
|
2017-06-02 18:40:22 +02:00
|
|
|
return
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2017-06-03 18:34:36 +02:00
|
|
|
sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, p.seBytes)
|
2017-06-02 18:12:58 +02:00
|
|
|
sePlayer.Play()
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2017-03-04 15:00:19 +01:00
|
|
|
func (p *Player) updateVolume() {
|
2017-06-03 16:53:44 +02:00
|
|
|
if p.input.isKeyPressed(ebiten.KeyZ) {
|
2017-06-03 16:37:00 +02:00
|
|
|
p.volume128--
|
2016-03-28 04:06:17 +02:00
|
|
|
}
|
2017-06-03 16:53:44 +02:00
|
|
|
if p.input.isKeyPressed(ebiten.KeyX) {
|
2017-06-03 16:37:00 +02:00
|
|
|
p.volume128++
|
2016-03-28 04:06:17 +02:00
|
|
|
}
|
2017-06-03 16:37:00 +02:00
|
|
|
if p.volume128 < 0 {
|
|
|
|
p.volume128 = 0
|
2016-03-28 04:06:17 +02:00
|
|
|
}
|
2017-06-03 16:37:00 +02:00
|
|
|
if 128 < p.volume128 {
|
|
|
|
p.volume128 = 128
|
2016-03-28 04:06:17 +02:00
|
|
|
}
|
2017-06-03 16:37:00 +02:00
|
|
|
p.audioPlayer.SetVolume(float64(p.volume128) / 128)
|
2016-03-28 04:06:17 +02:00
|
|
|
}
|
|
|
|
|
2017-06-02 18:40:22 +02:00
|
|
|
func (p *Player) updatePlayPause() {
|
2017-06-03 16:53:44 +02:00
|
|
|
if !p.input.isKeyTriggered(ebiten.KeyS) {
|
2017-06-02 18:40:22 +02:00
|
|
|
return
|
2016-03-20 17:38:15 +01:00
|
|
|
}
|
|
|
|
if p.audioPlayer.IsPlaying() {
|
2017-06-02 18:40:22 +02:00
|
|
|
p.audioPlayer.Pause()
|
|
|
|
return
|
2016-03-20 17:38:15 +01:00
|
|
|
}
|
2017-06-02 18:34:17 +02:00
|
|
|
p.audioPlayer.Play()
|
2016-03-20 17:38:15 +01:00
|
|
|
}
|
|
|
|
|
2017-03-04 15:00:19 +01:00
|
|
|
func (p *Player) updateBar() {
|
2016-11-28 19:38:08 +01:00
|
|
|
if p.seekedCh != nil {
|
2017-03-04 15:00:19 +01:00
|
|
|
return
|
2016-11-28 19:38:08 +01:00
|
|
|
}
|
2017-06-03 16:53:44 +02:00
|
|
|
if !p.input.isMouseButtonTriggered(ebiten.MouseButtonLeft) {
|
2017-03-04 15:00:19 +01:00
|
|
|
return
|
2016-03-19 17:40:10 +01:00
|
|
|
}
|
2017-06-03 18:34:36 +02:00
|
|
|
// Start seeking.
|
2016-03-19 17:40:10 +01:00
|
|
|
x, y := ebiten.CursorPosition()
|
|
|
|
bx, by, bw, bh := playerBarRect()
|
2016-04-18 19:45:51 +02:00
|
|
|
const padding = 4
|
|
|
|
if y < by-padding || by+bh+padding <= y {
|
2017-03-04 15:00:19 +01:00
|
|
|
return
|
2016-03-19 17:40:10 +01:00
|
|
|
}
|
|
|
|
if x < bx || bx+bw <= x {
|
2017-03-04 15:00:19 +01:00
|
|
|
return
|
2016-03-19 17:40:10 +01:00
|
|
|
}
|
2016-03-19 20:02:51 +01:00
|
|
|
pos := time.Duration(x-bx) * p.total / time.Duration(bw)
|
2016-11-28 19:38:08 +01:00
|
|
|
p.seekedCh = make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
p.seekedCh <- p.audioPlayer.Seek(pos)
|
|
|
|
}()
|
2016-03-19 17:40:10 +01:00
|
|
|
}
|
|
|
|
|
2016-03-28 17:06:37 +02:00
|
|
|
func (p *Player) close() error {
|
|
|
|
return p.audioPlayer.Close()
|
|
|
|
}
|
|
|
|
|
2017-06-03 16:23:02 +02:00
|
|
|
func (p *Player) draw(screen *ebiten.Image) {
|
2016-03-17 14:59:29 +01:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
2016-03-19 17:40:10 +01:00
|
|
|
x, y, w, h := playerBarRect()
|
|
|
|
op.GeoM.Translate(float64(x), float64(y))
|
2017-03-04 15:00:19 +01:00
|
|
|
screen.DrawImage(playerBarImage, op)
|
2016-03-20 17:38:15 +01:00
|
|
|
currentTimeStr := "00:00"
|
2017-06-03 16:23:02 +02:00
|
|
|
c := p.audioPlayer.Current()
|
2017-06-04 10:12:31 +02:00
|
|
|
prev := p.previousPos
|
|
|
|
p.previousPos = c
|
2016-03-19 15:45:36 +01:00
|
|
|
|
2017-06-03 16:23:02 +02:00
|
|
|
// Current Time
|
|
|
|
m := (c / time.Minute) % 100
|
|
|
|
s := (c / time.Second) % 60
|
|
|
|
currentTimeStr = fmt.Sprintf("%02d:%02d", m, s)
|
2016-03-19 15:45:36 +01:00
|
|
|
|
2017-06-03 16:23:02 +02:00
|
|
|
// Bar
|
|
|
|
cw, ch := playerCurrentImage.Size()
|
|
|
|
cx := int(time.Duration(w)*c/p.total) + x - cw/2
|
|
|
|
cy := y - (ch-h)/2
|
|
|
|
op = &ebiten.DrawImageOptions{}
|
|
|
|
op.GeoM.Translate(float64(cx), float64(cy))
|
|
|
|
screen.DrawImage(playerCurrentImage, op)
|
2016-03-17 14:59:29 +01:00
|
|
|
|
2016-03-19 15:45:36 +01:00
|
|
|
msg := fmt.Sprintf(`FPS: %0.2f
|
2016-03-20 17:38:15 +01:00
|
|
|
Press S to toggle Play/Pause
|
2016-03-27 12:10:16 +02:00
|
|
|
Press P to play SE
|
2016-03-28 04:06:17 +02:00
|
|
|
Press Z or X to change volume of the music
|
2016-03-19 15:45:36 +01:00
|
|
|
%s`, ebiten.CurrentFPS(), currentTimeStr)
|
2017-06-04 10:12:31 +02:00
|
|
|
if p.audioPlayer.IsPlaying() && prev == c {
|
|
|
|
msg += "\nLoading..."
|
2017-06-03 16:23:02 +02:00
|
|
|
}
|
|
|
|
ebitenutil.DebugPrint(screen, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func update(screen *ebiten.Image) error {
|
|
|
|
if err := musicPlayer.update(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ebiten.IsRunningSlowly() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
musicPlayer.draw(screen)
|
2016-03-07 20:20:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2017-06-03 16:23:02 +02:00
|
|
|
audioContext, err := audio.NewContext(sampleRate)
|
2016-04-02 19:59:44 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-06-03 16:23:02 +02:00
|
|
|
musicPlayer, err = NewPlayer(audioContext)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-03-09 19:02:55 +01:00
|
|
|
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil {
|
2016-03-07 20:20:59 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-03-28 17:06:37 +02:00
|
|
|
if musicPlayer != nil {
|
|
|
|
if err := musicPlayer.close(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-03-07 20:20:59 +01:00
|
|
|
}
|