summaryrefslogtreecommitdiff
path: root/src/tests/test_synth.cc
blob: c172da120317b4ed8df2df464afceb56d7cf7fbe (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
106
107
108
109
110
111
// This file is part of the 64k demo project.
// It tests the core functionality of the audio synthesis engine.
// Verifies voice triggering, registration, and rendering state.

#include "audio/synth.h"
#include <assert.h>
#include <stdio.h>
#include <cmath>

void test_registration() {
  synth_init();
  float data[DCT_SIZE * 2] = {0};
  Spectrogram spec = {data, data, 2};

  int id = synth_register_spectrogram(&spec);
  assert(id >= 0);
  assert(synth_get_active_voice_count() == 0);
  
  synth_unregister_spectrogram(id);
  // Re-register to check slot reuse
  int id2 = synth_register_spectrogram(&spec);
  assert(id2 == id); // Should reuse the slot 0
}

void test_trigger() {
  synth_init();
  float data[DCT_SIZE * 2] = {0};
  Spectrogram spec = {data, data, 2};
  int id = synth_register_spectrogram(&spec);

  synth_trigger_voice(id, 1.0f, 0.0f);
  assert(synth_get_active_voice_count() == 1);
}

void test_render() {
  synth_init();
  float data[DCT_SIZE * 2] = {0};
  // Put some signal in (DC component)
  data[0] = 100.0f; 

  Spectrogram spec = {data, data, 2};
  int id = synth_register_spectrogram(&spec);
  
  synth_trigger_voice(id, 1.0f, 0.0f);
  
  float output[1024] = {0}; 
  synth_render(output, 256);
  
  // Verify output is not all zero (IDCT of DC component should be constant)
  bool non_zero = false;
  for(int i=0; i<256; ++i) {
      if(std::abs(output[i]) > 1e-6f) non_zero = true;
  }
  assert(non_zero);
  
  // Test render with no voices
  synth_init(); // Reset
  float output2[1024] = {0};
  synth_render(output2, 256);
  for(int i=0; i<256; ++i) assert(output2[i] == 0.0f);
}

void test_update() {
    synth_init();
    float data[DCT_SIZE * 2] = {0};
    Spectrogram spec = {data, data, 2};
    int id = synth_register_spectrogram(&spec);
    
    float* back_buf = synth_begin_update(id);
    assert(back_buf != nullptr);
    // Write something
    back_buf[0] = 50.0f;
    synth_commit_update(id);
    
    // Test invalid ID
    assert(synth_begin_update(-1) == nullptr);
    synth_commit_update(-1); // Should not crash
}

void test_exhaustion() {
    synth_init();
    float data[DCT_SIZE * 2] = {0};
    Spectrogram spec = {data, data, 2};
    
    for(int i=0; i<MAX_SPECTROGRAMS; ++i) {
        int id = synth_register_spectrogram(&spec);
        assert(id >= 0);
    }
    // Next one should fail
    int id = synth_register_spectrogram(&spec);
    assert(id == -1);
}

void test_peak() {
    // Already called render in test_render.
    // Just call the getter.
    float peak = synth_get_output_peak();
    assert(peak >= 0.0f);
}

int main() {
  printf("Running SynthEngine tests...\n");
  test_registration();
  test_trigger();
  test_render();
  test_update();
  test_exhaustion();
  test_peak();
  printf("SynthEngine tests PASSED\n");
  return 0;
}