Skip to content

Commit

Permalink
feat(apu): Add config for buffer size
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jan 30, 2025
1 parent a6c647e commit 95893c4
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 15 deletions.
2 changes: 2 additions & 0 deletions config_example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ b_turbo = 'Numpad5'
enabled = true
# Output volume (between 0 and 1).
volume = 1.0
# Audio buffer size. Try increasing this if audio pops or stutters.
buffer_size = '70 KiB'

# Toggles specific audio channels.
[audio.channels]
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module gabe565.com/gones
go 1.23.5

require (
gabe565.com/utils v0.0.0-20241022183714-230f1ea91e9c
gabe565.com/utils v0.0.0-20250130072635-ed882da6fcdd
github.com/Masterminds/sprig/v3 v3.3.0
github.com/PuerkitoBio/goquery v1.10.1
github.com/dmarkham/enumer v1.5.10
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
gabe565.com/utils v0.0.0-20241022183714-230f1ea91e9c h1:ORHS+E/RxziZ625EvarvowzMHfrnsJiaHAiAhBgllxk=
gabe565.com/utils v0.0.0-20241022183714-230f1ea91e9c/go.mod h1:85awzsVzCoWcZgBZmKtE6kMiOAYd2aKrNyt1gNSV2Kg=
gabe565.com/utils v0.0.0-20250130072635-ed882da6fcdd h1:ATna6PCWp9+8x3hBC/miPdvu4uVDRj4zw2QYNW6OaV0=
gabe565.com/utils v0.0.0-20250130072635-ed882da6fcdd/go.mod h1:6FZP8OeF0k3hTsPClyDGEILcBg3hvGcSp4u4pmjMusY=
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
Expand Down
5 changes: 2 additions & 3 deletions internal/apu/apu.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ type CPU interface {

const (
FrameCounterRate = float64(consts.CPUFrequency) / 240.0
DefaultSampleRate = float64(consts.CPUFrequency) / float64(consts.AudioSampleRate) * consts.FrameRateDifference
BufferCap = consts.AudioSampleRate / 5 * 8
DefaultSampleRate = float64(consts.CPUFrequency) / float64(consts.AudioSampleRate) * consts.FrameRateDiff
)

//nolint:gochecknoglobals
Expand Down Expand Up @@ -57,7 +56,7 @@ func New(conf *config.Config) *APU {
Enabled: true,
SampleRate: DefaultSampleRate,
conf: &conf.Audio,
buf: newRingBuffer(BufferCap),
buf: newRingBuffer(int(conf.Audio.BufferSize)),

Square: [2]Square{{Channel1: true}, {}},
Noise: Noise{ShiftRegister: 1},
Expand Down
24 changes: 24 additions & 0 deletions internal/config/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package config

import (
"strings"

"gabe565.com/utils/bytefmt"
)

type Bytes int64

func (b Bytes) MarshalText() ([]byte, error) {
formatted := bytefmt.Encode(int64(b))
formatted = strings.Replace(formatted, ".00", "", 1)
return []byte(formatted), nil
}

func (b *Bytes) UnmarshalText(text []byte) error {
v, err := bytefmt.Decode(string(text))
if err != nil {
return err
}
*b = Bytes(v)
return nil
}
7 changes: 4 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ func (i Input) ResetHoldFrames() int {
}

type Audio struct {
Enabled bool `toml:"enabled" comment:"Enables audio output."`
Volume float64 `toml:"volume" comment:"Output volume (between 0 and 1)."`
Channels AudioChannels `toml:"channels" comment:"Toggles specific audio channels."`
Enabled bool `toml:"enabled" comment:"Enables audio output."`
Volume float64 `toml:"volume" comment:"Output volume (between 0 and 1)."`
Channels AudioChannels `toml:"channels" comment:"Toggles specific audio channels."`
BufferSize Bytes `toml:"buffer_size" comment:"Audio buffer size. Try increasing this if audio pops or stutters."`
}

type AudioChannels struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"time"

"gabe565.com/utils/bytefmt"
"github.com/hajimehoshi/ebiten/v2"
)

Expand Down Expand Up @@ -70,6 +71,7 @@ func NewDefault() *Config {
Noise: true,
PCM: true,
},
BufferSize: 70 * bytefmt.KiB,
},
}
}
18 changes: 18 additions & 0 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,23 @@ func fixConfig(k *koanf.Koanf) error {
}
}

// Audio buffer size
if raw := k.String("audio.buffer_size"); raw != "" {
var b Bytes
if err := b.UnmarshalText([]byte(raw)); err != nil {
return err
}
if int(b) < consts.AudioBufferBytes {
minVal, err := Bytes(consts.AudioBufferBytes).MarshalText()
if err != nil {
return err
}
slog.Warn("The minimum allowed buffer size is " + string(minVal) + ". Setting to default.")
if err := k.Set("audio.buffer_size", NewDefault().Audio.BufferSize); err != nil {
return err
}
}
}

return nil
}
15 changes: 9 additions & 6 deletions internal/consts/consts.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package consts

import "time"

const (
// PRGROMAddr is the memory address that PRG begins.
PRGROMAddr = 0x600
Expand All @@ -12,13 +14,14 @@ const (

CPUFrequency = 1789773

AudioSampleRate = 44100

FrameRate = 60

RealFrameRate = 60.0988118623484
AudioSampleRate = 44100
AudioBufferSize = time.Second / 20
AudioBytesPerSample = 4 * 2
AudioBufferBytes = int(AudioBufferSize * AudioBytesPerSample * AudioSampleRate / time.Second)

FrameRateDifference = FrameRate / RealFrameRate
TargetFrameRate = 60
HardwareFrameRate = 60.0988118623484
FrameRateDiff = TargetFrameRate / HardwareFrameRate

Width = 256
Height = 240
Expand Down

0 comments on commit 95893c4

Please sign in to comment.