diff options
Diffstat (limited to 'src/tests/gpu/test_spectool.cc')
| -rw-r--r-- | src/tests/gpu/test_spectool.cc | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/tests/gpu/test_spectool.cc b/src/tests/gpu/test_spectool.cc new file mode 100644 index 0000000..984322a --- /dev/null +++ b/src/tests/gpu/test_spectool.cc @@ -0,0 +1,69 @@ +// 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 <assert.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "miniaudio.h" + +// struct SpecHeader { ... } -> now in audio.h + +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; +} |
