Monday, September 5, 2022
HomeGame DevelopmentRaylib: audio popping and cracking

Raylib: audio popping and cracking


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} widespread downside is perhaps 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.

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 principal

import (
    rl "github.com/gen2brain/raylib-go/raylib"
    "math"
)

const sampleRate = 44100
const streamBufferSize = 1024

sort 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 principal() {
    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)))
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments