ebiten/examples/wav/main.go

98 lines
2.5 KiB
Go
Raw Normal View History

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.
// +build example jsgo
2017-08-07 19:04:37 +02:00
package main
import (
"log"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/wav"
"github.com/hajimehoshi/ebiten/ebitenutil"
2018-03-13 19:05:59 +01:00
raudio "github.com/hajimehoshi/ebiten/examples/resources/audio"
2017-08-07 19:04:37 +02:00
)
const (
screenWidth = 320
screenHeight = 240
sampleRate = 44100
)
var (
audioContext *audio.Context
audioPlayer *audio.Player
)
func init() {
var err error
2018-01-30 16:25:01 +01:00
// Initialize audio context.
2017-08-07 19:04:37 +02:00
audioContext, err = audio.NewContext(sampleRate)
if err != nil {
log.Fatal(err)
}
2018-03-13 19:05:59 +01:00
// In this example, embeded resource "Jab_wav" is used.
//
// 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
//
// f, err := os.Open("jab.wav")
// if err != nil {
// return err
// }
//
// d, err := wav.Decode(audioContext, f)
// ...
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.
2018-03-13 19:05:59 +01:00
d, err := wav.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Jab_wav))
2017-08-07 19:04:37 +02:00
if err != nil {
log.Fatal(err)
}
2018-01-30 16:25:01 +01:00
// Create an audio.Player that has one stream.
2017-08-07 19:04:37 +02:00
audioPlayer, err = audio.NewPlayer(audioContext, d)
if err != nil {
log.Fatal(err)
}
}
func update(screen *ebiten.Image) error {
if ebiten.IsKeyPressed(ebiten.KeyP) && !audioPlayer.IsPlaying() {
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.
2017-08-07 19:04:37 +02:00
audioPlayer.Rewind()
audioPlayer.Play()
}
if ebiten.IsDrawingSkipped() {
2017-08-07 19:04:37 +02:00
return nil
}
if audioPlayer.IsPlaying() {
ebitenutil.DebugPrint(screen, "Bump!")
} else {
ebitenutil.DebugPrint(screen, "Press P to play the wav")
}
return nil
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "WAV (Ebiten Demo)"); err != nil {
log.Fatal(err)
}
}