From 6d41f29b223c51ebca4713028a075ebf2ce44d5e Mon Sep 17 00:00:00 2001 From: skal Date: Mon, 2 Feb 2026 12:33:34 +0100 Subject: feat(build): Add check_all script and optimize spectool - Task #4b: Added scripts/check_all.sh to build and test all platform targets (native and Windows cross-compile) to ensure pre-commit stability. - Task #10: Modified spectool to trim both leading and trailing silent frames from generated .spec files, reducing asset size. --- tools/spectool.cc | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'tools/spectool.cc') diff --git a/tools/spectool.cc b/tools/spectool.cc index 3188424..a9bf551 100644 --- a/tools/spectool.cc +++ b/tools/spectool.cc @@ -71,23 +71,52 @@ int analyze_audio(const char* in_path, const char* out_path) { ma_decoder_uninit(&decoder); - // Trim trailing zero frames - int last_frame = spec_data.size() / DCT_SIZE; - while (last_frame > 0) { + // --- Trim Silent Frames --- + const float epsilon = 1e-6f; + int num_frames = spec_data.size() / DCT_SIZE; + int first_frame = 0; + int last_frame = num_frames; + + // Trim leading silent frames + for (int i = 0; i < num_frames; ++i) { bool all_zeros = true; - for (int i = 0; i < DCT_SIZE; ++i) { - if (spec_data[(last_frame - 1) * DCT_SIZE + i] != 0.0f) { + for (int j = 0; j < DCT_SIZE; ++j) { + if (fabsf(spec_data[i * DCT_SIZE + j]) > epsilon) { all_zeros = false; break; } } if (all_zeros) { - --last_frame; + first_frame = i + 1; } else { break; } } - spec_data.resize(last_frame * DCT_SIZE); + + // Trim trailing silent frames + for (int i = num_frames - 1; i >= first_frame; --i) { + bool all_zeros = true; + for (int j = 0; j < DCT_SIZE; ++j) { + if (fabsf(spec_data[i * DCT_SIZE + j]) > epsilon) { + all_zeros = false; + break; + } + } + if (all_zeros) { + last_frame = i; + } else { + break; + } + } + + // Create a new vector with the trimmed data + std::vector trimmed_data; + if (first_frame < last_frame) { + trimmed_data.assign(spec_data.begin() + first_frame * DCT_SIZE, + spec_data.begin() + last_frame * DCT_SIZE); + } + + printf("Trimming: Original frames: %d -> Trimmed frames: %zu\n", num_frames, trimmed_data.size() / DCT_SIZE); // Write to .spec file FILE* f_out = fopen(out_path, "wb"); @@ -100,10 +129,10 @@ int analyze_audio(const char* in_path, const char* out_path) { memcpy(header.magic, "SPEC", 4); header.version = 1; header.dct_size = DCT_SIZE; - header.num_frames = spec_data.size() / DCT_SIZE; + header.num_frames = trimmed_data.size() / DCT_SIZE; fwrite(&header, sizeof(SpecHeader), 1, f_out); - fwrite(spec_data.data(), sizeof(float), spec_data.size(), f_out); + fwrite(trimmed_data.data(), sizeof(float), trimmed_data.size(), f_out); fclose(f_out); printf("Analysis complete. Wrote %d spectral frames.\n", header.num_frames); -- cgit v1.2.3