Why does this code work on iOS 32 bit but not on 64 bit?

  

The code below receives MIDI data from a musical instrument connected to the iOS device. It works fine on a 32 bit iOS device. On 64 bit, the callback function is also called for every event, but the data received in ‘pktlist’ is invalid. What is wrong?

The data I receive in MidiReadProc is always the same:

pktlist^.numPackets = 1
lPacket.MIDItimestamp=$E4FE000100000961
lPacket.length=$0000
lPacket.data[0]=$00

The Callback function:

procedure MidiReadProc(pktlist: MIDIPacketListRef; refCon, connRefCon: Pointer); cdecl;
var
lPacket: MIDIPacket;
lPacketRef: MIDIPacketRef;
j: Integer;
lPtr: ^Byte;
begin
lPacketRef := MIDIPacketRef(@(pktlist^.Packet[0]));
for j := 0 to pktlist^.numPackets-1 do
begin
lPacket := lPacketRef^;
if (lPacket.length > 0) and (lPacket.data[0] <> $F0) then
//handle data here

//translation of the MIDIPacketNext Macro:
lPtr := @lPacketRef^.data[lPacketRef^.length];
lPacketRef := MIDIPacketRef((UInt64(lPtr) + 3) and (not 3));
end;
end;

In case it could be related to the header translation, here is the translation:

Extract from CoreMIDI.h:

typedef UInt64 MIDITimeStamp;

typedef struct MIDIPacketList MIDIPacketList;

struct MIDIPacket
{
MIDITimeStamp timeStamp;
UInt16 length;
Byte data[256];
};
typedef struct MIDIPacket MIDIPacket;

struct MIDIPacketList
{
UInt32 numPackets;
MIDIPacket packet[1];
};

typedef void
(*MIDIReadProc)(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon);

#if TARGET_CPU_ARM || TARGET_CPU_ARM64
// MIDIPacket must be 4-byte aligned
#define MIDIPacketNext(pkt) ((MIDIPacket *)(((uintptr_t)(&(pkt)->data[(pkt)->length]) + 3) & ~3))

Extract from CoreMIDI.pas (the translation of CoreMIDI.h is made by Pavel Jiri Strnad and is available here):

MIDITimeStamp = UInt64;

MIDIPacket = record
timeStamp: MIDITimeStamp;
length: UInt16;
data: array [0..255] of Byte;
end;
MIDIPacketRef = ^MIDIPacket;

MIDIPacketList = record
numPackets: UInt32;
packet: array [0..0] of MIDIPacket;
end;
MIDIPacketListRef = ^MIDIPacketList;

MIDIReadProc = procedure (pktlist: MIDIPacketListRef; readProcRefCon: pointer; srcConnRefCon: pointer); cdecl;

I am using Delphi 10.1 Berlin with XCode 7.3 and the device has iOS 9.3.

Comments are closed.