-------------------------------------------------------------------------------- --! @title XYFilter --! @brief XY Pad Driving a Filter's Cutoff and Resonance --! @category UIHelpers --! --! An XY pad for a filter insert: horizontal is cutoff, vertical is resonance. --! The XY widget does not hold values itself, it moves two script parameters --! named at construction - here the Cutoff and Resonance knobs - so both are --! saved with the preset, and their changed callbacks do the work of writing --! the filter. --! --! The script looks for the first insert of the Program, then of the first --! Layer, that has both a "Freq" and a "Q" parameter (Falcon's LowPass 24 and --! Biquad Filter, among others), and reports it when none is found rather than --! writing to nothing: setParameter ignores unknown names without an error. --! --! @demonstrates XY, Knob, Element::hasParameter, Element::setParameter, Label -------------------------------------------------------------------------------- local MIN_HZ, MAX_HZ = 20, 20000 -- First insert of the given list that exposes both parameters we drive. local function findFilter(inserts) for _, fx in ipairs(inserts or {}) do if fx:hasParameter("Freq") and fx:hasParameter("Q") then return fx end end end local filter = findFilter(Program.inserts) if not filter and #Program.layers > 0 then filter = findFilter(Program.layers[1].inserts) end -- The knobs are 0..1 so the pad moves evenly; the cutoff is mapped to Hz on an -- exponential curve, which is how the ear hears a sweep as even. local cutoff = Knob{"Cutoff", 0.5, 0, 1, unit = Unit.PercentNormalized} local resonance = Knob{"Resonance", 0.2, 0, 1, unit = Unit.PercentNormalized} cutoff.bounds = {230, 20, 90, 50} resonance.bounds = {230, 80, 90, 50} local pad = XY("Cutoff", "Resonance") -- names of the two knobs above pad.bounds = {10, 10, 200, 200} pad.backgroundColour = "2a2a2a" pad.handleColour = "ffa040" local status = Label("status") status.bounds = {230, 150, 240, 40} local function toHz(x) return MIN_HZ * (MAX_HZ / MIN_HZ) ^ x end local function fromHz(hz) return math.log(hz / MIN_HZ) / math.log(MAX_HZ / MIN_HZ) end if filter then status.text = "Controlling " .. filter.displayName -- Start from the filter's own settings so opening the script does not -- move the sound. A saved preset then overrides these, as it restores the -- knob values after the main chunk has run. local hz = math.max(MIN_HZ, math.min(MAX_HZ, filter:getParameter("Freq"))) cutoff:setValue(fromHz(hz), false) resonance:setValue(filter:getParameter("Q"), false) else status.text = "No filter with Freq and Q found: add a LowPass 24 insert" cutoff.enabled = false resonance.enabled = false pad.enabled = false end cutoff.changed = function(self) if filter then filter:setParameter("Freq", toHz(self.value)) end end resonance.changed = function(self) if filter then filter:setParameter("Q", self.value) end end setSize(480, 220) makePerformanceView()