summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-04 19:40:40 +0100
committerskal <pascal.massimino@gmail.com>2026-02-04 19:40:40 +0100
commit91933ce05ba157dc549d52ed6c00c71c457fca05 (patch)
treee01dedaa82a7ab5aad887c5eac1008172b0fe82b /tools
parenteb5dee66385760953f3b00706318fe0e4ce90b0e (diff)
feat: Audio playback stability, NOTE_ parsing fix, sample caching, and debug logging infrastructure
MILESTONE: Audio System Robustness & Debugging Core Audio Backend Optimization: - Fixed stop-and-go audio glitches caused by timing mismatch - Core Audio optimized for 44.1kHz (10ms periods), but 32kHz expected ~13.78ms - Added allowNominalSampleRateChange=TRUE to force OS-level 32kHz native - Added performanceProfile=conservative for 4096-frame buffers (128ms) - Result: Stable ~128ms callbacks, <1ms jitter, zero underruns Ring Buffer Improvements: - Increased capacity from 200ms to 400ms for tempo scaling headroom - Added comprehensive bounds checking with abort() on violations - Fixed tempo-scaled buffer fill: dt * g_tempo_scale - Buffer maintains 400ms fullness during 2.0x acceleration NOTE_ Parsing Fix & Sample Caching: - Fixed is_note_name() checking only first letter (A-G) - ASSET_KICK_1 was misidentified as A0 (27.5 Hz) - Required "NOTE_" prefix to distinguish notes from assets - Updated music.track to use NOTE_E2, NOTE_G4 format - Discovered resource exhaustion: 14 unique samples → 228 registrations - Implemented comprehensive caching in tracker_init() - Assets: loaded once from AssetManager, cached synth_id - Generated notes: created once, stored in persistent pool - Result: MAX_SPECTROGRAMS 256 → 32 (88% memory reduction) Debug Logging Infrastructure: - Created src/util/debug.h with 7 category macros (AUDIO, RING_BUFFER, TRACKER, SYNTH, 3D, ASSETS, GPU) - Added DEMO_ENABLE_DEBUG_LOGS CMake option (defines DEBUG_LOG_ALL) - Converted all diagnostic code to use category macros - Default build: macros compile to ((void)0) for zero runtime cost - Debug build: comprehensive logging for troubleshooting - Updated CONTRIBUTING.md with pre-commit policy Resource Analysis Tool: - Enhanced tracker_compiler to report pool sizes and cache potential - Analysis: 152/228 spectrograms without caching, 14 with caching - Tool generates optimization recommendations during compilation Files Changed: - CMakeLists.txt: Add DEBUG_LOG option - src/util/debug.h: New debug logging header (7 categories) - src/audio/miniaudio_backend.cc: Use DEBUG_AUDIO/DEBUG_RING_BUFFER - src/audio/ring_buffer.cc: Use DEBUG_RING_BUFFER for underruns - src/audio/tracker.cc: Implement sample caching, use DEBUG_TRACKER - src/audio/synth.cc: Use DEBUG_SYNTH for validation - src/audio/synth.h: Update MAX_SPECTROGRAMS (256→32), document caching - tools/tracker_compiler.cc: Fix is_note_name(), add resource analysis - assets/music.track: Update to use NOTE_ prefix format - doc/CONTRIBUTING.md: Add debug logging pre-commit policy - PROJECT_CONTEXT.md: Document milestone - TODO.md: Mark tasks completed Verification: - Default build: No debug output, audio plays correctly - Debug build: Comprehensive logging, audio plays correctly - Caching working: 14 unique samples cached at init - All tests passing (17/17) handoff(Claude): Audio system now stable with robust diagnostic infrastructure. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/tracker_compiler.cc97
1 files changed, 77 insertions, 20 deletions
diff --git a/tools/tracker_compiler.cc b/tools/tracker_compiler.cc
index 5669cf9..81d7913 100644
--- a/tools/tracker_compiler.cc
+++ b/tools/tracker_compiler.cc
@@ -13,13 +13,14 @@ enum SampleType {
ASSET
};
-// Convert note name (e.g., "C4", "A#3", "Eb2") to frequency in Hz
+// Convert note name (e.g., "NOTE_C4", "NOTE_A#3", "NOTE_Eb2") to frequency in Hz
+// CRITICAL: Now requires "NOTE_" prefix (changed to prevent ASSET_* confusion)
static float note_name_to_freq(const std::string& note_name) {
- if (note_name.empty())
+ if (note_name.size() < 7) // "NOTE_" + note + octave minimum
return 0.0f;
- // Parse note (C, C#, D, etc.) and octave
- const char* str = note_name.c_str();
+ // Skip "NOTE_" prefix (5 characters) to get to the actual note
+ const char* str = note_name.c_str() + 5;
char note_char = str[0];
int semitone = 0;
@@ -73,10 +74,16 @@ static float note_name_to_freq(const std::string& note_name) {
}
static bool is_note_name(const std::string& name) {
- if (name.empty())
+ // CRITICAL FIX: Require "NOTE_" prefix to avoid false positives with ASSET_*
+ // Valid: NOTE_E2, NOTE_A4, NOTE_C#3, NOTE_Bb5
+ // Invalid: ASSET_KICK_1, E2 (no prefix), etc.
+ if (name.size() < 7) // "NOTE_" + note + octave = minimum 7 chars (e.g. "NOTE_C4")
return false;
- const char first = name[0];
- return (first >= 'A' && first <= 'G');
+ if (name.substr(0, 5) != "NOTE_")
+ return false;
+ // Check that the 6th character (after "NOTE_") is a valid note letter A-G
+ const char note_letter = name[5];
+ return (note_letter >= 'A' && note_letter <= 'G');
}
struct Sample {
@@ -295,7 +302,22 @@ int main(int argc, char** argv) {
fprintf(out_file, " SCORE_TRIGGERS, %zu, %.1ff\n", score.size(), bpm);
fprintf(out_file, "};\n\n");
- // Calculate maximum simultaneous patterns for optimal resource allocation
+ // ============================================================================
+ // RESOURCE USAGE ANALYSIS
+ // ============================================================================
+
+ // Count unique samples
+ int asset_sample_count = 0;
+ int generated_sample_count = 0;
+ for (const auto& s : samples) {
+ if (s.type == ASSET) {
+ asset_sample_count++;
+ } else {
+ generated_sample_count++;
+ }
+ }
+
+ // Calculate maximum simultaneous pattern triggers
std::map<float, int> time_pattern_count;
for (const auto& t : score) {
time_pattern_count[t.time]++;
@@ -308,27 +330,62 @@ int main(int argc, char** argv) {
}
}
- // Add safety margin (2x) for overlapping pattern playback
- const int recommended_voices = max_simultaneous_patterns * 2;
- const int recommended_spectrograms = max_simultaneous_patterns * 2;
+ // Calculate maximum polyphony (events per pattern on average)
+ int total_events = 0;
+ for (const auto& p : patterns) {
+ total_events += p.events.size();
+ }
+ const int avg_events_per_pattern = patterns.empty() ? 0 : total_events / patterns.size();
+ const int estimated_max_polyphony = max_simultaneous_patterns * avg_events_per_pattern;
+
+ // Conservative recommendations with safety margins
+ // - Each asset sample needs 1 spectrogram slot (shared across all events)
+ // - Each generated note needs 1 spectrogram slot PER EVENT (no caching yet)
+ // - Add 50% safety margin for peak moments
+ const int min_spectrograms = asset_sample_count + (generated_sample_count * estimated_max_polyphony);
+ const int recommended_spectrograms = (int)(min_spectrograms * 1.5f);
+ const int recommended_voices = estimated_max_polyphony * 2;
- fprintf(out_file, "// Resource usage analysis:\n");
- fprintf(out_file, "// Maximum simultaneous pattern triggers: %d\n",
+ fprintf(out_file, "// ============================================================\n");
+ fprintf(out_file, "// RESOURCE USAGE ANALYSIS (for synth.h configuration)\n");
+ fprintf(out_file, "// ============================================================\n");
+ fprintf(out_file, "// Total samples: %d (%d assets + %d generated notes)\n",
+ (int)samples.size(), asset_sample_count, generated_sample_count);
+ fprintf(out_file, "// Max simultaneous pattern triggers: %d\n",
max_simultaneous_patterns);
- fprintf(out_file, "// Recommended MAX_VOICES: %d (current: see synth.h)\n",
- recommended_voices);
- fprintf(out_file,
- "// Recommended MAX_SPECTROGRAMS: %d (current: see synth.h)\n",
- recommended_spectrograms);
+ fprintf(out_file, "// Estimated max polyphony: %d voices\n",
+ estimated_max_polyphony);
+ fprintf(out_file, "// \n");
+ fprintf(out_file, "// REQUIRED (minimum to avoid pool exhaustion):\n");
+ fprintf(out_file, "// MAX_VOICES: %d\n", estimated_max_polyphony);
+ fprintf(out_file, "// MAX_SPECTROGRAMS: %d (no caching)\n", min_spectrograms);
+ fprintf(out_file, "// \n");
+ fprintf(out_file, "// RECOMMENDED (with 50%% safety margin):\n");
+ fprintf(out_file, "// MAX_VOICES: %d\n", recommended_voices);
+ fprintf(out_file, "// MAX_SPECTROGRAMS: %d (no caching)\n", recommended_spectrograms);
+ fprintf(out_file, "// \n");
+ fprintf(out_file, "// NOTE: With spectrogram caching by note parameters,\n");
+ fprintf(out_file, "// MAX_SPECTROGRAMS could be reduced to ~%d\n",
+ asset_sample_count + generated_sample_count);
+ fprintf(out_file, "// ============================================================\n\n");
fclose(out_file);
printf("Tracker compilation successful.\n");
printf(" Patterns: %zu\n", patterns.size());
printf(" Score triggers: %zu\n", score.size());
+ printf(" Samples: %d (%d assets + %d generated)\n",
+ (int)samples.size(), asset_sample_count, generated_sample_count);
printf(" Max simultaneous patterns: %d\n", max_simultaneous_patterns);
- printf(" Recommended MAX_VOICES: %d\n", recommended_voices);
- printf(" Recommended MAX_SPECTROGRAMS: %d\n", recommended_spectrograms);
+ printf(" Estimated max polyphony: %d voices\n", estimated_max_polyphony);
+ printf("\n");
+ printf("RESOURCE REQUIREMENTS:\n");
+ printf(" Required MAX_VOICES: %d\n", estimated_max_polyphony);
+ printf(" Required MAX_SPECTROGRAMS: %d (without caching)\n", min_spectrograms);
+ printf(" Recommended MAX_VOICES: %d (with safety margin)\n", recommended_voices);
+ printf(" Recommended MAX_SPECTROGRAMS: %d (with safety margin)\n", recommended_spectrograms);
+ printf(" With caching: MAX_SPECTROGRAMS could be ~%d\n",
+ asset_sample_count + generated_sample_count);
return 0;
}