summaryrefslogtreecommitdiff
path: root/src/main.cc
blob: 6428ed2a4d355f93ac8b31a6eb9c8c4df1720716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// 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 <GLFW/glfw3.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

#define DEMO_BPM 120.0f
#define SECONDS_PER_BEAT (60.0f / DEMO_BPM)
#define SPEC_FRAMES 16

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();

  generate_tone(g_spec_buffer_a, 440.0f); // A4
  generate_tone(g_spec_buffer_b, 0.0f);   // A5

  const Spectrogram spec = {g_spec_buffer_a, g_spec_buffer_b, SPEC_FRAMES};
  int tone_id = synth_register_spectrogram(&spec);

  // Dummy call to ensure asset system is linked
  size_t dummy_size;
  const uint8_t *dummy_asset = GetAsset(AssetId::ASSET_NULL_ASSET, &dummy_size);
  (void)dummy_asset;
  (void)dummy_size;

  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) {
      const float pan = (beat_count & 1) ? -.8 : .8;
      synth_trigger_voice(tone_id, 0.8f, pan);
      last_beat_time = current_time;
      beat_count++;

      if (beat_count % 4 == 0) {
        // Time to update the sound!
        float *back_buffer = synth_begin_update(tone_id);
        if (back_buffer) {
          generate_tone(back_buffer,
                        (beat_count % 8) == 4 ? 220.0f : 480.f); // A3
          synth_commit_update(tone_id);
        }
      }
    }

    int width, height;
    glfwGetFramebufferSize(platform_get_window(), &width, &height);
    float aspect_ratio = (float)width / (float)height;

    float visual_peak = fminf(synth_get_output_peak() * 80.0f, 1.0f);
    gpu_draw(visual_peak, aspect_ratio);
    audio_update();
  }

  audio_shutdown();
  gpu_shutdown();
  platform_shutdown();
  return 0;
}