summaryrefslogtreecommitdiff
path: root/src/audio/synth.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio/synth.cc')
-rw-r--r--src/audio/synth.cc77
1 files changed, 67 insertions, 10 deletions
diff --git a/src/audio/synth.cc b/src/audio/synth.cc
index 67bc46e..1afb501 100644
--- a/src/audio/synth.cc
+++ b/src/audio/synth.cc
@@ -5,15 +5,16 @@
#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(STRIP_ALL)
+#if defined(DEBUG_LOG_SYNTH)
#include "audio/audio.h"
#include "audio/audio_backend.h"
-#endif /* !defined(STRIP_ALL) */
+#endif /* defined(DEBUG_LOG_SYNTH) */
struct Voice {
bool active;
@@ -27,6 +28,7 @@ struct Voice {
float time_domain_buffer[DCT_SIZE];
int buffer_pos;
+ float fractional_pos; // Fractional sample position for tempo scaling
const volatile float* active_spectral_data;
};
@@ -41,18 +43,19 @@ 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(STRIP_ALL)
+#if defined(DEBUG_LOG_SYNTH)
static float g_elapsed_time_sec = 0.0f; // Tracks elapsed time for event hooks
-#endif /* !defined(STRIP_ALL) */
+#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(STRIP_ALL)
+#if defined(DEBUG_LOG_SYNTH)
g_elapsed_time_sec = 0.0f;
-#endif /* !defined(STRIP_ALL) */
+#endif /* defined(DEBUG_LOG_SYNTH) */
// Initialize the Hamming window once
hamming_window_512(g_hamming_window);
}
@@ -61,7 +64,42 @@ 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;
@@ -118,9 +156,26 @@ void synth_commit_update(int spectrogram_id) {
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];
@@ -136,17 +191,18 @@ void synth_trigger_voice(int spectrogram_id, float volume, float pan) {
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(STRIP_ALL)
+#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(STRIP_ALL) */
+#endif /* defined(DEBUG_LOG_SYNTH) */
return; // Voice triggered
}
@@ -199,6 +255,7 @@ void synth_render(float* output_buffer, int num_frames) {
left_sample += voice_sample * v.pan_left;
right_sample += voice_sample * v.pan_right;
+ // Advance voice position
++v.buffer_pos;
}
@@ -210,11 +267,11 @@ void synth_render(float* output_buffer, int num_frames) {
g_current_output_peak, fmaxf(fabsf(left_sample), fabsf(right_sample)));
}
-#if !defined(STRIP_ALL)
+#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(STRIP_ALL) */
+#endif /* defined(DEBUG_LOG_SYNTH) */
}
int synth_get_active_voice_count() {