Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compressed stream file output #41

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 59 additions & 34 deletions brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,84 @@
use std::path::Path;
use clap::{Parser, command, arg};
use log::debug;

use brro_compressor::compressor;
use brro_compressor::compressor::Compressor;
use brro_compressor::optimizer;
use brro_compressor::utils::reader;
use brro_compressor::utils::writer;
use brro_compressor::data::CompressedStream;
use brro_compressor::types::metric_tag::MetricTag;

/// 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
}

/// Processes the given input based on the provided arguments.
/// If `arguments.directory` is true, it processes all files in the directory.
/// Otherwise, it processes the individual file.
fn process_args(input_path: &str, arguments: &Args) {
let path = Path::new(input_path);

// If the input path points to a directory
if arguments.directory {
process_directory(path, arguments);
}
// If the input path points to a single file
else {
process_single_file(path, arguments);
}
}

/// Processes all files in a given directory.
fn process_directory(path: &Path, arguments: &Args) {
let new_name = format!("{}-compressed", path.file_name().unwrap().to_string_lossy());
let base_dir = path.with_file_name(new_name);

writer::initialize_directory(&base_dir).expect("Failed to initialize directory");
let files = reader::stream_reader(path).expect("Failed to read files from directory");

if arguments.directory {
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);
let optimizer_results_f: Vec<f64> = optimizer_results.iter().map(|&x| x as f64).collect();

let mut compressed: Vec<u8> = Vec::new();
if arguments.noop {
compressed = compressor::noop::noop(&optimizer_results_f);
} else if arguments.constant {
compressed = compressor::constant::constant(&optimizer_results_f);
}
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");
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");
let file_name = writer::replace_extension(&files.names[index], "bin");
let new_path = base_dir.join(&file_name);
write_compressed_data_to_path(&compressed_data, &new_path);
}
}

/// Processes a single file.
fn process_single_file(path: &Path, arguments: &Args) {
if let Some((vec, tag)) = reader::read_file(path).expect("Failed to read file") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of the error control here is always related to handling files. Either chain and propagate the error via ? or "unwrap()" and let it panic in case of failure anyway (We can't proceed and we haven't done anything at this stage).

let compressed_data = compress_data(&vec, &tag, arguments);

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_path = path.parent().unwrap().join(new_filename_string);
write_compressed_data_to_path(&compressed_data, &new_path);
}
}
}
}

/// Compresses the data based on the provided tag and arguments.
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 mut cs = CompressedStream::new();
if arguments.constant {
cs.compress_chunk_with(&optimizer_results_f, Compressor::Constant);
cs.to_bytes()
} else {
// TODO: Make this do something...
let cs = compress_file(None, Vec::new());
cs.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: &[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");
}


#[derive(Parser, Default, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
Expand All @@ -69,7 +95,6 @@ struct Args {
/// Forces Constant compressor
#[arg(long, action)]
constant: bool,

}

fn main() {
Expand Down
24 changes: 15 additions & 9 deletions brro-compressor/src/utils/reader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Implement a streaming reader here
use std::fs;
use std::io::{self, Read};
use std::io::{self, Error, Read};
use std::path::Path;
use log::debug;
use regex::Regex;
Expand Down Expand Up @@ -85,18 +85,24 @@ pub fn stream_reader(directory_path: &Path) -> io::Result<Files> {
}

// Check if the file is a WAV file
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)?;
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)?;
}
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 it's a WAV file, process it using the process_wav_file function
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)?;
Ok(None)
}

}
/*
Reads a WAV file, checks the channels and the information contained there. From that
information takes a decision on the best channel, block size and bitrate for the BRRO
Expand Down