Struct irapt::Irapt [−][src]
pub struct Irapt { /* fields omitted */ }
Expand description
Implementation of the IRAPT pitch estimation algorithm.
IRAPT is an “instantaneous” version of the Robust Algorithm for Pitch Tracking. Though pitch estimates are provided
every harmonics_estimation_interval
(0.005
seconds by default), a larger sliding window of
candidate_selection_window_duration
(0.3
seconds by default) is used to improve accuracy at the cost of a
small delay.
Implementations
Returns the Parameters
specified during construction.
Process input from a queue of samples in a VecDeque
.
As many samples as necessary to calculate the next pitch estimate are read from the VecDeque
, otherwise
None
is returned if more are required. To process as many samples as possible from the VecDeque
,
process
should be called repeatedly until None
is returned.
Input samples consumed are eventually removed from the front of the VecDeque
by process
, but a fixed-size
window of past samples are left remaining in the VecDeque
for access by later calls to process
and are
only removed when they are no longer needed.
Examples
use irapt::{Irapt, Parameters};
use std::collections::VecDeque;
use std::f64::consts::PI;
let parameters = Parameters::default();
let mut irapt = Irapt::new(parameters.clone()).expect("the default parameters should be valid");
// Use a 100Hz sine wave as an example input signal
let mut samples = (0..).map(|sample_index| f64::sin(sample_index as f64 / parameters.sample_rate * 2.0 * PI * 100.0));
// Collect half of a second of input
let mut sample_buffer = VecDeque::new();
sample_buffer.extend(samples.by_ref().take(parameters.sample_rate as usize / 2));
// Process as many samples as possible
while let Some(output) = irapt.process(&mut sample_buffer) {
let estimated_pitch = output.pitch_estimates().final_estimate();
println!("estimated pitch: {}Hz with energy {}", estimated_pitch.frequency, estimated_pitch.energy);
}
// Simulate that half of a second more samples have become availoble and process them
sample_buffer.extend(samples.by_ref().take(parameters.sample_rate as usize / 2));
while let Some(output) = irapt.process(&mut sample_buffer) {
let estimated_pitch = output.pitch_estimates().final_estimate();
println!("estimated pitch: {}Hz with energy {}", estimated_pitch.frequency, estimated_pitch.energy);
}
Resets all internal state associated with the sliding analysis window.
The internal state after a reset is equivalent to that of a newly constructed Irapt
. Resetting can be useful
to avoid causing artifacts in the analysis when skipping a number of samples in the input without processing
them.
Examples
use irapt::{Irapt, Parameters};
use std::collections::VecDeque;
use std::f64::consts::PI;
let parameters = Parameters::default();
let mut irapt = Irapt::new(parameters.clone()).expect("the default parameters should be valid");
// Use a 100Hz sine wave as an example input signal
let mut samples = (0..).map(|sample_index| f64::sin(sample_index as f64 / parameters.sample_rate * 2.0 * PI * 100.0));
// Collect half of a second of input
let mut sample_buffer = VecDeque::new();
sample_buffer.extend(samples.by_ref().take(parameters.sample_rate as usize / 2));
while let Some(output) = irapt.process(&mut sample_buffer) {
let estimated_pitch = output.pitch_estimates().final_estimate();
println!("estimated pitch: {}Hz with energy {}", estimated_pitch.frequency, estimated_pitch.energy);
}
// Simulate that many more samples have become available
let more_samples = samples.by_ref().take(parameters.sample_rate as usize * 10);
// Reset irapt, clear the input buffer, skip all but half a second of input samples, and process the rest
irapt.reset();
sample_buffer.clear();
sample_buffer.extend(more_samples.skip(parameters.sample_rate as usize * 19 / 2));
while let Some(output) = irapt.process(&mut sample_buffer) {
let estimated_pitch = output.pitch_estimates().final_estimate();
println!("estimated pitch: {}Hz with energy {}", estimated_pitch.frequency, estimated_pitch.energy);
}