Skip to main content
added 46 characters in body
Source Link
Dan Rosenstark
  • 70k
  • 60
  • 294
  • 429

I'm just leaving this here for my own reference. It's a full example based 100% on yours, but including the other side (receiving) and, my bad C code and the accepted answer's corrections (of course).

I'm just leaving this here for my own reference. It's a full example based 100% on yours, but including the other side (receiving) and my bad C code.

I'm just leaving this here for my own reference. It's a full example based 100% on yours, but including the other side (receiving), my bad C code and the accepted answer's corrections (of course).

Source Link
Dan Rosenstark
  • 70k
  • 60
  • 294
  • 429

I'm just leaving this here for my own reference. It's a full example based 100% on yours, but including the other side (receiving) and my bad C code.

#import "AppDelegate.h" @implementation AppDelegate @synthesize window = _window; #define NSLogError(c,str) do{if (c) NSLog(@"Error (%@): %u:%@", str, (unsigned int)c,[NSError errorWithDomain:NSMachErrorDomain code:c userInfo:nil]); }while(false) static void spit(Byte* values, int length, BOOL useHex) { NSMutableString *thing = [@"" mutableCopy]; for (int i=0; i<length; i++) { if (useHex) [thing appendFormat:@"0x%X ", values[i]]; else [thing appendFormat:@"%d ", values[i]]; } NSLog(@"Length=%d %@", length, thing); } - (void) startSending { MIDIEndpointRef midiOut; char pktBuffer[1024]; MIDIPacketList* pktList = (MIDIPacketList*) pktBuffer; MIDIPacket *pkt; Byte midiDataToSend[] = {0x91, 0x3c, 0x40}; int i; MIDISourceCreate(theMidiClient, CFSTR("Magical MIDI Source"), &midiOut); pkt = MIDIPacketListInit(pktList); pkt = MIDIPacketListAdd(pktList, 1024, pkt, 0, 3, midiDataToSend); for (i = 0; i < 100; i++) { if (pkt == NULL || MIDIReceived(midiOut, pktList)) { printf("failed to send the midi.\n"); } else { printf("sent!\n"); } sleep(1); } } void ReadProc(const MIDIPacketList *packetList, void *readProcRefCon, void *srcConnRefCon) { const MIDIPacket *packet = &packetList->packet[0]; for (int i = 0; i < packetList->numPackets; i++) { NSData *data = [NSData dataWithBytes:packet->data length:packet->length]; spit((Byte*)data.bytes, data.length, YES); packet = MIDIPacketNext(packet); } } - (void) setupReceiver { OSStatus s; MIDIEndpointRef virtualInTemp; NSString *inName = [NSString stringWithFormat:@"Magical MIDI Destination"]; s = MIDIDestinationCreate(theMidiClient, (__bridge CFStringRef)inName, ReadProc, (__bridge void *)self, &virtualInTemp); NSLogError(s, @"Create virtual MIDI in"); } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { MIDIClientCreate(CFSTR("Magical MIDI"), NULL, NULL, &theMidiClient); [self setupReceiver]; [self startSending]; } @end