foley.embedded_metadata#
What a pro sound file says about itself: Broadcast-WAV bext and iXML (#228, #231).
Commercial packs (BOOM, Pro Sound Effects, Sonniss GDC) describe each file inside it:
the EBU Broadcast-WAV bext chunk (Description, Originator, the origination
date) and an iXML chunk whose <USER> block carries the Soundminer / UCS fields
(CATEGORY, SUBCATEGORY, CATID, FXNAME, DESCRIPTION, KEYWORDS,
LIBRARY, MANUFACTURER, …). read_embedded_metadata() reads both with the
standard library and returns foley’s view of them:
{"caption": ..., "tags": [...], "ucs_catid": ..., "fx_name": ..., "library": ...,
"manufacturer": ..., "raw": {"bext": {...}, "ixml": {...}}}
Only the fields the file has are present. A file that is not a RIFF / RF64 / BW64 WAVE,
or has neither chunk, gives {}; a missing file raises FileNotFoundError.
write_embedded_metadata() is the other direction (#231): it puts foley’s fields
into a RIFF WAV’s bext and iXML chunks, replacing the fields it writes and
keeping the rest of each chunk, and leaves the audio bytes as they are.
FIELDS is the one table between the chunks and foley’s names, both ways: each
Field has a canonical write location (where an exporter puts the value,
and where a reader looks first) and ordered read_also fallbacks, plus the
decode / encode pair between the stored text and foley’s value.
fields_from_chunks() and chunks_from_fields() are its two directions, so
the writer of #231 reuses the table rather than inverting it. The names are foley’s
(caption, tags, ucs_* as on SoundRecord /
SoundEvent, fx_name as on the UCS filename parse, and
license / license_url / attribution as in the rights dict of
foley.export_sound()).
The caption is also written to bext Description (cut to its 256 bytes on a
character boundary): DAWs and most library tools (Pro Tools, BaseHead, Soundly) show
the BWF Description, while the iXML DESCRIPTION keeps the whole caption and is read
first, so a long caption still reads back whole.
This module sits beside foley.audio rather than in foley.index: it is
file-format I/O that both ingest (reading) and export (writing) use, stdlib-only.
Module Attributes
classic RIFF, EBU RF64 (Tech 3306), ITU-R BS.2088 BW64. |
|
A bext or iXML chunk bigger than this is skipped (real ones are a few kB). |
|
The fixed-width text fields at the start of a |
|
How keywords are separated in the wild, and how foley writes them. |
|
The names |
|
The names |
|
The size of a |
|
The root and |
|
The root and |
|
The root and |
|
foley field <-> chunk locations (see |
|
Chunk id -> (its name in |
|
Chunk name (as in |
|
A metadata chunk the file does not have yet is inserted before this one. |
Functions
|
A |
|
An iXML payload (UTF-8) with |
|
Where a writer puts each of foley's fields: |
|
foley's fields from parsed chunks ( |
|
|
|
The |
|
The text fields of a |
|
The |
|
foley's view of a WAV's |
|
The parsed, non-empty metadata chunks of a WAV ( |
|
Keywords from a |
|
|
|
|
|
|
Classes
|
One foley field and where it lives in a WAV's metadata chunks. |
|
A place a value lives: a |
- foley.embedded_metadata.BEXT = 'bext'#
The names
FIELDSlocations use for the two chunks (also therawkeys).
- foley.embedded_metadata.BEXT_FIELDS = (('description', 256), ('originator', 32), ('originator_reference', 32), ('origination_date', 10), ('origination_time', 8))#
The fixed-width text fields at the start of a
bextchunk (EBU Tech 3285).
- foley.embedded_metadata.BEXT_FIXED_BYTES = 602#
The size of a
bextchunk’s fixed part (EBU Tech 3285), before its CodingHistory.
- foley.embedded_metadata.CHUNK_BUILDERS = {'bext': (b'bext', <function build_bext>), 'ixml': (b'iXML', <function build_ixml>)}#
Chunk name (as in
FIELDSandraw) -> (its chunk id, its builder).
- foley.embedded_metadata.CHUNK_PARSERS = {b'bext': ('bext', <function parse_bext>), b'iXML': ('ixml', <function parse_ixml_user>)}#
Chunk id -> (its name in
rawandFIELDS, its parser).
- foley.embedded_metadata.FIELDS: tuple[Field, ...] = (Field(name='caption', write=Location(chunk='ixml', key='DESCRIPTION'), read_also=(Location(chunk='ixml', key='FXNAME'),), decode=<class 'str'>, encode=<class 'str'>, write_also=(Location(chunk='bext', key='description'),)), Field(name='tags', write=Location(chunk='ixml', key='KEYWORDS'), read_also=(), decode=<function split_keywords>, encode=<function join_keywords>, write_also=()), Field(name='fx_name', write=Location(chunk='ixml', key='FXNAME'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_catid', write=Location(chunk='ixml', key='CATID'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_category', write=Location(chunk='ixml', key='CATEGORY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_subcategory', write=Location(chunk='ixml', key='SUBCATEGORY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='library', write=Location(chunk='ixml', key='LIBRARY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='manufacturer', write=Location(chunk='ixml', key='MANUFACTURER'), read_also=(Location(chunk='bext', key='originator'),), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='designer', write=Location(chunk='ixml', key='DESIGNER'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='license', write=Location(chunk='ixml', key='LICENSE'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='license_url', write=Location(chunk='ixml', key='LICENSE_URL'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='attribution', write=Location(chunk='ixml', key='ATTRIBUTION'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()))#
foley field <-> chunk locations (see
Field).
- class foley.embedded_metadata.Field(name, write, read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=())[source]#
Bases:
objectOne foley field and where it lives in a WAV’s metadata chunks.
writeis the canonical location: a writer puts the value there and a reader looks there first.write_alsoare copies a writer also fills (for tools that read only there), read next.read_alsoare read-only fallbacks, tried in order.decodeturns the stored text into foley’s value;encodeturns it back.
- foley.embedded_metadata.INSERT_BEFORE = b'data'#
A metadata chunk the file does not have yet is inserted before this one.
- foley.embedded_metadata.IXML = 'ixml'#
The names
FIELDSlocations use for the two chunks (also therawkeys).
- foley.embedded_metadata.IXML_ROOT = 'BWFXML'#
The root and
<USER>tags of a new iXML document, and the iXML version it states.
- foley.embedded_metadata.IXML_USER = 'USER'#
The root and
<USER>tags of a new iXML document, and the iXML version it states.
- foley.embedded_metadata.IXML_VERSION = '1.61'#
The root and
<USER>tags of a new iXML document, and the iXML version it states.
- foley.embedded_metadata.KEYWORD_SEPARATORS = re.compile('[,;]')#
How keywords are separated in the wild, and how foley writes them.
- class foley.embedded_metadata.Location(chunk, key)[source]#
Bases:
objectA place a value lives: a
bextfield name or an iXML<USER>tag.
- foley.embedded_metadata.MAX_METADATA_CHUNK_BYTES = 4194304#
A bext or iXML chunk bigger than this is skipped (real ones are a few kB).
- foley.embedded_metadata.RIFF_FORMS = (b'RIFF', b'RF64', b'BW64')#
classic RIFF, EBU RF64 (Tech 3306), ITU-R BS.2088 BW64.
- Type:
The RIFF form types read
- foley.embedded_metadata.build_bext(fields, *, base=b'', report=<function _ignore>)[source]#
A
bextpayload withfields(BEXT_FIELDSnames) written overbase.baseis the file’s existingbextpayload, so what foley does not write (the time reference, UMID, loudness, CodingHistory) is kept; with none, the rest is zero (version 0: no UMID). Text is UTF-8 (ASCII is a subset), cut on a character boundary at the field’s width; aNonevalue empties its field.reportis accepted for the builder protocol (nothing in abextis ever lost).>>> parse_bext(build_bext({"description": "Rain"})) {'description': 'Rain'}
- Return type:
- foley.embedded_metadata.build_ixml(fields, *, base=b'', report=<function _ignore>)[source]#
An iXML payload (UTF-8) with
fieldsas<USER>children, written intobase.baseis the file’s existing iXML payload: its other elements, its comments, and the<USER>fields not infields, are kept; aNonevalue removes its field. Abasethat does not parse is replaced, andreporttold.>>> parse_ixml_user(build_ixml({"CATID": "DOORWood", "FXNAME": "Creak & slam"})) {'CATID': 'DOORWood', 'FXNAME': 'Creak & slam'}
- Return type:
- foley.embedded_metadata.chunks_from_fields(meta, *, fields=(Field(name='caption', write=Location(chunk='ixml', key='DESCRIPTION'), read_also=(Location(chunk='ixml', key='FXNAME'), ), decode=<class 'str'>, encode=<class 'str'>, write_also=(Location(chunk='bext', key='description'), )), Field(name='tags', write=Location(chunk='ixml', key='KEYWORDS'), read_also=(), decode=<function split_keywords>, encode=<function join_keywords>, write_also=()), Field(name='fx_name', write=Location(chunk='ixml', key='FXNAME'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_catid', write=Location(chunk='ixml', key='CATID'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_category', write=Location(chunk='ixml', key='CATEGORY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_subcategory', write=Location(chunk='ixml', key='SUBCATEGORY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='library', write=Location(chunk='ixml', key='LIBRARY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='manufacturer', write=Location(chunk='ixml', key='MANUFACTURER'), read_also=(Location(chunk='bext', key='originator'), ), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='designer', write=Location(chunk='ixml', key='DESIGNER'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='license', write=Location(chunk='ixml', key='LICENSE'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='license_url', write=Location(chunk='ixml', key='LICENSE_URL'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='attribution', write=Location(chunk='ixml', key='ATTRIBUTION'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=())), clear=())[source]#
Where a writer puts each of foley’s fields:
{chunk: {key: text}}(write_to).A field named in
clearthatmetahas no value for maps toNone: the writer removes it, so a file never keeps a stale value of a field the caller owns.>>> chunks_from_fields({"caption": "Rain", "tags": ["a", "b"], "other": 1}) {'ixml': {'DESCRIPTION': 'Rain', 'KEYWORDS': 'a, b'}, 'bext': {'description': 'Rain'}} >>> chunks_from_fields({"license": None}, clear=["license"]) {'ixml': {'LICENSE': None}}
- foley.embedded_metadata.fields_from_chunks(raw, *, fields=(Field(name='caption', write=Location(chunk='ixml', key='DESCRIPTION'), read_also=(Location(chunk='ixml', key='FXNAME'), ), decode=<class 'str'>, encode=<class 'str'>, write_also=(Location(chunk='bext', key='description'), )), Field(name='tags', write=Location(chunk='ixml', key='KEYWORDS'), read_also=(), decode=<function split_keywords>, encode=<function join_keywords>, write_also=()), Field(name='fx_name', write=Location(chunk='ixml', key='FXNAME'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_catid', write=Location(chunk='ixml', key='CATID'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_category', write=Location(chunk='ixml', key='CATEGORY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_subcategory', write=Location(chunk='ixml', key='SUBCATEGORY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='library', write=Location(chunk='ixml', key='LIBRARY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='manufacturer', write=Location(chunk='ixml', key='MANUFACTURER'), read_also=(Location(chunk='bext', key='originator'), ), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='designer', write=Location(chunk='ixml', key='DESIGNER'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='license', write=Location(chunk='ixml', key='LICENSE'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='license_url', write=Location(chunk='ixml', key='LICENSE_URL'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='attribution', write=Location(chunk='ixml', key='ATTRIBUTION'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=())))[source]#
foley’s fields from parsed chunks (
{"bext": {...}, "ixml": {...}}), non-empty only.>>> fields_from_chunks({"bext": {"description": "Rain"}, "ixml": {"KEYWORDS": "a;b"}}) {'caption': 'Rain', 'tags': ['a', 'b']}
- Return type:
- foley.embedded_metadata.iter_riff_chunks(f, *, forms=(b'RIFF', b'RF64', b'BW64'), stop_at_declared_end=False)[source]#
(chunk_id, offset, size)for each top-level chunk of a RIFF / RF64 / BW64 WAVE.Walks the chunk headers with seeks, never reading the audio, so a large file costs a few small reads. Stops quietly at a truncated chunk.
stop_at_declared_endalso stops at the RIFF’s declared end when that is inside the file: a writer must not fold trailing bytes into the WAVE, while a reader keeps going, since many files understate their RIFF size and still carrybext/ iXML after it. For RF64 / BW64, adatachunk sized0xFFFFFFFFtakes its real size fromds64’sdataSize.>>> import io >>> body = b"WAVE" + b"abcd" + struct.pack("<I", 1) + b"x\0" >>> wav = io.BytesIO(b"RIFF" + struct.pack("<I", len(body)) + body) >>> list(iter_riff_chunks(wav)) [(b'abcd', 20, 1)]
- foley.embedded_metadata.join_keywords(tags)[source]#
The
KEYWORDSstring fortags(the inverse ofsplit_keywords()).>>> join_keywords(["door", " creak "]) 'door, creak'
- Return type:
- foley.embedded_metadata.parse_bext(payload)[source]#
The text fields of a
bextchunk (NUL-padded; UTF-8 or Latin-1), non-empty only.
- foley.embedded_metadata.parse_ixml_user(payload)[source]#
The
<USER>fields of an iXML chunk (upper-cased tag -> text), or{}.The bytes go to the XML parser as they are, so the document’s own encoding declaration (UTF-8, Latin-1, UTF-16, …) is honoured.
- foley.embedded_metadata.read_embedded_metadata(path, *, max_chunk_bytes=4194304, fields=(Field(name='caption', write=Location(chunk='ixml', key='DESCRIPTION'), read_also=(Location(chunk='ixml', key='FXNAME'), ), decode=<class 'str'>, encode=<class 'str'>, write_also=(Location(chunk='bext', key='description'), )), Field(name='tags', write=Location(chunk='ixml', key='KEYWORDS'), read_also=(), decode=<function split_keywords>, encode=<function join_keywords>, write_also=()), Field(name='fx_name', write=Location(chunk='ixml', key='FXNAME'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_catid', write=Location(chunk='ixml', key='CATID'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_category', write=Location(chunk='ixml', key='CATEGORY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_subcategory', write=Location(chunk='ixml', key='SUBCATEGORY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='library', write=Location(chunk='ixml', key='LIBRARY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='manufacturer', write=Location(chunk='ixml', key='MANUFACTURER'), read_also=(Location(chunk='bext', key='originator'), ), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='designer', write=Location(chunk='ixml', key='DESIGNER'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='license', write=Location(chunk='ixml', key='LICENSE'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='license_url', write=Location(chunk='ixml', key='LICENSE_URL'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='attribution', write=Location(chunk='ixml', key='ATTRIBUTION'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=())))[source]#
foley’s view of a WAV’s
bextand iXML metadata (see the module docs), or{}.pathis a file path or the WAV’s bytes.>>> read_embedded_metadata(__file__) # not a WAV {}
- Return type:
- foley.embedded_metadata.read_metadata_chunks(path, *, max_chunk_bytes=4194304, parsers={b'bext': ('bext', <function parse_bext>), b'iXML': ('ixml', <function parse_ixml_user>)})[source]#
The parsed, non-empty metadata chunks of a WAV (
{"bext": ..., "ixml": ...}).pathis a file path or the WAV’s bytes. Chunks larger thanmax_chunk_bytesare skipped unread. A file that is not a WAV gives{}; one that cannot be opened (missing, a directory, no permission) raises theOSError, so a wrong path is not mistaken for a file without metadata.
- foley.embedded_metadata.split_keywords(text)[source]#
Keywords from a
KEYWORDSstring: split on , and ;, de-duplicated ignoring case.>>> split_keywords("door, creak; Wood ,DOOR") ['door', 'creak', 'Wood']
- foley.embedded_metadata.unique_words(words)[source]#
wordsstripped, without blanks or repeats (ignoring case), first spelling kept.>>> unique_words([" door", "DOOR", None, "", "creak"]) ['door', 'creak']
- foley.embedded_metadata.write_embedded_metadata(wav, meta, *, fields=(Field(name='caption', write=Location(chunk='ixml', key='DESCRIPTION'), read_also=(Location(chunk='ixml', key='FXNAME'), ), decode=<class 'str'>, encode=<class 'str'>, write_also=(Location(chunk='bext', key='description'), )), Field(name='tags', write=Location(chunk='ixml', key='KEYWORDS'), read_also=(), decode=<function split_keywords>, encode=<function join_keywords>, write_also=()), Field(name='fx_name', write=Location(chunk='ixml', key='FXNAME'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_catid', write=Location(chunk='ixml', key='CATID'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_category', write=Location(chunk='ixml', key='CATEGORY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='ucs_subcategory', write=Location(chunk='ixml', key='SUBCATEGORY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='library', write=Location(chunk='ixml', key='LIBRARY'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='manufacturer', write=Location(chunk='ixml', key='MANUFACTURER'), read_also=(Location(chunk='bext', key='originator'), ), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='designer', write=Location(chunk='ixml', key='DESIGNER'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='license', write=Location(chunk='ixml', key='LICENSE'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='license_url', write=Location(chunk='ixml', key='LICENSE_URL'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=()), Field(name='attribution', write=Location(chunk='ixml', key='ATTRIBUTION'), read_also=(), decode=<class 'str'>, encode=<class 'str'>, write_also=())), clear=(), report=<function _ignore>)[source]#
wavwith foley’s fieldsmetawritten whereFIELDSsays (bextandiXML): the inverse ofread_embedded_metadata().Fields named in
clearthatmetahas no value for are removed from the file (seechunks_from_fields()).wavcomes back unchanged when there is nothing to write. Seewrite_metadata_chunks()for what is kept, what is refused and what is told toreport.- Return type:
- foley.embedded_metadata.write_metadata_chunks(wav, chunks, *, builders={'bext': (b'bext', <function build_bext>), 'ixml': (b'iXML', <function build_ixml>)}, max_chunk_bytes=4194304, report=<function _ignore>)[source]#
wav(RIFF WAVE bytes) withchunks({"bext": {...}, "ixml": {...}}) written.A chunk the file has is rewritten in place (the first of its kind; any others are dropped, since a reader would see two), keeping what
chunksdoes not set (aNonevalue removes its field); one it lacks is inserted beforedata, unless all it would hold is removals. Every other chunk,dataincluded, is copied byte for byte; the RIFF size is recomputed and odd chunks are padded.What is lost is told to
report(one sentence each): duplicate chunks, an existing chunk overmax_chunk_bytes(replaced, not merged), an iXML that does not parse (replaced), and bytes after the last whole chunk or the RIFF’s declared end (dropped).- Raises:
ValueError –
wavis not a RIFF WAVE (RF64 / BW64 are refused: their sizes live inds64), has no wholedatachunk, would outgrow RIFF’s 4 GB, orchunksnames a chunk with no builder.- Return type: