Skip to content

Commit

Permalink
Formatted with rustfmt and added to to pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmarsc committed Sep 2, 2022
1 parent a7f84d1 commit 7827e1e
Show file tree
Hide file tree
Showing 23 changed files with 1,066 additions and 826 deletions.
9 changes: 8 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ repos:
language: system
args: ["--", "-D", "warnings"]
types: [rust]
pass_filenames: false
pass_filenames: false
- id: fmt
name: fmt
description: Check files formating with cargo fmt.
entry: cargo fmt
language: system
types: [rust]
args: ["--", "--check"]
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ apt-get install libsndfile1-dev
```

# Installation
TBD
```
cargo install audeye
```

# Build
1. [Install Rust](https://www.rust-lang.org/tools/install)
Expand All @@ -59,6 +61,13 @@ TBD
## Development
Please consider audeye is still in early development, feedbacks are very welcome

### Requirements
- [cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html)
- [libsndfile](#dependencies)
- [precommit](https://pre-commit.com/#install)
- [clippy](https://github.com/rust-lang/rust-clippy)
- [rustfmt](https://github.com/rust-lang/rustfmt)

### Contributing
If you wanna contribute, either make a PR (for small changes/adds) or contact me
on twitter / discord if you wanna get involved more deeply
Expand Down
54 changes: 28 additions & 26 deletions src/dsp/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt;
use std::marker::PhantomData;
use std::sync::mpsc::{Receiver, self};
use std::thread::{JoinHandle, self};
use std::sync::mpsc::{self, Receiver};
use std::thread::{self, JoinHandle};

extern crate sndfile;
use crate::sndfile::SndFile;
Expand All @@ -10,13 +10,13 @@ use super::normalization::compute_norm;

#[derive(Debug)]
pub struct DspErr {
msg: String
msg: String,
}

impl DspErr {
pub fn new(msg: &str) -> Self {
Self{
msg: msg.to_string()
Self {
msg: msg.to_string(),
}
}
}
Expand All @@ -28,28 +28,29 @@ impl fmt::Display for DspErr {
}

pub trait DspData<P> {
fn new(file: SndFile, parameter: P, normalize: Option<f64>) -> Result<Self, DspErr> where Self: Sized;
fn new(file: SndFile, parameter: P, normalize: Option<f64>) -> Result<Self, DspErr>
where
Self: Sized;
}


#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AsyncDspDataState {
Created,
Normalizing,
Processing,
Failed,
Finished
Finished,
}

pub struct AsyncDspData<T : DspData<P> + Send + 'static, P : Send + 'static> {
pub struct AsyncDspData<T: DspData<P> + Send + 'static, P: Send + 'static> {
state: AsyncDspDataState,
pub data: Option<T>,
rendered_rx: Receiver<AsyncDspDataState>,
process_handle: Option<JoinHandle<T>>,
phantom : PhantomData<P>
phantom: PhantomData<P>,
}

impl<T : DspData<P> + Send + 'static, P : Send + 'static> AsyncDspData<T, P> {
impl<T: DspData<P> + Send + 'static, P: Send + 'static> AsyncDspData<T, P> {
pub fn update_status(&mut self) -> bool {
let mut update_needed = false;

Expand All @@ -61,27 +62,27 @@ impl<T : DspData<P> + Send + 'static, P : Send + 'static> AsyncDspData<T, P> {
self.state = AsyncDspDataState::Finished;
update_needed = true;
break;
},
}
Ok(AsyncDspDataState::Failed) => {
// Failed to render, try to join to catch error
let opt_handle = self.process_handle.take();
match opt_handle {
Some(handle) => {
match handle.join() {
Ok(_) => panic!("Async rendering sent failed signal but succeeded"),
Err(err) => panic!("{:?}", err)
}
Some(handle) => match handle.join() {
Ok(_) => panic!("Async rendering sent failed signal but succeeded"),
Err(err) => panic!("{:?}", err),
},
None => panic!("Async rendering handle is None")
None => panic!("Async rendering handle is None"),
}
},
}
Ok(new_state) => {
self.state = new_state;
update_needed = true;
}
_ => { break; }
_ => {
break;
}
}
};
}

update_needed
}
Expand All @@ -99,14 +100,15 @@ impl<T : DspData<P> + Send + 'static, P : Send + 'static> AsyncDspData<T, P> {
match opt_handle {
Some(handle) => {
self.data = Some(handle.join().expect("Async rendering failed"));
},
None => panic!("Async rendering handle is None")
}
None => panic!("Async rendering handle is None"),
}
}

pub fn new(path: &std::path::PathBuf, parameters: P, normalize: bool) -> Self {
let mut snd = sndfile::OpenOptions::ReadOnly(sndfile::ReadOptions::Auto)
.from_path(path).expect("Could not open wave file");
.from_path(path)
.expect("Could not open wave file");
if !snd.is_seekable() {
panic!("Input file is not seekable");
}
Expand All @@ -131,7 +133,7 @@ impl<T : DspData<P> + Send + 'static, P : Send + 'static> AsyncDspData<T, P> {
// Success, we update the state and return the data
let _ = rendered_tx.send(AsyncDspDataState::Finished);
data
},
}
Err(dsp_err) => {
// Failure, we stop the program and display the error
let _ = rendered_tx.send(AsyncDspDataState::Failed);
Expand All @@ -145,7 +147,7 @@ impl<T : DspData<P> + Send + 'static, P : Send + 'static> AsyncDspData<T, P> {
data: None,
rendered_rx,
process_handle: Some(join_handle),
phantom: PhantomData::default()
phantom: PhantomData::default(),
}
}
}
10 changes: 6 additions & 4 deletions src/dsp/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod data;
mod normalization;
mod spectrogram;
mod time_window;
mod waveform;
mod data;
mod normalization;

pub use data::{AsyncDspData, AsyncDspDataState, DspData, DspErr};
pub use spectrogram::{Spectrogram, SpectrogramParameters};
pub use time_window::{
SidePaddingType, SidePaddingTypeParseError, WindowType, WindowTypeParseError, PADDING_HELP_TEXT,
};
pub use waveform::{Waveform, WaveformParameters, WaveformPoint};
pub use data::{DspData, DspErr, AsyncDspData, AsyncDspDataState};
pub use time_window::{WindowType, WindowTypeParseError, SidePaddingType, SidePaddingTypeParseError, PADDING_HELP_TEXT};
4 changes: 2 additions & 2 deletions src/dsp/normalization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ fn clamp(val: &i32) -> i32 {
pub fn compute_norm(sndfile: &mut SndFile) -> f64 {
let data: Vec<i32> = sndfile.read_all_to_vec().unwrap();

let max = data.par_iter().map(|val| { clamp(val).abs()}).max().unwrap();
let max = data.par_iter().map(|val| clamp(val).abs()).max().unwrap();

if max <= 0i32 {
return f64::EPSILON;
}
max as f64 / i32::MAX as f64
}
}
Loading

0 comments on commit 7827e1e

Please sign in to comment.