From 18035cee692609115ad33e3f1ba5c30356584211 Mon Sep 17 00:00:00 2001 From: Zachary Bednarke Date: Tue, 7 May 2024 19:37:13 -0700 Subject: [PATCH 1/8] first in fork --- examples/audio/raw_stream/main.go | 59 +++++++++++++++++-------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/examples/audio/raw_stream/main.go b/examples/audio/raw_stream/main.go index a0a952b..c970916 100644 --- a/examples/audio/raw_stream/main.go +++ b/examples/audio/raw_stream/main.go @@ -1,14 +1,19 @@ package main import ( + "fmt" "math" + "time" rl "github.com/gen2brain/raylib-go/raylib" ) const ( - maxSamples = 22050 - maxSamplesPerUpdate = 4096 + maxSamples = 6000 + sampleRate = 6000 + maxSamplesPerUpdate = 1600 + f = 440 + targetFPS = 240 ) func main() { @@ -16,56 +21,56 @@ func main() { rl.InitAudioDevice() - // Init raw audio stream (sample rate: 22050, sample size: 32bit-float, channels: 1-mono) - stream := rl.LoadAudioStream(22050, 32, 1) + // Init raw audio stream (sample rate: , sample size: 32bit-float, channels: 1-mono) + stream := rl.LoadAudioStream(maxSamples, 32, 1) - //// Fill audio stream with some samples (sine wave) + //// Fill create sine wave to play data := make([]float32, maxSamples) for i := 0; i < maxSamples; i++ { - data[i] = float32(math.Sin(float64((2*rl.Pi*float32(i))/2) * rl.Deg2rad)) + t := float32(i) / float32(maxSamples) + data[i] = float32(math.Sin(float64((2 * rl.Pi * f * t)))) } - // NOTE: The generated MAX_SAMPLES do not fit to close a perfect loop - // for that reason, there is a clip everytime audio stream is looped + // NOTE: The buffer can only be updated when it has been processed, so there is clipping in the time between + // buffer exhaustion and next load. rl.PlayAudioStream(stream) - totalSamples := int32(maxSamples) - samplesLeft := int32(totalSamples) - position := rl.NewVector2(0, 0) - rl.SetTargetFPS(30) + startTime := time.Now() + + rl.SetTargetFPS(targetFPS) for !rl.WindowShouldClose() { - // Refill audio stream if required + + // Refill audio stream if buffer is processed if rl.IsAudioStreamProcessed(stream) { - numSamples := int32(0) - if samplesLeft >= maxSamplesPerUpdate { - numSamples = maxSamplesPerUpdate + elapsedTime := time.Since(startTime).Seconds() + currentSampleIndex := int(math.Mod(elapsedTime*float64(sampleRate), float64(maxSamples))) + nextSampleIndex := currentSampleIndex + maxSamplesPerUpdate + + if nextSampleIndex > maxSamples { + nextSampleIndex = maxSamplesPerUpdate - (maxSamples - currentSampleIndex) + samplesToWrite := append(data[currentSampleIndex:], data[:nextSampleIndex]...) + rl.UpdateAudioStream(stream, samplesToWrite) } else { - numSamples = samplesLeft + samplesToWrite := data[currentSampleIndex:nextSampleIndex] + rl.UpdateAudioStream(stream, samplesToWrite) } - rl.UpdateAudioStream(stream, data[totalSamples-samplesLeft:]) - - samplesLeft -= numSamples - - // Reset samples feeding (loop audio) - if samplesLeft <= 0 { - samplesLeft = totalSamples - } + fmt.Printf("Writing samples from %d to %d \n", currentSampleIndex, nextSampleIndex) } rl.BeginDrawing() rl.ClearBackground(rl.RayWhite) - rl.DrawText("SINE WAVE SHOULD BE PLAYING!", 240, 140, 20, rl.LightGray) + rl.DrawText(fmt.Sprintf("%d Hz SINE WAVE SHOULD BE PLAYING!", f), 200, 140, 20, rl.LightGray) // NOTE: Draw a part of the sine wave (only screen width) for i := 0; i < int(rl.GetScreenWidth()); i++ { position.X = float32(i) - position.Y = 250 + 50*data[i] + position.Y = 250 + 10*data[i] rl.DrawPixelV(position, rl.Red) } From 95c2f5679acac0130b82cad8b2c1574b58fd7253 Mon Sep 17 00:00:00 2001 From: Zachary Bednarke Date: Tue, 7 May 2024 19:43:43 -0700 Subject: [PATCH 2/8] pretty it up --- examples/audio/raw_stream/main.go | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/examples/audio/raw_stream/main.go b/examples/audio/raw_stream/main.go index c970916..0c3552d 100644 --- a/examples/audio/raw_stream/main.go +++ b/examples/audio/raw_stream/main.go @@ -18,13 +18,13 @@ const ( func main() { rl.InitWindow(800, 450, "raylib [audio] example - raw audio streaming") - + position := rl.NewVector2(0, 0) rl.InitAudioDevice() // Init raw audio stream (sample rate: , sample size: 32bit-float, channels: 1-mono) stream := rl.LoadAudioStream(maxSamples, 32, 1) - //// Fill create sine wave to play + //// Create sine wave to play data := make([]float32, maxSamples) for i := 0; i < maxSamples; i++ { @@ -32,18 +32,13 @@ func main() { 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 - // buffer exhaustion and next load. + // NOTE: The buffer can only be updated when it has been processed. Time between buffer processing and next load and causes clipping rl.PlayAudioStream(stream) - position := rl.NewVector2(0, 0) - startTime := time.Now() - rl.SetTargetFPS(targetFPS) for !rl.WindowShouldClose() { - // Refill audio stream if buffer is processed if rl.IsAudioStreamProcessed(stream) { elapsedTime := time.Since(startTime).Seconds() @@ -52,18 +47,13 @@ func main() { if nextSampleIndex > maxSamples { nextSampleIndex = maxSamplesPerUpdate - (maxSamples - currentSampleIndex) - samplesToWrite := append(data[currentSampleIndex:], data[:nextSampleIndex]...) - rl.UpdateAudioStream(stream, samplesToWrite) + rl.UpdateAudioStream(stream, append(data[currentSampleIndex:], data[:nextSampleIndex]...)) } else { - samplesToWrite := data[currentSampleIndex:nextSampleIndex] - rl.UpdateAudioStream(stream, samplesToWrite) + rl.UpdateAudioStream(stream, data[currentSampleIndex:nextSampleIndex]) } - - fmt.Printf("Writing samples from %d to %d \n", currentSampleIndex, nextSampleIndex) } rl.BeginDrawing() - rl.ClearBackground(rl.RayWhite) rl.DrawText(fmt.Sprintf("%d Hz SINE WAVE SHOULD BE PLAYING!", f), 200, 140, 20, rl.LightGray) From 65392e706d2b03045ad1ef3fc3d1d40d80c63641 Mon Sep 17 00:00:00 2001 From: Zachary Bednarke Date: Tue, 7 May 2024 19:51:43 -0700 Subject: [PATCH 3/8] rename f t frequency --- examples/audio/raw_stream/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/audio/raw_stream/main.go b/examples/audio/raw_stream/main.go index 0c3552d..9b6a65f 100644 --- a/examples/audio/raw_stream/main.go +++ b/examples/audio/raw_stream/main.go @@ -12,7 +12,7 @@ const ( maxSamples = 6000 sampleRate = 6000 maxSamplesPerUpdate = 1600 - f = 440 + frequency = 440 targetFPS = 240 ) @@ -29,7 +29,7 @@ func main() { for i := 0; i < maxSamples; i++ { t := float32(i) / float32(maxSamples) - data[i] = float32(math.Sin(float64((2 * rl.Pi * f * t)))) + data[i] = float32(math.Sin(float64((2 * rl.Pi * frequency * t)))) } // NOTE: The buffer can only be updated when it has been processed. Time between buffer processing and next load and causes clipping @@ -55,7 +55,7 @@ func main() { rl.BeginDrawing() 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!", frequency), 200, 140, 20, rl.LightGray) // NOTE: Draw a part of the sine wave (only screen width) for i := 0; i < int(rl.GetScreenWidth()); i++ { From db4aa35b123c3dcf5ff1000362680c88e59397d6 Mon Sep 17 00:00:00 2001 From: Zachary Bednarke Date: Tue, 7 May 2024 19:54:07 -0700 Subject: [PATCH 4/8] rename variables --- examples/audio/raw_stream/main.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/audio/raw_stream/main.go b/examples/audio/raw_stream/main.go index 9b6a65f..4fb0a9a 100644 --- a/examples/audio/raw_stream/main.go +++ b/examples/audio/raw_stream/main.go @@ -9,11 +9,11 @@ import ( ) const ( - maxSamples = 6000 - sampleRate = 6000 - maxSamplesPerUpdate = 1600 - frequency = 440 - targetFPS = 240 + nSamples = 6000 + sampleRate = 6000 + nSamplesPerUpdate = 1600 + frequency = 440 + targetFPS = 240 ) func main() { @@ -21,14 +21,14 @@ func main() { position := rl.NewVector2(0, 0) rl.InitAudioDevice() - // Init raw audio stream (sample rate: , sample size: 32bit-float, channels: 1-mono) - stream := rl.LoadAudioStream(maxSamples, 32, 1) + // Init raw audio stream (sample rate: , sample size: 32bit-float, channels: 1-mono) + stream := rl.LoadAudioStream(nSamples, 32, 1) //// Create sine wave to play - data := make([]float32, maxSamples) + data := make([]float32, nSamples) - for i := 0; i < maxSamples; i++ { - t := float32(i) / float32(maxSamples) + for i := 0; i < nSamples; i++ { + t := float32(i) / float32(nSamples) data[i] = float32(math.Sin(float64((2 * rl.Pi * frequency * t)))) } @@ -42,11 +42,11 @@ func main() { // Refill audio stream if buffer is processed if rl.IsAudioStreamProcessed(stream) { elapsedTime := time.Since(startTime).Seconds() - currentSampleIndex := int(math.Mod(elapsedTime*float64(sampleRate), float64(maxSamples))) - nextSampleIndex := currentSampleIndex + maxSamplesPerUpdate + currentSampleIndex := int(math.Mod(elapsedTime*float64(sampleRate), float64(nSamples))) + nextSampleIndex := currentSampleIndex + nSamplesPerUpdate - if nextSampleIndex > maxSamples { - nextSampleIndex = maxSamplesPerUpdate - (maxSamples - currentSampleIndex) + if nextSampleIndex > nSamples { + nextSampleIndex = nSamplesPerUpdate - (nSamples - currentSampleIndex) rl.UpdateAudioStream(stream, append(data[currentSampleIndex:], data[:nextSampleIndex]...)) } else { rl.UpdateAudioStream(stream, data[currentSampleIndex:nextSampleIndex]) From a7fccc35a1dc25f76e52f3940d1bab0b4cc81f86 Mon Sep 17 00:00:00 2001 From: zbednarke Date: Tue, 7 May 2024 20:20:12 -0700 Subject: [PATCH 5/8] lower n samples per update to be on safe side --- examples/audio/raw_stream/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/audio/raw_stream/main.go b/examples/audio/raw_stream/main.go index 4fb0a9a..17c27c7 100644 --- a/examples/audio/raw_stream/main.go +++ b/examples/audio/raw_stream/main.go @@ -11,7 +11,7 @@ import ( const ( nSamples = 6000 sampleRate = 6000 - nSamplesPerUpdate = 1600 + nSamplesPerUpdate = 1400 frequency = 440 targetFPS = 240 ) From 1a0035bfeda77f0c635ab31037b4cb54048f4667 Mon Sep 17 00:00:00 2001 From: zbednarke Date: Tue, 7 May 2024 20:36:38 -0700 Subject: [PATCH 6/8] make buffer bigger --- examples/audio/raw_stream/main.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/audio/raw_stream/main.go b/examples/audio/raw_stream/main.go index 17c27c7..3f7cd82 100644 --- a/examples/audio/raw_stream/main.go +++ b/examples/audio/raw_stream/main.go @@ -9,16 +9,19 @@ import ( ) const ( - nSamples = 6000 - sampleRate = 6000 - nSamplesPerUpdate = 1400 - frequency = 440 - targetFPS = 240 + nSamples = 44000 * 10 + sampleRate = 44000 + bufferSize = nSamples + frequency = 440 + targetFPS = 240 ) func main() { rl.InitWindow(800, 450, "raylib [audio] example - raw audio streaming") position := rl.NewVector2(0, 0) + + rl.SetAudioStreamBufferSizeDefault(bufferSize) + rl.InitAudioDevice() // Init raw audio stream (sample rate: , sample size: 32bit-float, channels: 1-mono) @@ -43,10 +46,10 @@ func main() { if rl.IsAudioStreamProcessed(stream) { elapsedTime := time.Since(startTime).Seconds() currentSampleIndex := int(math.Mod(elapsedTime*float64(sampleRate), float64(nSamples))) - nextSampleIndex := currentSampleIndex + nSamplesPerUpdate + nextSampleIndex := currentSampleIndex + bufferSize if nextSampleIndex > nSamples { - nextSampleIndex = nSamplesPerUpdate - (nSamples - currentSampleIndex) + nextSampleIndex = bufferSize - (nSamples - currentSampleIndex) rl.UpdateAudioStream(stream, append(data[currentSampleIndex:], data[:nextSampleIndex]...)) } else { rl.UpdateAudioStream(stream, data[currentSampleIndex:nextSampleIndex]) From 74d0b22ad183036f89c66cee47633908bd0057eb Mon Sep 17 00:00:00 2001 From: zbednarke Date: Tue, 7 May 2024 20:38:52 -0700 Subject: [PATCH 7/8] lower sample rate --- examples/audio/raw_stream/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/audio/raw_stream/main.go b/examples/audio/raw_stream/main.go index 3f7cd82..05e653c 100644 --- a/examples/audio/raw_stream/main.go +++ b/examples/audio/raw_stream/main.go @@ -10,7 +10,7 @@ import ( const ( nSamples = 44000 * 10 - sampleRate = 44000 + sampleRate = 6000 bufferSize = nSamples frequency = 440 targetFPS = 240 From 266797ff04fe0aa7f239013a279f47ef3918a8cd Mon Sep 17 00:00:00 2001 From: zbednarke Date: Tue, 7 May 2024 20:42:31 -0700 Subject: [PATCH 8/8] sine graphic --- examples/audio/raw_stream/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/audio/raw_stream/main.go b/examples/audio/raw_stream/main.go index 05e653c..5a4f4bf 100644 --- a/examples/audio/raw_stream/main.go +++ b/examples/audio/raw_stream/main.go @@ -63,7 +63,7 @@ func main() { // NOTE: Draw a part of the sine wave (only screen width) for i := 0; i < int(rl.GetScreenWidth()); i++ { position.X = float32(i) - position.Y = 250 + 10*data[i] + position.Y = 250 + 10*data[i*nSamples/int(20*rl.GetScreenWidth())] rl.DrawPixelV(position, rl.Red) }