Skip to main content

Strategy

Trait Strategy 

Source
pub trait Strategy {
    // Required methods
    fn is_beat(&self, samples: &[i16]) -> Option<BeatInfo>;
    fn kind(&self) -> StrategyKind;
    fn name() -> &'static str
       where Self: Sized;
    fn description() -> &'static str
       where Self: Sized;
    fn min_duration_between_beats_ms() -> u32
       where Self: Sized;

    // Provided methods
    fn last_beat_beyond_threshold(&self, state: &AnalysisState) -> bool
       where Self: Sized { ... }
    fn amplitude_high_enough(&self, w_stats: &WindowStats) -> bool { ... }
}
Expand description

Common abstraction over a beat detection strategy. Each strategy keeps ongoing audio samples, for example from microphone. Strategies should have an internal mutable state via interior mutability to compare sample windows (and analysis) against previous values.

Required Methods§

Source

fn is_beat(&self, samples: &[i16]) -> Option<BeatInfo>

Checks if inside the samples window a new beat was recognized. If so, it returns Some with BeatInfo as payload.

Implementations may buffer previous samples and combine them with the latest, i.e. make a sliding window.

Source

fn kind(&self) -> StrategyKind

Convenient getter to get the StrategyKind of a strategy. This is a 1:1 mapping.

Source

fn name() -> &'static str
where Self: Sized,

A nice name for the algorithm, displayable in user interfaces.

Source

fn description() -> &'static str
where Self: Sized,

A textual description of the algorithm to help the user to select the right one.

Source

fn min_duration_between_beats_ms() -> u32
where Self: Sized,

Duration in ms after each beat. Useful do prevent the same beat to be detected as two beats. This is a constant per strategy, because more advanced strategies can cope with small durations (50ms) whereas “stupid”/basic strategies may need 400ms. This is a function instead of an associated constant, because otherwise the build fails with “Strategy cannot be made into an object”

Provided Methods§

Source

fn last_beat_beyond_threshold(&self, state: &AnalysisState) -> bool
where Self: Sized,

Common implementation for all strategies which checks if the last beat is beyond the threshold. Of not, we can return early and do not need to check if a beat is in the given sample.

Source

fn amplitude_high_enough(&self, w_stats: &WindowStats) -> bool

Common implementation for all strategies which checks if the current windows/frames max amplitude (i16) is above a value where a beat could happen in theory (discard noise/silence/break between songs)

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§