Question

I'm trying to use CoreAudio's MIDIPacketListAdd by doing this (on mac):

MIDIPacketListAdd(packetList, PACKETLIST_SIZE, currentPacket, mach_absolute_time, len, data);

I was thinking of calling the method multiple times before sending it out, so that my packetList will have > 1 packet in it. However when I do so the numPackets never increases, and what I am getting is packetList->packet[0] with the an ever increasing packet[0].data.

1) What's the correct way to use this method? I've looked at several examples, including the one from "Learning Core Audio" - they all seem to be sending just one packet.

2) Is there any performance improvement if I pack multiple packets in 1 packet list over sending 1 packet list with 1 packet multiple times?

Thanks, appreciate any help regarding this.

Edit for follow-up : Here's how I init the packet list

#define PACKETLIST_SIZE 512

- (void) initPacketList {

    if (packetList) {
        free(packetList);
        packetList = NULL;
    }

    packetList = (MIDIPacketList *)malloc(PACKETLIST_SIZE * sizeof(char));
    currentPacket = MIDIPacketListInit(packetList);
}

- (void) clearPacketList {

    packetList->numPackets = 0;
        currentPacket = MIDIPacketListInit(packetList);
}

- (void) addPacketToPacketList:(Byte*) data ofLength:(int) len {

    NSLog(@"length %d", len); 
    //NSLog(@"%lld", mach_absolute_time());
    currentPacket = MIDIPacketListAdd(packetList, PACKETLIST_SIZE, currentPacket, mach_absolute_time, len, data);

    //if (!currentPacket) exit(1);

}
Was it helpful?

Solution

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 see packetList->numPackets and currentPacket stay the same, but currentPacket->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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top