I am attempting to play a "easy" generated looped sinusoidal wave with raylib and Go however, though it performs I additionally get audible cracks.
Checking some on-line examples (like this: https://www.youtube.com/watch?v=p1VQuMziTek), I see {that a} frequent drawback is likely to be that the generated wave doesn’t sew completely on the extremes and might trigger these glitches, however making use of the identical repair as within the video (monitoring the part) appears to not work for me.
May it’s the buffer will get empty for too lengthy earlier than I can fill it once more? How can I debug this?
Right here is an remoted reproducible instance:
bundle foremost
import (
rl "github.com/gen2brain/raylib-go/raylib"
"math"
)
const sampleRate = 44100
const streamBufferSize = 1024
sort Oscillator struct {
part float64
phaseStride float64
}
func (o *Oscillator) advance() {
o.part += o.phaseStride
if o.part >= 1 {
o.part -= 1
}
}
func NewOscillator(frequency float32, sampleDuration float32) *Oscillator {
return &Oscillator{
part: 0,
phaseStride: float64(frequency * sampleDuration),
}
}
func foremost() {
rl.InitAudioDevice()
stream := rl.LoadAudioStream(
uint32(sampleRate),
32,
1,
)
defer rl.UnloadAudioStream(stream)
defer rl.CloseAudioDevice()
rl.SetAudioStreamVolume(stream, 0.10)
rl.SetAudioStreamBufferSizeDefault(streamBufferSize)
rl.PlayAudioStream(stream)
samples := make([]float32, streamBufferSize)
sampleDuration := float32(1) / sampleRate
oscillator := NewOscillator(440, sampleDuration)
for {
if rl.IsAudioStreamProcessed(stream) {
updateSamples(samples, oscillator)
rl.UpdateAudioStream(
stream,
samples,
streamBufferSize,
)
}
}
}
func updateSamples(samples []float32, oscillator *Oscillator) {
for t := 0; t < streamBufferSize; t++ {
oscillator.advance()
x := 2 * math.Pi * oscillator.part
samples[t] = float32(math.Sin(float64(x)))
}
}