pub trait Fingerprinter {
type Output;
type Config: Clone + Send + Sync;
// Required methods
fn name(&self) -> &'static str;
fn config(&self) -> &Self::Config;
fn required_sample_rate(&self) -> u32;
fn min_samples(&self) -> usize;
fn extract(&mut self, audio: AudioBuffer<'_>) -> Result<Self::Output>;
}Expand description
Offline (whole-buffer) fingerprinter.
Implementations are stateful between calls only insofar as they may
cache scratch buffers — the fingerprint of extract(a) does not depend
on any previous call.
§Example
use audiofp::{AudioBuffer, Fingerprinter, Result, SampleRate};
/// A toy fingerprinter that just sums absolute samples.
struct Energy;
impl Fingerprinter for Energy {
type Output = f32;
type Config = ();
fn name(&self) -> &'static str { "energy-v0" }
fn config(&self) -> &Self::Config { &() }
fn required_sample_rate(&self) -> u32 { 16_000 }
fn min_samples(&self) -> usize { 16_000 }
fn extract(&mut self, audio: AudioBuffer<'_>) -> Result<Self::Output> {
Ok(audio.samples.iter().map(|s| s.abs()).sum())
}
}
let mut fp = Energy;
let samples = vec![0.0_f32; 16_000];
let buf = AudioBuffer { samples: &samples, rate: SampleRate::HZ_16000 };
assert_eq!(fp.extract(buf).unwrap(), 0.0);Required Associated Types§
Required Methods§
Sourcefn name(&self) -> &'static str
fn name(&self) -> &'static str
Stable identifier for the algorithm and version, e.g. "wang-v1".
Useful when persisting fingerprints alongside the producer name.
Sourcefn required_sample_rate(&self) -> u32
fn required_sample_rate(&self) -> u32
Sample rate, in hertz, the fingerprinter expects its input at. Resampling is the caller’s responsibility.
Sourcefn min_samples(&self) -> usize
fn min_samples(&self) -> usize
Minimum buffer length, in samples, required to extract anything.
Calls with shorter inputs return AfpError::AudioTooShort.
Sourcefn extract(&mut self, audio: AudioBuffer<'_>) -> Result<Self::Output>
fn extract(&mut self, audio: AudioBuffer<'_>) -> Result<Self::Output>
Compute the fingerprint of audio.