Home Game Development Raylib: audio popping and cracking

Raylib: audio popping and cracking

0
Raylib: audio popping and cracking

[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} frequent drawback could 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 part) appears to not work for me.

Could it’s the buffer will get empty for too lengthy earlier than I’m capable of fill it once more? How can I debug this?

Here is an remoted reproducible instance:

bundle essential

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 essential() {
    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)))
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here