Help to randomize Layers

Hello,
the following script works for randomizing zones (and other parameters)
how can I adapt it in order to randomize the Layers instead of the zone?

-- Randomise Parameters Sample
local layer = this.parent
local zones = layer:findZones()
midiModule2 = this.parent:getMidiModule("Zone Select Sample")
reverb = this.parent:getBus():getEffect("Reverb")
VSTAmp = this.parent:getBus():getEffect("VST Amp")
StepFlanger = this.parent:getBus():getEffect("Step Flanger")
BitCrusher = this.parent:getBus():getEffect("H3 Bit Reduction")
Delay = this.parent:getBus():getEffect("Multi Delay")
rndTable = {

    -- table 1 elements, table 2 parameters
    {zones, {"Filter.Cutoff", "Filter.Resonance" , "Filter.ShapeA" , "LFO 1.WaveForm" , "LFO 1.RateSync" , "LFO 1.Shape" , "LFO 2.WaveForm" , "LFO 2.RateSync" , "LFO 2.Shape" , "SampleOsc.StretchFactor" , "SampleOsc.FormantShift" , "SampleOsc.FormantShiftKeytrack" , "SampleOsc.PSOLATransientSize"}},
    {{layer}, {"Pan" , "Octave"}},
    {{midiModule2}, {"activeZone"}},
    {{reverb}, {"Mix" , "TailMainTime" , "ERSelect"}},
    {{VSTAmp}, {"drive" , "bass" , "mid" , "treble" , "presence"}},
    {{StepFlanger}, {"Rate" , "Depth" , "Phase" , "FB"}},
    {{BitCrusher}, {"param2" , "param3" , "param4" , "param5", "Mix"}},
    {{Delay}, {"syncnote1" , "feedbackoverall" , "delayLR" , "delaymode", "Mix"}},
}

function randomiseParameters()
    if Rnd then
        for i = 1, #rndTable do
            local elements = rndTable[i][1]
            local parameters = rndTable[i][2]
            for j, parameter in ipairs(parameters) do
                local rnd = math.random()
                for k, element in ipairs(elements) do
                    element:setParameterNormalized(parameter, rnd)
                end
            end
        end        
        wait(50)
        Rnd = false
    end
end

defineParameter{name = "Rnd", default = false, onChanged = randomiseParameters, persistent = false}

My instrument has 4 Layers
So basically what I’m trying to do is to have a button that when clicked randomizes the sub-layers (A1 A2 B1 B2 etc etc) inside Layer 1, Layer 2, etc etc.

Thanks in advance for any help!

Which module has the randomise script? If it’s the Layer Selector you could do

layers = this.parent:findLayers()

You would probably need

zones = this.parent:findZones(true)

As none of the zones are directly inside the parent layer of the script module.

No, the Layer selector module is a simple script that I have connected to a menu for each Main Layer (Layer 1, Layer 2, Layer 3, and Layer 4) that let me change the sound for each layer (A1, A2, etc for Layer 1, B1, B2, etc for Layer 2 and so on).

layers = this.parent:findLayers()
scopes = {}

function getLayerNames()
	layerNames = {}
	for i, layer in ipairs(layers) do
		layerNames[i] = layer.name
		scopes[i] = "@0:" .. this.parent.name .. "/@0:" .. layer.name .. "/@type:Zone/"
	end
end

getLayerNames()

defineParameter("Scope", nil, 1, scopes)
defineParameter("LayerSelect", nil, 1, layerNames, function() Scope = LayerSelect end)


function onNote(event)
	playNote(event.note, event.velocity, -1, layers[LayerSelect])
end

I think I need to put the randomize script above all Layers because I need to connect it to a single button that when clicked randomizes all sub-Layers (A1 A2… B1 B2… C1 C2… D1 D2… etc etc)


Thanks for any help!

You could try this to filter out the sub layers:

layers = this.parent:findLayers(true, function (layer) return layer.level - this.level == 1 end)

If you also need other elements like zones, fx, midi modules, you may need to adapt your original script. As it is it will find no zones, midi modules and will look for fx on your program bus only.

You can try this to see what elements you get:

zones = this.parent:findZones(true)

-- sub layers
layers = this.parent:findLayers(true, function (layer) return layer.level - this.level == 1 end)

layerSelectors = this.parent:findMidiModules(true, "Layer Selector")

-- all Multi Delay FX
delays = this.parent:findEffects(true, "Multi Delay")

-- Multi Delay FX on layer busses
layerDelays = this.parent:findEffects(true, function (fx)
		return fx.name == "Multi Delay" and fx.level - this.level == 2
		end)

function printElements(elementsTable)
	for i, element in ipairs(elementsTable) do
		print(element.name, " Parent: ", element.parent.name)
	end
end

printElements(zones)
printElements(layers)
printElements(layerSelectors)
printElements(layerDelays)

So far, I have tried this, but with no significant success…
Thanks in advance for any help!

test_rndm.zip (1.9 MB)

-- sub layers
layers = this.parent:findLayers(true, function (layer) return layer.level - this.level == 1 end)
-- layer selector midi modules
layerSelectors = this.parent:findMidiModules(true, "Layer Selector")
-- all reverbs, delays, bit crushers on any bus
reverbs = this.parent:findEffects(true, "Reverb")
delays = this.parent:findEffects(true, "Multi Delay")
bitcrushers = this.parent:findEffects(true, "H3 Bit Reduction")


rndTable = {

-- table 1 elements, table 2 parameters
    
    {layers, {"Pan" , "Octave"}},
    {layerSelectors, {"LayerSelect"}},
    {reverbs, {"Mix" , "TailMainTime" , "ERSelect"}},    
    {bitcrushers, {"param2" , "param3" , "param4" , "param5", "Mix"}},
    {delays, {"syncnote1" , "feedbackoverall" , "delayLR" , "delaymode", "Mix"}},
}


function printElements(elementsTable)
    for i, element in ipairs(elementsTable) do
        print(element.name, " Parent: ", element.parent.name)
    end
end

printElements(layers)
printElements(layerSelectors)
printElements(reverbs)
printElements(delays)
printElements(bitcrushers)

 

function randomiseParameters()
    if Rnd then
        for i = 1, #rndTable do
            local elements = rndTable[i][1]
            local parameters = rndTable[i][2]
            for j, parameter in ipairs(parameters) do
                local rnd = math.random()
                for k, element in ipairs(elements) do
                    element:setParameterNormalized(parameter, rnd)
                end
            end
        end        
        wait(50)
        Rnd = false
    end
end

defineParameter{name = "Rnd", default = false, onChanged = randomiseParameters, persistent = false}