Skip to content

Commit

Permalink
cargo: clippy
Browse files Browse the repository at this point in the history
Ran `cargo +nightly clippy`.
  • Loading branch information
phip1611 committed Apr 28, 2024
1 parent b6769a6 commit 024c854
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
15 changes: 8 additions & 7 deletions src/audio_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ impl PartialEq for SampleInfo {

impl PartialOrd for SampleInfo {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.total_index.partial_cmp(&other.total_index)
Some(self.cmp(other))
}
}

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")
}
}

Expand Down Expand Up @@ -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<Item = f32>>(&mut self, mono_samples_iter: I) {
pub fn update<I: Iterator<Item = f32>>(&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;
});
Expand Down Expand Up @@ -123,7 +124,7 @@ impl AudioHistory {
}

/// Access the underlying data storage.
pub fn data(&self) -> &ConstGenericRingBuffer<f32, DEFAULT_BUFFER_SIZE> {
pub const fn data(&self) -> &ConstGenericRingBuffer<f32, DEFAULT_BUFFER_SIZE> {
&self.audio_buffer
}

Expand Down
6 changes: 3 additions & 3 deletions src/beat_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<Item = f32>>,
input: AudioInput<impl Iterator<Item = f32>>,
) -> Option<BeatInfo> {
match input {
AudioInput::SliceMono(slice) => {
Expand Down
31 changes: 19 additions & 12 deletions src/envelope_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -230,7 +229,15 @@ impl EnvelopeInfo {

impl PartialOrd for EnvelopeInfo {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
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.")
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand 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 {
Expand Down
4 changes: 2 additions & 2 deletions src/max_min_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'a> MaxMinIterator<'a> {
pub fn new(buffer: &'a AudioHistory, begin_index: Option<usize>) -> 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);
Expand All @@ -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;
Expand Down

0 comments on commit 024c854

Please sign in to comment.