diff --git a/brro-compressor/src/data.rs b/brro-compressor/src/data.rs index fee72d9..8b705ca 100644 --- a/brro-compressor/src/data.rs +++ b/brro-compressor/src/data.rs @@ -16,6 +16,7 @@ impl CompressedStream { } } + /// Compress a chunk of data adding it as a new frame to the current stream pub fn compress_chunk(&mut self, chunk: &[f64]) { let mut compressor_frame = CompressorFrame::new(None); compressor_frame.compress(chunk); @@ -23,6 +24,7 @@ impl CompressedStream { self.data_frames.push(compressor_frame); } + /// Compress a chunk of data with a specific compressor adding it as a new frame to the current stream pub fn compress_chunk_with(&mut self, chunk: &[f64], compressor: Compressor) { let mut compressor_frame = CompressorFrame::new(Some(compressor)); compressor_frame.compress(chunk); diff --git a/brro-compressor/src/lib.rs b/brro-compressor/src/lib.rs index e28faa3..da40d53 100644 --- a/brro-compressor/src/lib.rs +++ b/brro-compressor/src/lib.rs @@ -7,8 +7,8 @@ pub mod compressor; pub mod frame; pub mod preprocessor; pub mod utils; -mod header; -mod data; +pub mod header; +pub mod data; pub mod optimizer; pub mod types; diff --git a/brro-compressor/src/main.rs b/brro-compressor/src/main.rs index c91c3dd..af1e1b6 100644 --- a/brro-compressor/src/main.rs +++ b/brro-compressor/src/main.rs @@ -6,6 +6,18 @@ use brro_compressor::compressor; use brro_compressor::optimizer; use brro_compressor::utils::reader; use brro_compressor::utils::writer; +use brro_compressor::data::CompressedStream; + +/// Process a chunk of WAV content and compresses it +/// If a stream is provided it adds a chunk to that stream, otherwise creates a new one +fn compress_file(stream: Option, wav_content: Vec) -> CompressedStream { + let mut cs = match stream { + Some(cs) => cs, + None => CompressedStream::new() + }; + cs.compress_chunk(&wav_content); + cs +} fn process_args(input_path: &str, arguments: &Args) { let path = Path::new(input_path); @@ -35,7 +47,8 @@ fn process_args(input_path: &str, arguments: &Args) { writer::write_data_to_stream(&mut file, &compressed).expect("Failed to write compressed data"); } } else { - // process_file(input_path.into()); + // TODO: Make this do something... + compress_file(None, Vec::new()); } } diff --git a/brro-compressor/src/utils/reader.rs b/brro-compressor/src/utils/reader.rs index 72f2a95..276f6e3 100644 --- a/brro-compressor/src/utils/reader.rs +++ b/brro-compressor/src/utils/reader.rs @@ -41,6 +41,27 @@ pub struct Files { pub names: Vec, } +/// Read a file by chunks and processes the chunks +pub fn process_by_chunk(file_path: &Path) -> Result<(), std::io::Error> { + let mut file = match std::fs::File::open(file_path) { + Ok(f) => f, + Err(e) => panic!("{}", e) + }; + + let mut list_of_chunks = Vec::new(); + // 64KB at a time, assuming 64Bit samples, ~1024 samples. + let chunk_size = 0x10000; + + loop { + let mut chunk = Vec::with_capacity(chunk_size); + let n = file.by_ref().take(chunk_size as u64).read_to_end(&mut chunk)?; + if n == 0 { break; } + list_of_chunks.push(chunk); + if n < chunk_size { break; } + } + Ok(()) +} + // Function to read and process files in a directory pub fn stream_reader(directory_path: &Path) -> io::Result { let mut contents: Vec<(Vec, MetricTag)> = Vec::new();