summaryrefslogtreecommitdiff
path: root/src/audio/synth.cc
blob: 1afb50128741e00bed45044beafa94503a6bce6d (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// This file is part of the 64k demo project.
// It implements the multi-voice additive synthesis engine.
// Supports real-time spectrogram updates and peak detection.

#include "synth.h"
#include "audio/dct.h"
#include "audio/window.h"
#include "util/debug.h"
#include <atomic>
#include <math.h>
#include <stdio.h>  // For printf
#include <string.h> // For memset

#if defined(DEBUG_LOG_SYNTH)
#include "audio/audio.h"
#include "audio/audio_backend.h"
#endif /* defined(DEBUG_LOG_SYNTH) */

struct Voice {
  bool active;
  int spectrogram_id;
  float volume;
  float pan_left;
  float pan_right;

  int current_spectral_frame;
  int total_spectral_frames;

  float time_domain_buffer[DCT_SIZE];
  int buffer_pos;
  float fractional_pos;  // Fractional sample position for tempo scaling

  const volatile float* active_spectral_data;
};

static struct {
  Spectrogram spectrograms[MAX_SPECTROGRAMS];
  const volatile float* active_spectrogram_data[MAX_SPECTROGRAMS];
  bool spectrogram_registered[MAX_SPECTROGRAMS];
} g_synth_data;

static Voice g_voices[MAX_VOICES];
static volatile float g_current_output_peak =
    0.0f;                                   // Global peak for visualization
static float g_hamming_window[WINDOW_SIZE]; // Static window for optimization
static float g_tempo_scale = 1.0f;          // Playback speed multiplier

#if defined(DEBUG_LOG_SYNTH)
static float g_elapsed_time_sec = 0.0f; // Tracks elapsed time for event hooks
#endif /* defined(DEBUG_LOG_SYNTH) */

void synth_init() {
  memset(&g_synth_data, 0, sizeof(g_synth_data));
  memset(g_voices, 0, sizeof(g_voices));
  g_current_output_peak = 0.0f;
#if defined(DEBUG_LOG_SYNTH)
  g_elapsed_time_sec = 0.0f;
#endif /* defined(DEBUG_LOG_SYNTH) */
  // Initialize the Hamming window once
  hamming_window_512(g_hamming_window);
}

void synth_shutdown() {
  // Nothing to do here since we are not allocating memory
}

void synth_set_tempo_scale(float tempo_scale) {
  g_tempo_scale = tempo_scale;
}

int synth_register_spectrogram(const Spectrogram* spec) {
#if defined(DEBUG_LOG_SYNTH)
  // VALIDATION: Check spectrogram pointer and data
  if (spec == nullptr) {
    DEBUG_SYNTH( "[SYNTH ERROR] Null spectrogram pointer\n");
    return -1;
  }
  if (spec->spectral_data_a == nullptr || spec->spectral_data_b == nullptr) {
    DEBUG_SYNTH( "[SYNTH ERROR] Null spectral data pointers\n");
    return -1;
  }
  if (spec->num_frames <= 0 || spec->num_frames > 10000) {
    DEBUG_SYNTH( "[SYNTH ERROR] Invalid num_frames=%d (must be 1-10000)\n",
            spec->num_frames);
    return -1;
  }
  // VALIDATION: Check spectral data isn't all zeros (common corruption symptom)
  bool all_zero = true;
  const float* data = spec->spectral_data_a;
  const int samples_to_check = (spec->num_frames > 10) ? 10 * DCT_SIZE : spec->num_frames * DCT_SIZE;
  for (int j = 0; j < samples_to_check; ++j) {
    if (data[j] != 0.0f) {
      all_zero = false;
      break;
    }
  }
  if (all_zero) {
    DEBUG_SYNTH( "[SYNTH WARNING] Spectrogram appears to be all zeros (num_frames=%d)\n",
            spec->num_frames);
  }
#endif

  for (int i = 0; i < MAX_SPECTROGRAMS; ++i) {
    if (!g_synth_data.spectrogram_registered[i]) {
      g_synth_data.spectrograms[i] = *spec;
      g_synth_data.active_spectrogram_data[i] = spec->spectral_data_a;
      g_synth_data.spectrogram_registered[i] = true;
      return i;
    }
  }
  return -1; // No free slots
}

void synth_unregister_spectrogram(int spectrogram_id) {
  if (spectrogram_id >= 0 && spectrogram_id < MAX_SPECTROGRAMS) {
    g_synth_data.spectrogram_registered[spectrogram_id] = false;
  }
}

float* synth_begin_update(int spectrogram_id) {
  if (spectrogram_id < 0 || spectrogram_id >= MAX_SPECTROGRAMS ||
      !g_synth_data.spectrogram_registered[spectrogram_id]) {
    return nullptr;
  }

  const volatile float* active_ptr =
      g_synth_data.active_spectrogram_data[spectrogram_id];

  if (active_ptr == g_synth_data.spectrograms[spectrogram_id].spectral_data_a) {
    return (float*)(g_synth_data.spectrograms[spectrogram_id].spectral_data_b);
  } else {
    return (float*)(g_synth_data.spectrograms[spectrogram_id].spectral_data_a);
  }
}

void synth_commit_update(int spectrogram_id) {
  if (spectrogram_id < 0 || spectrogram_id >= MAX_SPECTROGRAMS ||
      !g_synth_data.spectrogram_registered[spectrogram_id]) {
    return;
  }

  const volatile float* old_active_ptr =
      g_synth_data.active_spectrogram_data[spectrogram_id];
  const float* new_active_ptr =
      (old_active_ptr ==
       g_synth_data.spectrograms[spectrogram_id].spectral_data_a)
          ? g_synth_data.spectrograms[spectrogram_id].spectral_data_b
          : g_synth_data.spectrograms[spectrogram_id].spectral_data_a;

  // Atomic swap using GCC/Clang builtins for thread safety
  __atomic_store_n(
      (const float**)&g_synth_data.active_spectrogram_data[spectrogram_id],
      new_active_ptr, __ATOMIC_RELEASE);
}

void synth_trigger_voice(int spectrogram_id, float volume, float pan) {
  if (spectrogram_id < 0 || spectrogram_id >= MAX_SPECTROGRAMS ||
      !g_synth_data.spectrogram_registered[spectrogram_id]) {
#if defined(DEBUG_LOG_SYNTH)
    DEBUG_SYNTH( "[SYNTH ERROR] Invalid spectrogram_id=%d in trigger_voice\n",
            spectrogram_id);
#endif
    return;
  }

#if defined(DEBUG_LOG_SYNTH)
  // VALIDATION: Check volume and pan ranges
  if (volume < 0.0f || volume > 2.0f) {
    DEBUG_SYNTH( "[SYNTH WARNING] Unusual volume=%.2f for spectrogram_id=%d\n",
            volume, spectrogram_id);
  }
  if (pan < -1.0f || pan > 1.0f) {
    DEBUG_SYNTH( "[SYNTH WARNING] Invalid pan=%.2f (clamping) for spectrogram_id=%d\n",
            pan, spectrogram_id);
    pan = (pan < -1.0f) ? -1.0f : 1.0f;
  }
#endif

  for (int i = 0; i < MAX_VOICES; ++i) {
    if (!g_voices[i].active) {
      Voice& v = g_voices[i];
      v.active = true;
      v.spectrogram_id = spectrogram_id;
      v.volume = volume;

      // Simple linear panning
      v.pan_left = (pan > 0.0f) ? (1.0f - pan) : 1.0f;
      v.pan_right = (pan < 0.0f) ? (1.0f + pan) : 1.0f;

      v.current_spectral_frame = 0;
      v.total_spectral_frames =
          g_synth_data.spectrograms[spectrogram_id].num_frames;
      v.buffer_pos = DCT_SIZE; // Force IDCT on first render
      v.fractional_pos = 0.0f; // Initialize fractional position for tempo scaling
      v.active_spectral_data =
          g_synth_data.active_spectrogram_data[spectrogram_id];

#if defined(DEBUG_LOG_SYNTH)
      // Notify backend of voice trigger event (for testing/tracking)
      AudioBackend* backend = audio_get_backend();
      if (backend != nullptr) {
        backend->on_voice_triggered(g_elapsed_time_sec, spectrogram_id, volume,
                                     pan);
      }
#endif /* defined(DEBUG_LOG_SYNTH) */

      return; // Voice triggered
    }
  }
}

void synth_render(float* output_buffer, int num_frames) {
  // Use the pre-calculated window
  // float window[WINDOW_SIZE];
  // hamming_window_512(window);

  // Faster decay for more responsive visuals
  g_current_output_peak *= 0.90f;

  for (int i = 0; i < num_frames; ++i) {
    float left_sample = 0.0f;
    float right_sample = 0.0f;

    for (int v_idx = 0; v_idx < MAX_VOICES; ++v_idx) {
      Voice& v = g_voices[v_idx];
      if (!v.active)
        continue;

      if (v.buffer_pos >= DCT_SIZE) {
        if (v.current_spectral_frame >= v.total_spectral_frames) {
          v.active = false;
          continue;
        }

        // Fetch the latest active spectrogram pointer for this voice
        v.active_spectral_data =
            g_synth_data.active_spectrogram_data[v.spectrogram_id];

        const float* spectral_frame = (const float*)v.active_spectral_data +
                                      (v.current_spectral_frame * DCT_SIZE);

        float windowed_frame[DCT_SIZE];
        for (int j = 0; j < DCT_SIZE; ++j) {
          windowed_frame[j] =
              spectral_frame[j] * g_hamming_window[j]; // Use static window
        }

        idct_512(windowed_frame, v.time_domain_buffer);

        v.buffer_pos = 0;
        ++v.current_spectral_frame;
      }

      float voice_sample = v.time_domain_buffer[v.buffer_pos] * v.volume;
      left_sample += voice_sample * v.pan_left;
      right_sample += voice_sample * v.pan_right;

      // Advance voice position
      ++v.buffer_pos;
    }

    output_buffer[i * 2] = left_sample;
    output_buffer[i * 2 + 1] = right_sample;

    // Update the peak with the new max (attack)
    g_current_output_peak = fmaxf(
        g_current_output_peak, fmaxf(fabsf(left_sample), fabsf(right_sample)));
  }

#if defined(DEBUG_LOG_SYNTH)
  // Update elapsed time for event tracking (32000 Hz sample rate)
  const float sample_rate = 32000.0f;
  g_elapsed_time_sec += (float)num_frames / sample_rate;
#endif /* defined(DEBUG_LOG_SYNTH) */
}

int synth_get_active_voice_count() {
  int count = 0;
  for (int i = 0; i < MAX_VOICES; ++i) {
    if (g_voices[i].active) {
      ++count;
    }
  }
  return count;
}

float synth_get_output_peak() {
  return g_current_output_peak;
}