pretty it up

This commit is contained in:
Zachary Bednarke 2024-05-07 19:43:43 -07:00
parent 18035cee69
commit 95c2f5679a

View file

@ -18,13 +18,13 @@ 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)
rl.InitAudioDevice() rl.InitAudioDevice()
// Init raw audio stream (sample rate: <maxSamples>, sample size: 32bit-float, channels: 1-mono) // Init raw audio stream (sample rate: <maxSamples>, sample size: 32bit-float, channels: 1-mono)
stream := rl.LoadAudioStream(maxSamples, 32, 1) stream := rl.LoadAudioStream(maxSamples, 32, 1)
//// Fill create sine wave to play //// Create sine wave to play
data := make([]float32, maxSamples) data := make([]float32, maxSamples)
for i := 0; i < maxSamples; i++ { for i := 0; i < maxSamples; i++ {
@ -32,18 +32,13 @@ func main() {
data[i] = float32(math.Sin(float64((2 * rl.Pi * f * t)))) data[i] = float32(math.Sin(float64((2 * rl.Pi * f * t))))
} }
// NOTE: The buffer can only be updated when it has been processed, so there is clipping in the time between // NOTE: The buffer can only be updated when it has been processed. Time between buffer processing and next load and causes clipping
// buffer exhaustion and next load.
rl.PlayAudioStream(stream) rl.PlayAudioStream(stream)
position := rl.NewVector2(0, 0)
startTime := time.Now() startTime := time.Now()
rl.SetTargetFPS(targetFPS) rl.SetTargetFPS(targetFPS)
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
// Refill audio stream if buffer is processed // Refill audio stream if buffer is processed
if rl.IsAudioStreamProcessed(stream) { if rl.IsAudioStreamProcessed(stream) {
elapsedTime := time.Since(startTime).Seconds() elapsedTime := time.Since(startTime).Seconds()
@ -52,18 +47,13 @@ func main() {
if nextSampleIndex > maxSamples { if nextSampleIndex > maxSamples {
nextSampleIndex = maxSamplesPerUpdate - (maxSamples - currentSampleIndex) nextSampleIndex = maxSamplesPerUpdate - (maxSamples - currentSampleIndex)
samplesToWrite := append(data[currentSampleIndex:], data[:nextSampleIndex]...) rl.UpdateAudioStream(stream, append(data[currentSampleIndex:], data[:nextSampleIndex]...))
rl.UpdateAudioStream(stream, samplesToWrite)
} else { } else {
samplesToWrite := data[currentSampleIndex:nextSampleIndex] rl.UpdateAudioStream(stream, data[currentSampleIndex:nextSampleIndex])
rl.UpdateAudioStream(stream, samplesToWrite)
} }
fmt.Printf("Writing samples from %d to %d \n", currentSampleIndex, nextSampleIndex)
} }
rl.BeginDrawing() rl.BeginDrawing()
rl.ClearBackground(rl.RayWhite) rl.ClearBackground(rl.RayWhite)
rl.DrawText(fmt.Sprintf("%d Hz SINE WAVE SHOULD BE PLAYING!", f), 200, 140, 20, rl.LightGray) rl.DrawText(fmt.Sprintf("%d Hz SINE WAVE SHOULD BE PLAYING!", f), 200, 140, 20, rl.LightGray)