MIDI über Camera Connection Kit

  • MIDI über Camera Connection Kit

    Seit iOS 4.2 unterstützt Apple ja offiziell MIDI über das Camera Connection Kit. Z.B. kommt es auch in GarageBand zum Einsatz.
    Ich kann leider null Informationen zu den entsprechenden APIs finden. Ich bin wohl einfach nur zu blöd, weil es auch in anderen Apps eingesetzt wird.
    Vielleicht kennt ja hier jemand einen Link o.ä.
  • Ich glaube, das läuft schon in einem separaten Thread:

    Quellcode

    1. /// IMPORTANT NOTE:
    2. /// MIDI input is received from a high priority background thread
    3. // Raised on main run loop
    4. /// NOTE: Raised on high-priority background thread.
    5. ///
    6. /// To do anything UI-ish, you must forward the event to the main runloop
    7. /// (e.g. use performSelectorOnMainThread:withObject:waitUntilDone:)
    8. ///
    9. /// Be careful about autoreleasing objects here - there is no NSAutoReleasePool.
    10. ///
    11. /// Handle the data like this:
    12. ///
    13. /// // for some function HandlePacketData(Byte *data, UInt16 length)
    14. /// const MIDIPacket *packet = &packetList->packet[0];
    15. /// for (int i = 0; i < packetList->numPackets; ++i)
    16. /// {
    17. /// HandlePacketData(packet->data, packet->length);
    18. /// packet = MIDIPacketNext(packet);
    19. /// }
    Alles anzeigen


    Quellcode

    1. static void PGMIDIReadProc(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon);
    2. // NOTE: Called on a separate high-priority thread, not the main runloop
    3. - (void) midiRead:(const MIDIPacketList *)pktlist
    4. {
    5. [delegate midiSource:self midiReceived:pktlist];
    6. }
    7. static
    8. void PGMIDIReadProc(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon)
    9. {
    10. PGMidiSource *self = (PGMidiSource*)srcConnRefCon;
    11. [self midiRead:pktlist];
    12. }
    Alles anzeigen


    Quellcode

    1. NSString *StringFromPacket(const MIDIPacket *packet)
    2. {
    3. // Note - this is not an example of MIDI parsing. I'm just dumping
    4. // some bytes for diagnostics.
    5. // See comments in PGMidiSourceDelegate for an example of how to
    6. // interpret the MIDIPacket structure.
    7. return [NSString stringWithFormat:@" %u bytes: [%02x,%02x,%02x]",
    8. packet->length,
    9. (packet->length > 0) ? packet->data[0] : 0,
    10. (packet->length > 1) ? packet->data[1] : 0,
    11. (packet->length > 2) ? packet->data[2] : 0
    12. ];
    13. }
    14. - (void) midiSource:(PGMidiSource*)midi midiReceived:(const MIDIPacketList *)packetList
    15. {
    16. [self performSelectorOnMainThread:@selector(addString:)
    17. withObject:@"MIDI received:"
    18. waitUntilDone:NO];
    19. const MIDIPacket *packet = &packetList->packet[0];
    20. for (int i = 0; i < packetList->numPackets; ++i)
    21. {
    22. [self performSelectorOnMainThread:@selector(addString:)
    23. withObject:StringFromPacket(packet)
    24. waitUntilDone:NO];
    25. packet = MIDIPacketNext(packet);
    26. }
    27. }
    Alles anzeigen