Skip to content

Commit

Permalink
wavbrro integrated into main!
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrolo committed Nov 3, 2023
1 parent 301b495 commit 6316b55
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion brro-compressor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ tempfile = "3.2"
average = "0.14.1"
regex = "1.9.1"
hound = "3.5"
median = "0.3.2"
median = "0.3.2"
wavbrro = { path = "../wavbrro" }
33 changes: 15 additions & 18 deletions brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use brro_compressor::compressor::Compressor;
use brro_compressor::data::CompressedStream;
use brro_compressor::optimizer::OptimizerPlan;
use brro_compressor::types::metric_tag::MetricTag;
use brro_compressor::utils::readers::{bro_reader, wav_reader};
use brro_compressor::utils::writers::wav_writer;
use brro_compressor::utils::readers::bro_reader;
use wavbrro::wavbrro::WavBrro;
use clap::{arg, command, Parser};
use log::{debug, error};
use std::error::Error;
Expand Down Expand Up @@ -55,29 +54,27 @@ fn process_single_file(mut file_path: PathBuf, arguments: &Args) -> Result<(), B
if arguments.verbose {
println!("Output={:?}", decompressed_data);
}
// TODO: Decompression shouldn't optimize the WAV
wav_writer::write_optimal_wav(file_path, decompressed_data, 1);
file_path.set_extension("wbro");
WavBrro::to_file_with_data(&file_path, &decompressed_data)
}
} else {
//read
if let Some(data) = wav_reader::read_file(&file_path)? {
let (vec, tag) = data;
if arguments.verbose {
println!("Input={:?}", vec);
}
//compress
let compressed_data = compress_data(&vec, &tag, arguments);

//write
file_path.set_extension("bro");
std::fs::write(file_path, compressed_data)?;
// Read an WavBRRO file and compress it
let data = WavBrro::from_file(&file_path);
if arguments.verbose {
println!("Input={:?}", data);
}
//compress
let compressed_data = compress_data(&data, arguments);

//write
file_path.set_extension("bro");
std::fs::write(file_path, compressed_data)?;
}
Ok(())
}

/// Compresses the data based on the provided tag and arguments.
fn compress_data(vec: &[f64], _tag: &MetricTag, arguments: &Args) -> Vec<u8> {
fn compress_data(vec: &[f64], arguments: &Args) -> Vec<u8> {
debug!("Compressing data!");
//let optimizer_results = optimizer::process_data(vec, tag);
// Create Optimization Plan and Stream for the data.
Expand Down
8 changes: 4 additions & 4 deletions brro-compressor/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ fn compress_dir(compressor: &str) {
let tmp_dir = tempdir().unwrap();
let input = tmp_dir.path().join("input");
std::fs::create_dir(&input).unwrap();
std::fs::copy("tests/wavs/memory_used.wav", input.join("1.wav")).unwrap();
std::fs::copy("tests/wavs/uptime.wav", input.join("2.wav")).unwrap();
std::fs::copy("tests/wbros/memory_used.wbro", input.join("1.wbro")).unwrap();
std::fs::copy("tests/wbros/uptime.wbro", input.join("2.wbro")).unwrap();

run_compressor(&[input.to_str().unwrap(), "--compressor", compressor]);
assert!(input.join("1.bro").is_file());
Expand All @@ -35,10 +35,10 @@ fn compress_dir(compressor: &str) {
fn compress_file(compressor: &str) {
let tmp_dir = tempdir().unwrap();
let path = tmp_dir.path();
std::fs::copy("tests/wavs/memory_used.wav", path.join("1.wav")).unwrap();
std::fs::copy("tests/wbros/memory_used.wbro", path.join("1.wbro")).unwrap();

run_compressor(&[
path.join("1.wav").to_str().unwrap(),
path.join("1.wbro").to_str().unwrap(),
"--compressor",
compressor,
]);
Expand Down
Binary file not shown.
Binary file not shown.
10 changes: 8 additions & 2 deletions tools/src/bin/wav2wbro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ fn join_u16_into_f64(bits: [u16; 4]) -> f64 {
((bits[3] as u64) << 48);


f64::from_bits(u64_bits)
let out = f64::from_bits(u64_bits);
if out.is_infinite() || out.is_nan() {
debug!("Found NaN/Infinite!");
}
out
}
// --- Legacy ends (I need to stop lying to myself...) ---

Expand All @@ -71,13 +75,15 @@ fn main() {
assert!(is_wav_file(&arguments.input));
let wav_data = read_metrics_from_wav(filename);
let mut wb = WavBrro::new();
wav_data.iter().for_each(|x| wb.add_sample(*x));
// Clean NaN
wav_data.iter().for_each(|x| if !x.is_nan() {wb.add_sample(*x)});
// Write the file
let wavbrro_file = format!("{}wbro", filename.strip_suffix("wav").unwrap());
wb.to_file(Path::new(&wavbrro_file));
// Checking the results
if arguments.validate {
let brro_data = wb.get_samples();
assert_eq!(wav_data, brro_data);
println!("File generated but data doesn't match! Tip: Check if NaN or Infinite is in the data.");
}
}
16 changes: 16 additions & 0 deletions wavbrro/src/wavbrro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ impl WavBrro {
self.chunks.push(Vec::with_capacity(MAX_CHUNK_SIZE));
}

// Receives a slice of f64 and writes in it's internal structure
fn load_slice(&mut self, data: &[f64]) {
let size = data.len();
let inner = data.chunks(MAX_CHUNK_SIZE).map(|s| s.into()).collect();
self.chunks = inner;
self.sample_count = size as u32;
}

pub fn add_sample(&mut self, sample: f64) {
if self.is_chunk_full() { self.create_chunk() }
self.chunks.last_mut().unwrap().push(sample);
Expand All @@ -81,6 +89,14 @@ impl WavBrro {
obj.get_samples()
}

// TODO: This will panic left and right, make it right
pub fn to_file_with_data(file_path: &Path, data: &[f64]) {
let mut wb = WavBrro::new();
wb.load_slice(data);
let bytes = wb.to_bytes();
write_wavbrro_file(file_path, &bytes);
}

// TODO: This will panic left and right, make it right
pub fn to_file(&self, file_path: &Path) {
let bytes = self.to_bytes();
Expand Down

0 comments on commit 6316b55

Please sign in to comment.