# 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()`](#foley.weave.broadcast.broadcast_bed) makes it in one
call and returns a [`BroadcastBed`](#foley.weave.broadcast.BroadcastBed) whose licence carries the rights of **both**
sources, narrowed, never widened:

```default
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:

1. **speech** — each line becomes a mono clip at `sr` ([`as_clip()`](#foley.weave.broadcast.as_clip)); lines given as
   text go through the injected `tts=` seam first. [`join_lines()`](#foley.weave.broadcast.join_lines) puts `gap_s` of
   silence after each line.
2. **bed** — a library sound id, raw bed audio (with its `bed_license`), or
   `bed_prompt` through [`foley.generate()`](foley.html.md#foley.generate) (budget, consent and licence rules
   unchanged; the `generate=` seam replaces it in tests).
3. **mix** — [`mix_program()`](#foley.weave.broadcast.mix_program) loops speech and bed to `duration` (the WEAVE
   [`fit_duration()`](foley.weave.mix.html.md#foley.weave.mix.fit_duration) loop with its equal-power seam), sets the bed
   > `bed_under_speech_db` below the speech by integrated loudness, and ducks it
   > ([`duck_envelope()`](#foley.weave.broadcast.duck_envelope)): the bed holds that level while a line is spoken and swells
   > up to `-duck_db` back 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).
4. **treatment** — the whole program goes through the device (a radio colours the crowd
   it carries as much as the voice): a named `TreatmentPreset` from
   `TREATMENTS` (band-pass -> RMS compressor -> lookahead peak limiter), or any
   > `(samples, sr) -> samples` callable.

Rights are checked first ([`mix_refusal()`](#foley.weave.broadcast.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()`](foley.index.ingest.html.md#foley.index.ingest.ingest_one), under its narrowed licence (the speech’s
lineage and the bed’s id recorded), so [`foley.export_sound()`](foley.html.md#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()`](#foley.weave.broadcast.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:

```default
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`](foley.base.html.md#foley.base.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`](foley.base.html.md#module-foley.base) and the numpy-free [`foley.weave.mix`](foley.weave.mix.html.md#module-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

| [`LINE_GAP_S`](#foley.weave.broadcast.LINE_GAP_S)                | Silence after each spoken line (s); the 0.6 s that read naturally in the #108 starter.           |
|----------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------|
| [`BED_UNDER_SPEECH_DB`](#foley.weave.broadcast.BED_UNDER_SPEECH_DB)       | -12 dB).                                                                                         |
| [`BED_DUCK_DB`](#foley.weave.broadcast.BED_DUCK_DB)               | How far the bed dips under each line, relative to its level between lines (dB).                  |
| [`ACTIVITY_THRESHOLD`](#foley.weave.broadcast.ACTIVITY_THRESHOLD)        | A looped speech-activity mask counts as speech above this value (its seams crossfade).           |
| [`LOOP_CROSSFADE_S`](#foley.weave.broadcast.LOOP_CROSSFADE_S)          | Equal-power crossfade at each loop seam of the speech and the bed (s).                           |
| [`DEFAULT_TREATMENT`](#foley.weave.broadcast.DEFAULT_TREATMENT)         | The device a broadcast is heard through when none is named.                                      |
| [`TREATMENT_CEILING_DB`](#foley.weave.broadcast.TREATMENT_CEILING_DB)      | The treatment's peak ceiling (dBFS) after make-up gain (the same as mixing.treatment's default). |
| [`BROADCAST_OP`](#foley.weave.broadcast.BROADCAST_OP)              | The `transformations` op recorded on the result's licence.                                       |
| [`UNKNOWN_LICENSE_ID`](#foley.weave.broadcast.UNKNOWN_LICENSE_ID)        | The licence id of rights that were not given (fail-closed, never refused).                       |
| [`STORE_OWNED_INGEST_KWARGS`](#foley.weave.broadcast.STORE_OWNED_INGEST_KWARGS) | The `ingest_one` keywords `store=True` sets itself (not `ingest_options`).                       |
| [`ClipLike`](#foley.weave.broadcast.ClipLike)                  | an array at `sr`, `(samples, its_sr)`, encoded bytes, or a file path.                            |

### Functions

| [`as_clip`](#foley.weave.broadcast.as_clip)(item, sample_rate)                       | `item` as a mono `float32` working array at `sample_rate`.                                                            |
|---------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------|
| [`bed_gain_db`](#foley.weave.broadcast.bed_gain_db)(speech, bed, sample_rate, \*[, ...]) | The gain that puts `bed` `under_db` below `speech` in integrated loudness.                                            |
| [`broadcast_bed`](#foley.weave.broadcast.broadcast_bed)(lines, \*[, bed, bed_prompt, ...]) | Speech lines heard through a radio/TV/PA/phone over a bed, looped for a scene (#108).                                 |
| [`broadcast_license`](#foley.weave.broadcast.broadcast_license)(bed, speech, \*, treatment)    | The broadcast's rights: the bed's, narrowed to the speech's (never widened).                                          |
| [`duck_envelope`](#foley.weave.broadcast.duck_envelope)(activity, sample_rate, \*[, ...])  | The bed's linear gain: `1` while speech is active, up to `-duck_db` dB above between.                                 |
| [`join_lines`](#foley.weave.broadcast.join_lines)(clips, sample_rate, \*[, gap_s])      | Concatenate mono `clips` with `gap_s` of silence after each one.                                                      |
| [`mix_program`](#foley.weave.broadcast.mix_program)(speech, bed, sample_rate, \*[, ...]) | Speech over the bed, both fitted to `duration` (default: the speech's length).                                        |
| [`mix_refusal`](#foley.weave.broadcast.mix_refusal)(source, \*[, share_alike])           | Why a sound with rights `source` may not be treated and mixed in, or `None`.                                          |
| [`speech_activity`](#foley.weave.broadcast.speech_activity)(clips, sample_rate, \*[, gap_s]) | Where [`join_lines()`](#foley.weave.broadcast.join_lines) puts speech: `1` over each clip, `0` in the gaps. |
| [`store_refusal`](#foley.weave.broadcast.store_refusal)(rights, library, \*[, ...])        | Why a broadcast with `rights` may not go into `library`, or `None`.                                                   |

### Classes

| [`BroadcastBed`](#foley.weave.broadcast.BroadcastBed)(audio, sr, license, sources, ...)   | A rendered broadcast bed: the audio and the rights of everything in it.   |
|---------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------|

### Exceptions

| [`BroadcastRefusal`](#foley.weave.broadcast.BroadcastRefusal)   | A source whose rights do not allow it into a broadcast bed (refused, fail-closed).   |
|---------------------------------------------------------------------|--------------------------------------------------------------------------------------|

### foley.weave.broadcast.ACTIVITY_THRESHOLD *: [float](https://docs.python.org/3/builtins/functions.html#float)* *= 0.5*

A looped speech-activity mask counts as speech above this value (its seams crossfade).

### foley.weave.broadcast.BED_DUCK_DB *: [float](https://docs.python.org/3/builtins/functions.html#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](https://docs.python.org/3/builtins/functions.html#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](https://docs.python.org/3/builtins/stdtypes.html#str)* *= 'broadcast_bed'*

The `transformations` op recorded on the result’s licence.

### *class* foley.weave.broadcast.BroadcastBed(audio, sr, license, sources, treatment, notes=list(), sound_id=None)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object)

A rendered broadcast bed: the audio and the rights of everything in it.

#### audio

Mono `float32` samples.

* **Type:**
  `ndarray`

#### sr

Sample rate (Hz).

* **Type:**
  [`int`](https://docs.python.org/3/builtins/functions.html#int)

#### license

The mix’s rights ([`broadcast_license()`](#foley.weave.broadcast.broadcast_license)): the bed’s narrowed to the
speech’s, never wider than either.

* **Type:**
  [`LicenseRecord`](foley.base.html.md#foley.base.LicenseRecord)

#### sources

`(bed, speech)` provenance as given: the bed’s
[`SoundRecord`](foley.base.html.md#foley.base.SoundRecord) (library or generated) or
[`LicenseRecord`](foley.base.html.md#foley.base.LicenseRecord), and the speech’s licence (`None` if not
given). For the credit line use [`credits`](#foley.weave.broadcast.BroadcastBed.credits), which reads both off
`license`.

* **Type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)

#### treatment

The device’s name (`'dry'` for none).

* **Type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

#### notes

What the caller should know (unmeasurable levels, missing rights, the
bed generation’s own notes, the ingest’s).

* **Type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)

#### sound_id

The library id it was stored under (`store=True`), else `None`.

* **Type:**
  [`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

#### *property* credits

the bed, “derived from” the speech.

* **Type:**
  The attribution [`Credits`](foley.provenance.html.md#foley.provenance.Credits)

#### *property* duration_s *: [float](https://docs.python.org/3/builtins/functions.html#float)*

Length of [`audio`](#foley.weave.broadcast.BroadcastBed.audio) in seconds.

### *exception* foley.weave.broadcast.BroadcastRefusal

Bases: [`ValueError`](https://docs.python.org/3/builtins/exceptions.html#ValueError)

A 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

alias of `ndarray` | [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple) | [`bytes`](https://docs.python.org/3/builtins/stdtypes.html#bytes) | [`str`](https://docs.python.org/3/builtins/stdtypes.html#str) | [`PathLike`](https://docs.python.org/3/library/os.html#os.PathLike)

### foley.weave.broadcast.DEFAULT_TREATMENT *: [str](https://docs.python.org/3/builtins/stdtypes.html#str)* *= 'am_radio'*

The device a broadcast is heard through when none is named.

### foley.weave.broadcast.LINE_GAP_S *: [float](https://docs.python.org/3/builtins/functions.html#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](https://docs.python.org/3/builtins/functions.html#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](https://docs.python.org/3/builtins/stdtypes.html#frozenset)[[str](https://docs.python.org/3/builtins/stdtypes.html#str)]* *= frozenset({'allow_ai_training_forbidden', 'library', 'license', 'restamp_duplicate', 'sound_id', 'store'})*

The `ingest_one` keywords `store=True` sets itself (not `ingest_options`).

### foley.weave.broadcast.TREATMENT_CEILING_DB *: [float](https://docs.python.org/3/builtins/functions.html#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](https://docs.python.org/3/builtins/stdtypes.html#str)* *= 'unknown'*

The licence id of rights that were not given (fail-closed, never refused).

### foley.weave.broadcast.as_clip(item, sample_rate)

`item` as a mono `float32` working array at `sample_rate`.

A bare array is taken to be at `sample_rate` already; `(samples, sr)` is
resampled; bytes and paths are decoded with [`foley.audio.load()`](foley.audio.html.md#foley.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)

The gain that puts `bed` `under_db` below `speech` in integrated loudness.

Returns `(gain_db, note)`. When either is too short or too quiet to measure, the
gain is `under_db` applied to the bed as is, and `note` says so. A boost is
capped at `mixing.loudness.MAX_GAIN_DB`.

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`float`](https://docs.python.org/3/builtins/functions.html#float), [`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]]

### 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)

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()`](#foley.weave.broadcast.mix_refusal)). Then the bed is generated (if prompted) and gated, and only
then is the text spoken.

* **Parameters:**
  * **lines** ([`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)) – The spoken lines, in order: each an array at `sr`, `(samples, sr)`,
    encoded bytes or a path; or text (a `str` that is not a file), which goes
    through `tts=`. Audio lines never go through `tts`.
  * **bed** – The bed: a library sound id, or its audio (any `lines` form).
  * **bed_prompt** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]) – Instead of `bed`: a prompt for [`foley.generate()`](foley.html.md#foley.generate) (its budget,
    paid-generation consent and licence gate apply unchanged).
  * **treatment** (`Treatment`) – The device: a `TREATMENTS` name (`'am_radio'` default,
    `'tv'`, `'pa'`, `'phone'`), a `TreatmentPreset`, any
    `(samples, sr) -> samples` callable (e.g. the fleet’s, once `mixing` has
    it), or `None` for dry. Applied to the whole program, crowd included.
  * **gap_s** ([`float`](https://docs.python.org/3/builtins/functions.html#float)) – Silence after each line (s).
  * **duration** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`float`](https://docs.python.org/3/builtins/functions.html#float)]) – Length of the result (s); `None`: the lines once, with their gaps.
  * **bed_under_speech_db** ([`float`](https://docs.python.org/3/builtins/functions.html#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`](https://docs.python.org/3/builtins/functions.html#float)) – How far the bed dips under each line relative to its level between
    lines (dB, negative): the bed sits `bed_under_speech_db` under the speech
    while a line is spoken and swells up to `-duck_db` back 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`](https://docs.python.org/3/builtins/functions.html#float)) – Time constant (s) of the bed’s dip into each line.
  * **duck_release_s** ([`float`](https://docs.python.org/3/builtins/functions.html#float)) – Time constant (s) of the bed’s swell after each line.
  * **loop_speech** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool)) – Repeat the lines to fill `duration` (else they play once).
  * **crossfade_s** ([`float`](https://docs.python.org/3/builtins/functions.html#float)) – Equal-power crossfade at each loop seam (s).
  * **ceiling_db** ([`float`](https://docs.python.org/3/builtins/functions.html#float)) – The result’s peak ceiling (dBFS), held whatever the treatment
    (`mixing.loudness.limit_peaks()`).
  * **speech_license** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`LicenseRecord`](foley.base.html.md#foley.base.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`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`LicenseRecord`](foley.base.html.md#foley.base.LicenseRecord)]) – The rights of raw bed audio given as `bed` (a library or
    generated bed carries its own).
  * **tts** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)[[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)], [`Union`](https://docs.python.org/3/library/typing.html#typing.Union)[`ndarray`, [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple), [`bytes`](https://docs.python.org/3/builtins/stdtypes.html#bytes), [`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [`PathLike`](https://docs.python.org/3/library/os.html#os.PathLike)]]]) – A `text -> audio` callable 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`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`Callable`](https://docs.python.org/3/library/typing.html#typing.Callable)]) – The bed generator (default [`foley.generate()`](foley.html.md#foley.generate); a test seam).
  * **generate_options** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]) – Keyword arguments for it (`backend`, `budget`,
    `duration_s`, `store`, …; not `library`).
  * **store** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool)) – Also ingest the result into `library` by value, under its licence
    (both sources’ lineage recorded), so [`foley.export_sound()`](foley.html.md#foley.export_sound) and the
    credits work on it; its id is the result’s `sound_id`. Checked before
    anything is spent ([`store_refusal()`](#foley.weave.broadcast.store_refusal)): for a prompted bed, the
    speech’s rights and a named backend’s licence row before generating.
  * **accept_ai_restricted** ([`bool`](https://docs.python.org/3/builtins/functions.html#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 a `store=True` is refused, fail-closed. The
    consent is to embed and store only: the stored licence still forbids
    training.
  * **restamp** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool)) – With `store=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()`](foley.licensing.html.md#foley.licensing.restamped_license) with `remade=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 needs `accept_ai_restricted`. Exports already
    made keep the credits they were exported with.
  * **ingest_options** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)]) – More keywords for [`foley.index.ingest.ingest_one()`](foley.index.ingest.html.md#foley.index.ingest.ingest_one)
    (`tagger`, `do_supervised`, `thresholds`, …) with `store=True`.
  * **sr** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`int`](https://docs.python.org/3/builtins/functions.html#int)]) – Working sample rate (default `foley.audio.WORKING_SAMPLE_RATE`).
* **Return type:**
  [`BroadcastBed`](#foley.weave.broadcast.BroadcastBed)
* **Returns:**
  A [`BroadcastBed`](#foley.weave.broadcast.BroadcastBed).
* **Raises:**
  * [**BroadcastRefusal**](#foley.weave.broadcast.BroadcastRefusal) – A source’s rights do not allow it in ([`mix_refusal()`](#foley.weave.broadcast.mix_refusal)),
        or `store=True` cannot store the result ([`store_refusal()`](#foley.weave.broadcast.store_refusal)).
  * [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError) – Unusable lines, treatment, bed form or ingest options (before
        anything is spent).
  * [**KeyError**](https://docs.python.org/3/builtins/exceptions.html#KeyError) – A bed id that is neither a file nor in the library.

### foley.weave.broadcast.broadcast_license(bed, speech, , treatment)

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`](foley.sources.edit.html.md#foley.sources.edit.EditLineage): the permissions both hold,
either’s attribution and notice, the stricter AI-use scope, and a `transformations`
entry 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=None` narrows to unknown rights. Raises
[`BroadcastRefusal`](#foley.weave.broadcast.BroadcastRefusal) for a source [`mix_refusal()`](#foley.weave.broadcast.mix_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`](foley.licensing.html.md#foley.licensing.SHARE_ALIKE_FLAGS): credit it, keep its bytes, and
nothing foley could use it for in a work of its own).

* **Return type:**
  [`LicenseRecord`](foley.base.html.md#foley.base.LicenseRecord)

### foley.weave.broadcast.duck_envelope(activity, sample_rate, , duck_db=-10.0, attack=0.02, release=0.3)

The bed’s linear gain: `1` while speech is active, up to `-duck_db` dB above between.

`activity` above [`ACTIVITY_THRESHOLD`](#foley.weave.broadcast.ACTIVITY_THRESHOLD) counts as speech; the transitions are
the WEAVE attack/release follower ([`attack_release_envelope()`](foley.weave.mix.html.md#foley.weave.mix.attack_release_envelope)),
so the bed dips into each line and swells back after it without clicks. A
positive `duck_db` would invert the effect, so it is refused.

```pycon
>>> 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)

Concatenate mono `clips` with `gap_s` of silence after each one.

```pycon
>>> 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)

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. When `activity` (where
the speech is spoken, as from [`speech_activity()`](#foley.weave.broadcast.speech_activity)) is given, the bed is ducked
([`duck_envelope()`](#foley.weave.broadcast.duck_envelope), with `duck_attack_s` / `duck_release_s`):
`bed_under_speech_db` under each line, up to `-duck_db` louder between them; without it, or with `duck_db=0`, the bed sits at
`bed_under_speech_db` throughout. Returns `(mono, notes)`.

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[`ndarray`, [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]]

### foley.weave.broadcast.mix_refusal(source, , share_alike=None)

Why a sound with rights `source` may not be treated and mixed in, or `None`.

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_FLAGS`](foley.licensing.html.md#foley.licensing.LICENSE_FLAGS) row (an `ND` one 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()`](foley.licensing.html.md#foley.licensing.shared_share_alike)): the mix is
then released under it. `share_alike` is that licence, and a source that carries
it ([`carries_share_alike()`](foley.licensing.html.md#foley.licensing.carries_share_alike)) is allowed: the licence grants
adaptation, so neither its missing flag row nor its flags refuse it.

```pycon
>>> 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
```

* **Return type:**
  [`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]

### foley.weave.broadcast.speech_activity(clips, sample_rate, , gap_s=0.6)

Where [`join_lines()`](#foley.weave.broadcast.join_lines) puts speech: `1` over each clip, `0` in the gaps.

```pycon
>>> 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)

Why a broadcast with `rights` may not go into `library`, or `None`.

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()`](foley.index.ingest.html.md#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.

* **Return type:**
  [`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)[[`str`](https://docs.python.org/3/builtins/stdtypes.html#str)]
