summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/audio/wav_dump_backend.cc52
-rw-r--r--src/audio/wav_dump_backend.h8
-rw-r--r--src/tests/test_jittered_audio.cc72
-rw-r--r--src/tests/test_wav_dump.cc46
4 files changed, 99 insertions, 79 deletions
diff --git a/src/audio/wav_dump_backend.cc b/src/audio/wav_dump_backend.cc
index d1acf66..9fa13f5 100644
--- a/src/audio/wav_dump_backend.cc
+++ b/src/audio/wav_dump_backend.cc
@@ -14,19 +14,24 @@
#include <string.h>
WavDumpBackend::WavDumpBackend()
- : wav_file_(nullptr),
- samples_written_(0),
- output_filename_("audio_dump.wav"),
- is_active_(false) {
+ : wav_file_(nullptr), samples_written_(0),
+ output_filename_("audio_dump.wav"), is_active_(false),
+ duration_sec_(60.0f) {
sample_buffer_.resize(kBufferSize);
}
-WavDumpBackend::~WavDumpBackend() { shutdown(); }
+WavDumpBackend::~WavDumpBackend() {
+ shutdown();
+}
void WavDumpBackend::set_output_file(const char* filename) {
output_filename_ = filename;
}
+void WavDumpBackend::set_duration(float seconds) {
+ duration_sec_ = seconds;
+}
+
void WavDumpBackend::init() {
// Open WAV file for writing
wav_file_ = fopen(output_filename_, "wb");
@@ -48,11 +53,11 @@ void WavDumpBackend::start() {
// Render audio in small chunks with tracker updates
// This matches the seek logic in main.cc
- const int max_duration_sec = 60;
- const float update_dt = 1.0f / 60.0f; // 60Hz update rate (matches main loop)
- const int frames_per_update = (int)(kSampleRate * update_dt); // ~533 frames
- const int samples_per_update = frames_per_update * 2; // Stereo: 2 samples per frame
- const int total_updates = (int)(max_duration_sec / update_dt);
+ const float update_dt = 1.0f / 60.0f; // 60Hz update rate (matches main loop)
+ const int frames_per_update = (int)(kSampleRate * update_dt); // ~533 frames
+ const int samples_per_update =
+ frames_per_update * 2; // Stereo: 2 samples per frame
+ const int total_updates = (int)(duration_sec_ / update_dt);
// Music time tracking
float music_time = 0.0f;
@@ -71,12 +76,12 @@ void WavDumpBackend::start() {
tempo_scale = 1.0f;
} else if (physical_time < 15.0f) {
const float progress = (physical_time - 10.0f) / 5.0f;
- tempo_scale = 1.0f + progress * 1.0f; // 1.0 → 2.0
+ tempo_scale = 1.0f + progress * 1.0f; // 1.0 → 2.0
} else if (physical_time < 20.0f) {
tempo_scale = 1.0f;
} else if (physical_time < 25.0f) {
const float progress = (physical_time - 20.0f) / 5.0f;
- tempo_scale = 1.0f - progress * 0.5f; // 1.0 → 0.5
+ tempo_scale = 1.0f - progress * 0.5f; // 1.0 → 0.5
} else {
tempo_scale = 1.0f;
}
@@ -99,8 +104,10 @@ void WavDumpBackend::start() {
// Convert float to int16 and write to WAV (stereo interleaved)
for (int i = 0; i < samples_per_update; ++i) {
float sample = chunk_buffer[i];
- if (sample > 1.0f) sample = 1.0f;
- if (sample < -1.0f) sample = -1.0f;
+ if (sample > 1.0f)
+ sample = 1.0f;
+ if (sample < -1.0f)
+ sample = -1.0f;
const int16_t sample_i16 = (int16_t)(sample * 32767.0f);
fwrite(&sample_i16, sizeof(int16_t), 1, wav_file_);
@@ -110,8 +117,8 @@ void WavDumpBackend::start() {
// Progress indicator
if (update_count % 60 == 0) {
- printf(" Rendering: %.1fs / %ds (music: %.1fs, tempo: %.2fx)\r",
- physical_time, max_duration_sec, music_time, tempo_scale);
+ printf(" Rendering: %.1fs / %.1fs (music: %.1fs, tempo: %.2fx)\r",
+ physical_time, duration_sec_, music_time, tempo_scale);
fflush(stdout);
}
@@ -120,8 +127,10 @@ void WavDumpBackend::start() {
}
printf(
- "\nWAV dump complete: %zu samples (%.2f seconds stereo, %.2f music time)\n",
- samples_written_, (float)samples_written_ / (kSampleRate * 2), music_time);
+ "\nWAV dump complete: %zu samples (%.2f seconds stereo, %.2f music "
+ "time)\n",
+ samples_written_, (float)samples_written_ / (kSampleRate * 2),
+ music_time);
is_active_ = false;
}
@@ -141,7 +150,7 @@ void WavDumpBackend::write_wav_header(FILE* file, uint32_t num_samples) {
// WAV file header structure
// Reference: http://soundfile.sapp.org/doc/WaveFormat/
- const uint32_t num_channels = 2; // Stereo (matches miniaudio config)
+ const uint32_t num_channels = 2; // Stereo (matches miniaudio config)
const uint32_t sample_rate = kSampleRate;
const uint32_t bits_per_sample = 16;
const uint32_t byte_rate = sample_rate * num_channels * bits_per_sample / 8;
@@ -158,7 +167,7 @@ void WavDumpBackend::write_wav_header(FILE* file, uint32_t num_samples) {
fwrite("fmt ", 1, 4, file);
const uint32_t subchunk1_size = 16;
fwrite(&subchunk1_size, 4, 1, file);
- const uint16_t audio_format = 1; // PCM
+ const uint16_t audio_format = 1; // PCM
fwrite(&audio_format, 2, 1, file);
fwrite(&num_channels, 2, 1, file);
fwrite(&sample_rate, 4, 1, file);
@@ -172,7 +181,8 @@ void WavDumpBackend::write_wav_header(FILE* file, uint32_t num_samples) {
}
void WavDumpBackend::update_wav_header() {
- if (wav_file_ == nullptr) return;
+ if (wav_file_ == nullptr)
+ return;
// Seek to beginning and rewrite header with actual sample count
fseek(wav_file_, 0, SEEK_SET);
diff --git a/src/audio/wav_dump_backend.h b/src/audio/wav_dump_backend.h
index b037fd1..eb6e011 100644
--- a/src/audio/wav_dump_backend.h
+++ b/src/audio/wav_dump_backend.h
@@ -26,8 +26,13 @@ class WavDumpBackend : public AudioBackend {
// Set output filename (call before init())
void set_output_file(const char* filename);
+ // Set duration in seconds (default: 60s, call before start())
+ void set_duration(float seconds);
+
// Get total samples written
- size_t get_samples_written() const { return samples_written_; }
+ size_t get_samples_written() const {
+ return samples_written_;
+ }
private:
// Write WAV header with known sample count
@@ -41,6 +46,7 @@ class WavDumpBackend : public AudioBackend {
size_t samples_written_;
const char* output_filename_;
bool is_active_;
+ float duration_sec_;
static const int kSampleRate = 32000;
static const int kBufferSize = 1024;
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);