Struct soundtouch::BPMDetect

source ·
pub struct BPMDetect(/* private fields */);
Expand description

Beats-per-minute (BPM) detection routine.

The beat detection algorithm works as follows:

  • Use function input_samples to input a chunks of samples to the class for analysis. It’s a good idea to enter a large sound file or stream in smallish chunks of around few kilosamples in order not to extinguish too much RAM memory.
  • Input sound data is decimated to approx 500 Hz to reduce calculation burden, which is basically ok as low (bass) frequencies mostly determine the beat rate. Simple averaging is used for anti-alias filtering because the resulting signal quality isn’t of that high importance.
  • Decimated sound data is enveloped, i.e. the amplitude shape is detected by taking absolute value that’s smoothed by sliding average. Signal levels that are below a couple of times the general RMS amplitude level are cut away to leave only notable peaks there.
  • Repeating sound patterns (e.g. beats) are detected by calculating short-term autocorrelation function of the enveloped signal.
  • After whole sound data file has been analyzed as above, the bpm level is detected by function get_bpm that finds the highest peak of the autocorrelation function, calculates it’s precise location and converts this reading to bpm’s.

Implementations§

source§

impl BPMDetect

source

pub fn new(num_channels: u32, sample_rate: u32) -> Self

Creates a new BPMDetect instance with the given channels and sample rate.

source

pub fn input_samples(&mut self, samples: &[f32])

Inputs a block of samples for analyzing: Envelopes the samples and then updates the autocorrelation estimation. When whole song data has been input in smaller blocks using this function, read the resulting bpm with get_bpm function.

Notice that data in samples array can be disrupted in processing.

source

pub fn get_bpm(&mut self) -> f32

Analyzes the results and returns the BPM rate. Use this function to read result after whole song data has been input to the class by consecutive calls of input_samples function.

source

pub fn get_beats( &mut self, pos: &mut [f32], values: &mut [f32], max_num: i32, ) -> i32

Get beat position arrays. Note: The array includes also really low beat detection values in absence of clear strong beats. Consumer may wish to filter low values away.

  • pos receive array of beat positions
  • values receive array of beat detection strengths
  • max_num indicates max.size of pos and values array.

You can query a suitable array sized by calling the query_size function. Returns the number of beats in the arrays.

source

pub fn query_size(&mut self, max_num: i32) -> i32

Queries a suitable array sized for get_beats.

source

pub fn update_beat_pos(&mut self, process_samples: i32)

Detects individual beat positions.

source

pub fn remove_bias(&mut self)

Removes constant bias from xcorr data.

source

pub fn calc_envelope(&mut self, samples: &mut [f32])

Calculates amplitude envelope for the buffer of samples. Result is output to samples.

source

pub fn decimate( &mut self, dest: &mut [f32], src: &[f32], numsamples: i32, ) -> i32

Decimates samples to approx. 500 Hz.

Returns the number of output samples.

source

pub fn update_x_corr(&mut self, process_samples: i32)

Updates auto-correlation function for given number of decimated samples that are read from the internal `buffer’ pipe (samples aren’t removed from the pipe though).

Trait Implementations§

source§

impl Default for BPMDetect

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Drop for BPMDetect

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for BPMDetect

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.