[ad_1]
I’m making an attempt to play a "simple" 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} widespread drawback is likely to be that the generated wave doesn’t sew completely on the extremes and may trigger these glitches, however making use of the identical repair as within the video (monitoring the section) appears to not work for me.
Could or not it’s the buffer will get empty for too lengthy earlier than I’m in a position to fill it once more? How can I debug this?
Here is an remoted reproducible instance:
bundle fundamental
import (
rl "github.com/gen2brain/raylib-go/raylib"
"math"
)
const sampleRate = 44100
const streamBufferSize = 1024
kind Oscillator struct {
section float64
phaseStride float64
}
func (o *Oscillator) advance() {
o.section += o.phaseStride
if o.section >= 1 {
o.section -= 1
}
}
func NewOscillator(frequency float32, sampleDuration float32) *Oscillator {
return &Oscillator{
section: 0,
phaseStride: float64(frequency * sampleDuration),
}
}
func fundamental() {
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.section
samples[t] = float32(math.Sin(float64(x)))
}
}
[ad_2]