传递bytearray作为方法的参数(CoreMIDI实现)

时间:2013-04-25 20:13:29

标签: objective-c arrays macos bytearray coremidi

我正在尝试使用CoreMIDI创建一个将MIDI信息输出到虚拟客户端的方法。 “action”方法是MIDIReceived,它以MIDI数据包的形式将midi数据发送到虚拟客户端。

下面,我创建了一个接受MIDI字节作为参数的方法,该方法应该将该方法添加到MIDI数据包列表中,然后将其发送到具有MIDIReceived的虚拟客户端。

它不起作用。

我已经测试了这段代码而没有尝试使用自定义方法 - 也就是说,手动输入midi字节数据,它运行正常。

我相信,我遇到的问题是我无法正确地将字节数组传递给该方法。

我得到的对象消息的错误是“预期表达式”。

如何将字节数组传递给方法? (最好不使用NSData)?

#import "AppDelegate.h"
#import <CoreMIDI/CoreMIDI.h>

MIDIClientRef     theMidiClient;
MIDIEndpointRef   midiOut;
char              pktBuffer[1024];
MIDIPacketList    *pktList = (MIDIPacketList*) pktBuffer;
MIDIPacket        *pkt;

@interface PacketCreateAndSend : NSObject
-(void) packetOut:(Byte*)midiByte;
@end

@implementation PacketCreateAndSend
-(void) packetOut:(Byte*)midiByte{
    Byte testByte = *midiByte;

 //initialize MIDI packet list:
    pkt = MIDIPacketListInit(pktList);

 //add packet to MIDI packet list
    pkt = MIDIPacketListAdd(pktList, 1024, pkt, 0, 3, &testByte); 

 //send packet list to virtual client:
    MIDIReceived(midiOut, pktList);
}
@end

@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

 //create MIDI client and source:
    MIDIClientCreate(CFSTR("Magical MIDI"), NULL, NULL, &theMidiClient);
    MIDISourceCreate(theMidiClient, CFSTR("virtual MIDI created"), &midiOut);

 //create instance of PacketCreateAndSend object:
    PacketCreateAndSend *testObject = [PacketCreateAndSend new];

 //(here is where the error occurs)
 //message object with MIDI byte data:
    [testObject packetOut:{0x90, 0x3d, 0x3d}];

}
@end

正如您所看到的,我想要做的就是创建一种将MIDI数据传输到虚拟源的简单方法,但这样做有些麻烦。

1 个答案:

答案 0 :(得分:1)

这是固定的,功能齐全的代码。谢谢你的帮助!

#import "AppDelegate.h"
#import <CoreMIDI/CoreMIDI.h>

MIDIClientRef     theMidiClient;
MIDIEndpointRef   midiOut;
char              pktBuffer[1024];
MIDIPacketList    *pktList = (MIDIPacketList*) pktBuffer;
MIDIPacket        *pkt;

@interface PacketCreateAndSend : NSObject

-(void) packetOut:(Byte[])midiByte;

@end

@implementation PacketCreateAndSend

-(void) packetOut:(Byte[])midiByte{

    pkt = MIDIPacketListInit(pktList);

    pkt = MIDIPacketListAdd(pktList, 1024, pkt, 0, 3, midiByte);

    MIDIReceived(midiOut, pktList);

}

@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

   //initialize MIDI client and source:
    MIDIClientCreate(CFSTR("Magical MIDI"), NULL, NULL, &theMidiClient);
    MIDISourceCreate(theMidiClient, CFSTR("virtual MIDI created"), &midiOut);


    PacketCreateAndSend *testObject = [PacketCreateAndSend new];


    Byte byteArray[] = {0x90, 0x3d, 0x3d};

   //send the midi packet:    
    [testObject packetOut:byteArray];
}
@end