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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-03-27 12:10:16 +02:00
|
|
|
"bytes"
|
2016-03-07 20:20:59 +01:00
|
|
|
"fmt"
|
2016-03-17 14:59:29 +01:00
|
|
|
"image/color"
|
2016-03-27 12:23:55 +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"
|
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
|
|
|
"github.com/hajimehoshi/ebiten/exp/audio"
|
2016-03-12 19:00:05 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/exp/audio/vorbis"
|
2016-03-27 12:10:16 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/exp/audio/wav"
|
2016-03-07 20:20:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
screenWidth = 320
|
|
|
|
screenHeight = 240
|
|
|
|
)
|
|
|
|
|
2016-03-17 14:59:29 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
playerBarImage.Fill(&color.RGBA{0x80, 0x80, 0x80, 0xff})
|
|
|
|
|
|
|
|
playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff})
|
|
|
|
}
|
|
|
|
|
2016-03-19 20:02:51 +01:00
|
|
|
type Player struct {
|
|
|
|
audioPlayer *audio.Player
|
|
|
|
total time.Duration
|
|
|
|
}
|
|
|
|
|
2016-03-08 18:45:56 +01:00
|
|
|
var (
|
|
|
|
audioContext *audio.Context
|
2016-03-27 12:10:16 +02:00
|
|
|
musicPlayer *Player
|
|
|
|
seStream *wav.Stream
|
2016-03-27 12:23:55 +02:00
|
|
|
seBuffer []byte
|
2016-03-27 12:10:16 +02:00
|
|
|
musicCh = make(chan *Player)
|
|
|
|
seCh = make(chan *wav.Stream)
|
2016-03-19 17:40:10 +01:00
|
|
|
mouseButtonState = map[ebiten.MouseButton]int{}
|
2016-03-20 17:38:15 +01:00
|
|
|
keyState = map[ebiten.Key]int{}
|
2016-03-28 04:06:17 +02:00
|
|
|
volume128 = 128
|
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
|
|
|
|
}
|
|
|
|
|
2016-03-28 17:06:37 +02:00
|
|
|
type SEStream struct {
|
|
|
|
*bytes.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SEStream) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-27 12:10:16 +02:00
|
|
|
func (p *Player) updateSE() error {
|
|
|
|
if seStream == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !ebiten.IsKeyPressed(ebiten.KeyP) {
|
|
|
|
keyState[ebiten.KeyP] = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
keyState[ebiten.KeyP]++
|
|
|
|
if keyState[ebiten.KeyP] != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-27 13:10:04 +02:00
|
|
|
// Clone the buffer so that we can play the same SE mutiple times.
|
|
|
|
// TODO(hajimehoshi): This consumes memory. Can we avoid this?
|
2016-03-27 12:23:55 +02:00
|
|
|
if seBuffer == nil {
|
|
|
|
b, err := ioutil.ReadAll(seStream)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
seBuffer = b
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2016-03-28 17:06:37 +02:00
|
|
|
sePlayer, err := audioContext.NewPlayer(&SEStream{bytes.NewReader(seBuffer)})
|
2016-03-27 12:10:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sePlayer.Play()
|
|
|
|
}
|
|
|
|
|
2016-03-28 04:06:17 +02:00
|
|
|
func (p *Player) updateVolume() error {
|
|
|
|
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)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-20 17:38:15 +01:00
|
|
|
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]++
|
|
|
|
if keyState[ebiten.KeyS] != 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if p.audioPlayer.IsPlaying() {
|
|
|
|
return p.audioPlayer.Pause()
|
|
|
|
}
|
|
|
|
return p.audioPlayer.Play()
|
|
|
|
}
|
|
|
|
|
2016-03-19 20:02:51 +01:00
|
|
|
func (p *Player) updateBar() error {
|
|
|
|
if p.audioPlayer == nil {
|
2016-03-19 17:40:10 +01:00
|
|
|
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()
|
|
|
|
if y < by || by+bh <= y {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if x < bx || bx+bw <= x {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-19 20:02:51 +01:00
|
|
|
pos := time.Duration(x-bx) * p.total / time.Duration(bw)
|
|
|
|
return 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()
|
|
|
|
}
|
|
|
|
|
2016-03-07 20:20:59 +01:00
|
|
|
func update(screen *ebiten.Image) error {
|
2016-03-10 16:01:00 +01:00
|
|
|
audioContext.Update()
|
2016-03-27 12:10:16 +02:00
|
|
|
if musicPlayer == nil {
|
2016-03-08 18:45:56 +01:00
|
|
|
select {
|
2016-03-27 12:10:16 +02:00
|
|
|
case musicPlayer = <-musicCh:
|
2016-03-08 18:45:56 +01:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2016-03-27 12:10:16 +02:00
|
|
|
if seStream == nil {
|
|
|
|
select {
|
|
|
|
case seStream = <-seCh:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if musicPlayer != nil {
|
|
|
|
if err := musicPlayer.updateBar(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := musicPlayer.updatePlayPause(); err != nil {
|
2016-03-19 20:02:51 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-03-27 12:10:16 +02:00
|
|
|
if err := musicPlayer.updateSE(); err != nil {
|
2016-03-20 17:38:15 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-03-28 04:06:17 +02:00
|
|
|
if err := musicPlayer.updateVolume(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-19 17:40:10 +01:00
|
|
|
}
|
|
|
|
|
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))
|
2016-03-17 14:59:29 +01:00
|
|
|
screen.DrawImage(playerBarImage, op)
|
2016-03-20 17:38:15 +01:00
|
|
|
currentTimeStr := "00:00"
|
2016-03-27 12:10:16 +02:00
|
|
|
if musicPlayer != nil {
|
|
|
|
c := musicPlayer.audioPlayer.Current()
|
2016-03-19 15:45:36 +01:00
|
|
|
|
|
|
|
// Current Time
|
|
|
|
m := (c / time.Minute) % 100
|
|
|
|
s := (c / time.Second) % 60
|
|
|
|
currentTimeStr = fmt.Sprintf("%02d:%02d", m, s)
|
|
|
|
|
|
|
|
// Bar
|
2016-03-17 14:59:29 +01:00
|
|
|
cw, ch := playerCurrentImage.Size()
|
2016-03-27 12:10:16 +02:00
|
|
|
cx := int(time.Duration(w)*c/musicPlayer.total) + x - cw/2
|
2016-03-19 17:40:10 +01:00
|
|
|
cy := y - (ch-h)/2
|
2016-03-17 14:59:29 +01:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
2016-03-19 17:40:10 +01:00
|
|
|
op.GeoM.Translate(float64(cx), float64(cy))
|
2016-03-17 14:59:29 +01:00
|
|
|
screen.DrawImage(playerCurrentImage, op)
|
|
|
|
}
|
|
|
|
|
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)
|
2016-03-27 12:10:16 +02:00
|
|
|
if musicPlayer == nil {
|
2016-03-08 18:45:56 +01:00
|
|
|
msg += "\nNow Loading..."
|
|
|
|
}
|
|
|
|
ebitenutil.DebugPrint(screen, msg)
|
2016-03-07 20:20:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2016-03-27 12:10:16 +02:00
|
|
|
wavF, err := ebitenutil.OpenFile("_resources/audio/jab.wav")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
oggF, err := ebitenutil.OpenFile("_resources/audio/ragtime.ogg")
|
2016-03-07 20:20:59 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-03-28 19:55:30 +02:00
|
|
|
const sampleRate = 22050
|
|
|
|
const bytesPerSample = 4 // TODO: This should be defined in audio package
|
|
|
|
audioContext = audio.NewContext(sampleRate)
|
2016-03-08 18:45:56 +01:00
|
|
|
go func() {
|
2016-03-27 12:10:16 +02:00
|
|
|
s, err := wav.Decode(audioContext, wavF)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
seCh <- s
|
|
|
|
close(seCh)
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
s, err := vorbis.Decode(audioContext, oggF)
|
2016-03-08 18:45:56 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-03-19 20:02:51 +01:00
|
|
|
p, err := audioContext.NewPlayer(s)
|
2016-03-08 18:45:56 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2016-03-27 12:10:16 +02:00
|
|
|
musicCh <- &Player{
|
2016-03-19 20:02:51 +01:00
|
|
|
audioPlayer: p,
|
2016-03-28 19:55:30 +02:00
|
|
|
total: time.Second * time.Duration(s.Size()) / bytesPerSample / sampleRate,
|
2016-03-19 20:02:51 +01:00
|
|
|
}
|
2016-03-27 12:10:16 +02:00
|
|
|
close(musicCh)
|
|
|
|
// TODO: Is this goroutine-safe?
|
2016-03-19 20:02:51 +01:00
|
|
|
p.Play()
|
2016-03-08 18:45:56 +01:00
|
|
|
}()
|
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
|
|
|
}
|