diff --git a/src/audio.c b/src/audio.c index 741136b..c6bcefd 100644 --- a/src/audio.c +++ b/src/audio.c @@ -1,9 +1,10 @@ #include "audio.h" #include "raylib.h" #include +#include #ifndef M_PI -#define M_PI 3.14159265358979323846 // xd +#define M_PI 3.14159265358979323846 // xd #endif #define SAMPLE_RATE 44100 @@ -11,11 +12,17 @@ // Generate a simple sine wave tone static void play_tone(float frequency, float duration, float volume) { - static float samples[SAMPLE_RATE]; int sample_count = (int)(SAMPLE_RATE * duration); if (sample_count > SAMPLE_RATE) sample_count = SAMPLE_RATE; + if (sample_count <= 0) + return; + + // Allocate samples dynamically to avoid shared static buffer corruption + float *samples = (float *)MemAlloc(sample_count * sizeof(float)); + if (samples == NULL) + return; // Generate sine wave for (int i = 0; i < sample_count; i++) { @@ -24,7 +31,7 @@ static void play_tone(float frequency, float duration, float volume) { // Apply simple envelope (fade in/out) float envelope = 1.0f; - int fade_samples = SAMPLE_RATE / 20; // 50ms fade + int fade_samples = SAMPLE_RATE / 20; // 50ms fade if (i < fade_samples) { envelope = (float)i / fade_samples; } else if (i > sample_count - fade_samples) { @@ -43,6 +50,9 @@ static void play_tone(float frequency, float duration, float volume) { Sound sound = LoadSoundFromWave(wave); PlaySound(sound); UnloadSound(sound); + + // Free the dynamically allocated buffer + MemFree(samples); } void audio_init(void) {