How do I iterate over a MIDIPacketList with RubyMotion?

420 Views Asked by At

How would I translate this PGMidi example code to RubyMotion? The documentation the MIDIPacketList mentions special caveats and I'm not sure how to account for them in RubyMotion. Example:

Note that the packets in the list, while defined as an array, may not be accessed as an array, since they are variable-length.

Here's the example, and further down, as far as I've gotten in the translation:

- (void) midiSource:(PGMidiSource*)midi midiReceived:(const MIDIPacketList *)packetList
{
    [self performSelectorOnMainThread:@selector(addString:)
                           withObject:@"MIDI received:"
                        waitUntilDone:NO];

    const MIDIPacket *packet = &packetList->packet[0];
    for (int i = 0; i < packetList->numPackets; ++i)
    {
        [self performSelectorOnMainThread:@selector(addString:)
                               withObject:StringFromPacket(packet)
                            waitUntilDone:NO];
        packet = MIDIPacketNext(packet);
    }
}

I know I'm losing the pointer referencing/dereferencing with the 'packet' local var and 'packetList', but I'm having a hard time figuring out how to correct it:

def midiSource(midi, midiReceived:packetList)
  self.performSelectorOnMainThread('addString',
                   withObject:'MIDI received:',
                   waitUntilDone:false)

  packet = packetList.packet[0]
  i = 0
  while i < packetList.numPackets
    i += 1

    self.performSelectorOnMainThread('addString',
                                     withObject:stringFromPacket(packet),
                                     waitUntilDone:false)
    packet = MIDIPacketNext(packet)
  end
end

Thanks for any guidance!

1

There are 1 best solutions below

1
On

If it's handling pointers you're looking for, RubyMotion provides the Pointer class. http://www.rubymotion.com/developer-center/guides/runtime/#_pointers