1
0
Fork 0
mirror of https://github.com/alemidev/scope-tui.git synced 2024-09-19 21:54:07 +02: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_size = 4
[*.rs]
indent_size = 2

View file

@ -17,3 +17,5 @@ crossterm = "0.25"
libpulse-binding = "2.0"
libpulse-simple-binding = "2.25"
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
}
pub struct ToneError {}
#[derive(Debug, thiserror::Error, derive_more::Display)]
pub struct ToneError();
#[derive(Debug, PartialEq, Clone)]
pub struct Note {
@ -13,23 +14,12 @@ pub struct Note {
octave: u32,
}
#[derive(Debug, thiserror::Error, derive_more::From, derive_more::Display)]
pub enum NoteError {
InvalidOctave(ParseIntError),
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 {
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 {
type Err = ToneError;
@ -76,7 +59,7 @@ impl FromStr for Tone {
"A" => Ok(Tone::A ),
"A#" | "Bb" => Ok(Tone::Bb),
"B" => Ok(Tone::B ),
_ => Err(ToneError { })
_ => Err(ToneError())
}
}
}
@ -85,15 +68,8 @@ impl Note {
pub fn tune_buffer_size(&self, sample_rate: u32) -> u32 {
let t = 1.0 / self.tone.freq(self.octave); // periodo ?
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 {
@ -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 {
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;
}
pub struct Signed16PCM {}
pub struct Signed16PCM();
/// TODO these are kinda inefficient, can they be faster?
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 channel = 0;
for chunk in data.chunks(2) {
@ -27,7 +27,5 @@ impl SampleParser for Signed16PCM {
out
}
fn sample_size(&self) -> usize {
return 2; // 16 bit, thus 2 bytes
}
fn sample_size(&self) -> usize { 2 } // 16 bit, thus 2 bytes
}