Dynamic FX for live loop

Hi,
I wonder if there is any way to dynamically add fx for playing live_loop. I know that I can add with_fx inside a live_loop but it have 2 problems:

  • It creates a new fx on each cycle, so wasting of resources
  • If my live_loop is listening for midi events, it won’t work properly with some FX, like flanger, because it will create a new FX for each note.

So only way is to create a bunch of nested with_fx sections with mix: 0, and toggle them later with the control method. But I don’t like it, and I think it will consume resources even with mix: 0 (not sure about that).

Is there any better solution? Basically I want dynamically enable/disable and chain effects without stopping live loops.

If anyone is interested:
I did some research and found, that I can save live_loop in a variable, and later kill it and immediately recreate, even if it’s waiting for sync or MIDI event.

Saving is a bit tricky, because I can’t use set, so I have to make something like that:

if get[:init] == nil
  thread = nil
  
  define :save_thread do |t|
    thread = t
  end
  
  define :kill_thread do
    thread.kill if thread != nil
  end
  
  set :init, true
end

With these 2 methods I can save live loops like that:

x = live_loop :test do
...
end

save_thread x

But another issue is that after killing, I need to change thread name, because sonic pi is thinking that it still alive for some reason.

Not a perfect solution, but will work for me.