1

I'm trying to create a method that outputs MIDI information to a virtual client using CoreMIDI. The "action" method is MIDIReceived, which sends midi data in the form of MIDI packets to a virtual client.

Below, I've created a method that accepts a MIDI byte as a parameter, which the method should add to a MIDI Packet List which is then sent to a virtual client with MIDIReceived.

It doesn't work.

I've tested this code without trying to use a custom method-- that is, manually inputing midi byte data, and it works fine.

The problem I have, I believe, is that I'm not able to properly pass a byte array to the method.

The error I get for the object message is "expected expression".

How can I pass a byte array to a method? (preferably without using 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

As you can see, all I want to do is create a simple way to transmit MIDI data to a virtual source, but am having some trouble doing so.

  • "How can I pass a byte array to a method?" - I don't understand what you mean by that. `[self someMethod:array];`, is it that? –  Apr 25 '13 at 20:20
  • I'm new to objective-c so I might be phrasing this incorrectly. The method "packetOut" of class "PacketCreateAndSend" has a parameter "midiByte". I'm trying to call this method with the parameter {0x90, 0x3d, 0x3d} but the compiler won't let me. [testObject packetOut:{0x90, 0x3d, 0x3d}]. – Matt Gilliam Apr 25 '13 at 21:25
  • You need to initialize a an array and pass a pointer to its first element. `int arr[] = { 1, 2, 3 }; [self foo:arr];` –  Apr 25 '13 at 21:35
  • I tried: `int byteArray[] = {0x90, 0x3d, 0x3d};` `[testObject packetOut:byteArray];` but get _"incompatible pointer types sending "int[3]" to parameter of type "Byte *" (aka "unsigned char *")_ – Matt Gilliam Apr 25 '13 at 21:45
  • I also modified the method interface and implementation to read: `-(void) packetOut:(int*)midiByte{ Byte testByte = *midiByte; //... }` and still no luck. If you can't tell I'm sort of fumbling around in the dark here. – Matt Gilliam Apr 25 '13 at 22:19
  • I got it working. I did `Byte byteArray[] = {0x90, 0x3d, 0x3d};` `[testObject packetOut:byteArray];` and `-(void) packetOut:(Byte[])midiByte{ Byte *testByte = midiByte; /*...*/ pkt = MIDIPacketListAdd(pktList, 1024, pkt, 0, 3, testByte); //... }` Thanks for the help! – Matt Gilliam Apr 26 '13 at 01:05
  • You don't even need the `Byte *testByte = midiByte;` line. Arrays decay into pointers when passed to a function. Please learn C before trying to do Objective-C. –  Apr 26 '13 at 04:28

1 Answers1

1

Here is the fixed, fully functional code. Thanks for the help!

#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