OSX Core MIDI- Calling MIDIPacketListAdd from NSTimer

岁酱吖の 提交于 2019-12-12 01:52:20

问题


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?

Thanks, Tom Jeffries


回答1:


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);
        }
    }
}


来源:https://stackoverflow.com/questions/7668390/osx-core-midi-calling-midipacketlistadd-from-nstimer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!