Skip to main content

Fingerprinter

Trait Fingerprinter 

Source
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§

Source

type Output

The fingerprint produced by this extractor (e.g. Vec<WangHash>).

Source

type Config: Clone + Send + Sync

Per-instance configuration this fingerprinter exposes to callers.

Required Methods§

Source

fn name(&self) -> &'static str

Stable identifier for the algorithm and version, e.g. "wang-v1". Useful when persisting fingerprints alongside the producer name.

Source

fn config(&self) -> &Self::Config

Borrow the configuration this instance was built with.

Source

fn required_sample_rate(&self) -> u32

Sample rate, in hertz, the fingerprinter expects its input at. Resampling is the caller’s responsibility.

Source

fn min_samples(&self) -> usize

Minimum buffer length, in samples, required to extract anything. Calls with shorter inputs return AfpError::AudioTooShort.

Source

fn extract(&mut self, audio: AudioBuffer<'_>) -> Result<Self::Output>

Compute the fingerprint of audio.

Implementors§