为什么这个简单的CoreMIDI程序不能产生MIDI输出? [英] Why doesn't this simple CoreMIDI program produce MIDI output?

查看:250
本文介绍了为什么这个简单的CoreMIDI程序不能产生MIDI输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的CoreMIDI OS X应用程序,可以发送MIDI数据.问题是它不起作用.它可以很好地编译并运行.它不会报告任何错误,并且不会崩溃.创建的源在MIDI监视器中变为可见.但是,没有MIDI数据出.

Here is an extremely simple CoreMIDI OS X application that sends MIDI data. The problem is that it doesn't work. It compiles fine, and runs. It reports no errors, and does not crash. The Source created becomes visible in MIDI Monitor. However, no MIDI data comes out.

有人可以让我知道我在做什么错吗?

Could somebody let me know what I'm doing wrong here?

#include <CoreMIDI/CoreMIDI.h>

int main(int argc, char *args[])
{
    MIDIClientRef   theMidiClient;
    MIDIEndpointRef midiOut;
    MIDIPortRef     outPort;
    char pktBuffer[1024];
    MIDIPacketList* pktList = (MIDIPacketList*) pktBuffer;
    MIDIPacket     *pkt;
    Byte            midiDataToSend[] = {0x91, 0x3c, 0x40};
    int             i;

    MIDIClientCreate(CFSTR("Magical MIDI"), NULL, NULL,
                     &theMidiClient);
    MIDISourceCreate(theMidiClient, CFSTR("Magical MIDI Source"),
                     &midiOut);
    MIDIOutputPortCreate(theMidiClient, CFSTR("Magical MIDI Out Port"),
                         &outPort);

    pkt = MIDIPacketListInit(pktList);
    pkt = MIDIPacketListAdd(pktList, 1024, pkt, 0, 3, midiDataToSend);

    for (i = 0; i < 100; i++) {
        if (pkt == NULL || MIDISend(outPort, midiOut, pktList)) {
            printf("failed to send the midi.\n");
        } else {
            printf("sent!\n");
        }
        sleep(1);
    }

return 0;
}

推荐答案

您正在调用MIDISourceCreate来创建虚拟MIDI源.

You're calling MIDISourceCreate to create a virtual MIDI source.

这意味着您的音源将出现在其他应用程序的MIDI设置UI中,并且这些应用程序可以选择是否收听您的音源.您的MIDI不会发送到任何物理MIDI端口,除非有其他应用程序将其引导到那里.这也意味着您的应用程序无法选择发送到MIDI的位置.我假设这就是您想要的.

This means that your source will appear in other apps' MIDI setup UI, and that those apps can choose whether or not to listen to your source. Your MIDI will not get sent to any physical MIDI ports, unless some other app happens to channel it there. It also means that your app has no choice as to where the MIDI it's sending goes. I'm assuming that's what you want.

创建虚拟源之后,使用MIDIReceived将MIDI消息从您的虚拟源传输到连接到该虚拟源的任何客户端.

After creating a virtual source, use MIDIReceived to transmit MIDI messages from your virtual source to any clients connected to the virtual source.

所以,做两件事:

  • 删除创建输出端口的代码.您不需要它.
  • MIDISend(outPort, midiOut, pktList)更改为:MIDIReceived(midiOut, pktlist).
  • Remove the code that creates the output port. You don't need it.
  • change MIDISend(outPort, midiOut, pktList) to: MIDIReceived(midiOut, pktlist).

那应该可以解决您的问题.

That should solve your problem.

那么输出端口有什么用呢?如果要将MIDI数据定向到特定的目标(例如物理MIDI端口),则不会创建虚拟MIDI源.相反:

So what are output ports good for? If you wanted to direct your MIDI data to a specific destination -- maybe a physical MIDI port -- you would NOT create a virtual MIDI source. Instead:

  1. 调用MIDIOutputPortCreate()建立输出端口
  2. 使用MIDIGetNumberOfDestinations()MIDIGetDestination()获取目的地列表并找到您感兴趣的目的地.
  3. 要将MIDI发送到一个目的地,请调用MIDISend(outputPort, destination, packetList).
  1. Call MIDIOutputPortCreate() to make an output port
  2. Use MIDIGetNumberOfDestinations() and MIDIGetDestination() to get the list of destinations and find the one you're interested in.
  3. To send MIDI to one destination, call MIDISend(outputPort, destination, packetList).

这篇关于为什么这个简单的CoreMIDI程序不能产生MIDI输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆