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

Compress stream #39

Merged
merged 4 commits into from
Oct 11, 2023
Merged

Compress stream #39

merged 4 commits into from
Oct 11, 2023

Conversation

cjrolo
Copy link
Collaborator

@cjrolo cjrolo commented Oct 10, 2023

This PR adds the capacity to compress a file with proper headers and structure.

Decompression is not yet tested. That would be a next pull request

Compressor::Constant => constant(data),
_ => todo!(),
_ => noop(data),
Copy link
Contributor

@rukai rukai Oct 10, 2023

Choose a reason for hiding this comment

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

imo its a lot better to halt the program and say "hey that compression type isnt implemented yet" i.e. todo!() than to silently do something incorrect. Makes debugging easier.

@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This match is effectively the same as .unwrap(), either .unwrap() or maybe use ? instead to bubble up the error.

/// Gets a binary stream and generates a Compressed Stream
pub fn from_bytes(data: &[u8]) -> Self {
let config = BinConfig::get();
match bincode::decode_from_slice(data, config) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This match is effectively the same as .unwrap(), maybe just dobincode::decode_from_slice(data, config).unwrap().0 instead.
Or maybe let (compressed_stream, _) = bincode::decode_from_slice(data, config).unwrap();

Copy link
Contributor

@rukai rukai left a comment

Choose a reason for hiding this comment

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

Left a few nits, the actual real problems here you have already marked as TODO's which is fine by me.

Copy link
Contributor

@Daniil-Golikov Daniil-Golikov left a comment

Choose a reason for hiding this comment

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

Agree with all the aforementioned points

/// 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,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can use unwrap_or(CompressedStream::new()) instead of this match

@@ -14,4 +17,78 @@ impl CompressedStream {
data_frames: Vec::new(),
}
}

/// Compress a chunk of data adding it as a new frame to the current stream
pub fn compress_chunk(&mut self, chunk: &[f64]) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This function is not needed. Remove it, we can just pass None to the next one.

@cjrolo cjrolo merged commit d1ee240 into main Oct 11, 2023
2 checks passed
@cjrolo cjrolo deleted the CompressStream branch January 10, 2024 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants