// This file is part of the 64k demo project. // It serves as the application entry point. // Orchestrates platform initialization, main loop, and subsystem coordination. #include "assets.h" // Include generated asset header #include "audio/audio.h" #include "audio/synth.h" #include "gpu/gpu.h" #include "platform.h" #include "util/math.h" #include #include #include #include #define DEMO_BPM 128.0f #define SECONDS_PER_BEAT (60.0f / DEMO_BPM) #define SPEC_FRAMES 16 struct SpecHeader { char magic[4]; int32_t version; int32_t dct_size; int32_t num_frames; }; int register_spec_asset(AssetId id) { size_t size; const uint8_t *data = GetAsset(id, &size); if (!data || size < sizeof(SpecHeader)) return -1; const SpecHeader *header = (const SpecHeader *)data; const float *spectral_data = (const float *)(data + sizeof(SpecHeader)); Spectrogram spec; spec.spectral_data_a = spectral_data; spec.spectral_data_b = spectral_data; // No double-buffer for static assets spec.num_frames = header->num_frames; return synth_register_spectrogram(&spec); } static float g_spec_buffer_a[SPEC_FRAMES * DCT_SIZE]; static float g_spec_buffer_b[SPEC_FRAMES * DCT_SIZE]; void generate_tone(float *buffer, float freq) { memset(buffer, 0, SPEC_FRAMES * DCT_SIZE * sizeof(float)); for (int frame = 0; frame < SPEC_FRAMES; ++frame) { float *spec_frame = buffer + frame * DCT_SIZE; float amplitude = powf(1.0f - (float)frame / SPEC_FRAMES, 2.0f); int bin = (int)(freq / (32000.0f / 2.0f) * DCT_SIZE); if (bin > 0 && bin < DCT_SIZE) { spec_frame[bin] = amplitude; } } } int main(int argc, char **argv) { bool fullscreen_enabled = false; #ifndef STRIP_ALL for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "--fullscreen") == 0) { fullscreen_enabled = true; break; } } #else (void)argc; (void)argv; fullscreen_enabled = true; #endif platform_init_window(fullscreen_enabled); gpu_init(platform_get_window()); audio_init(); // Register drum assets int kick_id = register_spec_asset(AssetId::ASSET_KICK_1); int snare_id = register_spec_asset(AssetId::ASSET_SNARE_1); int hihat_id = register_spec_asset(AssetId::ASSET_HIHAT_1); // Still keep the dynamic tone for bass generate_tone(g_spec_buffer_a, 110.0f); // A2 generate_tone(g_spec_buffer_b, 110.0f); const Spectrogram bass_spec = {g_spec_buffer_a, g_spec_buffer_b, SPEC_FRAMES}; int bass_id = synth_register_spectrogram(&bass_spec); double last_beat_time = 0.0; int beat_count = 0; while (!platform_should_close()) { platform_poll(); double current_time = platform_get_time(); if (current_time - last_beat_time > SECONDS_PER_BEAT / 2.0) { // 8th notes last_beat_time = current_time; const int step = beat_count % 16; // Kick on 1, 9, 11, 14... if (step == 0 || step == 8 || step == 10 || step == 13) { synth_trigger_voice(kick_id, 1.0f, 0.0f); } // Snare on 4, 12 if (step == 4 || step == 12) { synth_trigger_voice(snare_id, 0.8f, 0.0f); } // Hihat on every offbeat if (step % 2 == 1) { synth_trigger_voice(hihat_id, 0.5f, 0.3f); } // Bass pattern if (step % 4 == 0) { float bass_freq = (step < 8) ? 110.0f : 82.41f; // A2 then E2 float *back_buffer = synth_begin_update(bass_id); if (back_buffer) { generate_tone(back_buffer, bass_freq); synth_commit_update(bass_id); } synth_trigger_voice(bass_id, 0.6f, -0.2f); } beat_count++; } int width, height; glfwGetFramebufferSize(platform_get_window(), &width, &height); float aspect_ratio = (float)width / (float)height; // Adjusted multiplier for visuals (preventing constant 1.0 saturation) float raw_peak = synth_get_output_peak(); float visual_peak = fminf(raw_peak * 8.0f, 1.0f); gpu_draw(visual_peak, aspect_ratio, (float)current_time); audio_update(); } audio_shutdown(); gpu_shutdown(); platform_shutdown(); return 0; }