Ruby - Asynch cycling with waiting

For general discussion related FlowStone
Post Reply
Nowhk
Posts: 275
Joined: Mon Oct 27, 2014 6:45 pm

Ruby - Asynch cycling with waiting

Post by Nowhk »

Hi all,

I have this code:

Code: Select all

# init variables
def init

end

# event handler
def event i,v,t
   if i==1 then startLFO(t)
   end
end

def startLFO(t)
   for i in 0..100
      sleep 1
      watch "test",i
      if i==90 then
         i=0
      end
   end   
end

I'd like to iterate from 0 to 99 every (let say) 1 second, so I can see a sort of progression, without that the interface freeze. So a sort of wait-process, threaded.

Than, once I got the end, restart from 0. (I'm making a sort of custom LFO, with values that will be stored in an array and freq which is the "sleeping" time).

Where am I wrong? My DAW crash with that code...

Thanks!
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby - Asynch cycling with waiting

Post by tulamide »

Nowhk wrote:I'd like to iterate from 0 to 99 every (let say) 1 second, so I can see a sort of progression, without that the interface freeze. So a sort of wait-process, threaded.

Than, once I got the end, restart from 0. (I'm making a sort of custom LFO, with values that will be stored in an array and freq which is the "sleeping" time).


Create two trigger buttons. Create two trigger inputs in your RubyEdit and name them "start" and "stop". Connect the buttons with the inputs.

Use this code:

Code: Select all

def init
   @iteration = 0
   @end = false
end

def event i, v
   if i == "start"
      @end = false
      cycle
   elsif (i == 99) and (@end == false)
      cycle
   elsif i == "stop"
      @end = true
   end
end

def cycle
   @iteration += 1
   if @iteration == 100
      @iteration = 0
   end
   watch @iteration
   input 99, nil, time + 1
end


This is a basic asynchronous loop.
"There lies the dog buried" (German saying translated literally)
Nowhk
Posts: 275
Joined: Mon Oct 27, 2014 6:45 pm

Re: Ruby - Asynch cycling with waiting

Post by Nowhk »

tulamide wrote:
Nowhk wrote:I'd like to iterate from 0 to 99 every (let say) 1 second, so I can see a sort of progression, without that the interface freeze. So a sort of wait-process, threaded.

Than, once I got the end, restart from 0. (I'm making a sort of custom LFO, with values that will be stored in an array and freq which is the "sleeping" time).


Create two trigger buttons. Create two trigger inputs in your RubyEdit and name them "start" and "stop". Connect the buttons with the inputs.

Use this code:

Code: Select all

def init
   @iteration = 0
   @end = false
end

def event i, v
   if i == "start"
      @end = false
      cycle
   elsif (i == 99) and (@end == false)
      cycle
   elsif i == "stop"
      @end = true
   end
end

def cycle
   @iteration += 1
   if @iteration == 100
      @iteration = 0
   end
   watch @iteration
   input 99, nil, time + 1
end


This is a basic asynchronous loop.

Thanks for the reply. Uhm, in fact is that I need to work with play/stop of DAW, not triggering buttons...
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby - Asynch cycling with waiting

Post by tulamide »

There's a prim called "Playing". The non-stream version sends out a boolean. Connect it with a boolean input of the RubyEdit and name that input "playing". Change the event code with this one:

Code: Select all

def event i, v
   if i == "playing"
      if v == true
         @end = false
         cycle
      elsif v == false
         @end = true
      end
   elsif (i == 99) and (@end == false)
      cycle
   end
end
"There lies the dog buried" (German saying translated literally)
Nowhk
Posts: 275
Joined: Mon Oct 27, 2014 6:45 pm

Re: Ruby - Asynch cycling with waiting

Post by Nowhk »

I see. But uhm, the iteration is made calling the "event" function itself for what I see. What if I'd like to make 4 different/parallel asynch/waiting process? I risk that when I recall an event I don't know which target function call.

I mean: 4 different iteration, after 1,2,3,4 seconds respectively (that will use 4 different arrays of values, for example).
In this wait I can't...
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby - Asynch cycling with waiting

Post by tulamide »

I know people don't like to hear it. But you should read the user manual about Ruby. I used just one input (the one with index 99). You can use as many as you like. And since they are indexed, you know which input belongs to which cycle. You can cycle with different methods or extend the existing method to manage several different cycles.
"There lies the dog buried" (German saying translated literally)
Nowhk
Posts: 275
Joined: Mon Oct 27, 2014 6:45 pm

Re: Ruby - Asynch cycling with waiting

Post by Nowhk »

Got it:

Code: Select all

def init
   @end = false
   
   @lfo_index_1 = 0
   @lfo_freq_1 = 0.008
   
   @lfo_index_2 = 0
   @lfo_freq_2 = 0.05
end

def event i, v
   if i == "start"
      @end = false
      lfo_1
      lfo_2
   elsif (i == 10) and (@end == false)
      lfo_1
   elsif (i == 20) and (@end == false)
      lfo_2     
   elsif i == "stop"
      @end = true
   end
end

def lfo_1
   @lfo_index_1 += 1
   if @lfo_index_1 == 100
      @lfo_index_1 = 0
   end
   watch "LFO 1: ", @lfo_index_1
   input 10, nil, time + @lfo_freq_1
end

def lfo_2
   @lfo_index_2 += 1
   if @lfo_index_2 == 100
      @lfo_index_2 = 0
   end
   watch "LFO 2: ", @lfo_index_2
   input 20, nil, time + @lfo_freq_2
end

Thanks! Not sure which "input" number to use, I guess every number is ok (I've used 10 and 20 in this case).
Nowhk
Posts: 275
Joined: Mon Oct 27, 2014 6:45 pm

Re: Ruby - Asynch cycling with waiting

Post by Nowhk »

Another little question: how can I stop a previous triggered function? If I click on the trigger button "start" two time, it invoke the function again, so I can see two events working together.

Is there a way to "retrigger" and/or stop the previous function thread?
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby - Asynch cycling with waiting

Post by tulamide »

That goes beyond "simple".
The first question was relatively easy to answer. But it begins to get way more complex now, and I can't spend the time for it. You should consider using classes. Also, differentiating the different "threads" (they aren't real threads) can be done by using IDs.

Maybe someone else can go into more detail?
"There lies the dog buried" (German saying translated literally)
Post Reply