PDA

View Full Version : CoreMidi Devices




Bob2000
Apr 6, 2012, 09:38 AM
Good evening,
I am trying to play a note using CoreMidi library, the problem is that
I don't have any external MIDI device that receive messages I send.
I only have my macbook pro with MacOSX 10.6.8

My horrible code (I don't have checked error and other things, but, guys I first have to make it work) is in the following.
It simply try to send a note_on message to all destination of all entities of all device of my system.
The conclusion I get is that because I don't hear any sound there are no devices that can play sound connected. (is it correct?)

But.
I know that MIDI programs work fine within this system. So, to who these programs (like tuxguitar) send MIDI messages?
Which functions/library I have to use to do my dirty trivial job?
Please share some knowledge with me.
#include <iostream>
#include <iostream>
#include <cstdlib>
#include <CoreMIDI/CoreMIDI.h>
#include <errno.h>


using namespace std;
#define MESSAGESIZE 3

void playPacketListOnAllDevices(MIDIPortRef midiout, const MIDIPacketList* pktlist) {
// send MIDI message to all MIDI output devices connected to computer:
int devices = MIDIGetNumberOfDevices();
int i;
for (i=0; i<=devices;i++) {
MIDIDeviceRef d = MIDIGetDevice(i);
int entities = MIDIDeviceGetNumberOfEntities(d);
int j;
for(j=0;j<=entities;j++){
MIDIEntityRef e = MIDIDeviceGetEntity(d,j);
int destinations =MIDIEntityGetNumberOfDestinations(e);
int w;
for (w=0;w<=destinations;w++){
cout<<"("<<i<<" "<<j<<" "<<w<< ")\n";
OSStatus status;
int dest = MIDIGetDestination(w);
if (status = MIDISend(midiout, dest, pktlist)) {
printf("Problem sendint MIDI data on port %d\n", (int)dest);
exit(status);
}
}
}
}
}

int main(){
Byte buffer[1024]; // storage space for MIDI Packets (max 65536)
MIDIPacketList *packetlist = (MIDIPacketList*)buffer;
MIDIPacket *currentpacket = MIDIPacketListInit(packetlist);
MIDIClientRef midiclient = NULL;
MIDIPortRef midiout = NULL;
OSStatus status;
if (status = MIDIClientCreate(CFSTR("TeStInG"), NULL, NULL, &midiclient)) {
printf("Error trying to create MIDI Client structure: %d\n", status);
exit(status);
}
if (status = MIDIOutputPortCreate(midiclient, CFSTR("OuTpUt"), &midiout)) {
printf("Error trying to create MIDI output port: %d\n", status);
exit(status);
}

MIDITimeStamp timestamp = 0; // 0 will mean play now.
Byte noteon[MESSAGESIZE] = {0x90, 60, 90};
currentpacket = MIDIPacketListAdd(packetlist, sizeof(buffer), currentpacket, timestamp, MESSAGESIZE, noteon);
cout<<"Number of Devices is "<<(int)MIDIGetNumberOfDevices()<<"\n";
playPacketListOnAllDevices(midiout, packetlist);

return 0;}