Skip to content

Commit

Permalink
Merge pull request #67 from instaclustr/wavcleanup
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
joshuabvarghese authored Nov 10, 2023
2 parents 3675d82 + 9035bd5 commit 4495232
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 258 deletions.
1 change: 1 addition & 0 deletions brro-compressor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ fn process_directory(arguments: &Args) -> Result<(), Box<dyn Error>> {
for entry in std::fs::read_dir(arguments.input.clone())? {
let path = entry?.path();
if path.is_file() {
// We need to make sure we skip anything but BRO and WBRO, this can be done on single file processors
process_single_file(path, arguments)?;
}
}
Expand Down
2 changes: 2 additions & 0 deletions brro-compressor/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use log::debug;
use types::metric_tag::MetricTag;
use crate::{types, utils::{prev_power_of_two, f64_to_u64}, compressor::Compressor};

pub mod utils;

/// Max Frame size, this can aprox. 36h of data at 1point/sec rate, a little more than 1 week at 1point/5sec
/// and 1 month (30 days) at 1 point/20sec.
/// This would be aprox. 1MB of Raw data (131072 * 64bits).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
use std::path::PathBuf;
use hound::{WavSpec, WavWriter};
use log::info;
use log::debug;

// Function to create a streaming writer for a file
pub fn write_optimal_wav(mut path: PathBuf, data: Vec<f64>, channels: i32) {
let (bitdepth, dc, _fractional) = analyze_data(&data);
// Make DC a float for operations
let fdc = dc as f64;
let header: WavSpec = generate_wav_header(Some(channels), bitdepth as u16, 8000);
path.set_extension("wav");
let file = std::fs::OpenOptions::new().write(true).create(true).read(true).open(path).unwrap();
let mut wav_writer = WavWriter::new(file, header).unwrap();
for sample in data {
let _ = match bitdepth {
8 => wav_writer.write_sample(as_i8(sample-fdc)),
16 => wav_writer.write_sample(as_i16(sample-fdc)),
_ => wav_writer.write_sample(as_i32(sample-fdc))
};
}
let _ = wav_writer.finalize();
}
fn as_i8(value: f64) -> i8 {
split_n(value).0 as i8
}
Expand Down Expand Up @@ -71,25 +51,6 @@ fn split_n(x: f64) -> (i64, f64) {
}
}

fn join_u16_into_f64(bits: [u16; 4]) -> f64 {
let u64_bits = (bits[0] as u64) |
((bits[1] as u64) << 16) |
((bits[2] as u64) << 32) |
((bits[3] as u64) << 48);


f64::from_bits(u64_bits)
}
fn generate_wav_header(channels: Option<i32>, bitdepth: u16, samplerate: u32) -> WavSpec {

hound::WavSpec {
channels: channels.unwrap_or(4) as u16,
// TODO: Sample rate adaptations
sample_rate: samplerate,
bits_per_sample: bitdepth,
sample_format: hound::SampleFormat::Int
}
}
fn analyze_data(data: &Vec<f64>) -> (i32, i64, bool) {
let mut min: f64 = 0.0;
let mut max: f64 = 0.0;
Expand All @@ -112,9 +73,9 @@ fn analyze_data(data: &Vec<f64>) -> (i32, i64, bool) {
// Finding the bitdepth without the DC component
let recommended_bitdepth = find_bitdepth(max_int-min_int, min_int);
if !fractional {
info!(" Recommended Bitdepth: {} ", recommended_bitdepth);
debug!(" Recommended Bitdepth: {} ", recommended_bitdepth);
} else {
info!(" Fractional, Recommended Bitdepth: {}, Fractions max: {}", recommended_bitdepth, max_frac);
debug!(" Fractional, Recommended Bitdepth: {}, Fractions max: {}", recommended_bitdepth, max_frac);
}
(recommended_bitdepth, min_int, fractional)
}
Expand All @@ -135,6 +96,5 @@ fn find_bitdepth(max_int: i64, min_int: i64) -> i32 {
_ => 64
};


bitdepth.max(bitdepth_signed)
}
28 changes: 0 additions & 28 deletions brro-compressor/src/utils/file_type_detector.rs

This file was deleted.

3 changes: 0 additions & 3 deletions brro-compressor/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pub mod error;
pub mod writers;
pub mod readers;

mod file_type_detector;

// Is this the right place?
pub fn prev_power_of_two(n: usize) -> usize {
// n = 0 gives highest_bit_set_idx = 0.
Expand Down
3 changes: 1 addition & 2 deletions brro-compressor/src/utils/readers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod bro_reader;
pub mod wav_reader;
pub mod bro_reader;
174 changes: 0 additions & 174 deletions brro-compressor/src/utils/readers/wav_reader.rs

This file was deleted.

1 change: 0 additions & 1 deletion brro-compressor/src/utils/writers/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
pub mod wav_writer;
15 changes: 8 additions & 7 deletions wavbrro/src/wavbrro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ impl WavBrro {
}

// 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;
fn from_slice(data: &[f64]) -> Self {
let sample_count = data.len();
WavBrro {
sample_count: sample_count as u32,
bitdepth: 5,
chunks: data.chunks(MAX_CHUNK_SIZE).map(|s| s.into()).collect()
}
}

pub fn add_sample(&mut self, sample: f64) {
Expand Down Expand Up @@ -91,8 +93,7 @@ impl WavBrro {

// 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 wb = WavBrro::from_slice(data);
let bytes = wb.to_bytes();
write_wavbrro_file(file_path, &bytes);
}
Expand Down

0 comments on commit 4495232

Please sign in to comment.