2017-08-07 19:04:37 +02:00
|
|
|
// Copyright 2017 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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 (
|
2020-10-07 16:09:02 +02:00
|
|
|
"bytes"
|
2017-08-07 19:04:37 +02:00
|
|
|
"log"
|
|
|
|
|
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/wav"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
raudio "github.com/hajimehoshi/ebiten/v2/examples/resources/audio"
|
2020-10-09 22:03:51 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2017-08-07 19:04:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-30 18:01:11 +01:00
|
|
|
screenWidth = 640
|
|
|
|
screenHeight = 480
|
2021-05-25 15:09:43 +02:00
|
|
|
sampleRate = 48000
|
2017-08-07 19:04:37 +02:00
|
|
|
)
|
|
|
|
|
2020-06-07 14:36:46 +02:00
|
|
|
type Game struct {
|
2017-08-07 19:04:37 +02:00
|
|
|
audioContext *audio.Context
|
|
|
|
audioPlayer *audio.Player
|
2020-06-07 14:36:46 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 17:15:30 +02:00
|
|
|
func NewGame() (*Game, error) {
|
|
|
|
g := &Game{}
|
2017-08-07 19:04:37 +02:00
|
|
|
|
|
|
|
var err error
|
2018-01-30 16:25:01 +01:00
|
|
|
// Initialize audio context.
|
2020-10-04 19:25:11 +02:00
|
|
|
g.audioContext = audio.NewContext(sampleRate)
|
2017-08-07 19:04:37 +02:00
|
|
|
|
2021-05-25 15:32:46 +02:00
|
|
|
// In this example, embedded resource "Jab_wav" is used.
|
2018-03-13 19:05:59 +01:00
|
|
|
//
|
|
|
|
// If you want to use a wav file, open this and pass the file stream to wav.Decode.
|
|
|
|
// Note that file's Close() should not be closed here
|
2018-01-30 16:25:01 +01:00
|
|
|
// since audio.Player manages stream state.
|
2018-03-13 19:05:59 +01:00
|
|
|
//
|
2021-05-25 15:32:46 +02:00
|
|
|
// f, err := os.Open("jab.wav")
|
2018-03-13 19:05:59 +01:00
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
//
|
2022-06-30 18:37:07 +02:00
|
|
|
// d, err := wav.DecodeWithoutResampling(f)
|
2018-03-13 19:05:59 +01:00
|
|
|
// ...
|
2017-08-07 19:04:37 +02:00
|
|
|
|
2018-01-30 16:25:01 +01:00
|
|
|
// Decode wav-formatted data and retrieve decoded PCM stream.
|
2022-06-30 18:37:07 +02:00
|
|
|
d, err := wav.DecodeWithoutResampling(bytes.NewReader(raudio.Jab_wav))
|
2017-08-07 19:04:37 +02:00
|
|
|
if err != nil {
|
2021-07-21 17:15:30 +02:00
|
|
|
return nil, err
|
2017-08-07 19:04:37 +02:00
|
|
|
}
|
|
|
|
|
2018-01-30 16:25:01 +01:00
|
|
|
// Create an audio.Player that has one stream.
|
2021-07-22 09:39:02 +02:00
|
|
|
g.audioPlayer, err = g.audioContext.NewPlayer(d)
|
2017-08-07 19:04:37 +02:00
|
|
|
if err != nil {
|
2021-07-21 17:15:30 +02:00
|
|
|
return nil, err
|
2017-08-07 19:04:37 +02:00
|
|
|
}
|
2021-07-21 17:15:30 +02:00
|
|
|
|
|
|
|
return g, nil
|
2017-08-07 19:04:37 +02:00
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2020-10-09 22:03:51 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyP) {
|
2018-01-30 16:25:01 +01:00
|
|
|
// As audioPlayer has one stream and remembers the playing position,
|
|
|
|
// rewinding is needed before playing when reusing audioPlayer.
|
2022-09-09 18:52:46 +02:00
|
|
|
if err := g.audioPlayer.Rewind(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-07 14:36:46 +02:00
|
|
|
g.audioPlayer.Play()
|
2017-08-07 19:04:37 +02:00
|
|
|
}
|
2020-06-07 14:36:46 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
|
|
|
if g.audioPlayer.IsPlaying() {
|
2017-08-07 19:04:37 +02:00
|
|
|
ebitenutil.DebugPrint(screen, "Bump!")
|
|
|
|
} else {
|
|
|
|
ebitenutil.DebugPrint(screen, "Press P to play the wav")
|
|
|
|
}
|
2020-06-07 14:36:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
return screenWidth, screenHeight
|
2017-08-07 19:04:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2021-07-21 17:15:30 +02:00
|
|
|
g, err := NewGame()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-30 18:01:11 +01:00
|
|
|
ebiten.SetWindowSize(screenWidth, screenHeight)
|
2022-08-29 04:16:39 +02:00
|
|
|
ebiten.SetWindowTitle("WAV (Ebitengine Demo)")
|
2021-07-21 17:15:30 +02:00
|
|
|
if err := ebiten.RunGame(g); err != nil {
|
2017-08-07 19:04:37 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|