Hi!
There's something in setting MIDIPacket's timestamp that I'm missing.
I'm sending 2 events: "note on" and "note off", setting interval between them to 3 seconds.
My expectation is that events will be delivered with 3 seconds interval and I'll see
a log of received events with 3 seconds interval in MIDIMonitor (or hear a sound with
duration of 3 seconds).
Nevertheless, looking at MIDIMonitor, both events arrive exactly at the same time without any interval.
Could someone please point me to what is going wrong with my code (or maybe with my expectation)?
The test project (just a single file Command Line Tool) is at
Here's excerpt of how MIDIPackets are created:
======================================================
ByteCount noteBytesCount = 3;
Byte onBytes[] = {0x90, 0x3C, 0x7F}; // C3 on on 1st channel
Byte offBytes[] = {0x80, 0x3C, 0x7F}; // C3 off on 1st channel
UInt64 _onTime_ = mach_absolute_time();
UInt64 offTime = onTime + secondsToHostTime(3.0); //see below
ByteCount packetListSize = 1024;
MIDIPacketList *packetList = (MIDIPacketList *)malloc(packetListSize);
MIDIPacket *packet = MIDIPacketListInit(packetList);
packet = MIDIPacketListAdd(packetList, packetListSize, packet, onTime, noteBytesCount, &onBytes[0]);
packet = MIDIPacketListAdd(packetList, packetListSize, packet, offTime, noteBytesCount, &offBytes[0]);
MIDISend(port, endpoint, packetList);
// MIDIMonitor log:
// 08:46:58.362 To MIDI Monitor (Untitled) Note On 1 C3 127
// 08:46:58.362 To MIDI Monitor (Untitled) Note Off 1 C3 64
==========================================================
The conversion from seconds to host time format done this way:
==========================================================
UInt64 secondsToHostTime(Float64 inSeconds)
{
Float64 nanoseconds_per_second = 1000000000.0;
Float64 nanos = inSeconds * nanoseconds_per_second;
struct mach_timebase_info theTimeBaseInfo;
mach_timebase_info(&theTimeBaseInfo);
return (UInt64)(nanos * (Float64)theTimeBaseInfo.denom / (Float64)theTimeBaseInfo.numer);
}
==========================================================
Thank you in advance!
Vlad.