Skip to main content

StreamingFingerprinter

Trait StreamingFingerprinter 

Source
pub trait StreamingFingerprinter {
    type Frame;

    // Required methods
    fn push(&mut self, samples: &[f32]) -> Vec<(TimestampMs, Self::Frame)>;
    fn flush(&mut self) -> Vec<(TimestampMs, Self::Frame)>;
    fn latency_ms(&self) -> u32;

    // Provided methods
    fn push_with<F>(&mut self, samples: &[f32], callback: F) -> usize
       where F: FnMut(TimestampMs, &Self::Frame) { ... }
    fn flush_with<F>(&mut self, callback: F) -> usize
       where F: FnMut(TimestampMs, &Self::Frame) { ... }
}
Expand description

Streaming fingerprinter that emits zero-or-more frames per push.

Implementations must be non-blocking and bounded-allocation: any buffers needed for sustained operation are allocated at construction, not inside StreamingFingerprinter::push. This makes them suitable for invocation from realtime audio callbacks (when invoked through audiofp’s streaming orchestrator).

§Example

use audiofp::{StreamingFingerprinter, TimestampMs};

struct EveryThird { count: usize }

impl StreamingFingerprinter for EveryThird {
    type Frame = u32;
    fn push(&mut self, samples: &[f32]) -> Vec<(TimestampMs, u32)> {
        let mut out = Vec::new();
        for s in samples {
            self.count += 1;
            if self.count % 3 == 0 {
                out.push((TimestampMs(self.count as u64), s.to_bits()));
            }
        }
        out
    }
    fn flush(&mut self) -> Vec<(TimestampMs, u32)> { Vec::new() }
    fn latency_ms(&self) -> u32 { 0 }
}

let mut fp = EveryThird { count: 0 };
assert_eq!(fp.push(&[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]).len(), 2);

Required Associated Types§

Source

type Frame

One unit of fingerprint material the stream emits.

Required Methods§

Source

fn push(&mut self, samples: &[f32]) -> Vec<(TimestampMs, Self::Frame)>

Feed PCM samples and return any fingerprints that became available during this push.

Must not block and must not allocate beyond the per-instance budget set at construction.

Source

fn flush(&mut self) -> Vec<(TimestampMs, Self::Frame)>

Drain any pending fingerprint material at end-of-stream.

Source

fn latency_ms(&self) -> u32

Conservative upper bound on emission latency: from the time a sample enters push to the time the fingerprint covering it is returned.

Provided Methods§

Source

fn push_with<F>(&mut self, samples: &[f32], callback: F) -> usize
where F: FnMut(TimestampMs, &Self::Frame),

Feed PCM samples and invoke callback for each fingerprint frame that became available during this push.

Zero-allocation variant: the callback receives (TimestampMs, &Frame) by reference, avoiding the Vec allocation of push. Returns the number of frames emitted.

Default implementation delegates to push and iterates the result. Implementors should override for true zero-allocation behaviour.

Source

fn flush_with<F>(&mut self, callback: F) -> usize
where F: FnMut(TimestampMs, &Self::Frame),

Drain any pending fingerprint material at end-of-stream, invoking callback for each frame.

Zero-allocation variant of flush. Returns the number of frames emitted.

Default implementation delegates to flush and iterates the result.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§