summaryrefslogtreecommitdiff
path: root/src/tests/test_tracker.cc
blob: 7ef7172a7a400215a18bd795a6783dd3c6d183c5 (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
// This file is part of the 64k demo project.
// It tests the core functionality of the audio tracker engine.

#include "audio/gen.h"
#include "audio/synth.h"
#include "audio/tracker.h"
// #include "generated/music_data.h" // Will be generated by tracker_compiler
#include <assert.h>
#include <stdio.h>

// Forward declaration for generated data to avoid compilation issues before
// generation extern const NoteParams g_tracker_samples[]; extern const uint32_t
// g_tracker_samples_count; extern const TrackerPattern g_tracker_patterns[];
// extern const uint32_t g_tracker_patterns_count;
// extern const TrackerScore g_tracker_score;

void test_tracker_init() {
  synth_init();
  tracker_init();
  printf("Tracker init test PASSED\n");
}

void test_tracker_pattern_triggering() {
  synth_init();
  tracker_init();

  // At time 0.0f, 4 patterns are triggered:
  // - crash (1 event at beat 0.0)
  // - kick_basic (events at beat 0.0, 2.0, 2.5)
  // - snare_basic (events at beat 1.0, 3.0)
  // - hihat_stressed (events at beat 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5)
  // With the new event-based triggering, only events at beat 0.0 trigger immediately.

  // Test 1: At music_time = 0.0f, events at beat 0.0 trigger
  tracker_update(0.0f);
  printf("Actual active voice count at 0.0f: %d\n",
         synth_get_active_voice_count());
  // Expect 3 voices: crash (beat 0.0), kick_basic (beat 0.0), hihat_stressed
  // (beat 0.0)
  assert(synth_get_active_voice_count() == 3);

  // Test 2: At music_time = 0.25f (beat 0.5 @ 120 BPM), hihat event triggers
  // beat_duration = 60.0f / 120.0f = 0.5s per beat
  // beat 0.5 = 0.25s
  tracker_update(0.25f);
  printf("Actual active voice count at 0.25f: %d\n",
         synth_get_active_voice_count());
  // Expect 4 voices (3 previous + 1 new hihat)
  assert(synth_get_active_voice_count() == 4);

  // Test 3: At music_time = 0.5f (beat 1.0), snare event triggers
  tracker_update(0.5f);
  printf("Actual active voice count at 0.5f: %d\n",
         synth_get_active_voice_count());
  // Expect 6 voices (4 previous + snare + hihat at beat 1.0)
  assert(synth_get_active_voice_count() == 6);

  // Test 4: Advance further to 2.0f (beat 4.0), new pattern triggers at 2.0f
  tracker_update(2.0f);
  printf("Actual active voice count at 2.0f: %d\n",
         synth_get_active_voice_count());
  // Multiple events have triggered by now
  assert(synth_get_active_voice_count() > 0);

  printf("Tracker pattern triggering test PASSED\n");
}

int main() {
  printf("Running Tracker tests...\n");
  test_tracker_init();
  test_tracker_pattern_triggering();
  printf("Tracker tests PASSED\n");
  return 0;
}