1
0
Fork 0
mirror of https://github.com/alemidev/scope-tui.git synced 2024-11-26 23:54:48 +01:00

chore: thiserror, removed mut + comments, editorcfg

This commit is contained in:
əlemi 2023-09-17 03:51:44 +02:00
parent ba0174961a
commit 925659639a
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 13 additions and 39 deletions

View file

@ -6,3 +6,5 @@ charset = utf-8
indent_style = tab indent_style = tab
indent_size = 4 indent_size = 4
[*.rs]
indent_size = 2

View file

@ -17,3 +17,5 @@ crossterm = "0.25"
libpulse-binding = "2.0" libpulse-binding = "2.0"
libpulse-simple-binding = "2.25" libpulse-simple-binding = "2.25"
clap = { version = "4.0.32", features = ["derive"] } clap = { version = "4.0.32", features = ["derive"] }
derive_more = "0.99.17"
thiserror = "1.0.48"

View file

@ -5,7 +5,8 @@ pub enum Tone {
C, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B C, Db, D, Eb, E, F, Gb, G, Ab, A, Bb, B
} }
pub struct ToneError {} #[derive(Debug, thiserror::Error, derive_more::Display)]
pub struct ToneError();
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub struct Note { pub struct Note {
@ -13,23 +14,12 @@ pub struct Note {
octave: u32, octave: u32,
} }
#[derive(Debug, thiserror::Error, derive_more::From, derive_more::Display)]
pub enum NoteError { pub enum NoteError {
InvalidOctave(ParseIntError), InvalidOctave(ParseIntError),
InalidNote(ToneError), InalidNote(ToneError),
} }
impl From::<ToneError> for NoteError {
fn from(e: ToneError) -> Self {
NoteError::InalidNote(e)
}
}
impl From::<ParseIntError> for NoteError {
fn from(e: ParseIntError) -> Self {
NoteError::InvalidOctave(e)
}
}
impl FromStr for Note { impl FromStr for Note {
type Err = NoteError; type Err = NoteError;
@ -52,13 +42,6 @@ impl FromStr for Note {
} }
} }
// impl TryFrom::<String> for Note {
// type Error = NoteError;
// fn try_from(value: String) -> Result<Self, Self::Error> {
// value.as_str().parse::<Note>()
// }
// }
impl FromStr for Tone { impl FromStr for Tone {
type Err = ToneError; type Err = ToneError;
@ -76,7 +59,7 @@ impl FromStr for Tone {
"A" => Ok(Tone::A ), "A" => Ok(Tone::A ),
"A#" | "Bb" => Ok(Tone::Bb), "A#" | "Bb" => Ok(Tone::Bb),
"B" => Ok(Tone::B ), "B" => Ok(Tone::B ),
_ => Err(ToneError { }) _ => Err(ToneError())
} }
} }
} }
@ -85,15 +68,8 @@ impl Note {
pub fn tune_buffer_size(&self, sample_rate: u32) -> u32 { pub fn tune_buffer_size(&self, sample_rate: u32) -> u32 {
let t = 1.0 / self.tone.freq(self.octave); // periodo ? let t = 1.0 / self.tone.freq(self.octave); // periodo ?
let buf = (sample_rate as f32) * t; let buf = (sample_rate as f32) * t;
return (buf * 4.0).round() as u32; (buf * 4.0).round() as u32
} }
// pub fn tune_sample_rate(&self, octave:u32, buffer_size: u32) -> u32 {
// // TODO does it just work the same way?
// let t = 1.0 / self.freq(octave); // periodo ?
// let buf = (buffer_size as f32) * t;
// return (buf * 4.0).round() as u32;
// }
} }
impl Tone { impl Tone {
@ -122,9 +98,5 @@ impl Tone {
} }
} }
} }
// pub fn all() -> Vec<Note> {
// vec![Note::C, Note::Db, Note::D, Note::Eb, Note::E, Note::F, Note::Gb, Note::G, Note::Ab, Note::A, Note::Bb, Note::B]
// }
} }

View file

@ -8,15 +8,15 @@
// } // }
pub trait SampleParser { pub trait SampleParser {
fn oscilloscope(&self, data: &mut [u8], channels: u8) -> Vec<Vec<f64>>; fn oscilloscope(&self, data: &[u8], channels: u8) -> Vec<Vec<f64>>;
fn sample_size(&self) -> usize; fn sample_size(&self) -> usize;
} }
pub struct Signed16PCM {} pub struct Signed16PCM();
/// TODO these are kinda inefficient, can they be faster? /// TODO these are kinda inefficient, can they be faster?
impl SampleParser for Signed16PCM { impl SampleParser for Signed16PCM {
fn oscilloscope(&self, data: &mut [u8], channels: u8) -> Vec<Vec<f64>> { fn oscilloscope(&self, data: &[u8], channels: u8) -> Vec<Vec<f64>> {
let mut out = vec![vec![]; channels as usize]; let mut out = vec![vec![]; channels as usize];
let mut channel = 0; let mut channel = 0;
for chunk in data.chunks(2) { for chunk in data.chunks(2) {
@ -27,7 +27,5 @@ impl SampleParser for Signed16PCM {
out out
} }
fn sample_size(&self) -> usize { fn sample_size(&self) -> usize { 2 } // 16 bit, thus 2 bytes
return 2; // 16 bit, thus 2 bytes
}
} }