-------------------------------------------------------------------------------- --! @title FX Rack --! @brief A five-slot effect rack: add, swap, bypass, remove, reorder and edit inserts --! @category UIHelpers --! @asset FXRack.uvip Patch with the script and a Drive insert --! --! A rack of five slots showing the Program's insert chain, with an editor --! below it for the selected effect. The first free slot offers a menu to add --! an effect. Each effect has a power button, a remove button and a menu to --! change its type. The slot number is the handle: click it to select the --! effect, the editor then shows the parameters listed for its type in the FX --! table; drag it onto another effect to move the effect there. --! --! insertEffect, removeEffect and moveEffect change the structure of the --! program, so they run asynchronously: the call returns at once with an --! AsyncTask and the chain changes a little later. The rack is redrawn from --! Program.inserts in the completion callback, and ignores clicks while an --! edit is in flight so that slot numbers never refer to a stale chain. --! Changing the type replaces the effect with a new one at default settings: --! removeEffect followed by insertEffect on the same slot. --! --! moveEffect keeps the effect itself, with its settings and its audio state --! (a reverb tail carries on). The edits are part of the program: they are --! saved with it and are not undoable. --! --! Dragging uses a text payload: the number button carries "fx:" in its --! dragAndDropText, and a transparent DnDArea laid over every slot accepts the --! texts starting with "fx:". The area only catches the mouse during such a --! drag, so the slot's own controls stay usable. --! --! Widgets are created once, when the script loads, so the editor holds five --! ParamKnobs and retargets them with bind() whenever the selection changes. --! Each knob then takes its range, unit and response curve from the parameter --! it is bound to. --! --! @demonstrates Element::insertEffect, Element::removeEffect, Element::moveEffect, ParamKnob::bind, Button::dragAndDropText, DnDArea::textDropped, Element::parameterDefinitions, AsyncTask, Panel, Menu, ParamKnob, OnOffButton, Button, Label -------------------------------------------------------------------------------- local NUM_SLOTS = 5 local NUM_PARAMS = 5 -- Effects the rack can create: engine type name, name shown in menus, and the -- parameters the editor shows (at most NUM_PARAMS, continuous ones) local FX = { {type = "AnalogChorus", label = "Analog Chorus", params = {"Speed", "Width", "Intensity", "TransientEnhancement"}}, {type = "Phasor", label = "Phasor", params = {"Speed", "Depth", "Feedback", "MinFreq", "MaxFreq"}}, {type = "AnalogFlanger", label = "Analog Flanger", params = {"Speed", "Width", "Intensity", "TransientEnhancement"}}, {type = "Drive", label = "Drive", params = {"DriveAmount"}}, {type = "TapeEcho", label = "Tape Echo", params = {"Delay1", "Feedback", "Bass", "Treble", "Mix"}}, {type = "SparkVerb", label = "SparkVerb", params = {"RoomSize", "DecayTime", "PreDelay", "Width", "Mix"}}, } local FX_INDEX = {} -- type name -> position in FX, i.e. in the type menus local FX_LABELS = {} for i, fx in ipairs(FX) do FX_INDEX[fx.type] = i FX_LABELS[i] = fx.label end local MARGIN, GAP = 16, 8 local SLOT_W, SLOT_H = 108, 132 local WIDTH = 2 * MARGIN + NUM_SLOTS * SLOT_W + (NUM_SLOTS - 1) * GAP local EDITOR_Y = MARGIN + SLOT_H + GAP local EDITOR_H = 108 local COLOURS = { background = "161616", slot = "262626", slotEmpty = "1c1c1c", selected = "2f2f2f", text = "e6e6e6", dim = "6b6b6b", accent = "e0a030", } setBackgroundColour(COLOURS.background) local busy = false -- an edit is in flight: the chain is about to change local selected = 1 -- slot shown in the editor local slots = {} local editor = {} local hint -- Sets several widget properties at once. local function style(widget, props) for k, v in pairs(props) do widget[k] = v end return widget end local function labelOf(fx) local i = FX_INDEX[fx.type] return i and FX[i].label or fx.type end -------------------------------------------------------------------------------- -- Editor: the parameters listed in FX for the selected effect's type -------------------------------------------------------------------------------- -- Display names of an effect's parameters, keyed by parameter name local function displayNames(fx) local names = {} for _, def in ipairs(fx.parameterDefinitions) do names[def.name] = def.displayName ~= "" and def.displayName or def.name end return names end local function refreshEditor() local fx = Program.inserts[selected] local entry = fx and FX[FX_INDEX[fx.type]] local params = entry and entry.params or {} local names = fx and displayNames(fx) or {} editor.title.text = fx and (selected .. " " .. labelOf(fx)) or "No effect selected" for i, knob in ipairs(editor.knobs) do local param = params[i] knob.visible = param ~= nil if param then knob.displayName = names[param] or param knob:bind(fx, param) -- no-op when already bound to that parameter end end end -------------------------------------------------------------------------------- -- Rack drawing -------------------------------------------------------------------------------- local function refresh() local count = #Program.inserts selected = math.max(1, math.min(selected, count)) for i, s in ipairs(slots) do local fx = i <= count and Program.inserts[i] or nil local isFirstFree = (i == count + 1) if fx and i == selected then s.panel.backgroundColour = COLOURS.selected else s.panel.backgroundColour = fx and COLOURS.slot or COLOURS.slotEmpty end s.index.textColourOff = (fx and i == selected) and COLOURS.accent or COLOURS.dim -- Effects the rack can create get a type menu; any other type is just named local typeIndex = fx and FX_INDEX[fx.type] s.type.visible = typeIndex ~= nil if typeIndex then s.type:setValue(typeIndex, false) end s.name.visible = typeIndex == nil s.name.text = fx and fx.type or (isFirstFree and "" or "empty") s.name.textColour = fx and COLOURS.text or COLOURS.dim s.power.visible = fx ~= nil s.remove.visible = fx ~= nil s.add.visible = isFirstFree -- Only effects can be dragged, and only onto another effect s.index.dragAndDropText = fx and ("fx:" .. i) or "" s.drop.acceptedText = fx and "fx:" or "" if fx then s.power:setValue(not fx:getParameter("Bypass"), false) end end if count > NUM_SLOTS then hint.text = "Showing the first " .. NUM_SLOTS .. " of " .. count .. " inserts" else hint.text = count .. " / " .. NUM_SLOTS .. " slots used - click a slot number to edit it, drag it to move the effect" end refreshEditor() end -- Completion callback of every chain edit: the chain has changed, redraw it local function onEdited(task) busy = false if not task.success then print("FX Rack: the edit could not be applied") end refresh() end -------------------------------------------------------------------------------- -- Slot widgets -------------------------------------------------------------------------------- local addItems = {"+ Add FX"} for _, label in ipairs(FX_LABELS) do addItems[#addItems + 1] = label end local function makeSlot(i) local prefix = "Slot" .. i local panel = Panel(prefix) panel.bounds = {MARGIN + (i - 1) * (SLOT_W + GAP), MARGIN, SLOT_W, SLOT_H} local s = {panel = panel} -- The slot number is the handle: click to select the effect, drag to move it s.index = style(panel:Button(prefix .. "Select"), {bounds = {4, 4, 22, 20}, displayName = tostring(i), tooltip = "Click to edit, drag onto another effect to move", fontSize = 11, backgroundColourOff = "00000000", backgroundColourOn = "00000000"}) s.power = style(panel:OnOffButton(prefix .. "Power", true), {bounds = {SLOT_W - 58, 6, 26, 18}, displayName = "on", tooltip = "Bypass", fontSize = 10, persistent = false, backgroundColourOn = COLOURS.accent, textColourOn = COLOURS.background}) s.remove = style(panel:Button(prefix .. "Remove"), {bounds = {SLOT_W - 28, 6, 20, 18}, displayName = "x", tooltip = "Remove", fontSize = 11}) s.name = style(panel:Label(prefix .. "Name"), {bounds = {6, 42, SLOT_W - 12, 26}, fontSize = 15, align = "centred"}) s.type = style(panel:Menu(prefix .. "Type", FX_LABELS), {bounds = {8, 44, SLOT_W - 16, 24}, showLabel = false, tooltip = "Change the effect type", persistent = false}) s.add = style(panel:Menu(prefix .. "Add", addItems), {bounds = {8, 44, SLOT_W - 16, 24}, showLabel = false, tooltip = "Add an effect", persistent = false}) -- Created last so that it lies on top of the slot; transparent until a drag hovers it s.drop = style(panel:DnDArea(prefix .. "Drop"), {bounds = {0, 0, SLOT_W, SLOT_H}, backgroundColour = "00000000", droppingColour = "40e0a030"}) s.drop.textDropped = function(self) local from = tonumber(self.droppedText:sub(4)) -- "fx:" if busy or not from or from == i then return end busy = true selected = i -- follow the moved effect Program:moveEffect(from, i, onEdited) end s.index.changed = function() if busy or i > #Program.inserts then return end selected = i refresh() end s.power.changed = function(self) local fx = Program.inserts[i] if fx then fx:setParameter("Bypass", not self.value) end end s.remove.changed = function() if busy then return end busy = true Program:removeEffect(i, onEdited) end s.add.changed = function(self) if self.value == 1 then return end local fxType = FX[self.value - 1].type self:setValue(1, false) -- back to "+ Add FX" if busy then return end busy = true selected = i Program:insertEffect(fxType, i, onEdited) end s.type.changed = function(self) local fx = Program.inserts[i] local newType = FX[self.value].type if busy or not fx or fx.type == newType then return end busy = true selected = i -- Replace = remove, then insert the new type at the same slot once removed Program:removeEffect(i, function(task) if task.success then Program:insertEffect(newType, i, onEdited) else onEdited(task) end end) end return s end for i = 1, NUM_SLOTS do slots[i] = makeSlot(i) end -------------------------------------------------------------------------------- -- Editor widgets -------------------------------------------------------------------------------- local editorPanel = Panel("Editor") editorPanel.bounds = {MARGIN, EDITOR_Y, WIDTH - 2 * MARGIN, EDITOR_H} editorPanel.backgroundColour = COLOURS.slot editor.title = style(editorPanel:Label("EditorTitle"), {bounds = {12, 8, 300, 18}, fontSize = 13, textColour = COLOURS.text, align = "left"}) editor.knobs = {} -- Created unbound (name only); refreshEditor() binds each knob to the selected effect. -- persistent = false: the effect parameters are saved with the program already. local KNOB_W = (WIDTH - 2 * MARGIN - 24) / NUM_PARAMS for i = 1, NUM_PARAMS do local knob = editorPanel:ParamKnob("Param" .. i) -- A knob draws its name and value to the right of the dial: keep it short style(knob, {bounds = {12 + (i - 1) * KNOB_W, 44, KNOB_W - 4, 48}, persistent = false}) editor.knobs[i] = knob end hint = style(Label("Hint"), {bounds = {MARGIN, EDITOR_Y + EDITOR_H + 8, WIDTH - 2 * MARGIN, 18}, fontSize = 11, textColour = COLOURS.dim, align = "left"}) refresh() setSize(WIDTH, EDITOR_Y + EDITOR_H + 34) makePerformanceView()