foley.weave.broadcast#
A diegetic broadcast bed: speech lines heard through a radio/TV/PA/phone over a bed (#108).
A scene needs “the car radio”: a commentator’s lines over a stadium crowd, coloured as
the device that plays them, looped for the scene. broadcast_bed() makes it in one
call and returns a BroadcastBed whose licence carries the rights of both
sources, narrowed, never widened:
bed = foley.broadcast_bed(
[line1, line2], # speech already spoken (arrays, bytes, paths)
bed_prompt="football stadium crowd", # or bed=<library id or audio>
speech_license=speech_rights, # the speech's provenance, as given
duration=90.0,
)
bed.audio, bed.sr, bed.license
The pipeline is small pure stages, each its own function:
speech — each line becomes a mono clip at
sr(as_clip()); lines given as text go through the injectedtts=seam first.join_lines()putsgap_sof silence after each line.bed — a library sound id, raw bed audio (with its
bed_license), orbed_promptthroughfoley.generate()(budget, consent and licence rules unchanged; thegenerate=seam replaces it in tests).mix —
mix_program()loops speech and bed toduration(the WEAVEfit_duration()loop with its equal-power seam), sets the bedbed_under_speech_dbbelow the speech by integrated loudness, and ducks it (duck_envelope()): the bed holds that level while a line is spoken and swells up to-duck_dbback up between lines (a gap shorter than a few release time constants does not reach it: ~9.2 of 10 dB with the defaults;duck_db=0: a constant bed).treatment — the whole program goes through the device (a radio colours the crowd it carries as much as the voice): a named
TreatmentPresetfromTREATMENTS(band-pass -> RMS compressor -> lookahead peak limiter), or any(samples, sr) -> samplescallable.
Rights are checked first (mix_refusal(): no no-derivatives, territory-limited
or unmodifiable source on either side, and share-alike only when both sides are under
the identical share-alike licence, which the result is then released under), then the
bed is resolved or generated, and only then is any text spoken. A named generate backend’s licence row
is checked before it is called; a bed from an unnamed generation order can only be
gated on the clip it returns, so that refusal comes after the bed was paid for (never
after the speech). The result always ends in the peak limiter, whatever the
treatment.
Into the library. store=True ingests the result by value through the shared
ingest_one(), under its narrowed licence (the speech’s
lineage and the bed’s id recorded), so foley.export_sound() and the credits work
on it; the result’s sound_id names it. Whether it may be stored is checked before
anything is spent (store_refusal()): its bytes must be keepable, and when a
source’s licence does not allow AI use, embedding it needs accept_ai_restricted=True.
A byte-identical sound already stored with other credits is not handed back; a rerun
with corrected credits passes restamp=True to correct that record (never widened).
The treatment is a seam. It is an audio “look” with no foley-specific knowledge, so
its eventual home is the fleet’s audio package (mixing, beside
mixing.loudness), not foley; until it lands there, foley’s in-house preset chain
is the default (numpy + scipy, which foley[audio] already brings through
mixing[loudness]; the limiter is mixing.loudness.limit_peaks()). Swapping in
the fleet’s one is treatment=<its callable>.
Text-to-speech is a seam with no default. tts is any text -> audio callable
(an array at sr, (samples, sr), encoded bytes, or a path); foley adds no TTS
dependency. Two that exist in the fleet:
import voxy
tts = lambda text: voxy.text_to_speech(text, "commentator").audio # encoded bytes
from an.audio.elevenlabs_tts import ElevenLabsTTS
eleven = ElevenLabsTTS(output_format="pcm_44100") # a format soundfile decodes
tts = lambda text: eleven.synthesize(text, VOICE_ID).bytes_
The speech’s rights are never inferred from the TTS call: pass them as
speech_license (e.g. a LicenseRecord for the TTS vendor’s
terms). Without it the result is fail-closed: the speech counts as unknown rights.
Import cost: stdlib, foley.base and the numpy-free foley.weave.mix constants; numpy, scipy and mixing.loudness
are imported inside the functions that need them, so import foley.weave stays
dol-only.
Module Attributes
Silence after each spoken line (s); the 0.6 s that read naturally in the #108 starter. |
|
-12 dB). |
|
How far the bed dips under each line, relative to its level between lines (dB). |
|
A looped speech-activity mask counts as speech above this value (its seams crossfade). |
|
Equal-power crossfade at each loop seam of the speech and the bed (s). |
|
The device a broadcast is heard through when none is named. |
|
The treatment's peak ceiling (dBFS) after make-up gain (the same as mixing.treatment's default). |
|
The |
|
The licence id of rights that were not given (fail-closed, never refused). |
|
The |
|
an array at |
Functions
|
|
|
The gain that puts |
|
Speech lines heard through a radio/TV/PA/phone over a bed, looped for a scene (#108). |
|
The broadcast's rights: the bed's, narrowed to the speech's (never widened). |
|
The bed's linear gain: |
|
Concatenate mono |
|
Speech over the bed, both fitted to |
|
Why a sound with rights |
|
Where |
|
Why a broadcast with |
Classes
|
A rendered broadcast bed: the audio and the rights of everything in it. |
Exceptions
A source whose rights do not allow it into a broadcast bed (refused, fail-closed). |
- foley.weave.broadcast.ACTIVITY_THRESHOLD: float = 0.5#
A looped speech-activity mask counts as speech above this value (its seams crossfade).
- foley.weave.broadcast.BED_DUCK_DB: float = -10.0#
How far the bed dips under each line, relative to its level between lines (dB).
- foley.weave.broadcast.BED_UNDER_SPEECH_DB: float = -12.0#
-12 dB).
- Type:
Where the bed sits under the speech, in LU of integrated loudness (#108
- foley.weave.broadcast.BROADCAST_OP: str = 'broadcast_bed'#
The
transformationsop recorded on the result’s licence.
- class foley.weave.broadcast.BroadcastBed(audio, sr, license, sources, treatment, notes=list(), sound_id=None)[source]#
Bases:
objectA rendered broadcast bed: the audio and the rights of everything in it.
- audio#
Mono
float32samples.- Type:
ndarray
- license#
The mix’s rights (
broadcast_license()): the bed’s narrowed to the speech’s, never wider than either.- Type:
- sources#
(bed, speech)provenance as given: the bed’sSoundRecord(library or generated) orLicenseRecord, and the speech’s licence (Noneif not given). For the credit line usecredits, which reads both offlicense.- Type:
- notes#
What the caller should know (unmeasurable levels, missing rights, the bed generation’s own notes, the ingest’s).
- Type:
- exception foley.weave.broadcast.BroadcastRefusal[source]#
Bases:
ValueErrorA source whose rights do not allow it into a broadcast bed (refused, fail-closed).
- foley.weave.broadcast.ClipLike#
an array at
sr,(samples, its_sr), encoded bytes, or a file path.- Type:
One clip
- foley.weave.broadcast.DEFAULT_TREATMENT: str = 'am_radio'#
The device a broadcast is heard through when none is named.
- foley.weave.broadcast.LINE_GAP_S: float = 0.6#
Silence after each spoken line (s); the 0.6 s that read naturally in the #108 starter.
- foley.weave.broadcast.LOOP_CROSSFADE_S: float = 0.05#
Equal-power crossfade at each loop seam of the speech and the bed (s).
- foley.weave.broadcast.STORE_OWNED_INGEST_KWARGS: frozenset[str] = frozenset({'allow_ai_training_forbidden', 'library', 'license', 'restamp_duplicate', 'sound_id', 'store'})#
The
ingest_onekeywordsstore=Truesets itself (notingest_options).
- foley.weave.broadcast.TREATMENT_CEILING_DB: float = -1.0#
The treatment’s peak ceiling (dBFS) after make-up gain (the same as mixing.treatment’s default).
- foley.weave.broadcast.UNKNOWN_LICENSE_ID: str = 'unknown'#
The licence id of rights that were not given (fail-closed, never refused).
- foley.weave.broadcast.as_clip(item, sample_rate)[source]#
itemas a monofloat32working array atsample_rate.A bare array is taken to be at
sample_ratealready;(samples, sr)is resampled; bytes and paths are decoded withfoley.audio.load(). Integer PCM is scaled to full scale 1.0.- Return type:
ndarray
- foley.weave.broadcast.bed_gain_db(speech, bed, sample_rate, *, under_db=-12.0)[source]#
The gain that puts
bedunder_dbbelowspeechin integrated loudness.Returns
(gain_db, note). When either is too short or too quiet to measure, the gain isunder_dbapplied to the bed as is, andnotesays so. A boost is capped atmixing.loudness.MAX_GAIN_DB.
- foley.weave.broadcast.broadcast_bed(lines, *, bed=None, bed_prompt=None, treatment='am_radio', gap_s=0.6, duration=None, bed_under_speech_db=-12.0, duck_db=-10.0, duck_attack_s=0.02, duck_release_s=0.3, loop_speech=True, crossfade_s=0.05, ceiling_db=-1.0, speech_license=None, bed_license=None, tts=None, library=None, generate=None, generate_options=None, store=False, accept_ai_restricted=False, restamp=False, ingest_options=None, sr=None)[source]#
Speech lines heard through a radio/TV/PA/phone over a bed, looped for a scene (#108).
Everything that can be checked is checked before anything is paid for: the lines, the treatment, the bed’s form, and the rights of the speech and of a given bed (
mix_refusal()). Then the bed is generated (if prompted) and gated, and only then is the text spoken.- Parameters:
lines (
Sequence) – The spoken lines, in order: each an array atsr,(samples, sr), encoded bytes or a path; or text (astrthat is not a file), which goes throughtts=. Audio lines never go throughtts.bed – The bed: a library sound id, or its audio (any
linesform).bed_prompt (
Optional[str]) – Instead ofbed: a prompt forfoley.generate()(its budget, paid-generation consent and licence gate apply unchanged).treatment (
Treatment) – The device: aTREATMENTSname ('am_radio'default,'tv','pa','phone'), aTreatmentPreset, any(samples, sr) -> samplescallable (e.g. the fleet’s, oncemixinghas it), orNonefor dry. Applied to the whole program, crowd included.gap_s (
float) – Silence after each line (s).duration (
Optional[float]) – Length of the result (s);None: the lines once, with their gaps.bed_under_speech_db (
float) – The bed’s level under the speech (LU, integrated loudness), set before the treatment: the device’s compressor then narrows the gap a little, as a real one would.duck_db (
float) – How far the bed dips under each line relative to its level between lines (dB, negative): the bed sitsbed_under_speech_dbunder the speech while a line is spoken and swells up to-duck_dbback up in the gaps (short gaps fall short: ~9.2 dB with the defaults). Must be<= 0.0: a constant bed (the behaviour before #253).duck_attack_s (
float) – Time constant (s) of the bed’s dip into each line.duck_release_s (
float) – Time constant (s) of the bed’s swell after each line.loop_speech (
bool) – Repeat the lines to fillduration(else they play once).crossfade_s (
float) – Equal-power crossfade at each loop seam (s).ceiling_db (
float) – The result’s peak ceiling (dBFS), held whatever the treatment (mixing.loudness.limit_peaks()).speech_license (
Optional[LicenseRecord]) – The speech’s rights, as given (e.g. the TTS vendor’s terms).None: unknown, so the result’s rights fail closed.bed_license (
Optional[LicenseRecord]) – The rights of raw bed audio given asbed(a library or generated bed carries its own).tts (
Optional[Callable[[str],Union[ndarray,tuple,bytes,str,PathLike]]]) – Atext -> audiocallable that speaks text lines (no default: see the module docstring for voxy and an’s ElevenLabs provider).library – Where a bed id is looked up and a generated bed is stored (default: the process-wide library).
generate (
Optional[Callable]) – The bed generator (defaultfoley.generate(); a test seam).generate_options (
Optional[dict]) – Keyword arguments for it (backend,budget,duration_s,store, …; notlibrary).store (
bool) – Also ingest the result intolibraryby value, under its licence (both sources’ lineage recorded), sofoley.export_sound()and the credits work on it; its id is the result’ssound_id. Checked before anything is spent (store_refusal()): for a prompted bed, the speech’s rights and a named backend’s licence row before generating.accept_ai_restricted (
bool) – Consent to embed (index) the result when a source’s licence does not allow AI use (an AI-generated bed’s, a non-commercial speech’s). Without it such astore=Trueis refused, fail-closed. The consent is to embed and store only: the stored licence still forbids training.restamp (
bool) – Withstore=True: when a byte-identical sound is already stored with other credits, licence or lineage (a rerun with corrected credits), correct that stored record to this broadcast’s instead of refusing it (sound_id=None). Through the ingest’s re-stamp (foley.licensing.restamped_license()withremade=True): its credits and lineage are replaced, its rights only ever narrowed (a wider rerun is refused with a note, the record kept), and a licence that does not allow AI use still needsaccept_ai_restricted. Exports already made keep the credits they were exported with.ingest_options (
Optional[dict]) – More keywords forfoley.index.ingest.ingest_one()(tagger,do_supervised,thresholds, …) withstore=True.sr (
Optional[int]) – Working sample rate (defaultfoley.audio.WORKING_SAMPLE_RATE).
- Return type:
- Returns:
A
BroadcastBed.- Raises:
BroadcastRefusal – A source’s rights do not allow it in (
mix_refusal()), orstore=Truecannot store the result (store_refusal()).ValueError – Unusable lines, treatment, bed form or ingest options (before anything is spent).
KeyError – A bed id that is neither a file nor in the library.
- foley.weave.broadcast.broadcast_license(bed, speech, *, treatment)[source]#
The broadcast’s rights: the bed’s, narrowed to the speech’s (never widened).
A copy of the bed’s licence, narrowed to the speech’s through the same lineage an edit records (
foley.sources.edit.EditLineage: the permissions both hold, either’s attribution and notice, the stricter AI-use scope, and atransformationsentry the credits and content credential read as “derived from” the speech). It is AI-generated, and its AI disclosure recommended, if either source’s is; the generator named is the bed’s, else the speech’s. Its bytes may be stored only if both allow it, and its rights are verified only if both are. The bed’s own watermark and content-credential reference are dropped: they described the bed’s bytes, not the mix.speech=Nonenarrows to unknown rights. RaisesBroadcastRefusalfor a sourcemix_refusal()refuses.When both are under the identical share-alike licence (#253), so is the result: the bed’s licence id is kept, and the narrowing bounds its flags by that licence’s row (
SHARE_ALIKE_FLAGS: credit it, keep its bytes, and nothing foley could use it for in a work of its own).- Return type:
- foley.weave.broadcast.duck_envelope(activity, sample_rate, *, duck_db=-10.0, attack=0.02, release=0.3)[source]#
The bed’s linear gain:
1while speech is active, up to-duck_dbdB above between.activityaboveACTIVITY_THRESHOLDcounts as speech; the transitions are the WEAVE attack/release follower (attack_release_envelope()), so the bed dips into each line and swells back after it without clicks. A positiveduck_dbwould invert the effect, so it is refused.>>> duck_envelope([1.0, 1.0], 10, duck_db=-6.0).tolist() [1.0, 1.0]
- Return type:
ndarray
- foley.weave.broadcast.join_lines(clips, sample_rate, *, gap_s=0.6)[source]#
Concatenate mono
clipswithgap_sof silence after each one.>>> import numpy as np >>> join_lines([np.ones(2), np.ones(3)], 10, gap_s=0.1).tolist() [1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0]
- Return type:
ndarray
- foley.weave.broadcast.mix_program(speech, bed, sample_rate, *, duration=None, bed_under_speech_db=-12.0, activity=None, duck_db=-10.0, duck_attack_s=0.02, duck_release_s=0.3, loop_speech=True, crossfade_s=0.05)[source]#
Speech over the bed, both fitted to
duration(default: the speech’s length).The bed always loops (seamless, equal-power seams); the speech loops too when
loop_speech, else plays once and the bed carries on. Whenactivity(where the speech is spoken, as fromspeech_activity()) is given, the bed is ducked (duck_envelope(), withduck_attack_s/duck_release_s):bed_under_speech_dbunder each line, up to-duck_dblouder between them; without it, or withduck_db=0, the bed sits atbed_under_speech_dbthroughout. Returns(mono, notes).
- foley.weave.broadcast.mix_refusal(source, *, share_alike=None)[source]#
Why a sound with rights
sourcemay not be treated and mixed in, orNone.The broadcast’s licence is the bed’s narrowed to the speech’s, and the device modifies both sources. Refused: a share-alike licence (on the record or in its lineage) the broadcast cannot carry, a territory exclusion, a licence with no
LICENSE_FLAGSrow (anNDone among them), or one that forbids modification. Unknown rights ('unknown') are not refused: the result fails closed instead.The broadcast can carry a share-alike licence only when every source is under that identical licence (
shared_share_alike()): the mix is then released under it.share_alikeis that licence, and a source that carries it (carries_share_alike()) is allowed: the licence grants adaptation, so neither its missing flag row nor its flags refuse it.>>> from foley.licensing import apply_license_flags >>> ok = apply_license_flags(LicenseRecord(source="s", license_id="CC-BY-4.0")) >>> mix_refusal(ok) is None True >>> sa = LicenseRecord(source="s", license_id="CC-BY-SA-4.0") >>> mix_refusal(sa)[:40] 'its licence (CC-BY-SA-4.0) is share-alik' >>> mix_refusal(sa, share_alike="CC-BY-SA-4.0") is None True
- foley.weave.broadcast.speech_activity(clips, sample_rate, *, gap_s=0.6)[source]#
Where
join_lines()puts speech:1over each clip,0in the gaps.>>> import numpy as np >>> speech_activity([np.zeros(2), np.ones(1)], 10, gap_s=0.1).tolist() [1.0, 1.0, 0.0, 1.0, 0.0]
- Return type:
ndarray
- foley.weave.broadcast.store_refusal(rights, library, *, accept_ai_restricted=False)[source]#
Why a broadcast with
rightsmay not go intolibrary, orNone.Asked before anything is spent. The bytes are stored by value (a mix has no source to point at), so both sources must allow keeping them (
cache_bytes_ok). Indexing embeds the clip, which is AI use (foley.index.ingest.embedding_permitted()): when a source’s licence does not allow it (an AI-generated bed’s, a non-commercial speech’s), it needs the caller’s explicit consent,accept_ai_restricted, as every ingest does.