If you have a problem or need to report a bug please email : support@dsprobotics.com
There are 3 sections to this support area:
DOWNLOADS: access to product manuals, support files and drivers
HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects
USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here
NEW REGISTRATIONS - please contact us if you wish to register on the forum
Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright
Timed output in Ruby
8 posts
• Page 1 of 1
Timed output in Ruby
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)
Any ideas ?
Code is attached as well.
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 886 times
- Rocko
- Posts: 186
- Joined: Tue May 15, 2012 12:42 pm
Re: Timed output in Ruby
Try this.
- Attachments
-
- Test[tula].fsm
- (635 Bytes) Downloaded 885 times
"There lies the dog buried" (German saying translated literally)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Timed output in Ruby
Hi,
Many thanks tulamide. Very interesting.
I tried to understand the code myself, but I lack understanding for these commands:
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:
What does this command do?
Thanks,
Rocko
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
Hi,
I had also changed the code so that it does not 'wait' on non "cmd" lines, as so:
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 893 times
- Rocko
- Posts: 186
- Joined: Tue May 15, 2012 12:42 pm
Re: Timed output in Ruby
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.
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)
- tulamide
- Posts: 2714
- Joined: Sat Jun 21, 2014 2:48 pm
- Location: Germany
Re: Timed output in Ruby
The more I read this kind of topic the more hopeless I feel
I'm SO glad we have tulamide to field the Ruby stuff
Just saying!
Spogg
I'm SO glad we have tulamide to field the Ruby stuff
Just saying!
Spogg
-
Spogg - Posts: 3358
- Joined: Thu Nov 20, 2014 4:24 pm
- Location: Birmingham, England
Re: Timed output in Ruby
Thanks 'T' !!
Welcome back Spogg
Welcome back Spogg
- RJHollins
- Posts: 1571
- Joined: Thu Mar 08, 2012 7:58 pm
Re: Timed output in Ruby
Hi,
Indeed so.
Thanks Tulamide for the example and detailed explanation.
I will go over chapter 8 (again).
Rocko
Indeed so.
Thanks Tulamide for the example and detailed explanation.
I will go over chapter 8 (again).
Rocko
- Rocko
- Posts: 186
- Joined: Tue May 15, 2012 12:42 pm
8 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 81 guests