From 529731253f5add0faea24bf1194d3fd966291059 Mon Sep 17 00:00:00 2001 From: Carlos Rolo <3799585+cjrolo@users.noreply.github.com> Date: Fri, 13 Oct 2023 15:32:05 +0100 Subject: [PATCH] Compressors working! --- brro-compressor/src/compressor/constant.rs | 1 + brro-compressor/src/compressor/noop.rs | 3 ++- brro-compressor/src/main.rs | 17 ++++++++++------- brro-compressor/src/optimizer/mod.rs | 4 +++- brro-compressor/src/utils/reader.rs | 6 ++++++ 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/brro-compressor/src/compressor/constant.rs b/brro-compressor/src/compressor/constant.rs index 62e1f71..48da3e5 100644 --- a/brro-compressor/src/compressor/constant.rs +++ b/brro-compressor/src/compressor/constant.rs @@ -82,6 +82,7 @@ impl Constant { .map(|(k, v)| (k.try_into().unwrap(), *v)) .collect(); } + debug!("Compressed {} elements into {} elements!",data.len(), self.residuals.len()+1); } /// Receives a data stream and generates a Constant diff --git a/brro-compressor/src/compressor/noop.rs b/brro-compressor/src/compressor/noop.rs index d7a4e49..97d6b8c 100644 --- a/brro-compressor/src/compressor/noop.rs +++ b/brro-compressor/src/compressor/noop.rs @@ -33,6 +33,7 @@ impl Noop { /// "Compress" pub fn compress(&mut self, data: &[f64]) { self.data = Noop::optimize(data); + debug!("Compressed {} elements into {} elements!",data.len(), self.data.len()); } /// Receives a data stream and generates a Noop @@ -55,7 +56,7 @@ impl Noop { } pub fn noop(data: &[f64]) -> Vec { - info!("[Compressor] Initializing Noop Compressor"); + info!("Initializing Noop Compressor"); let mut c = Noop::new(data.len()); c.compress(data); c.to_bytes() diff --git a/brro-compressor/src/main.rs b/brro-compressor/src/main.rs index 06b32ae..e643480 100644 --- a/brro-compressor/src/main.rs +++ b/brro-compressor/src/main.rs @@ -38,8 +38,8 @@ fn process_directory(arguments: &Args) { for (index, data) in files.contents.iter().enumerate() { let (vec_data, tag) = data; let compressed_data = compress_data(vec_data, tag, arguments); - - let file_name = writer::replace_extension(&files.names[index], "bin"); + // BRO extension + let file_name = writer::replace_extension(&files.names[index], "bro"); let new_path = base_dir.join(&file_name); write_compressed_data_to_path(&compressed_data, &new_path); } @@ -47,13 +47,14 @@ fn process_directory(arguments: &Args) { /// Processes a single file. fn process_single_file(arguments: &Args) { + debug!("Processing single file..."); let (vec, tag) = reader::read_file(&arguments.input).expect("Failed to read file"); let compressed_data = compress_data(&vec, &tag, arguments); - if let Some(filename_osstr) = arguments.input.file_name() { if let Some(filename_str) = filename_osstr.to_str() { + // BRO extension let new_filename_string = - writer::replace_extension(&filename_str.to_string(), "bin"); + writer::replace_extension(&filename_str.to_string(), "bro"); let new_path = arguments.input.parent().unwrap().join(new_filename_string); write_compressed_data_to_path(&compressed_data, &new_path); } @@ -62,15 +63,16 @@ fn process_single_file(arguments: &Args) { /// Compresses the data based on the provided tag and arguments. fn compress_data(vec: &Vec, tag: &MetricTag, arguments: &Args) -> Vec { + debug!("Compressing data!"); let optimizer_results = optimizer::process_data(vec, tag); - let optimizer_results_f: Vec = optimizer_results.iter().map(|&x| x as f64).collect(); + let _optimizer_results_f: Vec = optimizer_results.iter().map(|&x| x as f64).collect(); let mut cs = CompressedStream::new(); if arguments.constant { - cs.compress_chunk_with(&optimizer_results_f, Compressor::Constant); + cs.compress_chunk_with(vec, Compressor::Constant); cs.to_bytes() } else { - cs.compress_chunk_with(&optimizer_results_f, Compressor::Noop); + cs.compress_chunk_with(vec, Compressor::Noop); cs.to_bytes() } } @@ -88,6 +90,7 @@ struct Args { /// input file input: PathBuf, + /// Processes a directory instead of a single file #[arg(short, action)] directory: bool, diff --git a/brro-compressor/src/optimizer/mod.rs b/brro-compressor/src/optimizer/mod.rs index 04d8b51..685e98c 100644 --- a/brro-compressor/src/optimizer/mod.rs +++ b/brro-compressor/src/optimizer/mod.rs @@ -46,7 +46,9 @@ fn to_median_filter(data: &Vec) -> Vec { filtered } - +/// This should look at the data and return an optimized dataset for a specific compressor, +/// If a compressor is hand picked, this should be skipped. +/// TODO: Make it do that pub fn process_data(wav_data: &Vec, tag: &MetricTag) -> Vec { let mut _bitdepth = 64; let mut _dc_component: i64 = 0; diff --git a/brro-compressor/src/utils/reader.rs b/brro-compressor/src/utils/reader.rs index 571016a..29bc416 100644 --- a/brro-compressor/src/utils/reader.rs +++ b/brro-compressor/src/utils/reader.rs @@ -24,6 +24,7 @@ fn process_wav_file(file_path: &Path) -> io::Result<(Vec, MetricTag)> { let full_path_str = file_path.to_str().unwrap_or(""); debug!("File: {} ,", full_path_str); let wav_data = read_metrics_from_wav(full_path_str); + debug!("Data Len: {}", wav_data.len()); // Depending on Metric Tag, apply a transformation let tag = tag_metric(full_path_str); Ok((wav_data, tag)) @@ -34,6 +35,11 @@ fn process_raw_file(file_path: &Path) -> io::Result<(Vec, MetricTag)> { todo!("Handle RAW file processing here (for example, decoding or encoding): {file_path:?}"); } +// Function to process a BRRO (Compressed) file +fn process_brro_file(file_path: &Path) -> io::Result<(Vec, MetricTag)> { + todo!("Handle BRRO file processing here (for example, decoding or encoding): {file_path:?}"); +} + pub struct Files { pub contents: Vec<(Vec, MetricTag)>, pub names: Vec,