Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
akatsuki105 committed Apr 14, 2021
1 parent 7c67c19 commit e1e8f16
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions pkg/gba/apu.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const (
SAMP_CYCLES = (CPU_FREQ_HZ / SND_FREQUENCY)
BUFF_SAMPLES = ((SND_SAMPLES) * 16 * 2)
BUFF_SAMPLES_MSK = ((BUFF_SAMPLES) - 1)
SAMPLE_TIME float64 = 1.0 / 32768
SAMPLE_TIME float64 = 1.0 / SND_FREQUENCY
STREAM_LEN = ((SND_SAMPLES) * 16 * 2 * 2)
)

const (
Expand Down Expand Up @@ -86,9 +87,9 @@ type SoundChan struct {
}

func newAPU() *APU {
stream = make([]byte, BUFF_SAMPLES)
stream = make([]byte, STREAM_LEN)

context, err := oto.NewContext(SND_FREQUENCY, 2, 2, BUFF_SAMPLES)
context, err := oto.NewContext(SND_FREQUENCY, 2, 2, STREAM_LEN)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -283,7 +284,7 @@ func (g *GBA) waveSample() int8 {
g.apu.chans[2].samples -= cycleSamples

waveSamples--
if waveSamples > 0 {
if waveSamples != 0 {
wavePosition = (wavePosition + 1) & 0b0011_1111
} else {
g.waveReset()
Expand Down Expand Up @@ -311,9 +312,9 @@ func (g *GBA) waveSample() int8 {
}

if samp >= 0 {
return int8(int16(samp) * PSG_MAX / 7)
return int8(float64(samp) / 7 * PSG_MAX)
}
return int8(int16(samp) * PSG_MIN / (-8))
return int8(float64(samp) / (-8) * PSG_MIN)
}

func (g *GBA) noiseSample() int8 {
Expand All @@ -323,12 +324,12 @@ func (g *GBA) noiseSample() int8 {

cnth := g._getRAM(ram.SOUND4CNT_H) // ctrl

// Actual frequency in Hertz
freqDiv, freqRsh := float64(cnth&0x7), float64((cnth>>4)&0xf)
if freqDiv == 0 {
freqDiv = 1 / 2
// Actual frequency in Hertz (524288 / r / 2^(s+1))
r, s := float64(cnth&0x7), float64((cnth>>4)&0xf)
if r == 0 {
r = 0.5
}
frequency := (524288 / freqDiv) / math.Pow(2, freqRsh+1)
frequency := (524288 / r) / math.Pow(2, s+1)

cntl := g._getRAM(ram.SOUND4CNT_L) // env
// Full length of the generated wave (if enabled) in seconds
Expand Down Expand Up @@ -422,7 +423,7 @@ var sndBuffer [BUFF_SAMPLES]int16
var stream []byte

func (g *GBA) soundMix() {
for i := 0; i < BUFF_SAMPLES; i += 4 {
for i := 0; i < STREAM_LEN; i += 4 {
snd := sndBuffer[sndCurPlay&BUFF_SAMPLES_MSK] << 6
stream[i+0], stream[i+1] = byte(snd), byte(snd>>8)
sndCurPlay++
Expand All @@ -432,7 +433,12 @@ func (g *GBA) soundMix() {
}

// Avoid desync between the Play cursor and the Write cursor
sndCurPlay += uint32(int32(sndCurWrite-sndCurPlay)>>8) & ^uint32(1)
delta := (int32(sndCurWrite-sndCurPlay) >> 8) - (int32(sndCurWrite-sndCurPlay)>>8)%2
if delta >= 0 {
sndCurPlay += uint32(delta)
} else {
sndCurPlay -= uint32(-delta)
}
}

var (
Expand Down Expand Up @@ -478,6 +484,9 @@ func (g *GBA) fifoALoad() {
for i := byte(0); i < fifoALen; i++ {
fifoA[i] = fifoA[i+1]
}
for i := fifoALen; i < 0x20; i++ {
fifoA[i] = 0
}
}

func (g *GBA) fifoBLoad() {
Expand All @@ -491,6 +500,9 @@ func (g *GBA) fifoBLoad() {
for i := byte(0); i < fifoBLen; i++ {
fifoB[i] = fifoB[i+1]
}
for i := fifoBLen; i < 0x20; i++ {
fifoB[i] = 0
}
}

var (
Expand Down

0 comments on commit e1e8f16

Please sign in to comment.