diff options
| -rw-r--r-- | src/Main.c | 24 |
1 files changed, 21 insertions, 3 deletions
@@ -10,7 +10,25 @@ SDL_AtomicInt is_speaking; SDL_AtomicInt app_running; -float shared_rms = 0.0f; +SDL_AtomicInt shared_rms_bits; + +static inline void atomic_store_float(SDL_AtomicInt *a, float f) { + union { + float f; + int i; + } u; + u.f = f; + SDL_SetAtomicInt(a, u.i); +} + +static inline float atomic_load_float(SDL_AtomicInt *a) { + union { + float f; + int i; + } u; + u.i = SDL_GetAtomicInt(a); + return u.f; +} int SDLCALL audio_thread_func(void *data) { (void)data; @@ -34,7 +52,7 @@ int SDLCALL audio_thread_func(void *data) { for (int i = 0; i < BUFFER_SIZE; i++) sum_squares += (double)buffer[i] * buffer[i]; float rms = (float)sqrt(sum_squares / BUFFER_SIZE); - shared_rms = rms; + atomic_store_float(&shared_rms_bits, rms); SDL_SetAtomicInt(&is_speaking, rms > VOLUME_THRESHOLD ? 1 : 0); } pa_simple_free(s); @@ -61,7 +79,7 @@ int main(void) { if (event.type == SDL_EVENT_QUIT) SDL_SetAtomicInt(&app_running, 0); } - float target_rms = shared_rms; + float target_rms = atomic_load_float(&shared_rms_bits); if (target_rms < VOLUME_THRESHOLD) target_rms = 0.0f; visual_rms += (target_rms - visual_rms) * LERP_SPEED; |
