foley.sources.freesound.adapter#

Freesound APIv2 retrieve adapter — CC0 (and CC-BY) sounds, stored strictly by-reference.

Freesound (report 01 §Freesound) is foley’s anchor retrieve source. This adapter turns a natural-language query into ranked, license-clean Candidates over the Freesound APIv2, honoring two load-bearing constraints:

  • CC0 by default, CC-BY on request, pushed into the query. The CC0 filter is sent as the native filter=license:"Creative Commons 0" so other sounds never leave the server (a license other than CC0 also asks for "Attribution": the credit-bearing sounds the keep() gate then admits only for a caller that can attribute). Each returned item is also re-checked fail-closed against the accepted_license_ids allowlist — the server filter is a promise foley does not control, so an NC / SA / ND item that slips through is dropped, never indexed.

  • By-reference storage (TOS). The Freesound API TOS forbids caching the audio bytes even for CC0, so every sound is licensed cache_bytes_ok=False (a per-item override on top of its own CC id — see api_license()). foley keeps the stable sound-page URI + provenance + the CLAP vector; the bytes fetched here (a token-tier preview

    transcode) are transient — embedded once by ingest_one(), then discarded, never persisted.

HTTP is dependency-injected (a Transport), so the adapter is fully testable with no network and import foley stays dol-only; the real requests lives only behind requests_transport() (the foley[freesound] extra). Ingestion (decode / QC / embed / tag / store) is NOT reimplemented here — the foley.sources.pull.add_from() façade routes every hit through the shared ingest_one pipeline (it wraps the corpus machinery, it does not fork it).

Module Attributes

Adapter

Registry convention (arioso): the loader imports adapter.Adapter.

Classes

Adapter

Registry convention (arioso): the loader imports adapter.Adapter.

FreesoundAdapter([config, api_key, http])

Live Freesound APIv2 retrieve adapter (a SourceAdapter).

foley.sources.freesound.adapter.Adapter#

Registry convention (arioso): the loader imports adapter.Adapter.

class foley.sources.freesound.adapter.FreesoundAdapter(config=None, *, api_key=None, http=None)[source]#

Bases: object

Live Freesound APIv2 retrieve adapter (a SourceAdapter).

Parameters:
  • config (Optional[dict]) – The SOURCE_CONFIG (defaults to the module’s). Passed positionally by the registry’s lazy loader (the arioso Adapter(config) convention).

  • api_key (Optional[str]) – The Freesound token. Defaults to $FREESOUND_API_KEY.

  • http (Optional[Transport]) – The injected Transport (defaults to requests_transport()); tests pass a fake.

property api_key: str#

The Freesound token (constructor, else a bound key, else $FREESOUND_API_KEY).

download(source_id, *, preview_url=None)[source]#

Return a sound’s transient preview bytes (token-tier; never cached).

Parameters:
  • source_id (str) – The Freesound id (either id form).

  • preview_url (Optional[str]) – Optional known preview URL (from a prior search hit) — used directly to save a round-trip; otherwise the sound instance is fetched to resolve it.

Return type:

bytes

Returns:

The preview audio bytes (embedded once, then discarded — see the by-reference storage contract).

Raises:

LookupError – If the sound exposes no preview.

get(source_id)[source]#

Resolve one Freesound id ('12345' or 'freesound:12345') to a record.

Raises:

LookupError – If the sound’s license is not in the accepted allowlist.

Return type:

SoundRecord

invoke_native(path, **params)[source]#

GET a Freesound APIv2 path with native params; return the parsed JSON.

The public escape hatch below foley’s translation (#61), e.g. invoke_native("/search/", query="rain", filter="ac_brightness:[50 TO 100]"). It bypasses the licence filter and the per-item licence re-check that search() applies: the caller owns licence gating for what it gets back (and the dispatch layer’s egress check is not run either).

Raises:

RuntimeError – On a non-200 response (with Freesound’s detail).

search(query, *, license='cc0', k=15, duration_range=None, sort=None, page=1, kind='sfx', similar_to=None, ucs_category=None, provider_params=None, **kw)[source]#

Search Freesound for query; return license-clean candidates.

The license filter is pushed into the native Solr filter so other sounds never leave the server; every returned item is still re-checked fail-closed before it becomes a candidate.

Parameters:
  • license (Optional[str]) – A licence id ('cc0' is an alias for CC0-1.0, 'cc-by' keeps the CC-BY ids). 'cc0' (default) sends the CC0 filter; another id asks for CC0 + Attribution; None sends no filter. Whatever the filter, only items under the asked id (and the accepted_license_ids allowlist) are kept, so NC / SA / ND never come back.

  • k (int) – Max results (Freesound caps page_size at 150).

  • duration_range (Optional[tuple[float, float]]) – Optional (min_s, max_s) native duration filter.

  • sort (Optional[str]) – relevance (default) | newest | popular | duration, translated to Freesound’s native key.

  • page (int) – The results page (1-based, k per page).

  • kind (str) – sfx (default, no filter) or music (the Broad Sound Taxonomy’s “Music” top level). With a ucs_category that maps to another top level it raises SearchArgumentError (nothing could match).

  • similar_to (Optional[str]) – A Freesound sound id (either id form): “more like this one”.

  • ucs_category (Optional[str]) – A UCS CatID or category, mapped to a coarse Broad Sound Taxonomy category pre-filter (no filter if it is not recognised; SearchArgumentError if kind asks for another top level).

  • provider_params (Optional[dict]) – Native Freesound search params foley has no affordance for, merged over the translated ones (#61), e.g. {"filter": "ac_brightness:[50 TO 100]"}. Per the config’s provider_params policy, a filter is appended to foley’s as one group (the licence filter cannot be replaced or widened, and a category clause is refused when kind / ucs_category already send one), fields are appended to foley’s (the licence fields cannot be dropped), and query is refused; the per-item licence re-check still applies.

  • **kw – Ignored extra affordances (on_unsupported_param='warn').

Return type:

list[Candidate]

Returns:

Up to k Candidates (origin=retrieved), each carrying a by-reference LicenseRecord, a transient preview_uri and the item JSON it was parsed from as raw.