diff options
| -rw-r--r-- | CMakeLists.txt | 15 | ||||
| -rw-r--r-- | HOWTO.md | 1 | ||||
| -rw-r--r-- | src/tests/test_spectool.cpp | 91 | ||||
| -rw-r--r-- | tools/spectool.cpp | 2 |
4 files changed, 107 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 7828ada..5f1167f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,6 +58,21 @@ if(DEMO_BUILD_TESTS) src ) add_test(NAME SynthEngineTest COMMAND test_synth) + + add_executable(test_spectool + src/tests/test_spectool.cpp + src/audio/audio.cpp # For miniaudio implementation + src/audio/window.cpp + src/audio/fdct.cpp + src/audio/synth.cpp + src/audio/idct.cpp + ) + target_include_directories(test_spectool PRIVATE + src + third_party + ) + add_dependencies(test_spectool spectool) + add_test(NAME SpectoolEndToEndTest COMMAND test_spectool) endif() option(DEMO_BUILD_TOOLS "Build tools" OFF) @@ -30,6 +30,7 @@ To build and run the tests, you need to enable the `DEMO_BUILD_TESTS` option in Available test suites: * `HammingWindowTest`: Verifies the properties of the Hamming window function. * `SynthEngineTest`: Verifies the core functionality of the audio synthesizer. +* `SpectoolEndToEndTest`: Performs an end-to-end test of the `spectool` by generating a WAV file, analyzing it, and verifying the output. ```bash cmake -S . -B build -DDEMO_BUILD_TESTS=ON diff --git a/src/tests/test_spectool.cpp b/src/tests/test_spectool.cpp new file mode 100644 index 0000000..ae64cc3 --- /dev/null +++ b/src/tests/test_spectool.cpp @@ -0,0 +1,91 @@ +#include <stdio.h> +#include <assert.h> +#include <stdlib.h> // For system() +#include <vector> +#include <math.h> +#include "miniaudio.h" +#include "audio/dct.h" // For DCT_SIZE + +// Re-defining the header here to avoid dependency on the tool's source +struct SpecHeader { + char magic[4]; + int32_t version; + int32_t dct_size; + int32_t num_frames; +}; + +#define TEST_SAMPLE_RATE 32000 +#define TEST_DURATION_SECONDS 1 +#define TEST_FREQ 440.0f +#define PI 3.14159265f + +int main() { + printf("Running spectool end-to-end test...\n"); + + const char* wav_path = "test_signal.wav"; + const char* spec_path = "test_signal.spec"; + const char* spectool_path = "./spectool"; // Assumes ctest is run from `build` dir + + // 1. Generate and save a WAV file + ma_encoder_config enc_config = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 1, TEST_SAMPLE_RATE); + ma_encoder encoder; + if (ma_encoder_init_file(wav_path, &enc_config, &encoder) != MA_SUCCESS) { + printf("TEST FAILED: Could not initialize WAV encoder.\n"); + return 1; + } + + std::vector<float> pcm_data(TEST_SAMPLE_RATE * TEST_DURATION_SECONDS); + for (size_t i = 0; i < pcm_data.size(); ++i) { + pcm_data[i] = 0.5f * sinf(2.0f * PI * TEST_FREQ * i / TEST_SAMPLE_RATE); + } + ma_uint64 frames_written; + ma_encoder_write_pcm_frames(&encoder, pcm_data.data(), pcm_data.size(), &frames_written); + ma_encoder_uninit(&encoder); + printf(" Generated %s\n", wav_path); + + // 2. Run spectool analyze + char command[512]; + snprintf(command, sizeof(command), "%s analyze %s %s", spectool_path, wav_path, spec_path); + printf(" Executing: %s\n", command); + int return_code = system(command); + assert(return_code == 0); + printf(" spectool executed successfully.\n"); + + // 3. Verify the output .spec file + FILE* f_spec = fopen(spec_path, "rb"); + assert(f_spec != NULL); + + SpecHeader header; + fread(&header, sizeof(SpecHeader), 1, f_spec); + assert(strncmp(header.magic, "SPEC", 4) == 0); + assert(header.version == 1); + assert(header.dct_size == DCT_SIZE); + + int expected_frames = (TEST_SAMPLE_RATE * TEST_DURATION_SECONDS) / DCT_SIZE; + if ((TEST_SAMPLE_RATE * TEST_DURATION_SECONDS) % DCT_SIZE != 0) { + expected_frames++; + } + assert(header.num_frames == expected_frames); + + printf(" .spec header verified.\n"); + + // Just check that we have some non-zero data + std::vector<float> spec_data(header.num_frames * header.dct_size); + fread(spec_data.data(), sizeof(float), spec_data.size(), f_spec); + fclose(f_spec); + + double total_energy = 0.0; + for(float val : spec_data) { + total_energy += fabs(val); + } + assert(total_energy > 0.0); + printf(" .spec data seems valid.\n"); + + // 4. Cleanup + remove(wav_path); + remove(spec_path); + printf(" Cleaned up temporary files.\n"); + + printf("...spectool test PASSED!\n"); + return 0; +} diff --git a/tools/spectool.cpp b/tools/spectool.cpp index 3b3d3aa..defb118 100644 --- a/tools/spectool.cpp +++ b/tools/spectool.cpp @@ -6,8 +6,6 @@ #include "platform.h" #include "audio/audio.h" -// Define MINIAUDIO_IMPLEMENTATION in one C/C++ file. -#define MINIAUDIO_IMPLEMENTATION #include "miniaudio.h" #include <vector> |
