43 lines
2.0 KiB
Swift
43 lines
2.0 KiB
Swift
// GrainState.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/// One slot in any granular generator's grain pool.
|
|
///
|
|
/// All fields are value types. The pool array is pre-allocated at generator init time;
|
|
/// `_computeSamples` modifies slots in-place — no allocation on the RT thread.
|
|
///
|
|
/// Fields used per generator variant:
|
|
/// - `BufferGrain`/`InputGrain`: `phase`, `phaseInc`, common window/pan fields.
|
|
/// - `SineGrain`: `synthPhase`, `synthPhaseInc`, common window/pan fields.
|
|
/// - `FMGrain`: `synthPhase`, `synthPhaseInc`, `modPhase`, `modPhaseInc`,
|
|
/// `modIndex`, common window/pan fields.
|
|
/// Unused fields default to 0 and are never read by the variant that doesn't need them.
|
|
struct GrainState {
|
|
var isActive: Bool = false
|
|
|
|
// Buffer-reading variants (BufferGrain, InputGrain)
|
|
var phase: Double = 0 // fractional buffer/circular-buffer frame
|
|
var phaseInc: Double = 1 // playback rate (frames per sample)
|
|
|
|
// Common
|
|
var remaining: Int = 0 // samples remaining in this grain
|
|
var amp: Double = 1.0 // captured amplitude at trigger time
|
|
// Equal-power L/R gains pre-computed from the pan position at trigger time.
|
|
// Storing gains avoids per-sample trig; both channels read the same values for
|
|
// every sample of this grain's lifetime.
|
|
var panGainL: Double = 0.0
|
|
var panGainR: Double = 0.0
|
|
var windowPhase: Double = 0 // position in window table (index units)
|
|
var windowInc: Double = 0 // window table increment per sample
|
|
|
|
// Synthesis variants (SineGrain, FMGrain)
|
|
var synthPhase: Double = 0 // carrier oscillator phase (table-index units)
|
|
var synthPhaseInc: Double = 0 // carrier phase increment per sample
|
|
var modPhase: Double = 0 // FM modulator phase (table-index units)
|
|
var modPhaseInc: Double = 0 // FM modulator phase increment per sample
|
|
var modIndex: Double = 0 // FM modulation index (β)
|
|
}
|