Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 6, 2012, 09:38 AM   #1
Bob2000
macrumors newbie
 
Join Date: Apr 2012
CoreMidi Devices

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.
Code:
#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;}
Bob2000 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:43 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC