The answer is in the documentation for MIDIPacketListAdd
.
Return Value
Returns null if there was not room in the packet for the event; otherwise returns a packet pointer which should be passed as curPacket in a subsequent call to this function.
So you probably want something like:
currentPacket = MIDIPacketListAdd(packetList, PACKETLIST_SIZE, currentPacket, mach_absolute_time, len, data);
Be sure to check if currentPacket
is NULL after each time.
Performance should be better if you put multiple packets in one packet list, since you're making fewer cross-process requests to the MIDI server, but honestly it's unlikely to be noticeable on a modern Mac.
Edit: here's what you're doing wrong.
packetList = (MIDIPacketList *)malloc(PACKETLIST_SIZE * sizeof(char));
sizeof(char)
is always 1 in the C language, by definition. malloc(PACKETLIST_SIZE)
is all you need.
currentPacket = MIDIPacketListAdd(packetList, PACKETLIST_SIZE, currentPacket, mach_absolute_time, len, data);
This gave me a compiler warning, because you are passing the address of the mach_absolute_time
function, not calling the function. You meant:
currentPacket = MIDIPacketListAdd(packetList, PACKETLIST_SIZE, currentPacket, mach_absolute_time(), len, data);
Once I did that, I saw packetList->numPackets
increase as expected.
However, note two things:
If you want to send the MIDI data immediately, just provide a timestamp of
0
.If you call
MIDIPacketListAdd
with the same timestamp as the previous call, then it just concatenates your data to the previous packet. You will seepacketList->numPackets
andcurrentPacket
stay the same, butcurrentPacket->length
will increase.
That's why you didn't see packetList->numPackets
increase: the address of mach_absolute_time
is a constant, so your "timestamp" was the same every time.