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.
|
|
|
|
|
2021-06-24 14:49:37 +02:00
|
|
|
//go:build example
|
2020-10-06 17:45:54 +02:00
|
|
|
// +build example
|
2016-08-25 17:40:39 +02:00
|
|
|
|
2017-08-07 19:09:45 +02:00
|
|
|
// This is an example to implement an audio player.
|
2017-08-07 19:06:24 +02:00
|
|
|
// See examples/wav for a simpler example to play a sound file.
|
|
|
|
|
2016-03-07 20:20:59 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-07 16:09:02 +02:00
|
|
|
"bytes"
|
2016-03-07 20:20:59 +01:00
|
|
|
"fmt"
|
2021-05-03 18:27:57 +02:00
|
|
|
"image"
|
2016-03-17 14:59:29 +01:00
|
|
|
"image/color"
|
2021-05-03 18:27:57 +02:00
|
|
|
_ "image/png"
|
2020-10-07 16:09:02 +02:00
|
|
|
"io"
|
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
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/mp3"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/vorbis"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/audio/wav"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
raudio "github.com/hajimehoshi/ebiten/v2/examples/resources/audio"
|
2021-05-03 18:27:57 +02:00
|
|
|
riaudio "github.com/hajimehoshi/ebiten/v2/examples/resources/images/audio"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2016-03-07 20:20:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-30 18:01:11 +01:00
|
|
|
screenWidth = 640
|
|
|
|
screenHeight = 480
|
2017-06-03 16:23:02 +02:00
|
|
|
|
2021-02-21 09:57:02 +01:00
|
|
|
sampleRate = 32000
|
2016-03-07 20:20:59 +01:00
|
|
|
)
|
|
|
|
|
2016-03-17 14:59:29 +01:00
|
|
|
var (
|
2017-08-18 05:11:59 +02:00
|
|
|
playerBarColor = color.RGBA{0x80, 0x80, 0x80, 0xff}
|
|
|
|
playerCurrentColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
|
2016-03-17 14:59:29 +01:00
|
|
|
)
|
|
|
|
|
2021-05-03 18:27:57 +02:00
|
|
|
var (
|
|
|
|
playButtonImage *ebiten.Image
|
|
|
|
pauseButtonImage *ebiten.Image
|
|
|
|
alertButtonImage *ebiten.Image
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
img, _, err := image.Decode(bytes.NewReader(riaudio.Play_png))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
playButtonImage = ebiten.NewImageFromImage(img)
|
|
|
|
|
|
|
|
img, _, err = image.Decode(bytes.NewReader(riaudio.Pause_png))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
pauseButtonImage = ebiten.NewImageFromImage(img)
|
|
|
|
|
|
|
|
img, _, err = image.Decode(bytes.NewReader(riaudio.Alert_png))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
alertButtonImage = ebiten.NewImageFromImage(img)
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:11:04 +01:00
|
|
|
type musicType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
typeOgg musicType = iota
|
|
|
|
typeMP3
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t musicType) String() string {
|
|
|
|
switch t {
|
|
|
|
case typeOgg:
|
|
|
|
return "Ogg"
|
|
|
|
case typeMP3:
|
|
|
|
return "MP3"
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 18:15:04 +01:00
|
|
|
// Player represents the current audio state.
|
2016-03-19 20:02:51 +01:00
|
|
|
type Player struct {
|
2021-07-10 16:28:32 +02:00
|
|
|
game *Game
|
2017-06-03 16:53:44 +02:00
|
|
|
audioContext *audio.Context
|
|
|
|
audioPlayer *audio.Player
|
2017-06-24 16:33:19 +02:00
|
|
|
current time.Duration
|
2017-06-03 16:53:44 +02:00
|
|
|
total time.Duration
|
2018-01-21 18:15:04 +01:00
|
|
|
seBytes []byte
|
|
|
|
seCh chan []byte
|
2017-06-03 16:53:44 +02:00
|
|
|
volume128 int
|
2019-10-29 16:11:04 +01:00
|
|
|
musicType musicType
|
2021-07-21 17:15:30 +02:00
|
|
|
|
|
|
|
playButtonPosition image.Point
|
|
|
|
alertButtonPosition image.Point
|
2016-03-19 20:02:51 +01:00
|
|
|
}
|
|
|
|
|
2016-03-19 17:40:10 +01:00
|
|
|
func playerBarRect() (x, y, w, h int) {
|
2020-10-30 18:01:11 +01:00
|
|
|
w, h = 600, 8
|
2016-03-19 17:40:10 +01:00
|
|
|
x = (screenWidth - w) / 2
|
|
|
|
y = screenHeight - h - 16
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:28:32 +02:00
|
|
|
func NewPlayer(game *Game, audioContext *audio.Context, musicType musicType) (*Player, error) {
|
2019-10-29 16:11:04 +01:00
|
|
|
type audioStream interface {
|
2020-10-07 16:09:02 +02:00
|
|
|
io.ReadSeeker
|
2019-10-29 16:11:04 +01:00
|
|
|
Length() int64
|
|
|
|
}
|
|
|
|
|
2017-06-03 16:23:02 +02:00
|
|
|
const bytesPerSample = 4 // TODO: This should be defined in audio package
|
2019-10-29 16:11:04 +01:00
|
|
|
|
|
|
|
var s audioStream
|
|
|
|
|
|
|
|
switch musicType {
|
|
|
|
case typeOgg:
|
|
|
|
var err error
|
2022-06-30 18:37:07 +02:00
|
|
|
s, err = vorbis.DecodeWithoutResampling(bytes.NewReader(raudio.Ragtime_ogg))
|
2019-10-29 16:11:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case typeMP3:
|
|
|
|
var err error
|
2022-06-30 18:37:07 +02:00
|
|
|
s, err = mp3.DecodeWithoutResampling(bytes.NewReader(raudio.Ragtime_mp3))
|
2019-10-29 16:11:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
2017-06-03 16:23:02 +02:00
|
|
|
}
|
2021-07-22 09:39:02 +02:00
|
|
|
p, err := audioContext.NewPlayer(s)
|
2017-06-03 16:23:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
player := &Player{
|
2021-07-10 16:28:32 +02:00
|
|
|
game: game,
|
2017-06-03 16:53:44 +02:00
|
|
|
audioContext: audioContext,
|
|
|
|
audioPlayer: p,
|
2018-01-08 17:25:38 +01:00
|
|
|
total: time.Second * time.Duration(s.Length()) / bytesPerSample / sampleRate,
|
2017-06-03 16:53:44 +02:00
|
|
|
volume128: 128,
|
2018-01-21 18:15:04 +01:00
|
|
|
seCh: make(chan []byte),
|
2019-10-29 16:11:04 +01:00
|
|
|
musicType: musicType,
|
2017-06-03 16:23:02 +02:00
|
|
|
}
|
2017-06-11 11:12:12 +02:00
|
|
|
if player.total == 0 {
|
|
|
|
player.total = 1
|
|
|
|
}
|
2021-07-21 17:15:30 +02:00
|
|
|
|
|
|
|
const buttonPadding = 16
|
|
|
|
w, _ := playButtonImage.Size()
|
|
|
|
player.playButtonPosition.X = (screenWidth - w*2 + buttonPadding*1) / 2
|
|
|
|
player.playButtonPosition.Y = screenHeight - 160
|
|
|
|
|
|
|
|
player.alertButtonPosition.X = player.playButtonPosition.X + w + buttonPadding
|
|
|
|
player.alertButtonPosition.Y = player.playButtonPosition.Y
|
|
|
|
|
2017-06-03 16:23:02 +02:00
|
|
|
player.audioPlayer.Play()
|
2017-06-03 18:34:36 +02:00
|
|
|
go func() {
|
2022-06-30 18:37:07 +02:00
|
|
|
s, err := wav.DecodeWithSampleRate(sampleRate, bytes.NewReader(raudio.Jab_wav))
|
2017-06-03 18:34:36 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-29 16:11:04 +01:00
|
|
|
func (p *Player) Close() error {
|
|
|
|
return p.audioPlayer.Close()
|
|
|
|
}
|
|
|
|
|
2017-06-03 16:23:02 +02:00
|
|
|
func (p *Player) update() error {
|
|
|
|
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
|
|
|
default:
|
|
|
|
}
|
2018-01-21 18:15:04 +01:00
|
|
|
|
2017-06-24 16:33:19 +02:00
|
|
|
if p.audioPlayer.IsPlaying() {
|
|
|
|
p.current = p.audioPlayer.Current()
|
|
|
|
}
|
2018-01-21 18:15:04 +01:00
|
|
|
p.seekBarIfNeeded()
|
|
|
|
p.switchPlayStateIfNeeded()
|
|
|
|
p.playSEIfNeeded()
|
|
|
|
p.updateVolumeIfNeeded()
|
|
|
|
|
2020-03-20 16:34:12 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyU) {
|
|
|
|
b := ebiten.IsRunnableOnUnfocused()
|
|
|
|
ebiten.SetRunnableOnUnfocused(!b)
|
2017-08-02 16:37:50 +02:00
|
|
|
}
|
2017-06-03 16:23:02 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-03 18:27:57 +02:00
|
|
|
func (p *Player) shouldPlaySE() bool {
|
2017-06-03 18:34:36 +02:00
|
|
|
if p.seBytes == nil {
|
2018-01-21 18:15:04 +01:00
|
|
|
// Bytes for the SE is not loaded yet.
|
2021-05-03 18:27:57 +02:00
|
|
|
return false
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2018-01-21 18:15:04 +01:00
|
|
|
|
2021-05-03 18:27:57 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyP) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
r := image.Rectangle{
|
2021-07-21 17:15:30 +02:00
|
|
|
Min: p.alertButtonPosition,
|
|
|
|
Max: p.alertButtonPosition.Add(image.Pt(alertButtonImage.Size())),
|
2021-05-03 18:27:57 +02:00
|
|
|
}
|
|
|
|
if image.Pt(ebiten.CursorPosition()).In(r) {
|
|
|
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-07-10 16:28:32 +02:00
|
|
|
for _, id := range p.game.justPressedTouchIDs {
|
2021-05-03 18:27:57 +02:00
|
|
|
if image.Pt(ebiten.TouchPosition(id)).In(r) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) playSEIfNeeded() {
|
|
|
|
if !p.shouldPlaySE() {
|
2017-06-02 18:40:22 +02:00
|
|
|
return
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
2021-07-22 09:39:02 +02:00
|
|
|
sePlayer := p.audioContext.NewPlayerFromBytes(p.seBytes)
|
2017-06-02 18:12:58 +02:00
|
|
|
sePlayer.Play()
|
2016-03-27 12:10:16 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 18:15:04 +01:00
|
|
|
func (p *Player) updateVolumeIfNeeded() {
|
2018-02-04 15:18:32 +01:00
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyZ) {
|
2017-06-03 16:37:00 +02:00
|
|
|
p.volume128--
|
2016-03-28 04:06:17 +02:00
|
|
|
}
|
2018-02-04 15:18:32 +01:00
|
|
|
if ebiten.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
|
|
|
}
|
|
|
|
|
2021-05-03 18:27:57 +02:00
|
|
|
func (p *Player) shouldSwitchPlayStateIfNeeded() bool {
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyS) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
r := image.Rectangle{
|
2021-07-21 17:15:30 +02:00
|
|
|
Min: p.playButtonPosition,
|
|
|
|
Max: p.playButtonPosition.Add(image.Pt(playButtonImage.Size())),
|
2021-05-03 18:27:57 +02:00
|
|
|
}
|
|
|
|
if image.Pt(ebiten.CursorPosition()).In(r) {
|
|
|
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-07-10 16:28:32 +02:00
|
|
|
for _, id := range p.game.justPressedTouchIDs {
|
2021-05-03 18:27:57 +02:00
|
|
|
if image.Pt(ebiten.TouchPosition(id)).In(r) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-01-21 18:15:04 +01:00
|
|
|
func (p *Player) switchPlayStateIfNeeded() {
|
2021-05-03 18:27:57 +02:00
|
|
|
if !p.shouldSwitchPlayStateIfNeeded() {
|
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
|
|
|
}
|
|
|
|
|
2021-07-10 16:28:32 +02:00
|
|
|
func (p *Player) justPressedPosition() (int, int, bool) {
|
2021-02-21 11:20:24 +01:00
|
|
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
|
|
|
x, y := ebiten.CursorPosition()
|
|
|
|
return x, y, true
|
2016-03-19 17:40:10 +01:00
|
|
|
}
|
2018-01-21 18:15:04 +01:00
|
|
|
|
2021-07-10 16:28:32 +02:00
|
|
|
if len(p.game.justPressedTouchIDs) > 0 {
|
|
|
|
x, y := ebiten.TouchPosition(p.game.justPressedTouchIDs[0])
|
2021-02-21 11:20:24 +01:00
|
|
|
return x, y, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) seekBarIfNeeded() {
|
2018-01-21 18:15:04 +01:00
|
|
|
// Calculate the next seeking position from the current cursor position.
|
2021-07-10 16:28:32 +02:00
|
|
|
x, y, ok := p.justPressedPosition()
|
2021-02-21 11:20:24 +01:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2016-03-19 17:40:10 +01:00
|
|
|
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)
|
2017-06-24 16:33:19 +02:00
|
|
|
p.current = pos
|
|
|
|
p.audioPlayer.Seek(pos)
|
2016-03-19 17:40:10 +01:00
|
|
|
}
|
|
|
|
|
2017-06-03 16:23:02 +02:00
|
|
|
func (p *Player) draw(screen *ebiten.Image) {
|
2018-01-21 18:15:04 +01:00
|
|
|
// Draw the bar.
|
2016-03-19 17:40:10 +01:00
|
|
|
x, y, w, h := playerBarRect()
|
2017-08-18 05:11:59 +02:00
|
|
|
ebitenutil.DrawRect(screen, float64(x), float64(y), float64(w), float64(h), playerBarColor)
|
|
|
|
|
2018-01-21 18:15:04 +01:00
|
|
|
// Draw the cursor on the bar.
|
2017-06-24 16:33:19 +02:00
|
|
|
c := p.current
|
2020-10-30 18:01:11 +01:00
|
|
|
cw, ch := 8, 20
|
2017-06-03 16:23:02 +02:00
|
|
|
cx := int(time.Duration(w)*c/p.total) + x - cw/2
|
|
|
|
cy := y - (ch-h)/2
|
2017-08-18 05:11:59 +02:00
|
|
|
ebitenutil.DrawRect(screen, float64(cx), float64(cy), float64(cw), float64(ch), playerCurrentColor)
|
2016-03-17 14:59:29 +01:00
|
|
|
|
2018-01-21 18:15:04 +01:00
|
|
|
// Compose the curren time text.
|
|
|
|
m := (c / time.Minute) % 100
|
|
|
|
s := (c / time.Second) % 60
|
|
|
|
currentTimeStr := fmt.Sprintf("%02d:%02d", m, s)
|
|
|
|
|
2021-05-03 18:27:57 +02:00
|
|
|
// Draw buttons
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
2021-07-21 17:15:30 +02:00
|
|
|
op.GeoM.Translate(float64(p.playButtonPosition.X), float64(p.playButtonPosition.Y))
|
2021-05-03 18:27:57 +02:00
|
|
|
if p.audioPlayer.IsPlaying() {
|
|
|
|
screen.DrawImage(pauseButtonImage, op)
|
|
|
|
} else {
|
|
|
|
screen.DrawImage(playButtonImage, op)
|
|
|
|
}
|
|
|
|
op.GeoM.Reset()
|
2021-07-21 17:15:30 +02:00
|
|
|
op.GeoM.Translate(float64(p.alertButtonPosition.X), float64(p.alertButtonPosition.Y))
|
2021-05-03 18:27:57 +02:00
|
|
|
screen.DrawImage(alertButtonImage, op)
|
|
|
|
|
2018-01-21 18:15:04 +01:00
|
|
|
// Draw the debug message.
|
2018-09-29 19:27:33 +02:00
|
|
|
msg := fmt.Sprintf(`TPS: %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
|
2020-03-20 16:34:12 +01:00
|
|
|
Press U to switch the runnable-on-unfocused state
|
2021-02-21 09:57:02 +01:00
|
|
|
Press A to switch Ogg and MP3 (Current: %s)
|
2019-01-04 17:43:22 +01:00
|
|
|
Current Time: %s
|
2019-10-29 16:11:04 +01:00
|
|
|
Current Volume: %d/128
|
2022-07-17 04:25:45 +02:00
|
|
|
Type: %s`, ebiten.ActualTPS(), p.musicType,
|
2021-02-21 09:57:02 +01:00
|
|
|
currentTimeStr, int(p.audioPlayer.Volume()*128), p.musicType)
|
2017-06-03 16:23:02 +02:00
|
|
|
ebitenutil.DebugPrint(screen, msg)
|
|
|
|
}
|
|
|
|
|
2020-04-12 11:18:45 +02:00
|
|
|
type Game struct {
|
2019-10-29 16:11:04 +01:00
|
|
|
musicPlayer *Player
|
2020-04-12 11:18:45 +02:00
|
|
|
musicPlayerCh chan *Player
|
|
|
|
errCh chan error
|
2021-07-10 16:28:32 +02:00
|
|
|
|
|
|
|
justPressedTouchIDs []ebiten.TouchID
|
2020-04-12 11:18:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGame() (*Game, error) {
|
2020-10-04 19:25:11 +02:00
|
|
|
audioContext := audio.NewContext(sampleRate)
|
2020-04-12 11:18:45 +02:00
|
|
|
|
2021-07-10 16:28:32 +02:00
|
|
|
g := &Game{
|
|
|
|
musicPlayerCh: make(chan *Player),
|
|
|
|
errCh: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := NewPlayer(g, audioContext, typeOgg)
|
2020-04-12 11:18:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-21 18:15:04 +01:00
|
|
|
|
2021-07-10 16:28:32 +02:00
|
|
|
g.musicPlayer = m
|
|
|
|
return g, nil
|
2020-04-12 11:18:45 +02:00
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2019-10-29 16:11:04 +01:00
|
|
|
select {
|
2020-04-12 11:18:45 +02:00
|
|
|
case p := <-g.musicPlayerCh:
|
|
|
|
g.musicPlayer = p
|
|
|
|
case err := <-g.errCh:
|
2017-06-03 16:23:02 +02:00
|
|
|
return err
|
2019-10-29 16:11:04 +01:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:28:32 +02:00
|
|
|
g.justPressedTouchIDs = inpututil.AppendJustPressedTouchIDs(g.justPressedTouchIDs[:0])
|
|
|
|
|
2020-04-12 11:18:45 +02:00
|
|
|
if g.musicPlayer != nil && inpututil.IsKeyJustPressed(ebiten.KeyA) {
|
2019-10-29 16:11:04 +01:00
|
|
|
var t musicType
|
2020-04-12 11:18:45 +02:00
|
|
|
switch g.musicPlayer.musicType {
|
2019-10-29 16:11:04 +01:00
|
|
|
case typeOgg:
|
|
|
|
t = typeMP3
|
|
|
|
case typeMP3:
|
|
|
|
t = typeOgg
|
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
|
2020-04-12 11:18:45 +02:00
|
|
|
g.musicPlayer.Close()
|
|
|
|
g.musicPlayer = nil
|
2019-10-29 16:11:04 +01:00
|
|
|
|
|
|
|
go func() {
|
2021-07-10 16:28:32 +02:00
|
|
|
p, err := NewPlayer(g, audio.CurrentContext(), t)
|
2019-10-29 16:11:04 +01:00
|
|
|
if err != nil {
|
2020-04-12 11:18:45 +02:00
|
|
|
g.errCh <- err
|
2019-10-29 16:11:04 +01:00
|
|
|
return
|
|
|
|
}
|
2020-04-12 11:18:45 +02:00
|
|
|
g.musicPlayerCh <- p
|
2019-10-29 16:11:04 +01:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-04-12 11:18:45 +02:00
|
|
|
if g.musicPlayer != nil {
|
|
|
|
if err := g.musicPlayer.update(); err != nil {
|
2019-10-29 16:11:04 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-06-03 16:23:02 +02:00
|
|
|
}
|
2020-04-12 11:18:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-01-21 18:15:04 +01:00
|
|
|
|
2020-04-12 11:18:45 +02:00
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
|
|
if g.musicPlayer != nil {
|
|
|
|
g.musicPlayer.draw(screen)
|
2017-06-03 16:23:02 +02:00
|
|
|
}
|
2020-04-12 11:18:45 +02:00
|
|
|
}
|
2018-01-21 18:15:04 +01:00
|
|
|
|
2020-04-12 11:18:45 +02:00
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
2016-03-07 20:20:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-10-30 18:01:11 +01:00
|
|
|
ebiten.SetWindowSize(screenWidth, screenHeight)
|
2020-04-12 11:18:45 +02:00
|
|
|
ebiten.SetWindowTitle("Audio (Ebiten Demo)")
|
|
|
|
g, err := NewGame()
|
2017-06-03 16:23:02 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-12 11:18:45 +02:00
|
|
|
if err := ebiten.RunGame(g); err != nil {
|
2016-03-07 20:20:59 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|