Help required to create script to randomly move between FlexPhraser variations (via LFO or otherwise)

Would somebody be able to help me with creating a Lua script that can be assigned to an LFO in order to sweep between the 8 FlexPhraser variations. I’m not wedded to it being an LFO as the source however it must have a depth knob.

Essentially I want the default to play on variation 1 and then, by turning up the depth knob, it will randomly select variation 1 to 8.

I’m in the final stages of my project so would really like to get this one sorted.

You may find this example in documentation interesting:

It changes the variation with each note though.

Thanks misohoza.

I wonder if I can change onNote(event) to something like ‘LFO(event)’ to trigger it other than note on.

Regardless I would like it triggered randomly as opposed to consecutively,

How flexible can the formulas be in the scripting?

Instead of variation = variation + 1 if I were to do something like variation *2 +3 that would at least mix it up a bit.

However, in Excel speak there would also need to be something along the lines of IFERROR(var 1).

Actually I notice the “iferror” is already there in the form of if variation > 8 then variation = 1

You can get random numbers by using e.g. math.random(1, 8)
That’s the easy bit. But how do you want to trigger this variation change? With each note, after certain amount of time…?

1 Like

Fantastic, we’re making progress.

Well, good question. Ideally another random between say 1/2 and 1/4 (I can always edit that later if it’s not working).

However, if that isn’t possible I’m considering using the User section in the Flex Phraser with each variation having different note durations (calculated in accordance with the rate on that variation, ie 1/4 for a 1/4 variation).

You can try this. Not sure if it will work correctly for what you want. Paste this into lua script module. After this script module you need to add trigger pads midi module and then either flexphraser or midi player. The idea is the script will change the state of trigger pad module. This trigger pad module can be used to select variation of flexphraser or midi module. But you need to assign those variations to trigger pads first.
random variation

local freq = 1
local phase = 0
local beatValues = {0.03125, 0.0625, 0.125, 0.25, 0.5, 1, 2, 3, 4}
local syncRates = {"1/128", "1/64", "1/32", "1/16", "1/8", "1/4", "2/4", "3/4", "4/4"}
local anyNotePressed = 0
local retrigger = false

defineParameter("note", nil, 6, syncRates)
defineParameter("syncMode", nil, 3, {"Off", "Tempo + Retrig", "Tempo + Beat"})
defineParameter("frequency", nil, 1, 0.01, 30)
defineParameter("retriggerMode", nil, 1, {"Off", "First Note", "Each Note"})
defineParameter("enable", nil, true)

function calcModulation()
    if retrigger then
        phase = 0
        retrigger = false
        if isPlaying() and syncMode == 3 then
            phase = (getBeatTime() / beatValues[note]) % 1

    rate = getSamplingRate() / 32
    freq = frequency
    if syncMode > 1 then
        freq = getTempo() / 60 / beatValues[note]

    phase = phase + freq / rate
    if phase >= 1 then
        phase = phase - 1
        local pad = math.random(1, 8)
        if enable then
            print("Pad ", pad)
            spawn(playTriggerPad, pad)

function onNote(e)
    if syncMode == 3 and isPlaying() or ((syncMode == 1 or syncMode == 2) and ((retriggerMode == 2 and anyNotePressed == 0) or retriggerMode == 3)) then
        retrigger = true
    anyNotePressed = anyNotePressed + 1

function onRelease(e)
    anyNotePressed = math.max(anyNotePressed - 1, 0)
1 Like

:partying_face: :partying_face: :partying_face:

Hi misohoza, this works exactly as I envisaged. Thanks so much!

In fact it’s so helpful that in Microsoft fashion I skipped a version number and went straight from v18 to v20.

The only downside is it makes editing the FlexPhraser literally impossible now because it’s constantly jumping around :laughing: Is there a way to assign the script to a button/knob (depth) so I can turn it off? I did expect to see a disable option on the MIDI modules with a right click but unfortunately not. Otherwise no big deal I can just move the script below the Trigger Pad while I edit the FlexPhraser.

Thanks once again misohoza, I’m in the final stages now!

Check the parameter list. The script has “enable” parameter. You can connect it to a macro page button and turn it on or off as needed.

1 Like

I hadn’t yet discovered the Parameter List Window but now I have it has opened up a world of options.

Script now mapped to a button. Yay!