1.0.0 release
This commit is contained in:
@@ -0,0 +1,367 @@
|
||||
# Multi-Channel Expansion (MCE)
|
||||
|
||||
Lyrebird supports multi-channel signal flow through two complementary mechanisms:
|
||||
|
||||
1. **Multi-row single Generators** — one Generator instance whose internal `_result` has
|
||||
multiple rows (`Pan2`, the `Pan4` family — `PanAz` / `PanAzDistance` / `PanXY` / `PanXYDistance`, `PanN`, `AmbiEncoder1`, `AmbiDecoder1`, `SVF`, multi-channel
|
||||
`In` / `BufferRead` / `PlayBuffer`, `FrequencyShifter.Audio`, `Chorus.Audio`,
|
||||
`TimeStretch.Audio`).
|
||||
2. **`GeneratorArray`** — explicit wrapper around N independent generators, each
|
||||
contributing one channel.
|
||||
|
||||
The `+ - * /` operators understand both forms — see [Multi-channel operators](#multi-channel-operators)
|
||||
below.
|
||||
|
||||
---
|
||||
|
||||
## `outputChannelCount`
|
||||
|
||||
Every `Generator` advertises its fixed output channel count via `var outputChannelCount: Int`.
|
||||
The default (provided by `GeneratorBase`) is `1`. Multi-row generators override to return their
|
||||
actual channel count (set at `init`, never changes at runtime). The arithmetic operators read
|
||||
this property to decide how to broadcast across operand pairs.
|
||||
|
||||
When writing a custom multi-row Generator, you **must** override `outputChannelCount` to match
|
||||
the row count of your `_result`. A missing override causes the `+ - * /` operators to treat
|
||||
your generator as mono, silently dropping channels 1..N-1.
|
||||
|
||||
---
|
||||
|
||||
## Multi-channel operators
|
||||
|
||||
The `+ - * /` operators on `Generator` (and `any ValidGeneratorInput`) produce a
|
||||
`BinaryOpGenerator` that is channel-aware. The output channel count and per-channel
|
||||
combination rule are decided at graph-construction time from each operand's
|
||||
`outputChannelCount`:
|
||||
|
||||
| lhs × rhs | Output | Behaviour |
|
||||
|-----------|--------|-----------|
|
||||
| `1 × 1` | 1-row | Classic scalar / mono fast path. |
|
||||
| `1 × N` | N-row | Mono `lhs` broadcasts: row[i] = lhs[0] op rhs[i]. |
|
||||
| `N × 1` | N-row | Mono `rhs` broadcasts: row[i] = lhs[i] op rhs[0]. |
|
||||
| `N × N` | N-row | Element-wise: row[i] = lhs[i] op rhs[i]. |
|
||||
| `N × M`, both > 1, N ≠ M | — | `precondition` traps at `init`. No implicit wrap-extend, no truncation — Lyrebird requires explicit per-channel composition. |
|
||||
|
||||
```swift
|
||||
// Stereo × scalar — both channels scale.
|
||||
let pan = Pan2(input: osc, pan: -0.3)
|
||||
output(bus: 0, source: pan * 0.5)
|
||||
|
||||
// Mono + stereo — mono broadcasts to both channels.
|
||||
let dry = OscSin.Audio(freq: 220)
|
||||
output(bus: 0, source: dry + pan)
|
||||
|
||||
// Stereo + stereo — element-wise.
|
||||
let dry2ch = input(bus: 0, channels: 2)
|
||||
let wet2ch = input(bus: 4, channels: 2)
|
||||
output(bus: 0, source: dry2ch * 0.7 + wet2ch * 0.3)
|
||||
|
||||
// 2 × 3 — traps. Compose explicitly via channel selection or GeneratorArray.
|
||||
// stereoGen * tripleGen // precondition fail at init
|
||||
```
|
||||
|
||||
### Channel-aware `mul:` / `add:` parameters
|
||||
|
||||
The `mul:` and `add:` parameters on every Generator (`OscSin.Audio(..., mul: x)`,
|
||||
`SVF.Audio(..., add: y)`, etc.) follow the same broadcast policy as the `*` / `+` operators
|
||||
above:
|
||||
|
||||
- A mono (1-channel) `mul`/`add` broadcasts to every output channel — the historical
|
||||
scalar-gain case. A scalar literal, a `ParameterNode`, a `Lag`, or any other single-channel
|
||||
generator works unchanged.
|
||||
- A multi-channel `mul`/`add` whose channel count matches the output applies element-wise
|
||||
per channel — useful for per-side gain on a stereo source, per-component scaling on an
|
||||
ambisonic encoder, etc.
|
||||
- A multi-channel `mul`/`add` whose channel count does **not** match the output traps via
|
||||
`precondition` at the apply site (same shape-mismatch policy as `BinaryOpGenerator`).
|
||||
|
||||
Use `*` for free composition with the broadcast rules; use `mul:` for scalar gain trims and
|
||||
matched-shape per-channel modulation that should ride alongside the generator's main output.
|
||||
|
||||
---
|
||||
|
||||
## GeneratorArray
|
||||
|
||||
`GeneratorArray` wraps N independent generators and produces an N-channel
|
||||
`[[LyrebirdFloat]]`. It conforms to `Generator`, so it participates in the fan-out cache
|
||||
and can be passed anywhere a `ValidGeneratorInput` is accepted.
|
||||
|
||||
```swift
|
||||
let arr = GeneratorArray([OscSin.Audio(freq: 440.0), OscSin.Audio(freq: 880.0)])
|
||||
```
|
||||
|
||||
### expand()
|
||||
|
||||
The `expand(_:factory:)` free function is the idiomatic way to create a `GeneratorArray`
|
||||
from an array of inputs:
|
||||
|
||||
```swift
|
||||
let oscs = expand([440.0, 880.0]) { OscSin.Audio(freq: $0) }
|
||||
output(bus: 0, source: oscs) // bus 0 = 440 Hz, bus 1 = 880 Hz
|
||||
```
|
||||
|
||||
### Element-wise operators
|
||||
|
||||
All four arithmetic operators work element-wise and return a new `GeneratorArray`:
|
||||
|
||||
```swift
|
||||
let quiet = oscs * 0.5 // each channel × 0.5
|
||||
let louder = 2.0 * oscs // scalar op GeneratorArray
|
||||
let sum = oscs + otherOscs // element-wise (counts must match)
|
||||
let diff = oscs - 0.1 // subtract scalar from each channel
|
||||
```
|
||||
|
||||
Mismatched-count `GeneratorArray op GeneratorArray` is caught by a `precondition` at
|
||||
build time.
|
||||
|
||||
`GeneratorArray op multi-row-generator` (e.g. `oscs * Pan2(...)`) is also caught by a
|
||||
`precondition`. `GeneratorArray._computeSamples` reads only row 0 of each inner generator,
|
||||
so a multi-row operand would silently drop channels 1..N-1 inside each element-wise
|
||||
product. The fix is to compose explicitly via `ChannelSelect` (`pan.left` / `pan.right`)
|
||||
or by zipping per-channel into a `GeneratorArray`. For arithmetic on multi-row
|
||||
generators directly, use the `+ - * /` operators on the generator itself (Pan2, SVF, etc.) —
|
||||
those broadcast properly without needing a `GeneratorArray` wrapper.
|
||||
|
||||
---
|
||||
|
||||
## Multi-channel Out
|
||||
|
||||
`Out` writes each channel of its source to consecutive buses automatically:
|
||||
|
||||
| Source channels | Buses written |
|
||||
|---|---|
|
||||
| 1 (mono — current default) | `bus` |
|
||||
| 2 (stereo) | `bus`, `bus + 1` |
|
||||
| N | `bus` … `bus + N - 1` |
|
||||
|
||||
```swift
|
||||
output(bus: 0, source: Pan2(input: osc, pan: 0.0)) // writes buses 0 and 1
|
||||
output(bus: 0, source: expand([440.0, 880.0]) { OscSin.Audio(freq: $0) }) // same
|
||||
```
|
||||
|
||||
Mono sources remain unaffected — they return one channel and write only `bus`.
|
||||
|
||||
---
|
||||
|
||||
## Pan2
|
||||
|
||||
Equal-power stereo panner. Maps mono input + pan position `[-1, 1]` to a two-channel
|
||||
output `[left, right]` using the sine/cosine law:
|
||||
|
||||
```
|
||||
angle = (pan + 1) / 2 × π/2
|
||||
left = cos(angle) × input
|
||||
right = sin(angle) × input
|
||||
```
|
||||
|
||||
Power invariant: `left² + right² = input²` at all pan positions.
|
||||
|
||||
```swift
|
||||
output(bus: 0, source: Pan2(input: OscSin.Audio(freq: 440.0), pan: -0.3))
|
||||
// bus 0 = left (slightly louder), bus 1 = right
|
||||
```
|
||||
|
||||
Pan position accepts any `ValidGeneratorInput` and may be modulated at audio rate.
|
||||
|
||||
---
|
||||
|
||||
## Multi-channel input
|
||||
|
||||
Read N consecutive buses with `input(bus:channels:)` inside `build()`, mirroring
|
||||
`output(bus:source:)`:
|
||||
|
||||
```swift
|
||||
// Read stereo from buses 2–3, apply gain, route to output buses 0–1
|
||||
output(bus: 0, source: input(bus: 2, channels: 2) * 0.5)
|
||||
```
|
||||
|
||||
To read hardware input, use the reserved bus offset:
|
||||
|
||||
```swift
|
||||
// Assuming engine configured with numOutputChannels = 2
|
||||
output(bus: 0, source: input(bus: engine.numOutputChannels, channels: 2))
|
||||
```
|
||||
|
||||
`In.Audio(bus:channels:)` (or `In.Control(bus:channels:)`) is the underlying constructor
|
||||
if you need it outside `SynthesizerBase`. The `bus:` parameter accepts any `ValidBus`:
|
||||
plain `Int`, `AudioBus`, or `ControlBus`.
|
||||
|
||||
---
|
||||
|
||||
## Common patterns
|
||||
|
||||
### Stereo oscillator pair
|
||||
|
||||
```swift
|
||||
override func build() {
|
||||
let oscs = expand([440.0, 880.0]) { OscSin.Audio(freq: $0) }
|
||||
output(bus: 0, source: oscs * 0.5)
|
||||
}
|
||||
```
|
||||
|
||||
### Stereo panner
|
||||
|
||||
```swift
|
||||
// Example — your synthesizer, defined by subclassing SynthesizerBase
|
||||
final class StereoPanner: SynthesizerBase {
|
||||
@LiveParam var pan: LyrebirdFloat = 0.0
|
||||
|
||||
override func build() {
|
||||
let osc = OscSin.Audio(freq: 440.0)
|
||||
output(bus: 0, source: Pan2(input: osc, pan: $pan))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Stereo FX return
|
||||
|
||||
```swift
|
||||
override func build() {
|
||||
// Read wet signal from internal buses 4–5, mix back to output
|
||||
let wet = input(bus: 4, channels: 2)
|
||||
let dry = input(bus: 0, channels: 2)
|
||||
output(bus: 0, source: dry * 0.7 + wet * 0.3)
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
---
|
||||
|
||||
## Inline Channel Selection
|
||||
|
||||
Multi-output generators can be decoupled into individual channels without bus routing.
|
||||
All `AudioGenerator` and `ControlGenerator` instances support a `[n]` subscript and a
|
||||
`.channel(n)` method that return a `ChannelSelect.Audio` (or `.Control`) wrapping the
|
||||
source and extracting channel `n`. Out-of-range indices return silence — no crash.
|
||||
|
||||
Named semantic aliases are provided on specific generators as ergonomic sugar over `[n]`.
|
||||
|
||||
### Named aliases
|
||||
|
||||
| Generator | Property | Channel | Notes |
|
||||
|---|---|---|---|
|
||||
| `Pan2` | `.left` | 0 | |
|
||||
| `Pan2` | `.right` | 1 | |
|
||||
| `Pan4` subclasses (`PanAz`, `PanAzDistance`, `PanXY`, `PanXYDistance`) | `.frontLeft` | 0 | inherited from `Pan4` |
|
||||
| `Pan4` subclasses | `.frontRight` | 1 | |
|
||||
| `Pan4` subclasses | `.rearRight` | 2 | |
|
||||
| `Pan4` subclasses | `.rearLeft` | 3 | |
|
||||
| `FreqTracker` | `.freq` | 0 | Hz, 0.0 when no pitch |
|
||||
| `FreqTracker` | `.confidence` | 1 | [0, 1] |
|
||||
|
||||
### Dynamic-channel generators
|
||||
|
||||
Generators whose channel count is determined at `init` also support `[n]`:
|
||||
|
||||
| Generator | Channel count | Index meaning |
|
||||
|---|---|---|
|
||||
| `PlayBuffer` | `buffer.channelCount` | one per audio file channel |
|
||||
| `PanN` | `channels` (init param) | one per speaker in the ring |
|
||||
| `AmbiEncoder1` | 4 | 0=W, 1=X, 2=Y, 3=Z |
|
||||
| `AmbiDecoder1` | `speakers.count` (init param) | one per loudspeaker |
|
||||
| `GeneratorArray` | `generators.count` | one per wrapped generator |
|
||||
|
||||
Out-of-range `[n]` always returns silence — no crash.
|
||||
|
||||
### Examples
|
||||
|
||||
```swift
|
||||
// Pan2 — named or numeric
|
||||
let pan = Pan2(input: src, pan: -0.3)
|
||||
output(bus: 0, source: pan.left) // same as pan[0]
|
||||
output(bus: 1, source: pan.right) // same as pan[1]
|
||||
|
||||
// Mono fold-down inline
|
||||
output(bus: 0, source: pan[0] + pan[1])
|
||||
|
||||
// FreqTracker — no buses required
|
||||
let tracker = FreqTracker(in: mic)
|
||||
let osc = OscSin.Audio(freq: tracker.freq * 2.0)
|
||||
output(bus: 0, source: osc * tracker.confidence)
|
||||
|
||||
// Generic subscript on any generator
|
||||
output(bus: 0, source: PanXY(input: src)[2]) // rear-right channel
|
||||
|
||||
// PlayBuffer — route channels independently
|
||||
let player = PlayBuffer.Audio(buffer: stereoBuffer)
|
||||
let rev = SomeReverb(input: player[0])
|
||||
output(bus: 0, source: player[0] * 0.7 + rev * 0.3)
|
||||
output(bus: 1, source: player[1])
|
||||
|
||||
// AmbiEncoder1 — extract individual B-format components
|
||||
let enc = AmbiEncoder1(input: src)
|
||||
let W = enc[0] // omni
|
||||
let X = enc[1] // front-back
|
||||
|
||||
// AmbiDecoder1 — per-speaker processing (encoder feeds decoder directly).
|
||||
// Use AmbiSpeakerLayout for the common symmetric rigs (quad / hexagon /
|
||||
// octagon / cube / doubleHexagon / doubleOctagon) or stackedRings(...).
|
||||
let dec = AmbiDecoder1(input: enc, speakers: AmbiSpeakerLayout.quad())
|
||||
output(bus: 0, source: dec[0] * frontGain)
|
||||
output(bus: 1, source: dec[1] * frontGain)
|
||||
output(bus: 2, source: dec[2])
|
||||
output(bus: 3, source: dec[3])
|
||||
|
||||
// PanN — extract individual speakers
|
||||
let ring = PanN(input: src, channels: 8, position: lfo)
|
||||
output(bus: 0, source: ring[0]) // speaker 0 only
|
||||
output(bus: 4, source: ring[4]) // speaker 4 only
|
||||
|
||||
// MCE decoupling — GeneratorArray subscript
|
||||
let oscs = expand([440.0, 880.0, 1320.0]) { OscSin.Audio(freq: $0) }
|
||||
output(bus: 0, source: oscs[0] + oscs[1]) // mix first two partials only
|
||||
|
||||
// Stereo signal → per-channel analysis → four signals inline (no buses)
|
||||
let stereo = PlayBuffer.Audio(buffer: stereoBuffer) // 2-channel
|
||||
let trackerL = FreqTracker(in: stereo[0]) // left → [freq, confidence]
|
||||
let trackerR = FreqTracker(in: stereo[1]) // right → [freq, confidence]
|
||||
output(bus: 0, source: OscSin.Audio(freq: trackerL.freq) * trackerL.confidence)
|
||||
output(bus: 1, source: OscSin.Audio(freq: trackerR.freq) * trackerR.confidence)
|
||||
```
|
||||
|
||||
### Fan-out guarantee
|
||||
|
||||
`pan.left` and `pan.right` both hold a reference to the same `Pan2` instance. When both
|
||||
are evaluated in one segment, the segment-serial cache ensures `Pan2._computeSamples`
|
||||
fires exactly once — both selectors see sample-consistent data at zero extra cost.
|
||||
|
||||
### GeneratorArray note
|
||||
|
||||
`GeneratorArray` also supports `[n]`, returning `ChannelSelect.Audio` that selects the
|
||||
Nth channel of the entire array's output. For the raw generator instance, use `.generators[n]`.
|
||||
|
||||
---
|
||||
|
||||
## Angle convention
|
||||
|
||||
Every panning and ambisonic generator that accepts an azimuth, elevation, spread,
|
||||
or rotation parameter takes the angle in **radians** — never degrees. Affected:
|
||||
`AmbiEncoder1` (`azimuth`, `elevation`), `AmbiDecoder1` (`speakers: [(az, el)]`),
|
||||
`AmbiDecoderMono` (`azimuth`, `elevation`), `AmbiEncoderStereo` (`spread`),
|
||||
`AmbiSpeakerLayout` (`elevation`, `azimuthOffset`), `PanAz` / `PanAzDistance`
|
||||
(`azimuth`).
|
||||
|
||||
Convert at the language-side boundary (UI knobs, OSC messages, score files
|
||||
authored in degrees) with `degreesToRadians(_:)` / `radiansToDegrees(_:)` from
|
||||
`LyrebirdMusicMath`. Array overloads and `Collection<Double>.degreesToRadians()` /
|
||||
`.radiansToDegrees()` are also available for batched layout setup.
|
||||
|
||||
```swift
|
||||
let enc = AmbiEncoder1(input: src, azimuth: degreesToRadians(45.0))
|
||||
```
|
||||
|
||||
The `Pan2` / `PanN` / `PanXY` family is the exception — those take normalised
|
||||
position in `[-1, 1]` and apply their own internal scaling.
|
||||
|
||||
---
|
||||
|
||||
## See Also
|
||||
|
||||
- `Sources/Lyrebird/Generators/ChannelSelect.swift` — `ChannelSelect.Audio/.Control`, `[n]` subscript
|
||||
- `Sources/Lyrebird/GeneratorCore/GeneratorArray.swift` — `GeneratorArray` type, operators, `expand()`
|
||||
- `Sources/Lyrebird/Generators/Pan2.swift` — stereo panner
|
||||
- `Sources/Lyrebird/Generators/Out.swift` — multi-channel write logic
|
||||
- `Sources/Lyrebird/Generators/In.swift` — `In.multi(bus:channels:)`
|
||||
- `docs/engine/bus-system.md` — bus layout and reserved ranges
|
||||
- `docs/protocols/writing-generators.md` — Pattern 4 (multi-output generators), Pattern 5 (MCE), Pattern 6 (channel selection)
|
||||
Reference in New Issue
Block a user