Timed output in Ruby

For general discussion related FlowStone
Post Reply
Rocko
Posts: 186
Joined: Tue May 15, 2012 12:42 pm

Timed output in Ruby

Post by Rocko »

Hi all,

I'm building my own kind of 'preset handler'.
It is supposed to read a text file and then send it out as SYSEX to a vintage synth.
The issue is that SYSEX commands should be timed out with a decent delay between one another.
Assume 'one commans a second' rate (it is much faster in reality of course).

So, I'm trying to read a text file, for various reasons the number of lines is unknown, assume 10-25.
The file has headers and errors, so I'm filtering out only the commands that start with 'cmd', as a basic filter.

The ruby module should output at one-line-a-second speed.
I've tried 'sleep' which doesn't work in Flowstone... Any other ideas, similar to 'sleep' ?

I've tried a 'busy loop' (just count to 1000) but it is system dependant. Not a fan of this solution.

This is what I have:

('s' is the string input to the ruby code)

Code: Select all


#
def event i,v,t

   len = @s.split(/\r/).length
   @i = 0

   while @i < (len-2) do

      #Take three first chars of the line
      a =  @s.split(/\r/)[@i].split[0][0..2]
            
      if ( a == 'cmd')
         output 0, @s.split(/\r/)[@i]
      
      #Busy loop (Sleep is not functional in FlowStone)   
      #for j in 0..100
      #   output 1, j
      #end   
                     
      end
       
       @i = @i + 1     
    
   end
end



Any ideas ?

Code is attached as well.
Attachments
Test.fsm
(597 Bytes) Downloaded 954 times
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Timed output in Ruby

Post by tulamide »

Try this.
Attachments
Test[tula].fsm
(635 Bytes) Downloaded 944 times
"There lies the dog buried" (German saying translated literally)
Rocko
Posts: 186
Joined: Tue May 15, 2012 12:42 pm

Re: Timed output in Ruby

Post by Rocko »

Hi,

Many thanks tulamide. Very interesting.

I tried to understand the code myself, but I lack understanding for these commands:

Code: Select all

input 99,nil, time+1 # 0.5 == in seconds from current time


isn't 'i' the reference to the input nuymber (which is single, only one input in this scehmatic) ?
I guess the single input (@ins[0]) receives the 'i=99' as well, even though there are no '99 inputs' ??

And:

Code: Select all

return 1


What does this command do?

Thanks,
Rocko
Rocko
Posts: 186
Joined: Tue May 15, 2012 12:42 pm

Re: Timed output in Ruby

Post by Rocko »

Hi,

I had also changed the code so that it does not 'wait' on non "cmd" lines, as so:

Code: Select all

def init
 @i = 0
 @len = 0
end

def event i,v,t
   if i == 0
      next_line
      @len = @s.split(/\r\n/).length ###
   elsif i == 99
      next_line
   end
end

def next_line
   
   if @i > (@len - 2)
      @i = 0
      return -1
   end
   
   #Take three first chars of the line
   a =  @s.split(/\r\n/)[@i].split[0][0..2]
            
   if ( a == 'cmd')
      output 0, @s.split(/\r\n/)[@i]
      input 99,nil, time+0.5 #Delay time
      else
      input 99,nil, time
   end
   
   @i += 1
      
   return 1
end
Attachments
TimedLoop_Tula_Version.fsm
(651 Bytes) Downloaded 954 times
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Timed output in Ruby

Post by tulamide »

Rocko wrote:isn't 'i' the reference to the input nuymber (which is single, only one input in this scehmatic) ?
I guess the single input (@ins[0]) receives the 'i=99' as well, even though there are no '99 inputs' ??

Correct. It is just a Ruby array, and Ruby arrays are of undefined length. You can add an entry anywhere you like. I chose 99 just to prevent any interference with possible future connections to the RubyEdit. It could as well have been 1 or 204.

Rocko wrote:And:

Code: Select all

return 1


What does this command do?

There is an equivalent, too. "return -1". They are used to inform, if the last line was reached (in which case the method returns -1), or not (returns 1). I didn't make use of the return values, which means, if you want to use it exactly as I showed in the example, you can safely remove both code lines.

Rocko wrote:Hi,

I had also changed the code so that it does not 'wait' on non "cmd" lines, as so:

Code: Select all

def init
 @i = 0
 @len = 0
end

def event i,v,t
   if i == 0
      next_line
      @len = @s.split(/\r\n/).length ###
   elsif i == 99
      next_line
   end
end

def next_line
   
   if @i > (@len - 2)
      @i = 0
      return -1
   end
   
   #Take three first chars of the line
   a =  @s.split(/\r\n/)[@i].split[0][0..2]
            
   if ( a == 'cmd')
      output 0, @s.split(/\r\n/)[@i]
      input 99,nil, time+0.5 #Delay time
      else
      input 99,nil, time
   end
   
   @i += 1
      
   return 1
end

The method looks good, but you have to calculate @len before you call next_line for the first time, or else all calculations are based on the last reported length. This works in this example, since it is always the same text. But if you use a text with more lines it will end too soon. If you use one with less lines it will end too late (resulting in an error).

For more information on event scheduling (and method scheduling), read chapter 8 of the user guide. It explains it in detail.
"There lies the dog buried" (German saying translated literally)
User avatar
Spogg
Posts: 3368
Joined: Thu Nov 20, 2014 4:24 pm
Location: Birmingham, England
Contact:

Re: Timed output in Ruby

Post by Spogg »

The more I read this kind of topic the more hopeless I feel :lol:

I'm SO glad we have tulamide to field the Ruby stuff :ugeek:

Just saying!

Spogg
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: Timed output in Ruby

Post by RJHollins »

Thanks 'T' !!

Welcome back Spogg 8-)
Rocko
Posts: 186
Joined: Tue May 15, 2012 12:42 pm

Re: Timed output in Ruby

Post by Rocko »

Hi,

Indeed so.
Thanks Tulamide for the example and detailed explanation.

I will go over chapter 8 (again).

Rocko
Post Reply