<?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; MIDI</title>
	<atom:link href="http://trnsfrmr.com/tag/midi/feed/" rel="self" type="application/rss+xml" />
	<link>http://trnsfrmr.com</link>
	<description>only fools &#38; horses work.</description>
	<lastBuildDate>Mon, 21 Feb 2011 15:28:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Bytes, Nibbles &amp; Bits for CoreMidi</title>
		<link>http://trnsfrmr.com/2010/03/12/bytes-nibbles-bits-for-coremidi/</link>
		<comments>http://trnsfrmr.com/2010/03/12/bytes-nibbles-bits-for-coremidi/#comments</comments>
		<pubDate>Fri, 12 Mar 2010 19:37:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Synth]]></category>
		<category><![CDATA[CoreMidi]]></category>
		<category><![CDATA[MIDI]]></category>

		<guid isPermaLink="false">http://trnsfrmr.com/?p=174</guid>
		<description><![CDATA[So I&#8217;m working on YAMP (yet another midi project and it took me a while to recollect all the binary arithmetic required to parse MIDI packets. Take a midi packet with 3 bytes in hexadecimal notation such as 99 3C 64. This stream encapsulates the following instructions: Play middle C on channel 10 at velocity [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m working on YAMP (yet another midi project <img src='http://trnsfrmr.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  and it took me a while to recollect all the binary arithmetic required to parse MIDI packets.</p>
<p>Take a midi packet with 3 bytes in hexadecimal notation such as <strong>99 3C 64</strong>. This stream encapsulates the following instructions:</p>
<p>Play middle C on channel 10 at velocity 100. Now lets strip it down to nibbles &amp; bits.</p>
<p>9 -&gt; Note on message. (Examples of other status messages are: 0xB for controller change, 0x C for program change &amp; 0xE for pitch-bend)</p>
<p>9 -&gt; Middle channel 10. (Range 0 -15)</p>
<p>3C -&gt;Middle C -&gt; Note 60</p>
<p>64 -&gt; 100 in decimal</p>
<p><strong>Determining if a stream begins with a status byte.</strong></p>
<p>A set high-bit on the first byte identifies the byte as a status message. This can be accomplished visually by converting <strong>99</strong> from hex to binary <strong>10011001</strong>,  and seeing if the first bit is 1. This can be tested mathematically with the following condtion:</p>
<p><strong>0&#215;99 &gt; 0x7F</strong></p>
<p>Once you have determined that the byte actually sets a status, you need to identify the actual status that is set. Some examples of status messages are: <strong>0xB</strong> for controller change, <strong>0xC</strong> for program change &amp; <strong>0xE</strong> for pitch-bend. To get the value of the left nibble, you can use the following expression.</p>
<p><strong>0&#215;99 &gt;&gt; 4</strong> (Shift the 4 places) or <strong>0&#215;99 / 0&#215;10</strong></p>
<p><strong>Determining the midi transmission channel.</strong></p>
<p>The second nibble (9<strong>9</strong>) of the first byte in the stream, represents the midi transmission channel. Channels are 0 indexed so they range from 0-F in hex or 0-15 in decimal. So we can tell quite easily that the transmission channel in our stream is 10 just by adding 1 to the second nibble. We can extract the 2nd nibble using the following expression:</p>
<p><strong>0&#215;99 % 0&#215;10</strong> (% = modulo)</p>
<p>and get the midi channel by adding 1 to the result</p>
<p><strong>(0&#215;99 % 0&#215;10) + 1</strong></p>
<p><strong><br />
</strong></p>
<p><strong>Determining the note number and velocity. </strong></p>
<p><strong><span style="font-weight: normal;">These are pretty simple. Just convert the Hex values to a decimal values</span></strong></p>
<p><strong><span style="font-weight: normal;">0x3c = 60</span></strong></p>
<p><strong><span style="font-weight: normal;">0&#215;64 = 100</span></strong></p>
<p>I&#8217;ll post my parser here when I&#8217;m done with it this weekend.</p>
]]></content:encoded>
			<wfw:commentRss>http://trnsfrmr.com/2010/03/12/bytes-nibbles-bits-for-coremidi/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<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>1</slash:comments>
		</item>
	</channel>
</rss>

