<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Trnsfrmr &#187; CoreFoundation</title>
	<atom:link href="http://trnsfrmr.com/tag/corefoundation/feed/" rel="self" type="application/rss+xml" />
	<link>http://trnsfrmr.com</link>
	<description>only fools &#38; horses work.</description>
	<lastBuildDate>Tue, 24 Aug 2010 14:48:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Getting started with CoreMidi</title>
		<link>http://trnsfrmr.com/2009/01/12/getting-started-with-coremidi/</link>
		<comments>http://trnsfrmr.com/2009/01/12/getting-started-with-coremidi/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 04:35:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[CoreFoundation]]></category>
		<category><![CDATA[CoreMidi]]></category>
		<category><![CDATA[MIDI]]></category>

		<guid isPermaLink="false">http://trnsfrmr.com/blog/?p=113</guid>
		<description><![CDATA[So I finally started on Octopussy. I&#8217;ve always found CoreMidi and procedural APIs in general a bit puzzling so I thought I would start with the simplest of tasks to help me demystify it all. My goal was simply to send a MIDI note on message from the Mac to the blofeld. After perusing MIDIService.h, [...]]]></description>
			<content:encoded><![CDATA[<p>So I finally started on <a href="/blog/octopussy">Octopussy</a>. I&#8217;ve always found CoreMidi and procedural APIs in general a bit puzzling so I thought I would start with the simplest of tasks to help me demystify it all. My goal was simply to send a MIDI note on message from the Mac to the blofeld.</p>
<p>After perusing MIDIService.h, I gathered that I had to ultimately use the MIDISend().</p>
<pre><span class="keyword">extern</span> <a class="type" href="file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/Carbon/Reference/ErrorHandler/Reference/reference.html#//apple_ref/c/tdef/OSStatus" target="_top"><span class="type">OSStatus</span></a> <a class="function" href="file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/MusicAudio/Reference/CACoreMIDIRef/MIDIServices/CompositePage.html#//apple_ref/c/func/MIDISend"><span class="function">MIDISend</span></a>(
    <a class="type" href="file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/MusicAudio/Reference/CACoreMIDIRef/MIDIServices/CompositePage.html#//apple_ref/c/tdef/MIDIPortRef"><span class="type">MIDIPortRef</span></a> <span class="param">port</span>,
    <a class="type" href="file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/MusicAudio/Reference/CACoreMIDIRef/MIDIServices/CompositePage.html#//apple_ref/c/tdef/MIDIEndpointRef"><span class="type">MIDIEndpointRef</span></a> <span class="param">dest</span>,
    <span class="keyword">const</span> <a class="type" href="file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.CoreReference.docset/Contents/Resources/Documents/documentation/MusicAudio/Reference/CACoreMIDIRef/MIDIServices/CompositePage.html#//apple_ref/c/tag/MIDIPacketList"><span class="type">MIDIPacketList</span></a> *<span class="param">pktlist</span>);</pre>
<p>When it was time to create a MIDIEndpointRef, I was presented with numerous paths. My initial instinct was to traverse some hierachy to get to the Blofeld MIDI Out but it turns out that each MIDIEntity can be reference using a unique id. I was hoping that this info would be available in the Audio MIDI Setup utility but it turns out you can only get at it via code. So I created a foundation tool with the following code.</p>
<pre><span>#import </span>&lt;Foundation/Foundation.h&gt;

<span>#import </span>&lt;CoreMIDI/CoreMIDI.h&gt;

<span>int</span> main (<span>int</span> argc, <span>const</span> <span>char</span> * argv[]) {

<span>    </span>NSAutoreleasePool<span> * pool = [[</span>NSAutoreleasePool<span> </span><span>alloc</span><span>] </span><span>init</span><span>];</span>

<span>	</span><span>MIDIDeviceRef</span> midiDevice;

<span><span>	</span></span><span>int</span><span> numOfDevices = </span>MIDIGetNumberOfDevices<span>();</span>

<span>	</span><span>int</span> numOfBlofeldEntities;

<span>	</span><span>for</span>(<span>int</span> i=<span>0</span>; i&lt; numOfDevices; i++ ){

<span>		</span>midiDevice = <span>MIDIGetDevice</span>(i);

<span>		</span><span>NSDictionary</span> *midiDeviceProperties;

<span>		</span><span>MIDIObjectGetProperties</span>(midiDevice, (<span>CFPropertyListRef</span> *)&amp;midiDeviceProperties, <span>YES</span>);

<span><span>		</span></span>NSLog(@"midiDeviceProperties:%@", midiDeviceProperties);

<span>	</span>}

    [pool <span>drain</span>];

    <span>return</span> <span>0</span>;

}</pre>
<p>After running the tool, I get the ff output. I need to run this on multiple Macs to verify that the uniqueID is actually the same regardless of which machine I run it on.</p>
<pre><strong>2009-01-11 23:24:31.273 MidiProperties[4198:10b] midiDeviceProperties-5:{</strong>

<strong>    SerialNumber = "8290071532-023547011532";</strong>

<strong>    USBLocationID = -46923776;</strong>

<strong>    USBVendorProduct = 317063187;</strong>

<strong>    "apple.midi.audiomidisetup.widget.xPosition" = 385;</strong>

<strong>    "apple.midi.audiomidisetup.widget.yPosition" = 203;</strong>

<strong>    driver = "com.apple.AppleMIDIUSBDriver";</strong>

<strong>    entities =     (</strong>

<strong>                {</strong>

<strong>            destinations =             (</strong>

<strong>                                {</strong>

<strong>                    name = "Blofeld MIDI out";</strong>

<strong>                    uniqueID = -934632258;</strong>

<strong>                }</strong>

<strong>            );</strong>

<strong>            embedded = 0;</strong>

<strong>            maxSysExSpeed = 3125;</strong>

<strong>            name = "Waldorf Blofeld";</strong>

<strong>            sources =             (</strong>

<strong>                                {</strong>

<strong>                    name = "Blofeld MIDI in";</strong>

<strong>                    uniqueID = 1439776402;</strong>

<strong>                }</strong>

<strong>            );</strong>

<strong>            uniqueID = -1524681845;</strong>

<strong>        }</strong>

<strong>    );</strong>

<strong>    image = "/Library/Audio/MIDI Devices/Access Music/Images/Virus TI Snow.tiff";</strong>

<strong>    manufacturer = "Waldorf Music GmbH";</strong>

<strong>    model = "Waldorf Blofeld";</strong>

<strong>    name = Blofeld;</strong>

<strong>    offline = 0;</strong>

<strong>    "receives MTC" = 0;</strong>

<strong>    "receives clock" = 0;</strong>

<strong>    "supports General MIDI" = 0;</strong>

<strong>    "supports MMC" = 0;</strong>

<strong>    "transmits MTC" = 0;</strong>

<strong>    "transmits clock" = 0;</strong>

<strong>    uniqueID = 1111148707;</strong>

<strong>}</strong></pre>
<p>Finally armed with the blofeld&#8217;s MIDI Out uniqueID, the rest was cake. Here is the code I used to send the note on message.</p>
<pre><span>#import </span>&lt;Foundation/Foundation.h&gt;

<span>#import </span>&lt;CoreMIDI/CoreMIDI.h&gt;

MIDIEndpointRef getEndpointWithUniqueID(<span>MIDIUniqueID</span> <span>id</span>){

<span><span>	</span></span>MIDIObjectRef<span> endPoint;</span>

<span><span>	</span></span>MIDIObjectType<span> foundObj;</span>

<span>	</span><span>MIDIObjectFindByUniqueID</span>(<span>id</span>, &amp;endPoint, &amp;foundObj);

<span><span>	</span></span><span>return</span><span> (</span>MIDIEndpointRef<span>) endPoint;</span>

}

MIDIClientRef getMidiClient(){

<span>	</span><span>MIDIClientRef</span> midiClient;

<span>	</span><span>NSString</span> *outPortName =<span>@"blofeldOut"</span>;

<span>	</span><span>MIDIClientCreate</span>((<span>CFStringRef</span>)outPortName, <span>NULL</span>, <span>NULL</span>, &amp;midiClient);

<span>	</span><span>return</span> midiClient;

}

MIDIPortRef getOutPutPort(){

<span><span>	</span></span>MIDIPortRef<span> outPort;</span>

<span>	</span><span>NSString</span> *outPortName =<span>@"blofeldOut"</span>;

<span>	</span><span>MIDIOutputPortCreate</span>(<span>getMidiClient</span>(), (<span>CFStringRef</span>)outPortName, &amp;outPort);

<span>	</span><span>return</span> outPort;

}

MIDIPacketList getMidiPacketList(){

<span><span>	</span></span>MIDIPacketList<span> packetList;</span>

<span>	</span>packetList.<span>numPackets</span> = <span>1</span>;

<span>	</span><span>MIDIPacket</span>* firstPacket = &amp;packetList.<span>packet</span>[<span>0</span>];

<span>	</span>firstPacket-&gt;<span>timeStamp</span> = <span>0</span>;<span>	</span><span>// send immediately</span>

<span>	</span>firstPacket-&gt;<span>length</span> = <span>3</span>;

<span>	</span>firstPacket-&gt;<span>data</span>[<span>0</span>] = <span>0x90</span>;

<span>	</span>firstPacket-&gt;<span>data</span>[<span>1</span>] = <span>60</span>;

<span>	</span>firstPacket-&gt;<span>data</span>[<span>2</span>] = <span>64</span>;

<span><span>	</span></span>// TODO: add end note sequence

<span>	</span><span>return</span> packetList;

}

<span>void</span> play_note(<span>void</span>) {

<span><span>	</span></span>NSAutoreleasePool<span> * pool = [[</span>NSAutoreleasePool<span> </span><span>alloc</span><span>] </span><span>init</span><span>]; </span>

<span><span>	</span></span><span>MIDIPacketList</span><span> packetList=</span>getMidiPacketList<span>();</span>

<span>	</span><span>MIDIUniqueID</span> blofeldEndpointID = -<span>934632258</span>;

<span>	</span><span>MIDIEndpointRef</span> blofeldEndpoint = <span>getEndpointWithUniqueID</span>(blofeldEndpointID);

<span>	</span><span>MIDISend</span>(<span>getOutPutPort</span>(), blofeldEndpoint, &amp;packetList);

<span>	</span><span>MIDIEndpointDispose</span>(blofeldEndpoint);

<span>	</span>[pool <span>drain</span>];

}

<span>int</span> main (<span>int</span> argc, <span>const</span> <span>char</span> * argv[]) {

<span>    </span>play_note<span>();</span>

    <span>return</span> <span>0</span>;

}</pre>
]]></content:encoded>
			<wfw:commentRss>http://trnsfrmr.com/2009/01/12/getting-started-with-coremidi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
