summaryrefslogtreecommitdiff
path: root/src/audio/tracker.cc
blob: 9f9263dac046018502722271e35efebbefcfaca2 (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
// This file is part of the 64k demo project.
// It implements the tracker runtime logic.

#include "tracker.h"
#include "audio/synth.h"
#include <cstring>
#include <vector>

static uint32_t g_last_trigger_idx = 0;

struct ManagedSpectrogram {
  int synth_id;
  float* data;
  bool active;
};

// Simple pool for dynamic spectrograms
static ManagedSpectrogram g_spec_pool[MAX_SPECTROGRAMS];

void tracker_init() {
  g_last_trigger_idx = 0;
  for (int i = 0; i < MAX_SPECTROGRAMS; ++i) {
    g_spec_pool[i].synth_id = -1;
    g_spec_pool[i].data = nullptr;
    g_spec_pool[i].active = false;
  }
}

static int get_free_pool_slot() {
  for (int i = 0; i < MAX_SPECTROGRAMS; ++i) {
    if (!g_spec_pool[i].active)
      return i;
  }
  // If no free slot, find one where the synth voice is inactive
  // (In a real implementation, we'd check if any voice is still using this)
  // For now, just wrap around or return -1
  return -1;
}

void tracker_update(float time_sec) {
  while (g_last_trigger_idx < g_tracker_score.num_triggers) {
    const TrackerPatternTrigger& trigger =
        g_tracker_score.triggers[g_last_trigger_idx];

    if (trigger.time_sec > time_sec)
      break;

    // Trigger pattern!
    const TrackerPattern& pattern = g_tracker_patterns[trigger.pattern_id];

    // Generate spectrogram for the pattern
    int dest_num_frames = 0;
    std::vector<float> pattern_data;

    float beat_to_sec = 60.0f / g_tracker_score.bpm;

    for (uint32_t i = 0; i < pattern.num_events; ++i) {
      const TrackerEvent& event = pattern.events[i];
      const NoteParams& params = g_tracker_samples[event.sample_id];

      int note_frames = 0;
      std::vector<float> note_data =
          generate_note_spectrogram(params, &note_frames);

      int frame_offset = (int)(event.beat * beat_to_sec * 32000.0f / DCT_SIZE);
      paste_spectrogram(pattern_data, &dest_num_frames, note_data, note_frames,
                        frame_offset);
    }

    // Register with synth
    int slot = get_free_pool_slot();
    if (slot != -1) {
      // Clean up old if needed
      if (g_spec_pool[slot].synth_id != -1) {
        synth_unregister_spectrogram(g_spec_pool[slot].synth_id);
        delete[] g_spec_pool[slot].data;
      }

      g_spec_pool[slot].data = new float[pattern_data.size()];
      memcpy(g_spec_pool[slot].data, pattern_data.data(),
             pattern_data.size() * sizeof(float));

      Spectrogram spec;
      spec.spectral_data_a = g_spec_pool[slot].data;
      spec.spectral_data_b = g_spec_pool[slot].data;
      spec.num_frames = dest_num_frames;

      g_spec_pool[slot].synth_id = synth_register_spectrogram(&spec);
      g_spec_pool[slot].active = true;

      synth_trigger_voice(g_spec_pool[slot].synth_id, 1.0f, 0.0f);
    }

    g_last_trigger_idx++;
  }
}