OSX Core MIDI- Calling MIDIPacketListAdd from NSTimer
I'm sending a string of MIDI packets using MIDIPacketListAdd. I want to change the packets dynamically during playback, so I'm using a NSTimer to add each packet before the scheduled time the packet should go out.
The code works perfectly, the address of the current packet is updated correctly, I have verified that all the data is correct. However, no MIDI data is being sent when MIDIPacketListAdd is called from the timer. I add the first two packets before starting the timer to make sure that the packets are sent to MIDIPacketListAdd before they need to be played.
I'm very puzzled. Any suggestions?
T开发者_如何学Pythonhanks, Tom Jeffries
Your description is exactly how I send my MIDI data as well, and it works fine. Maybe you could post your source code, without that it's a bit difficult to guess what is wrong.
Also, for testing, try setting your timestamps to zero, just to make sure you are able to send MIDI, after that you can put your timestamps back in to debug them separately.
In lieu of source, here is a simple method that sends MIDI, and I do call it from an NSTimer, and it based on some Apple examples and it works. Note 'outputPort' is something you should have created somewhere before attempting to send MIDI along with your client ref (I'll assume you have done that)
OSStatus s;
MIDIClientRef client;
MIDIPortRef outputPort;
s = MIDIClientCreate((CFStringRef)@"My Test MIDI Client", MyMIDINotifyProc, self, &client);
s = MIDIOutputPortCreate(client, (CFStringRef)@"My Test MIDI Output Port", &outputPort);
once you have those, you can send MIDI
- (void) MySendMidi:(const UInt8*)data size:(UInt32)bsize
{
NSLog(@"%s(%u bytes to core MIDI)", __func__, unsigned(bsize));
assert(bsize < 65536);
Byte packetBuffer[bsize+100];
MIDIPacketList *packetList = (MIDIPacketList*)packetBuffer;
MIDIPacket *packet = MIDIPacketListInit(packetList);
packet = MIDIPacketListAdd(packetList, sizeof(packetBuffer), packet, 0, bsize, data);
// Send it to every destination in the system...
for (ItemCount index = 0; index < MIDIGetNumberOfDestinations(); ++index)
{
MIDIEndpointRef outputEndpoint = MIDIGetDestination(index);
if (outputEndpoint)
{
// Send it
OSStatus s = MIDISend(outputPort, outputEndpoint, packetList);
}
}
}
精彩评论