// This file is part of the 64k demo project. // It performs an end-to-end test of the spectool's analysis capability. // Generates a test WAV, analyzes it, and verifies the resulting .spec file. #include "audio/audio.h" #include #include #include #include #include #include "miniaudio.h" // Redefine SpecHeader to avoid including spectool internals if possible, // but for an E2E test we need to know the format. struct SpecHeader { char magic[4]; int32_t version; int32_t dct_size; int32_t num_frames; }; void generate_test_wav(const char *path, int duration_seconds) { ma_encoder_config config = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 1, 32000); ma_encoder encoder; if (ma_encoder_init_file(path, &config, &encoder) != MA_SUCCESS) { printf("Failed to create test WAV file.\n"); exit(1); } int num_frames = 32000 * duration_seconds; for (int i = 0; i < num_frames; ++i) { float sample = 0.5f * sinf(2.0f * 3.14159f * 440.0f * i / 32000.0f); ma_encoder_write_pcm_frames(&encoder, &sample, 1, NULL); } ma_encoder_uninit(&encoder); } int main() { const char *test_wav = "test_input.wav"; const char *test_spec = "test_output.spec"; printf("Generating test WAV...\n"); generate_test_wav(test_wav, 1); printf("Running spectool analyze...\n"); char command[256]; snprintf(command, sizeof(command), "./spectool analyze %s %s", test_wav, test_spec); int ret = system(command); assert(ret == 0); printf("Verifying .spec file...\n"); FILE *f = fopen(test_spec, "rb"); assert(f != NULL); SpecHeader header; size_t read = fread(&header, sizeof(SpecHeader), 1, f); assert(read == 1); assert(strncmp(header.magic, "SPEC", 4) == 0); assert(header.version == 1); assert(header.dct_size == 512); assert(header.num_frames > 0); fclose(f); printf("Spectool E2E test PASSED\n"); // Clean up remove(test_wav); remove(test_spec); return 0; }