diff options
Diffstat (limited to 'src/tests')
| -rw-r--r-- | src/tests/test_jittered_audio.cc | 72 | ||||
| -rw-r--r-- | src/tests/test_wav_dump.cc | 46 |
2 files changed, 61 insertions, 57 deletions
diff --git a/src/tests/test_jittered_audio.cc b/src/tests/test_jittered_audio.cc index 92c9099..f880c74 100644 --- a/src/tests/test_jittered_audio.cc +++ b/src/tests/test_jittered_audio.cc @@ -24,9 +24,10 @@ void test_jittered_audio_basic() { // At 32kHz, 10ms = 320 samples = 160 frames (stereo) // Jitter of ±5ms means 5-15ms intervals, or 80-240 frames JitteredAudioBackend jittered_backend; - jittered_backend.set_base_interval(10.0f); // 10ms base interval - jittered_backend.set_jitter_amount(5.0f); // ±5ms jitter - jittered_backend.set_chunk_size_range(80, 240); // Realistic chunk sizes for 5-15ms + jittered_backend.set_base_interval(10.0f); // 10ms base interval + jittered_backend.set_jitter_amount(5.0f); // ±5ms jitter + jittered_backend.set_chunk_size_range( + 80, 240); // Realistic chunk sizes for 5-15ms audio_set_backend(&jittered_backend); audio_init(); @@ -35,13 +36,13 @@ void test_jittered_audio_basic() { audio_start(); assert(jittered_backend.is_running()); - // Simulate main loop for 2 seconds - const float total_time = 2.0f; - const float dt = 1.0f / 60.0f; // 60fps + // Simulate main loop for 0.5 seconds (quick stress test) + const float total_time = 0.5f; + const float dt = 1.0f / 60.0f; // 60fps float music_time = 0.0f; for (float t = 0.0f; t < total_time; t += dt) { - music_time += dt; // Normal tempo + music_time += dt; // Normal tempo // Update tracker and fill buffer tracker_update(music_time); @@ -61,13 +62,13 @@ void test_jittered_audio_basic() { printf(" Frames consumed: %d\n", frames_consumed); printf(" Underruns: %d\n", underruns); - // Should have consumed roughly 2 seconds worth of audio - // At 32kHz stereo: 2 seconds = 64000 samples = 32000 frames - assert(frames_consumed > 24000); // At least 1.5 seconds (48000 samples) - assert(frames_consumed < 40000); // At most 2.5 seconds (80000 samples) + // Should have consumed roughly 0.5 seconds worth of audio + // At 32kHz stereo: 0.5 seconds = 16000 samples = 8000 frames + assert(frames_consumed > 4000); // At least 0.25 seconds (8000 samples) + assert(frames_consumed < 12000); // At most 0.75 seconds (24000 samples) // Underruns are acceptable in this test, but shouldn't be excessive - assert(underruns < 50); // Less than 50 underruns in 2 seconds + assert(underruns < 20); // Less than 20 underruns in 0.5 seconds printf(" ✓ Basic jittered audio consumption PASSED\n"); } @@ -83,9 +84,9 @@ void test_jittered_audio_with_acceleration() { // At 32kHz, 15ms = 480 samples = 240 frames (stereo) // Jitter of ±10ms means 5-25ms intervals, or 80-400 frames JitteredAudioBackend jittered_backend; - jittered_backend.set_base_interval(15.0f); // Slower consumption - jittered_backend.set_jitter_amount(10.0f); // High jitter - jittered_backend.set_chunk_size_range(80, 400); // Realistic stress test range + jittered_backend.set_base_interval(15.0f); // Slower consumption + jittered_backend.set_jitter_amount(10.0f); // High jitter + jittered_backend.set_chunk_size_range(80, 400); // Realistic stress test range audio_set_backend(&jittered_backend); audio_init(); @@ -94,19 +95,19 @@ void test_jittered_audio_with_acceleration() { audio_start(); // Simulate acceleration scenario (similar to real demo) - const float total_time = 10.0f; + const float total_time = 3.0f; const float dt = 1.0f / 60.0f; float music_time = 0.0f; float physical_time = 0.0f; - for (int frame = 0; frame < 600; ++frame) { // 10 seconds @ 60fps + for (int frame = 0; frame < 180; ++frame) { // 3 seconds @ 60fps physical_time = frame * dt; - // Variable tempo (accelerate from 5-10s) + // Variable tempo (accelerate from 1.5-3s) float tempo_scale = 1.0f; - if (physical_time >= 5.0f && physical_time < 10.0f) { - const float progress = (physical_time - 5.0f) / 5.0f; - tempo_scale = 1.0f + progress * 1.0f; // 1.0 → 2.0 + if (physical_time >= 1.5f && physical_time < 3.0f) { + const float progress = (physical_time - 1.5f) / 1.5f; + tempo_scale = 1.0f + progress * 1.0f; // 1.0 → 2.0 } music_time += dt * tempo_scale; @@ -118,12 +119,14 @@ void test_jittered_audio_with_acceleration() { // Sleep to simulate frame time std::this_thread::sleep_for(std::chrono::milliseconds(16)); - // Progress indicator - if (frame % 60 == 0) { - printf(" Frame %d: music_time=%.2fs, tempo=%.2fx, consumed=%d frames, underruns=%d\r", - frame, music_time, tempo_scale, - jittered_backend.get_total_frames_consumed(), - jittered_backend.get_underrun_count()); + // Progress indicator (every 30 frames for shorter test) + if (frame % 30 == 0) { + printf( + " Frame %d: music_time=%.2fs, tempo=%.2fx, consumed=%d frames, " + "underruns=%d\r", + frame, music_time, tempo_scale, + jittered_backend.get_total_frames_consumed(), + jittered_backend.get_underrun_count()); fflush(stdout); } } @@ -139,14 +142,15 @@ void test_jittered_audio_with_acceleration() { printf(" Total frames consumed: %d\n", frames_consumed); printf(" Total underruns: %d\n", underruns); - // Should have consumed roughly 12.5 seconds worth of audio - // (10 seconds physical time with acceleration 1.0x → 2.0x) - // At 32kHz stereo: 12.5 seconds = 400000 samples = 200000 frames - assert(frames_consumed > 120000); // At least 7.5 seconds (240000 samples) - assert(frames_consumed < 240000); // At most 15 seconds (480000 samples) + // Should have consumed roughly 3.75 seconds worth of audio + // (3 seconds physical time with acceleration 1.0x → 2.0x) + // At 32kHz stereo: 3.75 seconds = 120000 samples = 60000 frames + assert(frames_consumed > 40000); // At least 2.5 seconds (80000 samples) + assert(frames_consumed < 80000); // At most 5 seconds (160000 samples) - // During acceleration with jitter, some underruns are expected but not excessive - assert(underruns < 200); // Less than 200 underruns in 10 seconds + // During acceleration with jitter, some underruns are expected but not + // excessive + assert(underruns < 60); // Less than 60 underruns in 3 seconds printf(" ✓ Jittered audio with acceleration PASSED\n"); } diff --git a/src/tests/test_wav_dump.cc b/src/tests/test_wav_dump.cc index 753d548..f350330 100644 --- a/src/tests/test_wav_dump.cc +++ b/src/tests/test_wav_dump.cc @@ -1,10 +1,10 @@ // This file is part of the 64k demo project. // Regression test for WAV dump backend to prevent format mismatches. -#include "audio/wav_dump_backend.h" #include "audio/audio.h" #include "audio/synth.h" #include "audio/tracker.h" +#include "audio/wav_dump_backend.h" #include <assert.h> #include <stdio.h> #include <string.h> @@ -13,18 +13,18 @@ // Helper to read WAV header and verify format struct WavHeader { - char riff[4]; // "RIFF" - uint32_t chunk_size; // File size - 8 - char wave[4]; // "WAVE" - char fmt[4]; // "fmt " + char riff[4]; // "RIFF" + uint32_t chunk_size; // File size - 8 + char wave[4]; // "WAVE" + char fmt[4]; // "fmt " uint32_t subchunk1_size; - uint16_t audio_format; // 1 = PCM + uint16_t audio_format; // 1 = PCM uint16_t num_channels; uint32_t sample_rate; uint32_t byte_rate; uint16_t block_align; uint16_t bits_per_sample; - char data[4]; // "data" + char data[4]; // "data" uint32_t data_size; }; @@ -36,6 +36,7 @@ void test_wav_format_matches_live_audio() { // Create WAV dump backend WavDumpBackend wav_backend; wav_backend.set_output_file(test_file); + wav_backend.set_duration(2.0f); // Only 2 seconds for quick testing audio_set_backend(&wav_backend); // Initialize audio system (calls synth_init internally) @@ -45,7 +46,7 @@ void test_wav_format_matches_live_audio() { tracker_init(); // Manually trigger some audio for testing - tracker_update(0.0f); // Trigger patterns at t=0 + tracker_update(0.0f); // Trigger patterns at t=0 // Render short duration (1 second = 60 updates @ 60Hz) for (int i = 0; i < 60; ++i) { @@ -55,7 +56,7 @@ void test_wav_format_matches_live_audio() { // Simulate audio render (WavDumpBackend will handle this in start()) } - audio_start(); // This triggers the actual WAV rendering + audio_start(); // This triggers the actual WAV rendering audio_shutdown(); // Read and verify WAV header @@ -74,7 +75,7 @@ void test_wav_format_matches_live_audio() { // CRITICAL: Verify stereo format (matches miniaudio config) printf(" Checking num_channels...\n"); - assert(header.num_channels == 2); // MUST be stereo! + assert(header.num_channels == 2); // MUST be stereo! // Verify sample rate matches miniaudio printf(" Checking sample_rate...\n"); @@ -86,7 +87,7 @@ void test_wav_format_matches_live_audio() { // Verify audio format is PCM printf(" Checking audio_format...\n"); - assert(header.audio_format == 1); // PCM + assert(header.audio_format == 1); // PCM // Verify calculated values printf(" Checking byte_rate...\n"); @@ -99,24 +100,23 @@ void test_wav_format_matches_live_audio() { header.num_channels * (header.bits_per_sample / 8); assert(header.block_align == expected_block_align); - // Verify data size is reasonable (60 seconds of audio) + // Verify data size is reasonable (2 seconds of audio) printf(" Checking data_size...\n"); const uint32_t bytes_per_sample = header.bits_per_sample / 8; const uint32_t expected_bytes_per_sec = header.sample_rate * header.num_channels * bytes_per_sample; - const uint32_t expected_size_60s = expected_bytes_per_sec * 60; + const uint32_t expected_size_2s = expected_bytes_per_sec * 2; - printf(" Data size: %u bytes (expected ~%u bytes for 60s)\n", - header.data_size, expected_size_60s); + printf(" Data size: %u bytes (expected ~%u bytes for 2s)\n", + header.data_size, expected_size_2s); - // Be lenient: allow 50-70 seconds worth of data - const uint32_t expected_min_size = expected_bytes_per_sec * 50; - const uint32_t expected_max_size = expected_bytes_per_sec * 70; + // Be lenient: allow 1.5-2.5 seconds worth of data + const uint32_t expected_min_size = expected_bytes_per_sec * 1.5; + const uint32_t expected_max_size = expected_bytes_per_sec * 2.5; - // Note: Currently seeing 2x expected size - may be a header writing bug // For now, accept if stereo format is correct (main regression test goal) if (header.data_size < expected_min_size || - header.data_size > expected_max_size * 2) { + header.data_size > expected_max_size) { printf(" WARNING: Data size outside expected range\n"); // Don't fail on this for now - stereo format is the critical check } @@ -136,7 +136,7 @@ void test_wav_format_matches_live_audio() { printf(" Checking for actual audio data...\n"); printf(" Non-zero samples: %d / 1000\n", non_zero_count); - assert(non_zero_count > 100); // Should have plenty of non-zero samples + assert(non_zero_count > 100); // Should have plenty of non-zero samples fclose(f); @@ -155,8 +155,8 @@ void test_wav_stereo_buffer_size() { const int sample_rate = 32000; const float update_dt = 1.0f / 60.0f; - const int frames_per_update = (int)(sample_rate * update_dt); // ~533 - const int samples_per_update = frames_per_update * 2; // ~1066 (stereo) + const int frames_per_update = (int)(sample_rate * update_dt); // ~533 + const int samples_per_update = frames_per_update * 2; // ~1066 (stereo) printf(" Update rate: 60 Hz\n"); printf(" Frames per update: %d\n", frames_per_update); |
