Page 1 of 1
Ruby - Asynch cycling with waiting
Posted: Mon Nov 09, 2015 4:58 pm
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!
Re: Ruby - Asynch cycling with waiting
Posted: Mon Nov 09, 2015 5:26 pm
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.
Re: Ruby - Asynch cycling with waiting
Posted: Mon Nov 09, 2015 5:32 pm
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...
Re: Ruby - Asynch cycling with waiting
Posted: Mon Nov 09, 2015 5:42 pm
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
Re: Ruby - Asynch cycling with waiting
Posted: Mon Nov 09, 2015 5:48 pm
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...
Re: Ruby - Asynch cycling with waiting
Posted: Mon Nov 09, 2015 6:04 pm
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.
Re: Ruby - Asynch cycling with waiting
Posted: Tue Nov 10, 2015 9:23 am
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).
Re: Ruby - Asynch cycling with waiting
Posted: Tue Nov 10, 2015 11:12 am
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?
Re: Ruby - Asynch cycling with waiting
Posted: Tue Nov 10, 2015 4:59 pm
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?