Core MIDI: when I send a MIDIPacketList using MIDISend() only the first packet is being sent

StackOverflow https://stackoverflow.com/questions/15283406

Pergunta

I am trying to send a MIDIPacketList containing two packets that describe controller position change message relating to a x-y style controller.

The function i'm trying to implement takes the an x and y position, and then creates the packets and sends them to the selected target device as follows:

- (void)matrixCtrlSetPosX:(int)posX PosY:()posY {

    MIDIPacketList packetList;
    packetList.numPackets = 2;

    packetList.packet[0].length = 3;
    packetList.packet[0].data[0] = 0xB0;        // status: controller change
    packetList.packet[0].data[1] = 0x32;        // controller number 50
    packetList.packet[0].data[2] = (Byte)posX;  // value (x position)
    packetList.packet[0].timeStamp = 0;

    packetList.packet[1].length = 3;
    packetList.packet[1].data[0] = 0xB0;        // status: controller change
    packetList.packet[1].data[1] = 0x33;        // controller number 51
    packetList.packet[1].data[2] = (Byte)posY;  // value (y position)
    packetList.packet[1].timeStamp = 0;

    CheckError(MIDISend(_outputPort, _destinationEndpoint, &packetList), "Couldn't send MIDI packet list");
}

The problem I am having is that the program only appears to be sending out the first packet.

I have tried splitting the output into two separate MIDIPacketLists and two making two calls to MIDISend(), which does work, but I am sure that there must be something trivial I am missing out in building the midi packet list so that the two messages can be sent in one call to MIDISend(). I just cannot seem to figure out what the problem is here! Anyone here had experience doing this, or am I going about this the wrong way entirely?

Foi útil?

Solução

Just declaring the MIDIPacketList doesn't allocate memory or set up the structure. There's a process to adding packets to the list. Here's a quick and dirty example:

- (void)matrixCtrlSetPosX:(int)posX PosY:(int)posY {
    MIDITimeStamp timestamp = 0;
    const ByteCount MESSAGELENGTH = 6;
    Byte buffer[1024];             // storage space for MIDI Packets
    MIDIPacketList *packetlist = (MIDIPacketList*)buffer;
    MIDIPacket *currentpacket = MIDIPacketListInit(packetlist);
    Byte msgs[MESSAGELENGTH] = {0xB0, 0x32, (Byte)posX, 0xB0, 0x33, (Byte)posY};
    currentpacket = MIDIPacketListAdd(packetlist, sizeof(buffer),
                                      currentpacket, timestamp, MESSAGELENGTH, msgs);

    CheckError(MIDISend(_outputPort, _destinationEndpoint, packetlist), "Couldn't send MIDI packet list");
}

I adapted this code from testout.c found here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top