audio fix

This commit is contained in:
Jairinho 2022-03-01 21:45:06 -03:00
parent 20f2660faf
commit e6b150136c
No known key found for this signature in database
GPG Key ID: 954589B18A21D5B6
2 changed files with 14 additions and 5 deletions

Binary file not shown.

19
main.go
View File

@ -1,12 +1,14 @@
package main package main
import ( import (
"bytes"
_ "embed" _ "embed"
"log" "log"
"time" "time"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio" "github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/audio/wav"
"github.com/tangzero/chip8-emulator/chip8" "github.com/tangzero/chip8-emulator/chip8"
) )
@ -92,8 +94,11 @@ func KeyPressed(key uint8) bool {
} }
func SoundPlayer(sound []byte) (func(), func()) { func SoundPlayer(sound []byte) (func(), func()) {
player := audio.NewContext(chip8.SampleRate).NewPlayerFromBytes(sound) stream, err := wav.DecodeWithSampleRate(chip8.SampleRate, bytes.NewReader(sound))
player.SetVolume(0.3) assert(err)
player, err := audio.NewContext(chip8.SampleRate).NewPlayer(stream)
assert(err)
player.SetVolume(0.5)
return PlaySound(player), StopSound(player) return PlaySound(player), StopSound(player)
} }
@ -109,7 +114,7 @@ func PlaySound(player *audio.Player) func() {
func StopSound(player *audio.Player) func() { func StopSound(player *audio.Player) func() {
return func() { return func() {
player.Pause() player.Pause()
player.Seek(0) assert(player.Rewind())
} }
} }
@ -124,7 +129,11 @@ func main() {
ebiten.SetWindowSize(Width, Height) ebiten.SetWindowSize(Width, Height)
ebiten.SetWindowTitle("CHIP-8 : " + rom.Name) ebiten.SetWindowTitle("CHIP-8 : " + rom.Name)
if err := ebiten.RunGame(&gui); err != nil { assert(ebiten.RunGame(&gui))
log.Fatal(err) }
func assert(err error) {
if err != nil {
log.Panic(err)
} }
} }