foley.index.protocols#
Structural contracts for the foley index (the retrieval boundary).
These three Protocol``s are the small, swappable seams report 04 §6.2 calls for:
keep the boundary as *protocols*, give each a zero-config sensible default, and
let every piece be replaced by keyword injection. They are the SSOT for the
Index-stage contracts (report 10 §4.2) and are deliberately kept here, separate
from any concrete backend, so the façade (:mod:`foley.index.library`) and the
search logic (:mod:`foley.index.search`) depend only on the interface — never on
``torch / lancedb / sqlite_vec.
Embedder— the joint text<->audio space (CLAP default).
VectorIndex— approximate-nearest-neighbour over embeddings.
KeywordIndex— BM25 / full-text over tags + caption.
A single backend object may satisfy both index protocols at once (LanceDB holds
the vector column and the FTS index in one table); the façade simply passes it as
both vindex and kindex.
This module is stdlib-only: ndarray appears solely in annotations, imported
under TYPE_CHECKING so import foley.index.protocols never pulls numpy.
Classes
|
Produce one natural-language sentence describing a clip (report 03). |
|
A joint text<->audio embedding space (CLAP by default). |
|
BM25 / full-text index over each sound's tags + caption. |
|
Map a clip to |
|
Approximate-nearest-neighbour store over embedding vectors. |
- class foley.index.protocols.Captioner(*args, **kwargs)[source]#
Bases:
ProtocolProduce one natural-language sentence describing a clip (report 03).
The caption feeds the BM25 keyword index and human display. Default is a dedicated AAC model (EnCLAP); Qwen2-Audio is a richer promptable upgrade. Both are
foley[caption]adapters plugged in behind this protocol.
- class foley.index.protocols.Embedder(*args, **kwargs)[source]#
Bases:
ProtocolA joint text<->audio embedding space (CLAP by default).
One space serves both text->audio search (
embed_texta query) and audio<->audio similarity (embed_audioa clip). Implementations MUST return L2-normalizedfloat32arrays so a plain inner product is cosine similarity, and MUST stampmodel_id/dimso mixed-model libraries stay coherent (eachSoundRecordrecords theembedding_model/embedding_dimit was indexed under).- model_id#
The checkpoint id (e.g.
'laion/larger_clap_general').
- dim#
The embedding dimensionality (e.g.
512).
- embed_audio(wav, sr)[source]#
Embed one audio clip.
- Parameters:
wav (
ndarray) – A working-array clip (float32, mono preferred). CLAP expects 48 kHz; implementations resample as needed.sr (
int) – The clip’s sample rate in Hz.
- Return type:
ndarray- Returns:
A 1-D
(dim,)L2-normalizedfloat32array.
- class foley.index.protocols.KeywordIndex(*args, **kwargs)[source]#
Bases:
ProtocolBM25 / full-text index over each sound’s tags + caption.
The default is LanceDB’s Tantivy FTS (report 04 §3.4); SQLite FTS5 is the single-file fallback. Same
wherepush-down contract asVectorIndex.
- class foley.index.protocols.Tagger(*args, **kwargs)[source]#
Bases:
ProtocolMap a clip to
(label, score)pairs against a label vocabulary.Used on ingest to auto-fill
SoundRecord.audioset_labels(supervised) andtags(zero-shot). The default supervised tagger is PANNs CNN14 over AudioSet; the default zero-shot tagger scores a clip against a custom/UCS label set via CLAP (report 03). BEATs/AST are drop-in upgrades. Consumes the working array (float32, any sr — the impl resamples to its model’s rate).
- class foley.index.protocols.VectorIndex(*args, **kwargs)[source]#
Bases:
ProtocolApproximate-nearest-neighbour store over embedding vectors.
The default is LanceDB (report 04 §2); Qdrant/pgvector/sqlite-vec bind the same protocol behind the scenes.
whereis an optional metadata push-down the façade may pass; a backend that cannot push filters down MAY ignore it (the façade over-fetches and post-filters to stay correct either way).- get_vector(id)[source]#
Return the stored vector for
id(orNoneif absent).Needed by
SoundLibrary.similar(fetch a sound’s own vector, then runknn()) and by the optional CLAP rerank (score keyword-only hits).- Return type:
Optional[ndarray]