Support

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

2 midi events in one object name-Ruby

For general discussion related FlowStone

2 midi events in one object name-Ruby

Postby nix » Tue Dec 24, 2013 2:48 pm

Arrgh!
There is something really simple I am missing here,
So I want to make 2 different MIDI events into one object.

I want @off to have these 2 events encompassed>

@off1[i] = Midi.new 128,1,127-i,127
@off2[i] = Midi.new 128,2,127-i,0

I want to send both of these at the same time.
So I'm hoping I can get the object @off to reference both,
either in the init or event

How to? plsthx 8D
User avatar
nix
 
Posts: 817
Joined: Tue Jul 13, 2010 10:51 am

Re: 2 midi events in one object name-Ruby

Postby trogluddite » Tue Dec 24, 2013 8:32 pm

Here's a nice "general purpose" way...

First define this method...
Code: Select all
def output_all(out_idx, *args)
  args.flatten.each{|item| output(out_idx, item)}
end


This new method 'output_all' allows a very flexible way to send objects - the first argument is the output index, just like the usual 'output' command - but what follows can be any of....
Code: Select all
output_all(0, object)  ->  output a single object (the same as 'output')
output_all(0, object1, object2, object3....)  ->  output all of the listed objects in turn.
output_all(0, array)  ->  Output all of the object in the array in turn.

When you use the 'list' methods, you can even include arrays in the list - they will be 'opened out' and each entry treated as an item in the list.
And, if an array is nested, it will be 'flattened out' into a single long list - and all items output.

Now you can simply collect together your 'double Midi's' into an array, and then send it all at once...

Code: Select all
@off[i] = [ Midi.new(128, 1, 127-i, 127), Midi.new(128, 2, 127-i, 0) ]
output_all(0, @off[i])

You'll notice that I always bracket my method arguments - Midi.new(a, b, c d) rather than Midi.new a, b, c d - it's a good habit to get into because otherwise statements like that one give the parser a headache - e.g...
Code: Select all
@off[i] = [ Midi.new 128, 1, 127-i, 127, Midi.new 128, 2, 127-i, 0) ]

How to decide which commas separate the Midi.new arguments, and which separate the array entries? Ruby will do it's best to resolve this kind of thing - but such code is very ambiguous and may not give the expected results!
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: 2 midi events in one object name-Ruby

Postby nix » Sat Dec 28, 2013 5:01 am

Thanks a whole bundle Trog,
NuBeat made the mod.
Hmm, it wasn't as simple as I thought.
Much thanks!
I hope this is handy for others sending MIDI,
there is very little documentation-
neat solution.
User avatar
nix
 
Posts: 817
Joined: Tue Jul 13, 2010 10:51 am


Return to General

Who is online

Users browsing this forum: No registered users and 104 guests