From 6c09e3bdb5128dca64c457c3a6ebeb32adf98c10 Mon Sep 17 00:00:00 2001 From: skal Date: Thu, 5 Feb 2026 19:59:21 +0100 Subject: feat(audio): Complete Phase 2 - Migrate tests to AudioEngine (Task #56) Migrated all tracker-related tests to use AudioEngine instead of directly calling synth_init() and tracker_init(), eliminating fragile initialization order dependencies. Tests Migrated: - test_tracker.cc: Basic tracker functionality - test_tracker_timing.cc: Timing verification with MockAudioBackend (7 tests) - test_variable_tempo.cc: Variable tempo scaling (6 tests) - test_wav_dump.cc: WAV dump backend verification Migration Pattern: - Added AudioEngine include to all test files - Replaced synth_init() + tracker_init() with AudioEngine::init() - Replaced tracker_update(time) with engine.update(time) - Added engine.shutdown() at end of each test function - Preserved audio_init()/audio_shutdown() where needed for backends Results: - All 20 tests pass (100% pass rate) - Test suite time: 8.13s (slightly faster) - No regressions in test behavior - Cleaner API with single initialization entry point Next Steps (Phase 3): - Migrate main.cc and production code to use AudioEngine - Add backwards compatibility shims during transition handoff(Claude): Completed Task #56 Phase 2 - all tracker tests now use AudioEngine. The initialization order fragility is eliminated in test code. Ready for Phase 3 (production integration). --- src/tests/test_variable_tempo.cc | 101 +++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 41 deletions(-) (limited to 'src/tests/test_variable_tempo.cc') diff --git a/src/tests/test_variable_tempo.cc b/src/tests/test_variable_tempo.cc index 3deb97e..533f398 100644 --- a/src/tests/test_variable_tempo.cc +++ b/src/tests/test_variable_tempo.cc @@ -2,13 +2,13 @@ // It tests variable tempo system with music_time scaling. // Verifies 2x speed-up and 2x slow-down reset tricks. -#include "audio/mock_audio_backend.h" #include "audio/audio.h" -#include "audio/synth.h" +#include "audio/audio_engine.h" +#include "audio/mock_audio_backend.h" #include "audio/tracker.h" #include -#include #include +#include #if !defined(STRIP_ALL) @@ -19,7 +19,7 @@ static float calc_physical_time(float music_time, float tempo_scale) { // Helper: Simulate music time advancement static float advance_music_time(float current_music_time, float dt, - float tempo_scale) { + float tempo_scale) { return current_music_time + (dt * tempo_scale); } @@ -29,8 +29,10 @@ void test_basic_tempo_scaling() { MockAudioBackend backend; audio_set_backend(&backend); - synth_init(); - tracker_init(); + AudioEngine engine; + engine.init(); + engine.load_music_data(&g_tracker_score, g_tracker_samples, + g_tracker_sample_assets, g_tracker_samples_count); // Test 1: Normal tempo (1.0x) { @@ -40,9 +42,9 @@ void test_basic_tempo_scaling() { // Simulate 1 second of physical time for (int i = 0; i < 10; ++i) { - float dt = 0.1f; // 100ms physical steps + float dt = 0.1f; // 100ms physical steps music_time += dt * tempo_scale; - tracker_update(music_time); + engine.update(music_time); } // After 1 second physical time at 1.0x tempo: @@ -54,7 +56,7 @@ void test_basic_tempo_scaling() { // Test 2: Fast tempo (2.0x) { backend.clear_events(); - tracker_init(); // Reset tracker + engine.reset(); // Reset engine float music_time = 0.0f; float tempo_scale = 2.0f; @@ -62,7 +64,7 @@ void test_basic_tempo_scaling() { for (int i = 0; i < 10; ++i) { float dt = 0.1f; music_time += dt * tempo_scale; - tracker_update(music_time); + engine.update(music_time); } // After 1 second physical time at 2.0x tempo: @@ -74,7 +76,7 @@ void test_basic_tempo_scaling() { // Test 3: Slow tempo (0.5x) { backend.clear_events(); - synth_init(); + engine.reset(); float music_time = 0.0f; float tempo_scale = 0.5f; @@ -82,7 +84,7 @@ void test_basic_tempo_scaling() { for (int i = 0; i < 10; ++i) { float dt = 0.1f; music_time += dt * tempo_scale; - tracker_update(music_time); + engine.update(music_time); } // After 1 second physical time at 0.5x tempo: @@ -91,6 +93,7 @@ void test_basic_tempo_scaling() { assert(std::abs(music_time - 0.5f) < 0.01f); } + engine.shutdown(); printf(" ✓ Basic tempo scaling works correctly\n"); } @@ -100,30 +103,32 @@ void test_2x_speedup_reset_trick() { MockAudioBackend backend; audio_set_backend(&backend); - synth_init(); - tracker_init(); + AudioEngine engine; + engine.init(); + engine.load_music_data(&g_tracker_score, g_tracker_samples, + g_tracker_sample_assets, g_tracker_samples_count); // Scenario: Accelerate to 2.0x, then reset to 1.0x float music_time = 0.0f; float tempo_scale = 1.0f; float physical_time = 0.0f; - const float dt = 0.1f; // 100ms steps + const float dt = 0.1f; // 100ms steps // Phase 1: Accelerate from 1.0x to 2.0x over 5 seconds printf(" Phase 1: Accelerating 1.0x → 2.0x\n"); for (int i = 0; i < 50; ++i) { physical_time += dt; - tempo_scale = 1.0f + (physical_time / 5.0f); // Linear acceleration + tempo_scale = 1.0f + (physical_time / 5.0f); // Linear acceleration tempo_scale = fminf(tempo_scale, 2.0f); music_time += dt * tempo_scale; - tracker_update(music_time); + engine.update(music_time); } printf(" After 5s physical: tempo=%.2fx, music_time=%.3f\n", tempo_scale, music_time); - assert(tempo_scale >= 1.99f); // Should be at 2.0x + assert(tempo_scale >= 1.99f); // Should be at 2.0x // Record state before reset const float music_time_before_reset = music_time; @@ -137,7 +142,7 @@ void test_2x_speedup_reset_trick() { for (int i = 0; i < 20; ++i) { physical_time += dt; music_time += dt * tempo_scale; - tracker_update(music_time); + engine.update(music_time); } printf(" After reset + 2s: tempo=%.2fx, music_time=%.3f\n", tempo_scale, @@ -148,6 +153,7 @@ void test_2x_speedup_reset_trick() { printf(" Music time delta: %.3f (expected ~2.0)\n", music_time_delta); assert(std::abs(music_time_delta - 2.0f) < 0.1f); + engine.shutdown(); printf(" ✓ 2x speed-up reset trick verified\n"); } @@ -157,8 +163,10 @@ void test_2x_slowdown_reset_trick() { MockAudioBackend backend; audio_set_backend(&backend); - synth_init(); - tracker_init(); + AudioEngine engine; + engine.init(); + engine.load_music_data(&g_tracker_score, g_tracker_samples, + g_tracker_sample_assets, g_tracker_samples_count); // Scenario: Decelerate to 0.5x, then reset to 1.0x float music_time = 0.0f; @@ -171,16 +179,16 @@ void test_2x_slowdown_reset_trick() { printf(" Phase 1: Decelerating 1.0x → 0.5x\n"); for (int i = 0; i < 50; ++i) { physical_time += dt; - tempo_scale = 1.0f - (physical_time / 10.0f); // Linear deceleration + tempo_scale = 1.0f - (physical_time / 10.0f); // Linear deceleration tempo_scale = fmaxf(tempo_scale, 0.5f); music_time += dt * tempo_scale; - tracker_update(music_time); + engine.update(music_time); } printf(" After 5s physical: tempo=%.2fx, music_time=%.3f\n", tempo_scale, music_time); - assert(tempo_scale <= 0.51f); // Should be at 0.5x + assert(tempo_scale <= 0.51f); // Should be at 0.5x // Record state before reset const float music_time_before_reset = music_time; @@ -193,7 +201,7 @@ void test_2x_slowdown_reset_trick() { for (int i = 0; i < 20; ++i) { physical_time += dt; music_time += dt * tempo_scale; - tracker_update(music_time); + engine.update(music_time); } printf(" After reset + 2s: tempo=%.2fx, music_time=%.3f\n", tempo_scale, @@ -204,6 +212,7 @@ void test_2x_slowdown_reset_trick() { printf(" Music time delta: %.3f (expected ~2.0)\n", music_time_delta); assert(std::abs(music_time_delta - 2.0f) < 0.1f); + engine.shutdown(); printf(" ✓ 2x slow-down reset trick verified\n"); } @@ -213,8 +222,10 @@ void test_pattern_density_swap() { MockAudioBackend backend; audio_set_backend(&backend); - synth_init(); - tracker_init(); + AudioEngine engine; + engine.init(); + engine.load_music_data(&g_tracker_score, g_tracker_samples, + g_tracker_sample_assets, g_tracker_samples_count); // Simulate: sparse pattern → accelerate → reset + dense pattern float music_time = 0.0f; @@ -224,7 +235,7 @@ void test_pattern_density_swap() { printf(" Phase 1: Sparse pattern, normal tempo\n"); for (float t = 0.0f; t < 3.0f; t += 0.1f) { music_time += 0.1f * tempo_scale; - tracker_update(music_time); + engine.update(music_time); } const size_t sparse_events = backend.get_events().size(); printf(" Events during sparse phase: %zu\n", sparse_events); @@ -234,7 +245,7 @@ void test_pattern_density_swap() { tempo_scale = 2.0f; for (float t = 0.0f; t < 2.0f; t += 0.1f) { music_time += 0.1f * tempo_scale; - tracker_update(music_time); + engine.update(music_time); } const size_t events_at_2x = backend.get_events().size() - sparse_events; printf(" Additional events during 2.0x: %zu\n", events_at_2x); @@ -249,7 +260,7 @@ void test_pattern_density_swap() { const size_t events_before_reset_phase = backend.get_events().size(); for (float t = 0.0f; t < 2.0f; t += 0.1f) { music_time += 0.1f * tempo_scale; - tracker_update(music_time); + engine.update(music_time); } const size_t events_after_reset = backend.get_events().size(); @@ -259,6 +270,7 @@ void test_pattern_density_swap() { // Verify patterns triggered throughout assert(backend.get_events().size() > 0); + engine.shutdown(); printf(" ✓ Pattern density swap points verified\n"); } @@ -268,14 +280,16 @@ void test_continuous_acceleration() { MockAudioBackend backend; audio_set_backend(&backend); - synth_init(); - tracker_init(); + AudioEngine engine; + engine.init(); + engine.load_music_data(&g_tracker_score, g_tracker_samples, + g_tracker_sample_assets, g_tracker_samples_count); float music_time = 0.0f; float tempo_scale = 0.5f; float physical_time = 0.0f; - const float dt = 0.05f; // 50ms steps for smoother curve + const float dt = 0.05f; // 50ms steps for smoother curve // Accelerate from 0.5x to 2.0x over 10 seconds printf(" Accelerating 0.5x → 2.0x over 10 seconds\n"); @@ -285,12 +299,12 @@ void test_continuous_acceleration() { for (int i = 0; i < 200; ++i) { physical_time += dt; - float progress = physical_time / 10.0f; // 0.0 to 1.0 + float progress = physical_time / 10.0f; // 0.0 to 1.0 tempo_scale = min_tempo + progress * (max_tempo - min_tempo); tempo_scale = fmaxf(min_tempo, fminf(max_tempo, tempo_scale)); music_time += dt * tempo_scale; - tracker_update(music_time); + engine.update(music_time); // Log at key points if (i % 50 == 0) { @@ -305,12 +319,14 @@ void test_continuous_acceleration() { assert(tempo_scale >= 1.99f); // Verify music_time progressed correctly - // Integral of (0.5 + 1.5t/10) from 0 to 10 = 0.5*10 + 1.5*10²/(2*10) = 5 + 7.5 = 12.5 + // Integral of (0.5 + 1.5t/10) from 0 to 10 = 0.5*10 + 1.5*10²/(2*10) = 5 + // + 7.5 = 12.5 const float expected_music_time = 12.5f; printf(" Expected music_time: %.3f, actual: %.3f\n", expected_music_time, music_time); assert(std::abs(music_time - expected_music_time) < 0.5f); + engine.shutdown(); printf(" ✓ Continuous acceleration verified\n"); } @@ -320,8 +336,10 @@ void test_oscillating_tempo() { MockAudioBackend backend; audio_set_backend(&backend); - synth_init(); - tracker_init(); + AudioEngine engine; + engine.init(); + engine.load_music_data(&g_tracker_score, g_tracker_samples, + g_tracker_sample_assets, g_tracker_samples_count); float music_time = 0.0f; float physical_time = 0.0f; @@ -336,7 +354,7 @@ void test_oscillating_tempo() { float tempo_scale = 1.0f + 0.2f * sinf(physical_time * 2.0f); music_time += dt * tempo_scale; - tracker_update(music_time); + engine.update(music_time); if (i % 25 == 0) { printf(" t=%.2fs: tempo=%.3fx, music_time=%.3f\n", physical_time, @@ -344,14 +362,15 @@ void test_oscillating_tempo() { } } - // After oscillation, music_time should be approximately equal to physical_time - // (since average tempo is 1.0x) + // After oscillation, music_time should be approximately equal to + // physical_time (since average tempo is 1.0x) printf(" Final: physical_time=%.2fs, music_time=%.3f (expected ~%.2f)\n", physical_time, music_time, physical_time); // Allow some tolerance for integral error assert(std::abs(music_time - physical_time) < 0.5f); + engine.shutdown(); printf(" ✓ Oscillating tempo verified\n"); } -- cgit v1.2.3