From 70f8860a5e088bd31938d5d600d829ac0efb6cec Mon Sep 17 00:00:00 2001 From: Stevan Freeborn Date: Sat, 18 Jul 2026 15:16:59 -0500 Subject: [PATCH] feat(audio): add raylib-based sound buzzer emulation --- main.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index f793f3f..d88c1f5 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #define MEMORY_SIZE 4096 @@ -294,6 +295,29 @@ int main(int argc, char *argv[]) { return load_rom_result; } + // Setup audio + InitAudioDevice(); + + // Generate 440Hz Sine wave beep in memory + int sampleRate = 44100; + float durationSeconds = 0.1f; + int frameCount = sampleRate * durationSeconds; + float *data = (float *)malloc(frameCount * sizeof(float)); + if (data != NULL) { + for (int i = 0; i < frameCount; i++) { + data[i] = sinf(2.0f * PI * 440.0f * ((float)i / sampleRate)) * 0.2f; + } + } + Wave wave = { 0 }; + wave.frameCount = frameCount; + wave.sampleRate = sampleRate; + wave.sampleSize = 32; + wave.channels = 1; + wave.data = data; + + Sound beep_sound = LoadSoundFromWave(wave); + UnloadWave(wave); // Frees the 'data' buffer in system RAM + int scale_factor = 10; int display_width = SCREEN_WIDTH * scale_factor; int display_height = SCREEN_HEIGHT * scale_factor; @@ -498,9 +522,13 @@ int main(int argc, char *argv[]) { if (chip->st > 0) { chip->st--; - // TODO: Play sound + if (!IsSoundPlaying(beep_sound)) { + PlaySound(beep_sound); + } } else { - // TODO: Play sound + if (IsSoundPlaying(beep_sound)) { + StopSound(beep_sound); + } } BeginDrawing(); @@ -521,6 +549,9 @@ int main(int argc, char *argv[]) { free(chip); + UnloadSound(beep_sound); + CloseAudioDevice(); + CloseWindow(); return COMPLETED_SUCCESSFULLY;