Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c MIDISend:在iPhone上播放音符_Objective C_Midi_Coremidi - Fatal编程技术网

Objective c MIDISend:在iPhone上播放音符

Objective c MIDISend:在iPhone上播放音符,objective-c,midi,coremidi,Objective C,Midi,Coremidi,我正在尝试生成一个音符,使用Objective-C和MIDI通过iPhone扬声器播放。我有下面的代码,但它没有做任何事情。我做错了什么 MIDIPacketList packetList; packetList.numPackets = 1; MIDIPacket* firstPacket = &packetList.packet[0]; firstPacket->timeStamp = 0; // send immediately firstPacket->len

我正在尝试生成一个音符,使用Objective-C和MIDI通过iPhone扬声器播放。我有下面的代码,但它没有做任何事情。我做错了什么

MIDIPacketList packetList;

packetList.numPackets = 1;

MIDIPacket* firstPacket = &packetList.packet[0];

firstPacket->timeStamp = 0; // send immediately

firstPacket->length = 3;

firstPacket->data[0] = 0x90;

firstPacket->data[1] = 80;

firstPacket->data[2] = 120;

MIDIPacketList pklt=packetList;

MIDISend(MIDIGetSource(0), MIDIGetDestination(0), &pklt);

你有三个问题:

  • 声明
    MIDIPacketList
    不会分配内存或初始化结构
  • 您正在将MIDIGetSource的结果(它返回一个
    MIDIEndpointRef
    )作为第一个参数传递到
    MIDISend
    ,在那里它需要一个
    MIDIPortRef
    。(您可能忽略了有关此问题的编译器警告。永远不要忽略编译器警告。)
  • 在iOS中发送MIDI音符不会发出任何声音。如果您没有将外部MIDI设备连接到iOS设备,则需要使用CoreAudio设置可以生成声音的设备。这超出了这个答案的范围
  • 因此,此代码将运行,但除非您有外部硬件,否则不会发出任何声音:

    //Look to see if there's anything that will actually play MIDI notes
    NSLog(@"There are %lu destinations", MIDIGetNumberOfDestinations());
    
    // Prepare MIDI Interface Client/Port for writing MIDI data:
    MIDIClientRef midiclient = 0;
    MIDIPortRef midiout = 0;
    OSStatus status;
    status = MIDIClientCreate(CFSTR("Test client"), NULL, NULL, &midiclient);
    if (status) {
        NSLog(@"Error trying to create MIDI Client structure: %d", (int)status);
    }
    status = MIDIOutputPortCreate(midiclient, CFSTR("Test port"), &midiout);
    if (status) {
        NSLog(@"Error trying to create MIDI output port: %d", (int)status);
    }
    
    Byte buffer[128];
    MIDIPacketList *packetlist = (MIDIPacketList *)buffer;
    MIDIPacket *currentpacket = MIDIPacketListInit(packetlist);
    NSInteger messageSize = 3; //Note On is a three-byte message
    Byte msg[3] = {0x90, 80, 120};
    MIDITimeStamp timestamp = 0;
    currentpacket = MIDIPacketListAdd(packetlist, sizeof(buffer), currentpacket, timestamp, messageSize, msg);
    MIDISend(midiout, MIDIGetDestination(0), packetlist);