summaryrefslogtreecommitdiff
path: root/tools/spectool.cc
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-02 12:33:34 +0100
committerskal <pascal.massimino@gmail.com>2026-02-02 12:33:34 +0100
commit6d41f29b223c51ebca4713028a075ebf2ce44d5e (patch)
tree564c730ad531696ccc32d64423f36e3052c1acd4 /tools/spectool.cc
parent108fbf93e2d3f0c7f7d3a39a597f3d70c00e33bd (diff)
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.
Diffstat (limited to 'tools/spectool.cc')
-rw-r--r--tools/spectool.cc47
1 files changed, 38 insertions, 9 deletions
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<float> 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);