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_samplesto 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_bpmthat finds the highest peak of the autocorrelation function, calculates it’s precise location and converts this reading to bpm’s.
Implementations§
source§impl BPMDetect
impl BPMDetect
sourcepub fn new(num_channels: u32, sample_rate: u32) -> Self
pub fn new(num_channels: u32, sample_rate: u32) -> Self
Creates a new BPMDetect instance with the given channels and sample rate.
sourcepub fn input_samples(&mut self, samples: &[f32])
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.
sourcepub fn get_bpm(&mut self) -> f32
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.
sourcepub fn get_beats(
&mut self,
pos: &mut [f32],
values: &mut [f32],
max_num: i32,
) -> i32
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.
posreceive array of beat positionsvaluesreceive array of beat detection strengthsmax_numindicates max.size ofposandvaluesarray.
You can query a suitable array sized by calling the query_size function.
Returns the number of beats in the arrays.
sourcepub fn query_size(&mut self, max_num: i32) -> i32
pub fn query_size(&mut self, max_num: i32) -> i32
Queries a suitable array sized for get_beats.
sourcepub fn update_beat_pos(&mut self, process_samples: i32)
pub fn update_beat_pos(&mut self, process_samples: i32)
Detects individual beat positions.
sourcepub fn remove_bias(&mut self)
pub fn remove_bias(&mut self)
Removes constant bias from xcorr data.
sourcepub fn calc_envelope(&mut self, samples: &mut [f32])
pub fn calc_envelope(&mut self, samples: &mut [f32])
Calculates amplitude envelope for the buffer of samples.
Result is output to samples.
sourcepub fn decimate(
&mut self,
dest: &mut [f32],
src: &[f32],
numsamples: i32,
) -> i32
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.
sourcepub fn update_x_corr(&mut self, process_samples: i32)
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).