Page 1 of 1

2 midi events in one object name-Ruby

Posted: Tue Dec 24, 2013 2:48 pm
by nix
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

Re: 2 midi events in one object name-Ruby

Posted: Tue Dec 24, 2013 8:32 pm
by trogluddite
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!

Re: 2 midi events in one object name-Ruby

Posted: Sat Dec 28, 2013 5:01 am
by nix
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.