examples/wav: add -bits option to specify 8bits

This commit is contained in:
Hajime Hoshi 2024-06-22 01:25:04 +09:00
parent 0eb2b9980d
commit 1cd6a1f10c
3 changed files with 18 additions and 1 deletions

View File

@ -22,6 +22,9 @@ var (
//go:embed jab.wav
Jab_wav []byte
//go:embed jab8.wav
Jab8_wav []byte
//go:embed jump.ogg
Jump_ogg []byte

Binary file not shown.

View File

@ -16,6 +16,8 @@ package main
import (
"bytes"
"flag"
"io"
"log"
"github.com/hajimehoshi/ebiten/v2"
@ -32,6 +34,10 @@ const (
sampleRate = 48000
)
var (
flagBitsPerSample = flag.Int("bits", 16, "bits per sample")
)
type Game struct {
audioContext *audio.Context
audioPlayer *audio.Player
@ -59,7 +65,14 @@ func NewGame() (*Game, error) {
// ...
// Decode wav-formatted data and retrieve decoded PCM stream.
d, err := wav.DecodeWithoutResampling(bytes.NewReader(raudio.Jab_wav))
var r io.Reader
switch *flagBitsPerSample {
case 8:
r = bytes.NewReader(raudio.Jab8_wav)
default:
r = bytes.NewReader(raudio.Jab_wav)
}
d, err := wav.DecodeWithoutResampling(r)
if err != nil {
return nil, err
}
@ -99,6 +112,7 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
}
func main() {
flag.Parse()
g, err := NewGame()
if err != nil {
log.Fatal(err)