-
Notifications
You must be signed in to change notification settings - Fork 1
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
Wavbrro improvements #69
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6baf406
Proper error control
cjrolo 67643f5
TODO added
cjrolo e70a2ed
Misc. improvements
cjrolo d8bd77e
Merge branch 'main' into wavbrro-improvements
joshuabvarghese 04cf9fb
Merge branch 'main' into wavbrro-improvements
cjrolo b0a8d94
Fixed bad merge
cjrolo 3ccce65
Merge branch 'main' into wavbrro-improvements
cjrolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
filename=$1 | ||
target/debug/brro-compressor --compressor fft --error 1 --verbose ../../wbro-july/$filename.wbro > ../../$filename.m | ||
target/debug/brro-compressor -u --verbose ../../wbro-july/$filename.bro >> ../../$filename.m | ||
echo "plot(Input,'b', Output,'r')" >> ../../$filename.m | ||
echo "print -dpng $filename.png" >> ../../$filename.m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
use rkyv::{Archive, Deserialize, Serialize}; | ||
use std::{io, fmt, result, error}; | ||
use std::path::Path; | ||
|
||
use crate::read::{is_wavbrro_file, read_wavbrro_file}; | ||
|
@@ -83,12 +84,12 @@ impl WavBrro { | |
|
||
// This should be generic, but first implementation is going to be Vec f64 | ||
// TODO: This will panic left and right, make it right | ||
pub fn from_file(file_path: &Path) -> Vec<f64> { | ||
pub fn from_file(file_path: &Path) -> Result<Vec<f64>, Error> { | ||
// Check if the header is correct | ||
assert!(is_wavbrro_file(file_path)); | ||
let bytes = read_wavbrro_file(file_path).unwrap(); | ||
if !is_wavbrro_file(file_path)? {return Err(Error::FormatError);}; | ||
let bytes = read_wavbrro_file(file_path)?; | ||
let obj = WavBrro::from_bytes(&bytes); | ||
obj.get_samples() | ||
Ok(obj.get_samples()) | ||
} | ||
|
||
// TODO: This will panic left and right, make it right | ||
|
@@ -114,6 +115,84 @@ impl WavBrro { | |
|
||
} | ||
|
||
// Error class is based on https://codeberg.org/ruuda/hound/src/branch/master given the similarities | ||
// between the formats (WAV and WAVBRRO). | ||
#[derive(Debug)] | ||
pub enum Error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://docs.rs/thiserror/latest/thiserror/ can be helpful when writing error enums |
||
/// An IO error occured in the underlying reader or writer. | ||
IoError(io::Error), | ||
/// It's not WAVBRRO | ||
FormatError, | ||
/// The sample has more bits than the destination type. | ||
/// | ||
/// When iterating using the `samples` iterator, this means that the | ||
/// destination type (produced by the iterator) is not wide enough to hold | ||
/// the sample. When writing, this means that the sample cannot be written, | ||
/// because it requires more bits than the bits per sample specified. | ||
TooWide, | ||
/// The Sample format is not supported. | ||
Unsupported, | ||
/// The sample format is different than the destination format. | ||
/// | ||
/// When iterating using the `samples` iterator, this means the destination | ||
/// type (produced by the iterator) has a different sample format than the | ||
/// samples in the wav file. | ||
/// | ||
/// For example, this will occur if the user attempts to produce `i32` | ||
/// samples (which have a `SampleFormat::Int`) from a wav file that | ||
/// contains floating point data (`SampleFormat::Float`). | ||
InvalidSampleFormat, | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, formatter: &mut fmt::Formatter) -> result::Result<(), fmt::Error> { | ||
match *self { | ||
Error::IoError(ref err) => err.fmt(formatter), | ||
Error::FormatError => { | ||
formatter.write_str("Wrong WAVBRRO file!") | ||
} | ||
Error::TooWide => { | ||
formatter.write_str("The sample has more bits than the destination type.") | ||
} | ||
Error::Unsupported => { | ||
formatter.write_str("The WAVBRRO format of the file is not supported.") | ||
} | ||
Error::InvalidSampleFormat => { | ||
formatter.write_str("The sample format differs from the destination format.") | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl From<io::Error> for Error { | ||
fn from(err: io::Error) -> Error { | ||
Error::IoError(err) | ||
} | ||
} | ||
|
||
impl error::Error for Error { | ||
fn description(&self) -> &str { | ||
match *self { | ||
// TODO: I don't know if this is actually the right way to do! | ||
Error::IoError(ref _err) => "IO Error", | ||
Error::TooWide => "the sample has more bits than the destination type", | ||
Error::Unsupported => "the wave format of the file is not supported", | ||
Error::InvalidSampleFormat => "the sample format differs from the destination format", | ||
Error::FormatError => "the file is not of the WAVBRRO format", | ||
} | ||
} | ||
|
||
fn cause(&self) -> Option<&dyn error::Error> { | ||
match *self { | ||
Error::IoError(ref err) => Some(err), | ||
Error::TooWide => None, | ||
Error::Unsupported => None, | ||
Error::InvalidSampleFormat => None, | ||
Error::FormatError => None, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
@@ -152,7 +231,7 @@ mod tests { | |
wb.add_sample(3.0); | ||
wb.to_file(path); | ||
let result = is_wavbrro_file(path); | ||
assert!(result); | ||
assert!(result.unwrap()); | ||
std::fs::remove_file(path).expect("Failed to remove temporary file"); | ||
} | ||
|
||
|
@@ -165,7 +244,7 @@ mod tests { | |
wb.add_sample(3.0); | ||
wb.to_file(path); | ||
let data = WavBrro::from_file(path); | ||
assert_eq!(data, [1.0, 2.0, 3.0]); | ||
assert_eq!(data.unwrap(), [1.0, 2.0, 3.0]); | ||
std::fs::remove_file(path).expect("Failed to remove temporary file"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what this means lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you walk a dir with
std::fs::read_dir
it will call the underlying OS function, and if new files are created while you're walking the dir, they will be returned. Since the compressor is creating files,entry
will be one of the created files here and there, and there will be an output WARN/ERROR message.A check has to be created for this case and ignore those files.