make buffer bigger

This commit is contained in:
zbednarke 2024-05-07 20:36:38 -07:00
parent a7fccc35a1
commit 1a0035bfed

View file

@ -9,9 +9,9 @@ import (
) )
const ( const (
nSamples = 6000 nSamples = 44000 * 10
sampleRate = 6000 sampleRate = 44000
nSamplesPerUpdate = 1400 bufferSize = nSamples
frequency = 440 frequency = 440
targetFPS = 240 targetFPS = 240
) )
@ -19,6 +19,9 @@ const (
func main() { func main() {
rl.InitWindow(800, 450, "raylib [audio] example - raw audio streaming") rl.InitWindow(800, 450, "raylib [audio] example - raw audio streaming")
position := rl.NewVector2(0, 0) position := rl.NewVector2(0, 0)
rl.SetAudioStreamBufferSizeDefault(bufferSize)
rl.InitAudioDevice() rl.InitAudioDevice()
// Init raw audio stream (sample rate: <nSamples>, sample size: 32bit-float, channels: 1-mono) // Init raw audio stream (sample rate: <nSamples>, sample size: 32bit-float, channels: 1-mono)
@ -43,10 +46,10 @@ func main() {
if rl.IsAudioStreamProcessed(stream) { if rl.IsAudioStreamProcessed(stream) {
elapsedTime := time.Since(startTime).Seconds() elapsedTime := time.Since(startTime).Seconds()
currentSampleIndex := int(math.Mod(elapsedTime*float64(sampleRate), float64(nSamples))) currentSampleIndex := int(math.Mod(elapsedTime*float64(sampleRate), float64(nSamples)))
nextSampleIndex := currentSampleIndex + nSamplesPerUpdate nextSampleIndex := currentSampleIndex + bufferSize
if nextSampleIndex > nSamples { if nextSampleIndex > nSamples {
nextSampleIndex = nSamplesPerUpdate - (nSamples - currentSampleIndex) nextSampleIndex = bufferSize - (nSamples - currentSampleIndex)
rl.UpdateAudioStream(stream, append(data[currentSampleIndex:], data[:nextSampleIndex]...)) rl.UpdateAudioStream(stream, append(data[currentSampleIndex:], data[:nextSampleIndex]...))
} else { } else {
rl.UpdateAudioStream(stream, data[currentSampleIndex:nextSampleIndex]) rl.UpdateAudioStream(stream, data[currentSampleIndex:nextSampleIndex])