Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 6, 2012, 10: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

Similar Threads
thread Thread Starter Forum Replies Last Post
CoreMIDI gone mad! jakesaunders27 Digital Audio 1 Jan 20, 2014 04:31 PM
Nearly 300 Million Devices Running iOS 6, 60% of All iOS Devices Ever MacRumors MacRumors.com News Discussion 139 Jan 31, 2013 02:10 PM
What do you name your devices? mentaluproar Community Discussion 69 Jan 5, 2013 04:06 PM
External devices not appearing on Mountain Lion desktop or devices list. stop.the.clocks Mac Basics and Help 1 Aug 17, 2012 02:14 PM
2 out of 3 devices down.. devices not registered? se99jmk iOS 6 12 Jun 11, 2012 07:06 PM

Forum Jump

All times are GMT -5. The time now is 01:55 PM.

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

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