Skip to content

Commit

Permalink
Chunk processing, Compress Method
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrolo committed Oct 6, 2023
1 parent 2d2245c commit 1ee4b6e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions brro-compressor/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ 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);
compressor_frame.close();
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);
Expand Down
4 changes: 2 additions & 2 deletions brro-compressor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
15 changes: 14 additions & 1 deletion brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompressedStream>, wav_content: Vec<f64>) -> 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);
Expand Down Expand Up @@ -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());
}
}

Expand Down
21 changes: 21 additions & 0 deletions brro-compressor/src/utils/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ pub struct Files {
pub names: Vec<String>,
}

/// 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<Files> {
let mut contents: Vec<(Vec<f64>, MetricTag)> = Vec::new();
Expand Down

0 comments on commit 1ee4b6e

Please sign in to comment.