From 024c85442a7477a3815dbaa03530c1417b8ad161 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Sun, 28 Apr 2024 13:47:11 +0200 Subject: [PATCH] cargo: clippy Ran `cargo +nightly clippy`. --- src/audio_history.rs | 15 ++++++++------- src/beat_detector.rs | 6 +++--- src/envelope_iterator.rs | 31 +++++++++++++++++++------------ src/lib.rs | 9 +++++---- src/max_min_iterator.rs | 4 ++-- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/audio_history.rs b/src/audio_history.rs index 9d3da3b..c0ebb78 100644 --- a/src/audio_history.rs +++ b/src/audio_history.rs @@ -51,7 +51,7 @@ impl PartialEq for SampleInfo { impl PartialOrd for SampleInfo { fn partial_cmp(&self, other: &Self) -> Option { - self.total_index.partial_cmp(&other.total_index) + Some(self.cmp(other)) } } @@ -59,7 +59,9 @@ impl Eq for SampleInfo {} impl Ord for SampleInfo { fn cmp(&self, other: &Self) -> Ordering { - self.partial_cmp(other).unwrap() + self.total_index + .partial_cmp(&other.total_index) + .expect("Should be comparable") } } @@ -87,14 +89,13 @@ impl AudioHistory { /// Update the audio history with fresh samples. The audio samples are /// expected to be in mono channel format. - pub fn update<'a, I: Iterator>(&mut self, mono_samples_iter: I) { + pub fn update>(&mut self, mono_samples_iter: I) { let mut len = 0; mono_samples_iter - .inspect(|sample| { + .for_each(|sample| { debug_assert!(sample.is_finite()); debug_assert!(sample.abs() <= 1.0); - }) - .for_each(|sample| { + self.audio_buffer.push(sample); len += 1; }); @@ -123,7 +124,7 @@ impl AudioHistory { } /// Access the underlying data storage. - pub fn data(&self) -> &ConstGenericRingBuffer { + pub const fn data(&self) -> &ConstGenericRingBuffer { &self.audio_buffer } diff --git a/src/beat_detector.rs b/src/beat_detector.rs index 865697e..5bc86e7 100644 --- a/src/beat_detector.rs +++ b/src/beat_detector.rs @@ -42,7 +42,7 @@ pub struct BeatDetector { impl BeatDetector { pub fn new(sampling_frequency_hz: f32) -> Self { - let lowpass_filter = BeatDetector::create_lowpass_filter(sampling_frequency_hz); + let lowpass_filter = Self::create_lowpass_filter(sampling_frequency_hz); Self { lowpass_filter, history: AudioHistory::new(sampling_frequency_hz), @@ -59,9 +59,9 @@ impl BeatDetector { /// /// From experience, Linux audio input libraries give you a 20-40ms audio /// buffer every 20-40ms with the latest data. That's a good rule of thumb. - pub fn detect_beat<'a>( + pub fn detect_beat( &mut self, - input: AudioInput<'a, impl Iterator>, + input: AudioInput>, ) -> Option { match input { AudioInput::SliceMono(slice) => { diff --git a/src/envelope_iterator.rs b/src/envelope_iterator.rs index da0adf5..f0f6128 100644 --- a/src/envelope_iterator.rs +++ b/src/envelope_iterator.rs @@ -67,8 +67,8 @@ impl Iterator for EnvelopeIterator<'_> { // Skip noise. let envelope_begin = MaxMinIterator::new(self.buffer, Some(self.index)) - .skip_while(|info| info.value.abs() < ENVELOPE_MIN_VALUE) - .next()?; + // Find the first item that is not noise. + .find(|info| info.value.abs() >= ENVELOPE_MIN_VALUE)?; // Update index to prevent unnecessary iterations on next // invocation. @@ -165,22 +165,21 @@ fn find_descending_peak_trend_end(buffer: &AudioHistory, begin_index: usize) -> let next_to_current_factor = val_next / val_curr; debug_assert!(next_to_current_factor > 1.0); - let nextnext_continues_descending_trend = next_to_current_factor - <= MAX_NEXT_TO_CURR_OUT_OF_LINE_FACTOR - && val_nextnext <= val_curr; - nextnext_continues_descending_trend + // nextnext continues descending trend + next_to_current_factor <= MAX_NEXT_TO_CURR_OUT_OF_LINE_FACTOR + && val_nextnext <= val_curr }) .last() .map(|(current, _)| current) } /// Information about an envelope. -#[derive(Clone, Copy, Debug, Default, Eq, Ord)] +#[derive(Clone, Copy, Debug, Default, Eq)] pub struct EnvelopeInfo { - from: SampleInfo, - to: SampleInfo, - max: SampleInfo, + pub from: SampleInfo, + pub to: SampleInfo, + pub max: SampleInfo, } impl EnvelopeInfo { @@ -207,7 +206,7 @@ impl EnvelopeInfo { /// |___| /// |___| /// ``` - pub fn overlap(&self, other: &Self) -> bool { + pub const fn overlap(&self, other: &Self) -> bool { let self_from = self.from.total_index; let self_to = self.to.total_index; let other_from = other.from.total_index; @@ -230,7 +229,15 @@ impl EnvelopeInfo { impl PartialOrd for EnvelopeInfo { fn partial_cmp(&self, other: &Self) -> Option { - self.from.partial_cmp(&other.from) + Some(self.cmp(other)) + } +} + +impl Ord for EnvelopeInfo { + fn cmp(&self, other: &Self) -> Ordering { + self.from + .partial_cmp(&other.from) + .expect("Only valid f32 should be here.") } } diff --git a/src/lib.rs b/src/lib.rs index d8c8665..cf5b1ee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,8 @@ SOFTWARE. #![allow( clippy::suboptimal_flops, clippy::redundant_pub_crate, - clippy::fallible_impl_from + clippy::fallible_impl_from, + clippy::multiple_crate_versions )] #![deny(missing_debug_implementations)] #![deny(rustdoc::all)] @@ -87,16 +88,16 @@ mod envelope_iterator; mod max_min_iterator; mod root_iterator; +mod beat_detector; /// PRIVATE. For tests and helper binaries. #[cfg(test)] mod test_utils; -mod beat_detector; pub use audio_history::{AudioHistory, SampleInfo}; -pub use envelope_iterator::{EnvelopeIterator, EnvelopeInfo}; +pub use beat_detector::{AudioInput, BeatDetector, BeatInfo}; +pub use envelope_iterator::{EnvelopeInfo, EnvelopeIterator}; use max_min_iterator::MaxMinIterator; use root_iterator::RootIterator; -pub use beat_detector::{BeatDetector, BeatInfo, AudioInput}; #[cfg(test)] mod tests { diff --git a/src/max_min_iterator.rs b/src/max_min_iterator.rs index 86ae5c7..afd270b 100644 --- a/src/max_min_iterator.rs +++ b/src/max_min_iterator.rs @@ -23,7 +23,7 @@ impl<'a> MaxMinIterator<'a> { pub fn new(buffer: &'a AudioHistory, begin_index: Option) -> Self { let index = begin_index.unwrap_or(0); assert!(index < buffer.data().len()); - let index = RootIterator::new(&buffer, Some(index)) + let index = RootIterator::new(buffer, Some(index)) .next() .map(|info| info.index) .unwrap_or(buffer.data().len() - 1); @@ -42,7 +42,7 @@ impl Iterator for MaxMinIterator<'_> { } let begin_index = self.index; - let end_index = RootIterator::new(&self.buffer, Some(begin_index)) + let end_index = RootIterator::new(self.buffer, Some(begin_index)) .next()? .index; let sample_count = end_index - begin_index;