Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil-Golikov committed Oct 11, 2023
1 parent 8f8dd39 commit 27db256
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
26 changes: 9 additions & 17 deletions brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn process_single_file(path: &Path, arguments: &Args) {

if let Some(filename_osstr) = path.file_name() {
if let Some(filename_str) = filename_osstr.to_str() {
let new_filename_string = writer::replace_extension(&filename_str.to_string(),"bin");
let new_filename_string = writer::replace_extension(&filename_str.to_string(), "bin");
let new_path = path.parent().unwrap().join(new_filename_string);
write_compressed_data_to_path(&compressed_data, &new_path);
}
Expand All @@ -62,27 +62,18 @@ fn compress_data(vec: &Vec<f64>, tag: &MetricTag, arguments: &Args) -> Vec<u8> {
let optimizer_results = optimizer::process_data(vec, tag);
let optimizer_results_f: Vec<f64> = optimizer_results.iter().map(|&x| x as f64).collect();

let cs = CompressedStream::new();
let mut cs = CompressedStream::new();
if arguments.constant {
compress_file(Compressor::Constant, cs, optimizer_results_f)
cs.compress_chunk_with(&optimizer_results_f, Compressor::Constant);
cs.to_bytes()
} else {
compress_file(Compressor::Noop, cs, optimizer_results_f)
}
}
fn compress_file(compressor: Compressor, mut stream: CompressedStream, wav_content: Vec<f64>) -> Vec<u8> {
return match compressor {
Compressor::Constant =>{
stream.compress_chunk_with(&wav_content, Compressor::Constant);
stream.to_bytes()
}
_ =>{
stream.compress_chunk_with(&wav_content, Compressor::Noop);
stream.to_bytes()
}
cs.compress_chunk_with(&optimizer_results_f, Compressor::Noop);
cs.to_bytes()
}
}

/// Writes the compressed data to the specified path.
fn write_compressed_data_to_path(compressed: &Vec<u8>, path: &Path) {
fn write_compressed_data_to_path(compressed: &[u8], path: &Path) {
let mut file = writer::create_streaming_writer(path).expect("Failed to create a streaming writer");
writer::write_data_to_stream(&mut file, compressed).expect("Failed to write compressed data");
}
Expand All @@ -105,6 +96,7 @@ struct Args {
#[arg(long, action)]
constant: bool,
}

fn main() {
env_logger::init();
let arguments = Args::parse();
Expand Down
24 changes: 11 additions & 13 deletions brro-compressor/src/utils/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn is_wav_file(file_path: &Path) -> io::Result<bool> {
}

// Function to process a WAV file
pub fn process_wav_file(file_path: &Path) -> io::Result<(Vec<f64>, MetricTag)> {
fn process_wav_file(file_path: &Path) -> io::Result<(Vec<f64>, 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);
Expand All @@ -42,7 +42,7 @@ pub struct Files {
}

/// Read a file by chunks and processes the chunks
pub fn process_by_chunk(file_path: &Path) -> Result<(Vec<Vec<u8>>), std::io::Error> {
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)
Expand All @@ -59,7 +59,7 @@ pub fn process_by_chunk(file_path: &Path) -> Result<(Vec<Vec<u8>>), std::io::Err
list_of_chunks.push(chunk);
if n < chunk_size { break; }
}
Ok(list_of_chunks)
Ok(())
}

// Function to read and process files in a directory
Expand All @@ -85,25 +85,23 @@ pub fn stream_reader(directory_path: &Path) -> io::Result<Files> {
}

// Check if the file is a WAV file
let res= read_file(&file_path)?;
match res {
Some((vec, tag)) => contents.push((vec, tag)),
_ => (),
}
let res = read_file(&file_path)?;
if let Some((vec, tag)) = res { contents.push((vec, tag)) }
}
Ok(Files {contents, names})
}

pub fn read_file(file_path: &Path) -> Result<Option<(Vec<f64>, MetricTag)>, Error> {
if is_wav_file(&file_path)? {
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)?;
return Ok(Option::from(wav_result));
let wav_result = process_wav_file(file_path)?;
Ok(Option::from(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)?;
process_raw_file(file_path)?;
Ok(None)
}
Ok(None)

}
/*
Reads a WAV file, checks the channels and the information contained there. From that
Expand Down

0 comments on commit 27db256

Please sign in to comment.