playing sounds... kinda
This commit is contained in:
parent
c3bc952330
commit
6fbee32542
|
@ -1,10 +1,14 @@
|
||||||
package chip8
|
package chip8
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
_ "embed"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"image"
|
"image"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed sound.wav
|
||||||
|
var Sound []byte
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MemorySize = 4096 // 4KB of memory
|
MemorySize = 4096 // 4KB of memory
|
||||||
InstructionSize = 2 // 2 bytes long instructions
|
InstructionSize = 2 // 2 bytes long instructions
|
||||||
|
@ -21,6 +25,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type KeyPressed func(key uint8) bool
|
type KeyPressed func(key uint8) bool
|
||||||
|
type PlaySound func(sound []byte) func()
|
||||||
|
|
||||||
type ROM struct {
|
type ROM struct {
|
||||||
Name string
|
Name string
|
||||||
|
@ -38,11 +43,14 @@ type Emulator struct {
|
||||||
ROM ROM // game rom
|
ROM ROM // game rom
|
||||||
Display *image.RGBA // display buffer
|
Display *image.RGBA // display buffer
|
||||||
KeyPressed KeyPressed // input function
|
KeyPressed KeyPressed // input function
|
||||||
|
PlaySound PlaySound
|
||||||
|
StopSound func()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEmulator(keyPressed KeyPressed) *Emulator {
|
func NewEmulator(keyPressed KeyPressed, playSound PlaySound) *Emulator {
|
||||||
emulator := new(Emulator)
|
emulator := new(Emulator)
|
||||||
emulator.KeyPressed = keyPressed
|
emulator.KeyPressed = keyPressed
|
||||||
|
emulator.PlaySound = playSound
|
||||||
emulator.Stack = NewStack()
|
emulator.Stack = NewStack()
|
||||||
emulator.Display = image.NewRGBA(image.Rect(0, 0, Width, Height))
|
emulator.Display = image.NewRGBA(image.Rect(0, 0, Width, Height))
|
||||||
emulator.Reset()
|
emulator.Reset()
|
||||||
|
@ -93,7 +101,17 @@ func (emulator *Emulator) UpdateTimers() {
|
||||||
emulator.DT -= 1
|
emulator.DT -= 1
|
||||||
}
|
}
|
||||||
if emulator.ST > 0 {
|
if emulator.ST > 0 {
|
||||||
|
if emulator.StopSound == nil {
|
||||||
|
_ = 1
|
||||||
|
emulator.StopSound = emulator.PlaySound(Sound)
|
||||||
|
}
|
||||||
|
|
||||||
emulator.ST -= 1
|
emulator.ST -= 1
|
||||||
|
} else {
|
||||||
|
if emulator.StopSound != nil {
|
||||||
|
emulator.StopSound()
|
||||||
|
emulator.StopSound = nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
BIN
chip8/sound.wav
Normal file
BIN
chip8/sound.wav
Normal file
Binary file not shown.
1
go.mod
1
go.mod
|
@ -10,6 +10,7 @@ require (
|
||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.0 // indirect
|
github.com/davecgh/go-spew v1.1.0 // indirect
|
||||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be // indirect
|
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be // indirect
|
||||||
|
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.2 // indirect
|
||||||
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 // indirect
|
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 // indirect
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
|
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -8,7 +8,9 @@ github.com/hajimehoshi/ebiten/v2 v2.2.5 h1:i6NdS6pEi5kgfTh+4XAVCVtCXxjTyxzU1cj1o
|
||||||
github.com/hajimehoshi/ebiten/v2 v2.2.5/go.mod h1:olKl/qqhMBBAm2oI7Zy292nCtE+nitlmYKNF3UpbFn0=
|
github.com/hajimehoshi/ebiten/v2 v2.2.5/go.mod h1:olKl/qqhMBBAm2oI7Zy292nCtE+nitlmYKNF3UpbFn0=
|
||||||
github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
|
github.com/hajimehoshi/file2byteslice v0.0.0-20210813153925-5340248a8f41/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
|
||||||
github.com/hajimehoshi/go-mp3 v0.3.2/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
|
github.com/hajimehoshi/go-mp3 v0.3.2/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
|
||||||
|
github.com/hajimehoshi/oto v0.6.1 h1:7cJz/zRQV4aJvMSSRqzN2TImoVVMpE0BCY4nrNJaDOM=
|
||||||
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
|
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
|
||||||
|
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.2 h1:DV2DcbY3YLuLB9gI9R1GT9TPOo92lUeWveV8ci1sBLk=
|
||||||
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.2/go.mod h1:rUKQmwMkqmRxe+IAof9+tuYA2ofm8cAWXFmSfzDN8vQ=
|
github.com/hajimehoshi/oto/v2 v2.1.0-alpha.2/go.mod h1:rUKQmwMkqmRxe+IAof9+tuYA2ofm8cAWXFmSfzDN8vQ=
|
||||||
github.com/jakecoffman/cp v1.1.0/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
|
github.com/jakecoffman/cp v1.1.0/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
|
||||||
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 h1:dy+DS31tGEGCsZzB45HmJJNHjur8GDgtRNX9U7HnSX4=
|
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 h1:dy+DS31tGEGCsZzB45HmJJNHjur8GDgtRNX9U7HnSX4=
|
||||||
|
|
26
main.go
26
main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/audio"
|
||||||
"github.com/tangzero/chip8-emulator/chip8"
|
"github.com/tangzero/chip8-emulator/chip8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -50,8 +51,9 @@ var KeyMapping = []ebiten.Key{
|
||||||
}
|
}
|
||||||
|
|
||||||
type UI struct {
|
type UI struct {
|
||||||
Emulator *chip8.Emulator
|
Emulator *chip8.Emulator
|
||||||
State State
|
State State
|
||||||
|
AudioContext *audio.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *UI) Run() {
|
func (ui *UI) Run() {
|
||||||
|
@ -85,17 +87,27 @@ func (ui *UI) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||||
return Width, Height
|
return Width, Height
|
||||||
}
|
}
|
||||||
|
|
||||||
func KeyPressed(key uint8) bool {
|
func (ui *UI) KeyPressed(key uint8) bool {
|
||||||
return ebiten.IsKeyPressed(KeyMapping[key])
|
return ebiten.IsKeyPressed(KeyMapping[key])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ui *UI) PlaySound(sound []byte) func() {
|
||||||
|
player := ui.AudioContext.NewPlayerFromBytes(sound)
|
||||||
|
player.SetVolume(0.3)
|
||||||
|
player.Play()
|
||||||
|
return func() {
|
||||||
|
player.Pause()
|
||||||
|
_ = player.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rom := LoadROM()
|
rom := LoadROM()
|
||||||
|
|
||||||
ui := UI{
|
ui := UI{}
|
||||||
Emulator: chip8.NewEmulator(KeyPressed),
|
ui.Emulator = chip8.NewEmulator(ui.KeyPressed, ui.PlaySound)
|
||||||
State: LoadingState,
|
ui.State = LoadingState
|
||||||
}
|
ui.AudioContext = audio.NewContext(44100)
|
||||||
ui.Emulator.LoadROM(rom)
|
ui.Emulator.LoadROM(rom)
|
||||||
|
|
||||||
ebiten.SetWindowSize(Width, Height)
|
ebiten.SetWindowSize(Width, Height)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user