Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
akatsuki105 committed Apr 13, 2021
1 parent 0076783 commit 608a9a6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module mettaur
go 1.16

require (
github.com/anthonynsimon/bild v0.13.0 // indirect
github.com/anthonynsimon/bild v0.13.0
github.com/hajimehoshi/ebiten/v2 v2.0.8
github.com/hajimehoshi/oto v0.7.1
)
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2/go.mod h1:tQ2
github.com/gofrs/flock v0.8.0 h1:MSdYClljsF3PbENUUEx85nkWfJSGfzYI9yEBZOJz6CY=
github.com/gofrs/flock v0.8.0/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/hajimehoshi/bitmapfont/v2 v2.1.0/go.mod h1:2BnYrkTQGThpr/CY6LorYtt/zEPNzvE/ND69CRTaHMs=
github.com/hajimehoshi/ebiten/v2 v2.0.6 h1:sHNymgI+q80xasP69oFyrpup6r2qCNsKxqwsGEh6PWE=
github.com/hajimehoshi/ebiten/v2 v2.0.6/go.mod h1:uS3OjMW3f2DRDMtWoIF7yMMmrMkv+fZ6pXcwR1pfA0Y=
github.com/hajimehoshi/ebiten/v2 v2.0.8 h1:+LPEcUhsLS3swxf2LtBb2V1TO72YoGPArWPqxzXEDYI=
github.com/hajimehoshi/ebiten/v2 v2.0.8/go.mod h1:uS3OjMW3f2DRDMtWoIF7yMMmrMkv+fZ6pXcwR1pfA0Y=
github.com/hajimehoshi/file2byteslice v0.0.0-20200812174855-0e5e8a80490e/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
github.com/hajimehoshi/go-mp3 v0.3.1/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
github.com/hajimehoshi/oto v0.6.8 h1:yRb3EJQ4lAkBgZYheqmdH6Lr77RV9nSWFsK/jwWdTNY=
github.com/hajimehoshi/oto v0.6.8/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
github.com/hajimehoshi/oto v0.7.1 h1:I7maFPz5MBCwiutOrz++DLdbr4rTzBsbBuV2VpgU9kk=
github.com/hajimehoshi/oto v0.7.1/go.mod h1:wovJ8WWMfFKvP587mhHgot/MBr4DnNy9m6EepeVGnos=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jakecoffman/cp v1.0.0/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
Expand Down
40 changes: 27 additions & 13 deletions pkg/gba/apu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"mettaur/pkg/util"
"time"

"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/oto"
)

var waveSamples byte
Expand Down Expand Up @@ -40,8 +40,8 @@ var dutyLookUp = [4]float64{0.125, 0.25, 0.5, 0.75}
var dutyLookUpi = [4]float64{0.875, 0.75, 0.5, 0.25}

type APU struct {
context *audio.Context
player *audio.Player
context *oto.Context
player *oto.Player
chans [4]*SoundChan
}

Expand All @@ -56,27 +56,41 @@ type SoundChan struct {

func newAPU() *APU {
stream = make([]byte, BUFF_SAMPLES*2)
context := audio.NewContext(SND_FREQUENCY)
player := audio.NewPlayerFromBytes(context, stream)

context, err := oto.NewContext(SND_FREQUENCY, 2, 2, 512)
if err != nil {
panic(err)
}

player := context.NewPlayer()
return &APU{
context: context,
player: player,
chans: [4]*SoundChan{&SoundChan{}, &SoundChan{}, &SoundChan{}, &SoundChan{}},
}
}

func (g *GBA) exitAPU() {
g.apu.context.Close()
}

func (g *GBA) playSound() {
go func() {
for range time.Tick(time.Second / 120) {
g.soundMix()
if !g.apu.player.IsPlaying() {
g.apu.player.Rewind()
go g.apu.player.Play()
}
for range time.Tick(time.Second / 60) {
g.updateSound()
g.writeSound()
}
}()
}

func (g *GBA) writeSound() {
g.apu.player.Write(stream)
}

func (g *GBA) updateSound() {
g.soundMix()
}

func (g *GBA) squareSample(ch int) int8 {
cntx := g._getRAM(ram.SOUNDCNT_X)
if !util.Bit(cntx, ch) {
Expand Down Expand Up @@ -372,10 +386,10 @@ var stream []byte
func (g *GBA) soundMix() {
for i := 0; i < len(sndBuffer); i += 4 {
stream[i+0] = byte(sndBuffer[sndCurPlay&BUFF_SAMPLES_MSK] << 6)
stream[i+1] = byte((sndBuffer[sndCurPlay&BUFF_SAMPLES_MSK] << 6) >> 8)
stream[i+1] = byte(sndBuffer[sndCurPlay&BUFF_SAMPLES_MSK]>>8) << 6
sndCurPlay++
stream[i+2] = byte(sndBuffer[sndCurPlay&BUFF_SAMPLES_MSK] << 6)
stream[i+3] = byte((sndBuffer[sndCurPlay&BUFF_SAMPLES_MSK] << 6) >> 8)
stream[i+3] = byte(sndBuffer[sndCurPlay&BUFF_SAMPLES_MSK]>>8) << 6
sndCurPlay++
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/gba/gba.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (g *GBA) Exit(s string) {
if debug {
g.PrintHistory()
}
g.exitAPU()
panic("")
}

Expand Down Expand Up @@ -187,6 +188,7 @@ func (g *GBA) Update() {
g.joypad.Read()
}

g.soundBufferWrap()
g.Frame++
}

Expand Down

0 comments on commit 608a9a6

Please sign in to comment.