From f9eef542adc0cfdd6cfbfffa6f15bac2fd6e8e97 Mon Sep 17 00:00:00 2001 From: skal Date: Wed, 28 Jan 2026 03:18:50 +0100 Subject: clang-format --- tools/spectool.cc | 14 +++--- tools/specview.cc | 143 ++++++++++++++++++++++++++++-------------------------- 2 files changed, 81 insertions(+), 76 deletions(-) diff --git a/tools/spectool.cc b/tools/spectool.cc index 292de39..d2f4e54 100644 --- a/tools/spectool.cc +++ b/tools/spectool.cc @@ -101,13 +101,13 @@ int play_spec(const char *in_path) { return 1; } - std::vector spec_data(header.num_frames * header.dct_size); - fread(spec_data.data(), sizeof(float), spec_data.size(), f_in); - fclose(f_in); - - platform_init_window(false); - audio_init(); - Spectrogram spec; + std::vector spec_data(header.num_frames * header.dct_size); + fread(spec_data.data(), sizeof(float), spec_data.size(), f_in); + fclose(f_in); + + platform_init_window(false); + audio_init(); + Spectrogram spec; spec.spectral_data_a = spec_data.data(); spec.spectral_data_b = spec_data.data(); // Point both to the same buffer for playback diff --git a/tools/specview.cc b/tools/specview.cc index 77b362c..d2ce914 100644 --- a/tools/specview.cc +++ b/tools/specview.cc @@ -1,94 +1,99 @@ +#include // For std::max_element #include #include #include -#include // For std::max_element // Redefine SpecHeader from spectool.cc struct SpecHeader { - char magic[4]; - int32_t version; - int32_t dct_size; - int32_t num_frames; + char magic[4]; + int32_t version; + int32_t dct_size; + int32_t num_frames; }; void print_usage() { - printf("Usage: specview \n"); - printf("Displays an ASCII representation of a spectrogram file.\n"); + printf("Usage: specview \n"); + printf("Displays an ASCII representation of a spectrogram file.\n"); } -int main(int argc, char** argv) { - if (argc < 2) { - print_usage(); - return 1; - } +int main(int argc, char **argv) { + if (argc < 2) { + print_usage(); + return 1; + } - const char* input_path = argv[1]; + const char *input_path = argv[1]; - FILE* f_in = fopen(input_path, "rb"); - if (!f_in) { - printf("Error: Failed to open input file: %s\n", input_path); - return 1; - } + FILE *f_in = fopen(input_path, "rb"); + if (!f_in) { + printf("Error: Failed to open input file: %s\n", input_path); + return 1; + } - SpecHeader header; - if (fread(&header, sizeof(SpecHeader), 1, f_in) != 1 || strncmp(header.magic, "SPEC", 4) != 0) { - printf("Error: Invalid spectrogram file format.\n"); - fclose(f_in); - return 1; - } + SpecHeader header; + if (fread(&header, sizeof(SpecHeader), 1, f_in) != 1 || + strncmp(header.magic, "SPEC", 4) != 0) { + printf("Error: Invalid spectrogram file format.\n"); + fclose(f_in); + return 1; + } - if (header.version != 1) { - printf("Error: Unsupported spectrogram version %d.\n", header.version); - fclose(f_in); - return 1; - } + if (header.version != 1) { + printf("Error: Unsupported spectrogram version %d.\n", header.version); + fclose(f_in); + return 1; + } - std::vector spec_data(header.num_frames * header.dct_size); - if (fread(spec_data.data(), sizeof(float), spec_data.size(), f_in) != spec_data.size()) { - printf("Error: Failed to read all spectrogram data.\n"); - fclose(f_in); - return 1; - } + std::vector spec_data(header.num_frames * header.dct_size); + if (fread(spec_data.data(), sizeof(float), spec_data.size(), f_in) != + spec_data.size()) { + printf("Error: Failed to read all spectrogram data.\n"); fclose(f_in); + return 1; + } + fclose(f_in); - printf("Spectrogram: %s\n", input_path); - printf(" DCT Size: %d\n", header.dct_size); - printf(" Num Frames: %d\n", header.num_frames); + printf("Spectrogram: %s\n", input_path); + printf(" DCT Size: %d\n", header.dct_size); + printf(" Num Frames: %d\n", header.num_frames); - // Find max magnitude for normalization - float max_mag = 0.0f; - for (float val : spec_data) { - max_mag = std::max(max_mag, fabsf(val)); - } - if (max_mag == 0.0f) max_mag = 1.0f; // Avoid division by zero + // Find max magnitude for normalization + float max_mag = 0.0f; + for (float val : spec_data) { + max_mag = std::max(max_mag, fabsf(val)); + } + if (max_mag == 0.0f) + max_mag = 1.0f; // Avoid division by zero + + // ASCII visualization + const char *gradient = " .:-=+*#%@"; + int gradient_len = strlen(gradient); + + printf("\nASCII Visualization:\n"); + for (int frame = 0; frame < header.num_frames; ++frame) { + printf("%4d: ", frame); + const float *current_frame_data = + spec_data.data() + frame * header.dct_size; - // ASCII visualization - const char* gradient = " .:-=+*#%@"; - int gradient_len = strlen(gradient); + // Average bins into fewer columns for better fit on console + const int display_cols = 80; // Max console width + const int bins_per_col = + header.dct_size / display_cols; // Divide into 80 bins - printf("\nASCII Visualization:\n"); - for (int frame = 0; frame < header.num_frames; ++frame) { - printf("%4d: ", frame); - const float* current_frame_data = spec_data.data() + frame * header.dct_size; - - // Average bins into fewer columns for better fit on console - const int display_cols = 80; // Max console width - const int bins_per_col = header.dct_size / display_cols; // Divide into 80 bins - - for (int col = 0; col < display_cols; ++col) { - float sum_mag = 0.0f; - for (int bin_idx = 0; bin_idx < bins_per_col; ++bin_idx) { - int current_bin = col * bins_per_col + bin_idx; - if (current_bin < header.dct_size) { - sum_mag += fabsf(current_frame_data[current_bin]); - } - } - float avg_mag = sum_mag / bins_per_col; - int char_idx = (int)((avg_mag / max_mag) * (gradient_len - 1)); - printf("%c", gradient[char_idx]); + for (int col = 0; col < display_cols; ++col) { + float sum_mag = 0.0f; + for (int bin_idx = 0; bin_idx < bins_per_col; ++bin_idx) { + int current_bin = col * bins_per_col + bin_idx; + if (current_bin < header.dct_size) { + sum_mag += fabsf(current_frame_data[current_bin]); } - printf("\n"); + } + float avg_mag = sum_mag / bins_per_col; + int char_idx = (int)((avg_mag / max_mag) * (gradient_len - 1)); + printf("%c", gradient[char_idx]); } + printf("\n"); + } - return 0; + return 0; } -- cgit v1.2.3