本文介绍了OSX Core MIDI-从NSTimer调用MIDIPacketListAdd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MIDIPacketListAdd发送一串MIDI数据包.我想在播放过程中动态更改数据包,因此我正在使用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.

代码运行正常,当前数据包的地址已正确更新,我已验证所有数据均正确.但是,从计时器调用MIDIPacketListAdd时,没有发送MIDI数据.我在启动计时器之前添加了前两个数据包,以确保在需要播放之前将数据包发送到MIDIPacketListAdd.

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?

谢谢汤姆·杰弗里斯(Tom Jeffries)

Thanks, Tom Jeffries

推荐答案

您的描述也正是我发送MIDI数据的方式,并且工作正常.也许您可以发布源代码,否则就很难猜测出什么地方了.

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.

另外,为了进行测试,请尝试将时间戳设置为零,以确保能够发送MIDI,然后再放回时间戳以分别调试它们.

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.

代替源代码,这里有一个发送MIDI的简单方法,我确实是从NSTimer调用它的,它基于Apple的一些示例,并且可以正常工作.注意'outputPort'是您应该在尝试与客户端参考一起发送MIDI之前在某个地方创建的(我假设您已经完成了)

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

一旦有了这些,就可以发送MIDI

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

这篇关于OSX Core MIDI-从NSTimer调用MIDIPacketListAdd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 15:35