diff --git a/brro-compressor/src/main.rs b/brro-compressor/src/main.rs index 4cb4453..0651568 100644 --- a/brro-compressor/src/main.rs +++ b/brro-compressor/src/main.rs @@ -1,9 +1,9 @@ -use std::{path::Path}; +use std::path::Path; use clap::{Parser, command, arg}; -use log::{debug, error, info}; +use log::debug; use brro_compressor::compressor; -use brro_compressor::optimizer::optimizer; +use brro_compressor::optimizer; use brro_compressor::utils::reader; use brro_compressor::utils::writer; @@ -16,8 +16,8 @@ fn process_args(input_path: &str, arguments: &Args) { writer::initialize_directory(&base_dir).expect("Failed to initialize directory"); if arguments.directory { - let (file_contents, file_names) = reader::stream_reader(path).expect("TODO: panic message"); - for (index, data) in file_contents.iter().enumerate() { + let files = reader::stream_reader(path).expect("TODO: panic message"); + for (index, data) in files.contents.iter().enumerate() { let (vec_data, tag) = data; let optimizer_results = optimizer::process_data(vec_data, tag); @@ -30,7 +30,7 @@ fn process_args(input_path: &str, arguments: &Args) { compressed = compressor::constant::constant(&optimizer_results_f); } - let file_name = writer::replace_extension(&file_names[index], "txt)"); + let file_name = writer::replace_extension(&files.names[index], "txt)"); let new_path = base_dir.join(&file_name); let mut file = writer::create_streaming_writer(&new_path).expect("TODO: panic message"); writer::write_data_to_stream(&mut file, &compressed).expect("Failed to write compressed data"); diff --git a/brro-compressor/src/optimizer/mod.rs b/brro-compressor/src/optimizer/mod.rs index bec2274..04d8b51 100644 --- a/brro-compressor/src/optimizer/mod.rs +++ b/brro-compressor/src/optimizer/mod.rs @@ -1 +1,68 @@ -pub mod optimizer; \ No newline at end of file +// Lucas - Once the project is far enough along I strongly reccomend reenabling dead code checks +#![allow(dead_code)] + +use median::Filter; +use log::debug; +use types::metric_tag::MetricTag; +use crate::types; + +impl MetricTag { + #[allow(clippy::wrong_self_convention)] + fn from_float(&self, x: f64) -> i64 { + match self { + MetricTag::Other => { + 0 + } + MetricTag::NotFloat | MetricTag::QuasiRandom => { + x as i64 + } + MetricTag::Percent(y) => { + to_multiply_and_truncate(x, *y) + } + MetricTag::Duration(y) => { + to_multiply_and_truncate(x, *y) + } + MetricTag::Bytes(y) => { + (x as i64) / (*y as i64) + } + } + } +} + +/// Converts a float via multiplication and truncation +fn to_multiply_and_truncate(number: f64, mul: i32) -> i64 { + (number * mul as f64) as i64 +} + +fn to_median_filter(data: &Vec) -> Vec { + let mut filtered = Vec::with_capacity(data.len()); + // 10minutes of data + let mut filter = Filter::new(50); + for point in data { + let point_int = MetricTag::QuasiRandom.from_float(*point); + let median = filter.consume(point_int); + filtered.push(median) + } + filtered +} + + +pub fn process_data(wav_data: &Vec, tag: &MetricTag) -> Vec { + let mut _bitdepth = 64; + let mut _dc_component: i64 = 0; + let mut _fractional = true; + + debug!("Tag: {:?}", tag); + let data = match tag { + MetricTag::Other => Vec::new(), + MetricTag::QuasiRandom => to_median_filter(wav_data), + _ => { + wav_data + .iter() + .map(|x| tag.from_float(*x)) + .collect() + } + }; + _fractional = false; + data +} \ No newline at end of file diff --git a/brro-compressor/src/optimizer/optimizer.rs b/brro-compressor/src/optimizer/optimizer.rs deleted file mode 100644 index f6bf515..0000000 --- a/brro-compressor/src/optimizer/optimizer.rs +++ /dev/null @@ -1,68 +0,0 @@ -// Lucas - Once the project is far enough along I strongly reccomend reenabling dead code checks -#![allow(dead_code)] - -use median::Filter; -use log::{debug, error, info}; -use types::metric_tag::MetricTag; -use crate::types; - -impl MetricTag { - #[allow(clippy::wrong_self_convention)] - fn from_float(&self, x: f64) -> i64 { - match self { - MetricTag::Other => { - 0 - } - MetricTag::NotFloat | MetricTag::QuasiRandom => { - x as i64 - } - MetricTag::Percent(y) => { - to_multiply_and_truncate(x, *y) - } - MetricTag::Duration(y) => { - to_multiply_and_truncate(x, *y) - } - MetricTag::Bytes(y) => { - (x as i64) / (*y as i64) - } - } - } -} - -/// Converts a float via multiplication and truncation -fn to_multiply_and_truncate(number: f64, mul: i32) -> i64 { - (number * mul as f64) as i64 -} - -fn to_median_filter(data: &Vec) -> Vec { - let mut filtered = Vec::with_capacity(data.len()); - // 10minutes of data - let mut filter = Filter::new(50); - for point in data { - let point_int = MetricTag::QuasiRandom.from_float(*point); - let median = filter.consume(point_int); - filtered.push(median) - } - filtered -} - - -pub fn process_data(wav_data: &Vec, tag: &MetricTag) -> Vec { - let mut _bitdepth = 64; - let mut _dc_component: i64 = 0; - let mut _fractional = true; - - debug!("Tag: {:?}", tag); - let data = match tag { - MetricTag::Other => Vec::new(), - MetricTag::QuasiRandom => to_median_filter(&wav_data), - _ => { - wav_data - .iter() - .map(|x| tag.from_float(*x)) - .collect() - } - }; - _fractional = false; - return data; -} \ No newline at end of file diff --git a/brro-compressor/src/utils/reader.rs b/brro-compressor/src/utils/reader.rs index 6be0c3c..72f2a95 100644 --- a/brro-compressor/src/utils/reader.rs +++ b/brro-compressor/src/utils/reader.rs @@ -1,12 +1,13 @@ // Implement a streaming reader here use std::fs; use std::io::{self, Read}; -use std::path::{Path, PathBuf}; +use std::path::Path; use log::debug; use regex::Regex; use types::metric_tag::MetricTag; use crate::types; + // Function to check if a file is a WAV file fn is_wav_file(file_path: &Path) -> io::Result { // Open the file for reading and read the first 12 bytes (header) of the file @@ -25,7 +26,7 @@ fn process_wav_file(file_path: &Path) -> io::Result<(Vec, MetricTag)> { let wav_data = read_metrics_from_wav(full_path_str); // Depending on Metric Tag, apply a transformation let tag = tag_metric(full_path_str); - return Ok((wav_data, tag)); + Ok((wav_data, tag)) } // Function to process a RAW file @@ -35,11 +36,16 @@ fn process_raw_file(file_path: &Path) -> io::Result<()> { Ok(()) } +pub struct Files { + pub contents: Vec<(Vec, MetricTag)>, + pub names: Vec, +} + // Function to read and process files in a directory -pub fn stream_reader(directory_path: &Path) -> io::Result<(Vec<(Vec, MetricTag)>, Vec)> { - let mut results: Vec<(Vec, MetricTag)> = Vec::new(); +pub fn stream_reader(directory_path: &Path) -> io::Result { + let mut contents: Vec<(Vec, MetricTag)> = Vec::new(); - let mut filenames: Vec = Vec::new(); + let mut names: Vec = Vec::new(); // Iterate through entries (files and subdirectories) in the given directory @@ -53,7 +59,7 @@ pub fn stream_reader(directory_path: &Path) -> io::Result<(Vec<(Vec, Metric // Add the filename to the list if let Some(filename) = file_path.file_name() { if let Some(filename_str) = filename.to_str() { - filenames.push(filename_str.to_string()); + names.push(filename_str.to_string()); } } @@ -61,13 +67,13 @@ pub fn stream_reader(directory_path: &Path) -> io::Result<(Vec<(Vec, Metric if is_wav_file(&file_path)? { // If it's a WAV file, process it using the process_wav_file function let wav_result = process_wav_file(&file_path)?; - results.push(wav_result); + contents.push(wav_result); } else { // If it's not a WAV file, process it as a RAW file using the process_raw_file function process_raw_file(&file_path)?; } } - Ok((results, filenames)) + Ok(Files {contents, names}) } /* diff --git a/brro-compressor/src/utils/writer.rs b/brro-compressor/src/utils/writer.rs index 15e53c5..d9d10c7 100644 --- a/brro-compressor/src/utils/writer.rs +++ b/brro-compressor/src/utils/writer.rs @@ -1,6 +1,6 @@ use std::fs::File; use std::io::{self, Write}; -use std::path::Path; +use std::path::{Path, PathBuf}; // Function to create a streaming writer for a file pub fn create_streaming_writer(file_path: &Path) -> io::Result { @@ -10,18 +10,11 @@ pub fn create_streaming_writer(file_path: &Path) -> io::Result { // Function to write data to a streaming writer pub fn write_data_to_stream(writer: &mut File, data: &[u8]) -> io::Result<()> { - // Convert each byte to a string representation and collect them into a Vec - let strings: Vec = data.iter().map(|&byte| byte.to_string()).collect(); - - // Join the Vec into a single String, separated by spaces (or any other delimiter you prefer) - let output = strings.join(" "); - - writer.write_all(output.as_bytes())?; - Ok(()) + writer.write_all(data) } pub fn initialize_directory(base_dir: &Path) -> io::Result<()> { if !base_dir.exists() { - fs::create_dir_all(base_dir)?; + std::fs::create_dir_all(base_dir)?; } Ok(()) }