summaryrefslogtreecommitdiff
path: root/doc/archive
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-16 17:25:57 +0100
committerskal <pascal.massimino@gmail.com>2026-02-16 17:25:57 +0100
commit7eb38fb10c7bea8d07889d2563fbc076307f8050 (patch)
tree3899ad5148e5ac1cca6afc9cf1720cf8417f5c1b /doc/archive
parent18a3ccb2a1335d3747657e0b764af31fd723856e (diff)
docs: streamline and consolidate markdown documentationHEADmain
Remove 530 lines of redundant content, archive dated docs, compact CNN training sections, fix inconsistencies (effect count, test status). Improves maintainability and reduces context load for AI agents. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'doc/archive')
-rw-r--r--doc/archive/AUDIO_WAV_DRIFT_BUG.md185
-rw-r--r--doc/archive/FILE_HIERARCHY_CLEANUP_2026-02-13.md202
2 files changed, 387 insertions, 0 deletions
diff --git a/doc/archive/AUDIO_WAV_DRIFT_BUG.md b/doc/archive/AUDIO_WAV_DRIFT_BUG.md
new file mode 100644
index 0000000..050dd49
--- /dev/null
+++ b/doc/archive/AUDIO_WAV_DRIFT_BUG.md
@@ -0,0 +1,185 @@
+# Audio WAV Drift Bug Investigation
+
+**Date:** 2026-02-15
+**Status:** ACCEPTABLE (to be continued)
+**Current State:** -150ms drift at beat 64b, no glitches
+
+## Problem Statement
+
+Timeline viewer shows progressive visual drift between audio waveform and beat grid markers:
+- At beat 8 (5.33s @ 90 BPM): kick waveform appears **-30ms early** (left of grid line)
+- At beat 60 (40.0s @ 90 BPM): kick waveform appears **-180ms early** (left of grid line)
+
+Progressive drift rate: ~4.3ms/second
+
+## Initial Hypotheses (Ruled Out)
+
+### 1. ❌ Viewer Display Bug
+- **Tested:** Sample rate detection in viewer (32kHz correctly detected)
+- **Result:** Viewer BPM = 90 (correct), `pixelsPerSecond` mapping correct
+- **Conclusion:** Not a viewer rendering issue
+
+### 2. ❌ WAV File Content Error
+- **Tested:** Direct WAV sample position analysis via Python
+- **Result:** Actual kick positions in WAV file:
+ ```
+ Beat | Expected(s) | WAV(s) | Drift
+ -----|-------------|----------|-------
+ 8 | 5.3333 | 5.3526 | +19ms (LATE)
+ 60 | 40.0000 | 39.9980 | -2ms (nearly perfect)
+ ```
+- **Conclusion:** WAV file samples are at correct positions; visual drift not in WAV content
+
+### 3. ❌ Frame Truncation (Partial Cause)
+- **Issue:** `frames_per_update = (int)(32000 * (1/60))` = 533 frames (truncates 0.333)
+- **Impact:** Loses 0.333 frames/update = 10.4μs/frame
+- **Total drift over 40s:** 2400 frames × 10.4μs = **25ms**
+- **Conclusion:** Explains 25ms of 180ms, but not sufficient
+
+## Root Cause Discovery
+
+### Investigation Method
+Added debug tracking to `audio_render_ahead()` (audio.cc:115):
+```cpp
+static int64_t g_total_render_calls = 0;
+static int64_t g_total_frames_rendered = 0;
+
+// Track actual frames rendered vs expected
+const int64_t actual_rendered = frames_after - frames_before;
+g_total_render_calls++;
+g_total_frames_rendered += actual_rendered;
+```
+
+### Critical Finding: Over-Rendering
+
+**WAV dump @ 40s (2400 iterations):**
+```
+Expected frames: 1,279,200 (2400 × 533)
+Actual rendered: 1,290,933
+Difference: +11,733 frames = +366.66ms EXTRA audio
+```
+
+**Pattern observed every 10s (600 calls @ 60fps):**
+```
+[RENDER_DRIFT] calls=600 expect=319800 actual=331533 drift=-366.66ms
+[RENDER_DRIFT] calls=1200 expect=639600 actual=651333 drift=-366.66ms
+[RENDER_DRIFT] calls=1800 expect=959400 actual=971133 drift=-366.66ms
+[RENDER_DRIFT] calls=2400 expect=1279200 actual=1290933 drift=-366.66ms
+```
+
+### Why This Causes Visual Drift
+
+**WAV Dump Flow (main.cc:289-302):**
+1. `fill_audio_buffer(update_dt)` → calls `audio_render_ahead()`
+ - Renders audio into ring buffer
+ - **BUG:** Renders MORE than `chunk_frames` due to buffer management loop
+2. `ring_buffer->read(chunk_buffer, samples_per_update)`
+ - Reads exactly 533 frames from ring buffer
+3. `wav_backend.write_audio(chunk_buffer, samples_per_update)`
+ - Writes exactly 533 frames to WAV
+
+**Result:** Ring buffer accumulates 11,733 extra frames over 40s.
+
+### Timing Shift Mechanism
+
+Ring buffer acts as FIFO queue with 400ms lookahead:
+- Initially fills to 400ms (12,800 frames)
+- Each iteration: renders 533.333 (actual: ~536) frames, reads 533 frames
+- Net accumulation: ~3 frames/iteration
+- After 2400 iterations: 12,800 + (2400 × 3) = 20,000 frames buffer size
+
+Events trigger at correct `music_time` but get written to ring buffer position that's ahead. When WAV reads from buffer, it reads from older position, causing events to appear EARLIER in WAV file than their nominal music_time.
+
+## Technical Details
+
+### Code Locations
+
+**Truncation point 1:** `main.cc:282`
+```cpp
+const int frames_per_update = (int)(32000 * update_dt); // 533.333 → 533
+```
+
+**Truncation point 2:** `audio.cc:105`
+```cpp
+const int chunk_frames = (int)(dt * RING_BUFFER_SAMPLE_RATE); // 533.333 → 533
+```
+
+**Over-render loop:** `audio.cc:112-229`
+```cpp
+while (true) {
+ // Keeps rendering until buffer >= target_lookahead
+ // Renders MORE than chunk_frames due to buffer management
+ ...
+}
+```
+
+### Why 366ms Per 10s?
+
+At 60fps, 10s = 600 iterations:
+- Expected: 600 × 533 = 319,800 frames
+- Actual: 331,533 frames
+- Extra: 11,733 frames ÷ 600 = **19.55 frames extra per iteration**
+
+But `chunk_frames = 533`, so we render 533 + 19.55 = **~552.55 frames per call** on average.
+
+Discrepancy from 533.333 expected: 552.55 - 533.333 = **19.22 frames/call over-render**
+
+This 19.22 frames = 0.6ms per iteration accumulates to 366ms per 10s.
+
+## Proposed Fix
+
+### Option 1: Match Render to Read (Recommended)
+In WAV dump mode, ensure `audio_render_ahead()` renders exactly `frames_per_update`:
+```cpp
+// main.cc WAV dump loop
+const int frames_per_update = (int)(32000 * update_dt);
+audio_render_ahead(g_music_time, update_dt, /* force_exact_amount */ frames_per_update);
+```
+
+Modify `audio_render_ahead()` to accept optional exact frame count and render precisely that amount instead of filling to target lookahead.
+
+### Option 2: Round Instead of Truncate
+```cpp
+const int frames_per_update = (int)(32000 * update_dt + 0.5f); // Round: 533.333 → 533
+```
+Reduces truncation error but doesn't solve over-rendering.
+
+### Option 3: Use Double Precision + Accumulator
+```cpp
+static double accumulated_time = 0.0;
+accumulated_time += update_dt;
+const int frames_to_render = (int)(accumulated_time * 32000);
+accumulated_time -= frames_to_render / 32000.0;
+```
+Eliminates cumulative truncation error.
+
+## Related Issues
+
+- `tracker.cc:237` TODO comment mentions "180ms drift over 63 beats" - this is the same bug
+- Ring buffer lookahead (400ms) is separate from drift (not the cause)
+- Web Audio API `outputLatency` in viewer is unrelated (affects playback, not waveform display)
+
+## Verification Steps
+
+1. ✅ Measure WAV sample positions directly (Python script)
+2. ✅ Add render tracking debug output
+3. ✅ Confirm over-rendering (366ms per 10s)
+4. ✅ Implement partial fix (bypass ring buffer, direct render)
+5. ⚠️ Current result: -150ms drift at beat 64b (acceptable, needs further work)
+
+## Current Implementation (main.cc:286-308)
+
+**WAV dump now bypasses ring buffer entirely:**
+1. **Frame accumulator**: Calculates exact frames per update (no truncation)
+2. **Direct render**: Calls `synth_render()` directly with exact frame count
+3. **No ring buffer**: Eliminates buffer management complexity
+4. **Result**: No glitches, but -150ms drift remains
+
+**Remaining issue:** Drift persists despite direct rendering. Likely related to tempo scaling or audio engine state management. Acceptable for now.
+
+## Notes
+
+- Viewer waveform rendering is CORRECT - displays WAV content accurately
+- Bug is in demo's WAV generation, specifically ring buffer management in `audio_render_ahead()`
+- Progressive nature of drift (30ms → 180ms) indicates accumulation, not one-time offset
+- Fix must ensure rendered frames = read frames in WAV dump mode
diff --git a/doc/archive/FILE_HIERARCHY_CLEANUP_2026-02-13.md b/doc/archive/FILE_HIERARCHY_CLEANUP_2026-02-13.md
new file mode 100644
index 0000000..8af5efd
--- /dev/null
+++ b/doc/archive/FILE_HIERARCHY_CLEANUP_2026-02-13.md
@@ -0,0 +1,202 @@
+# File Hierarchy Cleanup - February 13, 2026
+
+## Summary
+
+Comprehensive reorganization of project file structure for improved maintainability and code reuse.
+
+---
+
+## Changes Implemented
+
+### 1. Application Entry Points → `src/app/`
+
+**Before:**
+```
+src/
+ main.cc
+ stub_main.cc
+ test_demo.cc
+```
+
+**After:**
+```
+src/app/
+ main.cc
+ stub_main.cc
+ test_demo.cc
+```
+
+**Impact:** Cleaner src/ directory, separates application code from libraries.
+
+---
+
+### 2. Workspace Reorganization
+
+**Before:**
+```
+workspaces/main/
+ assets/music/*.spec
+ shaders/*.wgsl
+ obj/*.obj
+
+assets/
+ demo.seq
+ music.track
+ originals/
+ common/
+ final/
+```
+
+**After:**
+```
+workspaces/{main,test}/
+ music/ # Audio samples (.spec)
+ weights/ # CNN binary weights (.bin)
+ obj/ # 3D models (.obj)
+ shaders/ # Workspace-specific WGSL only
+
+common/
+ shaders/ # Shared WGSL utilities
+ math/
+ render/
+ compute/
+
+tools/
+ originals/ # Source audio files
+ test_demo.seq
+ test_demo.track
+```
+
+**Removed:**
+- `assets/` directory (legacy structure)
+- `assets/common/` (replaced by `common/`)
+- `assets/final/` (superseded by workspaces)
+- `assets/originals/` → `tools/originals/`
+
+---
+
+### 3. Shared Shader System
+
+**Problem:** 36 duplicate shader files across workspaces (byte-identical).
+
+**Solution:** Implemented Option 1 from `SHADER_REUSE_INVESTIGATION.md`.
+
+**Structure:**
+```
+common/shaders/
+ common_uniforms.wgsl
+ lighting.wgsl
+ passthrough.wgsl
+ ray_box.wgsl
+ ray_triangle.wgsl
+ sdf_primitives.wgsl
+ skybox.wgsl
+ math/
+ common_utils.wgsl
+ noise.wgsl
+ sdf_shapes.wgsl
+ sdf_utils.wgsl
+ render/
+ lighting_utils.wgsl
+ scene_query_bvh.wgsl
+ scene_query_linear.wgsl
+ shadows.wgsl
+ compute/
+ gen_blend.wgsl
+ gen_grid.wgsl
+ gen_mask.wgsl
+ gen_noise.wgsl
+ gen_perlin.wgsl
+```
+
+**Reference in assets.txt:**
+```
+SHADER_COMMON_UNIFORMS, NONE, ../../common/shaders/common_uniforms.wgsl
+SHADER_MATH_NOISE, NONE, ../../common/shaders/math/noise.wgsl
+```
+
+**Asset Packer Enhancement:**
+- Added `#include <filesystem>` for path normalization
+- Implemented `lexically_normal()` to resolve `../../common/` references
+- Cross-platform path handling for workspace-relative includes
+
+---
+
+## Updated Files
+
+### Configuration
+- `workspaces/main/workspace.cfg` - Updated asset_dirs and shader_dirs
+- `workspaces/test/workspace.cfg` - Updated asset_dirs and shader_dirs
+- `workspaces/main/assets.txt` - Common shader references
+- `workspaces/test/assets.txt` - Common shader references
+
+### Build System
+- `cmake/DemoExecutables.cmake` - src/app/ paths, test_demo paths
+- `cmake/DemoCodegen.cmake` - Removed legacy fallback paths
+- `cmake/Validation.cmake` - Workspace shader paths
+
+### Tools
+- `tools/asset_packer.cc` - Filesystem path normalization
+- `scripts/gen_spectrograms.sh` - tools/originals/ paths
+- `scripts/train_cnn_v2_full.sh` - workspaces/main/weights/ paths
+- `training/export_cnn_v2_weights.py` - workspaces/main/weights/ paths
+
+### Application
+- `src/app/main.cc` - Hot-reload workspace paths
+
+---
+
+## Metrics
+
+**File Reduction:**
+- Removed 36 duplicate shader files
+- Deleted legacy assets/ structure (~70 files)
+- Net: ~100 files eliminated
+
+**Disk Space:**
+- Common shaders: 20 files
+- Per-workspace shaders: ~30-35 files
+- Saved: ~36 shader duplicates
+
+**Workspace Structure:**
+```
+workspaces/main/: 31 shaders (workspace-specific)
+workspaces/test/: 19 shaders (workspace-specific)
+common/: 20 shaders (shared)
+Total unique: 70 shaders (vs 106 before)
+```
+
+---
+
+## Benefits
+
+1. **Single Source of Truth:** Common shaders in one location
+2. **No Duplication:** Bug fixes apply everywhere automatically
+3. **Clear Separation:** Common vs workspace-specific code
+4. **Size Optimization:** Important for 64k target
+5. **Maintainability:** Easier to understand and modify
+6. **Workspace Isolation:** Each workspace still self-contained for specific content
+
+---
+
+## Migration Notes
+
+**For New Workspaces:**
+1. Create `workspaces/new_workspace/` with subdirs: `music/`, `weights/`, `obj/`, `shaders/`
+2. Reference common shaders: `../../common/shaders/...`
+3. Add workspace-specific shaders to local `shaders/`
+4. Update `workspace.cfg`: `asset_dirs = ["music/", "weights/", "obj/"]`
+
+**For Common Shader Changes:**
+- Edit files in `common/shaders/`
+- Changes apply to all workspaces immediately
+- Run full build to verify all workspaces
+
+---
+
+## Documentation Updated
+
+- `doc/WORKSPACE_SYSTEM.md` - New structure reflected
+- `doc/SHADER_REUSE_INVESTIGATION.md` - Implementation status
+- `doc/PROJECT_CONTEXT.md` - Current project state
+- `doc/FILE_HIERARCHY_CLEANUP_2026-02-13.md` - This document