passing a bytearray as a parameter of a method (CoreMIDI implementation)
Asked Answered
B

1

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.

Bereave answered 25/4, 2013 at 20:13 Comment(7)
"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?Apothecary
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}].Bereave
You need to initialize a an array and pass a pointer to its first element. int arr[] = { 1, 2, 3 }; [self foo:arr];Apothecary
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 *")Bereave
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.Bereave
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!Bereave
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.Apothecary
B
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
Bereave answered 26/4, 2013 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.