<?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>Last Ray of Hope &#187; .NET</title>
	<atom:link href="http://www.lastrayofhope.com/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lastrayofhope.com</link>
	<description>Home of Kaluriel Hargrove</description>
	<lastBuildDate>Thu, 02 Feb 2012 00:21:18 +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>C# &#8211; Endian Swap</title>
		<link>http://www.lastrayofhope.com/2010/01/03/csharp-endian-swap/</link>
		<comments>http://www.lastrayofhope.com/2010/01/03/csharp-endian-swap/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 16:37:16 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Csharp]]></category>
		<category><![CDATA[Midi]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2883</guid>
		<description><![CDATA[In 1726, there were tensions between Lilliput and Blefuscu, the problem being soft-boiled eggs. The inhabitants of Lilliput crack open their eggs at the small end, while the inhabitants of Blefuscu crack open theirs at the big end. In 2010, while writing a Midi file parser in C#, I needed the ability to swap the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2010/01/gullivers_travels.jpg"><img class="alignleft size-medium wp-image-2894" title="Gulliver's Travels" src="http://www.lastrayofhope.com/wp-content/uploads/2010/01/gullivers_travels-184x300.jpg" alt="" width="110" height="180" /></a>In 1726, there were tensions between Lilliput and Blefuscu, the problem being soft-boiled eggs. The inhabitants of Lilliput crack open their eggs at the small end, while the inhabitants of Blefuscu crack open theirs at the big end.</p>
<p>In 2010, while writing a Midi file parser in C#, I needed the ability to swap the endian of the data, since Midi files are stored as Big Endian, converting them into Little Endian.</p>
<p>I would have thought there would be some function within the BitConverter class to be able to swap, but there didn't appear to be, so I wrote my own.</p>
<p>Since swapping endian is just reversing the byte order, the simplest method would be to get the bytes of a number, and reverse the array, then reconstruct the number using the reversed bytes.</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="csharp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw1">using</span> <span class="co3">System</span><span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw1">namespace</span> Midi
<span class="br0">&#123;</span>
<span class="kw4">class</span> Endian
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="kw1">public</span> <span class="kw1">static</span> UInt16 SwapUInt16<span class="br0">&#40;</span> UInt16 inValue <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw4">byte</span><span class="br0">&#91;</span><span class="br0">&#93;</span> byteArray <span class="sy0">=</span> BitConverter.<span class="me1">GetBytes</span><span class="br0">&#40;</span>inValue<span class="br0">&#41;</span><span class="sy0">;</span>
		Array.<span class="me1">Reverse</span><span class="br0">&#40;</span>byteArray<span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="kw1">return</span> BitConverter.<span class="me1">ToUInt16</span><span class="br0">&#40;</span>byteArray,0<span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">public</span> <span class="kw1">static</span> UInt32 SwapUInt32<span class="br0">&#40;</span> UInt32 inValue <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw4">byte</span><span class="br0">&#91;</span><span class="br0">&#93;</span> byteArray <span class="sy0">=</span> BitConverter.<span class="me1">GetBytes</span><span class="br0">&#40;</span>inValue<span class="br0">&#41;</span><span class="sy0">;</span>
		Array.<span class="me1">Reverse</span><span class="br0">&#40;</span>byteArray<span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="kw1">return</span> BitConverter.<span class="me1">ToUInt32</span><span class="br0">&#40;</span>byteArray,0<span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>But this method is slower than just using a bitshift.</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="csharp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw1">using</span> <span class="co3">System</span><span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw1">namespace</span> Midi
<span class="br0">&#123;</span>
<span class="kw4">class</span> Endian
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="kw1">public</span> <span class="kw1">static</span> UInt16 SwapUInt16<span class="br0">&#40;</span> UInt16 inValue <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">return</span> <span class="br0">&#40;</span>UInt16<span class="br0">&#41;</span><span class="br0">&#40;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>inValue <span class="sy0">&amp;</span> 0xff00<span class="br0">&#41;</span> <span class="sy0">&gt;&gt;</span> 8<span class="br0">&#41;</span> <span class="sy0">|</span>
				 <span class="br0">&#40;</span><span class="br0">&#40;</span>inValue <span class="sy0">&amp;</span> 0x00ff<span class="br0">&#41;</span> <span class="sy0">&lt;&lt;</span> 8<span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">public</span> <span class="kw1">static</span> UInt32 SwapUInt32<span class="br0">&#40;</span> UInt32 inValue <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw1">return</span> <span class="br0">&#40;</span>UInt32<span class="br0">&#41;</span><span class="br0">&#40;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>inValue <span class="sy0">&amp;</span> 0xff000000<span class="br0">&#41;</span> <span class="sy0">&gt;&gt;</span> 24<span class="br0">&#41;</span> <span class="sy0">|</span>
				 <span class="br0">&#40;</span><span class="br0">&#40;</span>inValue <span class="sy0">&amp;</span> 0x00ff0000<span class="br0">&#41;</span> <span class="sy0">&gt;&gt;</span> 8<span class="br0">&#41;</span> <span class="sy0">|</span>
				 <span class="br0">&#40;</span><span class="br0">&#40;</span>inValue <span class="sy0">&amp;</span> 0x0000ff00<span class="br0">&#41;</span> <span class="sy0">&lt;&lt;</span> 8<span class="br0">&#41;</span> <span class="sy0">|</span>
				 <span class="br0">&#40;</span><span class="br0">&#40;</span>inValue <span class="sy0">&amp;</span> 0x000000ff<span class="br0">&#41;</span> <span class="sy0">&lt;&lt;</span> 24<span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
	<span class="br0">&#125;</span>
<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>Since any the there are only 16 bit and 32 bit numbers in the Midi file format, and anything larger is of Variable Length Quantity, these are the only two required.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2010/01/03/csharp-endian-swap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Midi Delta Time Ticks to Seconds</title>
		<link>http://www.lastrayofhope.com/2009/12/23/midi-delta-time-ticks-to-seconds/</link>
		<comments>http://www.lastrayofhope.com/2009/12/23/midi-delta-time-ticks-to-seconds/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 17:56:12 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Csharp]]></category>
		<category><![CDATA[Midi]]></category>
		<category><![CDATA[Music]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2915</guid>
		<description><![CDATA[One of the main problems with parsing midi files, is the amount of bad documentation people have put on their websites for converting delta time ticks into seconds or milliseconds. Eventually I got ahold of the Midi Standard 1.0 which helped me solve this riddle for when calculating it myself, and will hopefully help others [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/12/3_quarter_time.gif"><img class="alignleft size-full wp-image-2921" title="3 Quarter Time" src="http://www.lastrayofhope.com/wp-content/uploads/2009/12/3_quarter_time.gif" alt="" width="193" height="80" /></a>One of the main problems with parsing midi files, is the amount of bad documentation people have put on their websites for converting delta time ticks into seconds or milliseconds.</p>
<p>Eventually I got ahold of the Midi Standard 1.0 which helped me solve this riddle for when calculating it myself, and will hopefully help others who have this problem.</p>
<p>When calculating delta time, "Set Tempo" and "Time Signature" meta events need to be handled. Also remember that the raw denominator for "Time Signature" meta events needs to be used as the exponent for raising two to a power:</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="kw4">const</span> <span class="kw4">float</span> kTimeSignatureDenominator <span class="sy1">=</span> powf<span class="br0">&#40;</span> 2, rawTimeSignatureDenominator <span class="br0">&#41;</span><span class="sy4">;</span></pre></div></div>
<p>&nbsp;</p>
<p>This first page will cover correctly calculating "Beats Per Minute" (BPM) correctly. And although a lot of examples seem to think you require this to convert delta time ticks to seconds or milliseconds, you don't.</p>
<h1>What is a Beat?</h1>
<p>In music, there is something called a <strong>Bar</strong>, which is a segment of time defined by a given number of beats of a given duration. The values which define a <strong>Bar</strong>, are called the <strong>Time Signature</strong>.</p>
<p>A <strong>Time Signature</strong>, is two numbers, one on top of the other. The numerator describes the number of <strong>Beats</strong> in a <strong>Bar</strong>, while the denominator describes of what note value a <strong>Beat</strong> is.</p>
<p>So 4/4 would be four <strong>quarter-notes</strong> per <strong>Bar</strong>, while 4/2 would be four <strong>half-notes</strong> per <strong>Bar</strong>, 4/8 would be four <strong>eighth-notes</strong> per <strong>Bar</strong>, and 2/4 would be two <strong>quarter-notes</strong> per <strong>Bar</strong>.</p>
<h1>Calculating BPM</h1>
<p>Now a lot of examples say to default the tempo to 120 BPM, and the time signature to 4/4, which is correct. But when handling the "Set Tempo" meta event, the same examples just use the value straight out to calculate the BPM, which is incorrect if the time signature has changed.</p>
<p>The "Set Tempo" meta event in midi only deals in quarter notes, so if the time signature is 4/8, a quarter-note is not a Beat since its described as an eighth-note, so using it to calculate BPM  on its own is incorrect.</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="kw4">const</span> <span class="kw4">float</span> kOneMinuteInMicroseconds <span class="sy1">=</span> <span class="nu0">60000000</span><span class="sy4">;</span>
&nbsp;
<span class="co1">// This is wrong if the time signature is not 4/4</span>
<span class="kw4">float</span> BPM <span class="sy1">=</span> kOneMinuteInMicroseconds <span class="sy2">/</span> newMicrosecondsPerQuarterNote<span class="sy4">;</span></pre></div></div>
<p>&nbsp;</p>
<p>So how do we solve this? We use the new time signature to scale our BPM to the correct value.</p>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="cpp" style="font-family:monospace;"><span class="kw4">const</span> <span class="kw4">float</span> kOneMinuteInMicroseconds <span class="sy1">=</span> <span class="nu0">60000000</span><span class="sy4">;</span>
<span class="kw4">const</span> <span class="kw4">float</span> kTimeSignatureNumerator <span class="sy1">=</span> <span class="nu17">4.0f</span><span class="sy4">;</span> <span class="co1">// For show only, use the actual time signature numerator</span>
<span class="kw4">const</span> <span class="kw4">float</span> kTimeSignatureDenominator <span class="sy1">=</span> <span class="nu17">4.0f</span><span class="sy4">;</span> <span class="co1">// For show only, use the actual time signature denominator</span>
<span class="co1">// This is correct</span>
<span class="kw4">float</span> BPM <span class="sy1">=</span> <span class="br0">&#40;</span> kOneMinuteInMicroseconds <span class="sy2">/</span> newMicrosecondsPerQuarterNote <span class="br0">&#41;</span> <span class="sy2">*</span> <span class="br0">&#40;</span> kTimeSignatureDenominator <span class="sy2">/</span> 4.0f <span class="br0">&#41;</span><span class="sy4">;</span></pre></div></div>
<p>&nbsp;</p>
<p>In the code above, we divide the time signature denominator by four (The number four is the denominator value for quarter note), this gives us the number of notes per quarter note. Four divided by four is one, so the BPM remains the same.</p>
<p>If the value of kTimeSignatureDenominator was "8.0f", then eight divided by four is two (there are two eighth-notes in a quarter note).</p>
<p>The next page will cover the actual conversion of delta time ticks to seconds (or milliseconds).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/12/23/midi-delta-time-ticks-to-seconds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# &#8211; Midi Variable Length Quantity</title>
		<link>http://www.lastrayofhope.com/2009/12/22/c-midi-variable-length-quantity/</link>
		<comments>http://www.lastrayofhope.com/2009/12/22/c-midi-variable-length-quantity/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 17:31:22 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Csharp]]></category>
		<category><![CDATA[Midi]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2900</guid>
		<description><![CDATA[On the way home on the train for Christmas, I began porting my Midi parsing class over to C#. The snippet below shows how to read to read a Variable Length Quantity (sometimes known as a Variable Length Value). The way it works is very simple, it checks the first byte to see if the [...]]]></description>
			<content:encoded><![CDATA[<p>On the way home on the train for Christmas, I began porting my Midi parsing class over to C#.</p>
<p>The snippet below shows how to read to read a Variable Length Quantity (sometimes known as a Variable Length Value).</p>
<p>The way it works is very simple, it checks the first byte to see if the MSB is set, if it set it will keep iterating, moving along one byte and doing the same, shifting the previous value across 7 bits until the MSB isn't set.</p>
<p>If it isn't set on the initial test, the value from that byte will be used for the value of the VLQ.</p>
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="csharp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw1">using</span> <span class="co3">System</span><span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw1">namespace</span> Midi
<span class="br0">&#123;</span>
<span class="kw4">class</span> VariableLengthQuantity
<span class="br0">&#123;</span>
	<span class="co1">//</span>
	<span class="co1">// Functions</span>
	<span class="kw1">public</span> VariableLengthQuantity<span class="br0">&#40;</span> <span class="kw4">byte</span><span class="br0">&#91;</span><span class="br0">&#93;</span> inVariableData, <span class="kw4">int</span> inOffset <span class="br0">&#41;</span>
	<span class="br0">&#123;</span>
		<span class="kw4">int</span> offset <span class="sy0">=</span> inOffset<span class="sy0">;</span>
&nbsp;
		<span class="co1">// At least one byte is always used</span>
		m_value <span class="sy0">=</span> inVariableData<span class="br0">&#91;</span>offset<span class="br0">&#93;</span><span class="sy0">;</span>
		m_numBytes <span class="sy0">=</span> <span class="nu0">1</span><span class="sy0">;</span>
		<span class="sy0">++</span>offset<span class="sy0">;</span>
&nbsp;
		<span class="co1">//</span>
		<span class="kw1">if</span><span class="br0">&#40;</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>m_value <span class="sy0">&amp;</span> 0x80<span class="br0">&#41;</span> <span class="sy0">!=</span> 0<span class="br0">&#41;</span> <span class="br0">&#41;</span>
		<span class="br0">&#123;</span>
			UInt32 c<span class="sy0">;</span>
&nbsp;
			<span class="co1">//</span>
			m_value <span class="sy0">&amp;=</span> 0x7F<span class="sy0">;</span>
&nbsp;
			<span class="co1">//</span>
			<span class="kw1">do</span>
			<span class="br0">&#123;</span>
				<span class="co1">//</span>
				c <span class="sy0">=</span> inVariableData<span class="br0">&#91;</span>offset<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
				<span class="co1">//</span>
				m_value <span class="sy0">=</span> <span class="br0">&#40;</span>m_value <span class="sy0">&lt;&lt;</span> 7<span class="br0">&#41;</span> <span class="sy0">+</span> <span class="br0">&#40;</span>c <span class="sy0">&amp;</span> 0x7F<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
				<span class="co1">//</span>
				<span class="sy0">++</span>m_numBytes<span class="sy0">;</span>
				<span class="sy0">++</span>offset<span class="sy0">;</span>
			<span class="br0">&#125;</span> <span class="kw1">while</span><span class="br0">&#40;</span> <span class="br0">&#40;</span>c <span class="sy0">&amp;</span> 0x80<span class="br0">&#41;</span> <span class="sy0">!=</span> 0 <span class="br0">&#41;</span><span class="sy0">;</span>
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">public</span> UInt32 Value
	<span class="br0">&#123;</span>
		get
		<span class="br0">&#123;</span>
			<span class="kw1">return</span> m_value<span class="sy0">;</span>
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="kw1">public</span> UInt32 NumBytes
	<span class="br0">&#123;</span>
		get
		<span class="br0">&#123;</span>
			<span class="kw1">return</span> m_numBytes<span class="sy0">;</span>
		<span class="br0">&#125;</span>
	<span class="br0">&#125;</span>
&nbsp;
	<span class="co1">//</span>
	<span class="co1">// Attributes</span>
	UInt32 m_value<span class="sy0">;</span>
	UInt32 m_numBytes<span class="sy0">;</span>
<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/12/22/c-midi-variable-length-quantity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# &#8211; Mono on Mac OS X</title>
		<link>http://www.lastrayofhope.com/2009/07/05/csharp-mono-on-mac-os-x/</link>
		<comments>http://www.lastrayofhope.com/2009/07/05/csharp-mono-on-mac-os-x/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 02:34:40 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Csharp]]></category>
		<category><![CDATA[MacOSX]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[VMWare]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2557</guid>
		<description><![CDATA[I'm always up for trying new ways of not having to boot up VMWare Fusion, and when searching for C# code, I came by a blog about something using Mono. Mono is a cross platform solution for using .NET and it is open source, which means I can write, compile and execute C# in MacOSX. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/07/Mono_project_logo.png"><img class="alignleft size-medium wp-image-2620" title="Mono Project Logo" src="http://www.lastrayofhope.com/wp-content/uploads/2009/07/Mono_project_logo-251x300.png" alt="Mono Project Logo" width="151" height="180" /></a>I'm always up for trying new ways of not having to boot up VMWare Fusion, and when searching for C# code, I came by a blog about something using <a href="http://mono-project.com/Main_Page" target="_blank">Mono</a>. Mono is a cross platform solution for using .NET and it is open source, which means I can write, compile and execute C# in MacOSX.</p>
<p>I found its strangely easy to setup, though I have yet to get WinForms to work from the examples, I have got console apps working. And with the console app, I have been able to use XPath.</p>
<p>The only downside was it took an hour to download from their website, which is quite bad for only 50MB.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/07/05/csharp-mono-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# &#8211; XPath and Converting a Relative Path to Absolute</title>
		<link>http://www.lastrayofhope.com/2009/07/04/csharp-xpath-and-converting-a-relative-path-to-absolute/</link>
		<comments>http://www.lastrayofhope.com/2009/07/04/csharp-xpath-and-converting-a-relative-path-to-absolute/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 22:03:34 +0000</pubDate>
		<dc:creator>Kaluriel</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Csharp]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.lastrayofhope.com/?p=2550</guid>
		<description><![CDATA[Today whilst doing some C# programming, I came by a path within a file that was relative to the path I was reading it from. Since my application wasn't in the same place, and I didn't want it to be be for it to be, I needed to convert it to an absolute path. This [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lastrayofhope.com/wp-content/uploads/2009/07/star_ocean_the_last_hope.jpg"><img class="alignleft size-medium wp-image-2551" title="Star Ocean - The Last Hope" src="http://www.lastrayofhope.com/wp-content/uploads/2009/07/star_ocean_the_last_hope-300x168.jpg" alt="Star Ocean - The Last Hope" width="180" height="101" /></a>Today whilst doing some C# programming, I came by a path within a file that was relative to the path I was reading it from. Since my application wasn't in the same place, and I didn't want it to be be for it to be, I needed to convert it to an absolute path.</p>
<p>This is C#, it should be simple right?</p>
<p>Well apparently <strong>System.IO.Path.Combine()</strong> doesn't take into account "./" or "../" and just concatenates and added an additional forward slash if needed.</p>
<p>After a bit of searching, and many different examples (most of which just converted absolute to relative), I found what I needed. This method used <strong>System.IO.Path.GetFullPath()</strong> with <strong>System.IO.Path.Combine()</strong> (If I used the relative path on its own with GetFullPath() I just got the relative path from the current executable directory).</p>
<p>I made it into a single function I can call but I was surprised that there wasn't something already to do this considering what C# has impressed me with so far for thinking ahead.</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="csharp" style="font-family:monospace;"><span class="co1">//</span>
<span class="co1">//</span>
<span class="kw1">using</span> <span class="co3">System.IO</span><span class="sy0">;</span>
&nbsp;
<span class="co1">//</span>
<span class="co1">//</span>
<span class="kw1">static</span> <span class="kw4">string</span> GetAbsoluteFromRelative<span class="br0">&#40;</span> <span class="kw4">string</span> inBasePath, <span class="kw4">string</span> inRelativePath <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw1">return</span> Path.<span class="me1">GetFullPath</span><span class="br0">&#40;</span> Path.<span class="me1">Combine</span><span class="br0">&#40;</span> inBasePath, inRelativePath <span class="br0">&#41;</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>It hasn't tainted my image of C# though, I still adore it for its XML XPath node iterator library thing that has MySQL searching features.</p>
<p>I also found a spiffy thing I didn't know about XPath as well. If I have a set of data like so.</p>
<blockquote><p>&lt;FileList&gt;</p>
<p style="padding-left: 30px;">&lt;Folder&gt;</p>
<p style="padding-left: 60px;">&lt;File&gt;MyFile&lt;/File&gt;</p>
<p style="padding-left: 30px;">&lt;/Folder&gt;</p>
<p style="padding-left: 30px;">&lt;File&gt;MyOtherFile&lt;/File&gt;</p>
<p>&lt;/FileList&gt;</p></blockquote>
<p>I can iterate through all file nodes within file list with a double-slash before "File" within my node select call.</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.lastrayofhope.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="csharp" style="font-family:monospace;">XPathNavigator nav<span class="sy0">;</span>
&nbsp;
<span class="co1">// ...</span>
&nbsp;
XPathNodeIterator fileNodes <span class="sy0">=</span> nav.<span class="me1">Select</span><span class="br0">&#40;</span><span class="st0">&quot;/FileList//File&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="kw1">while</span><span class="br0">&#40;</span> fileNodes.<span class="me1">MoveNext</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	Console.<span class="me1">WriteLine</span><span class="br0">&#40;</span> fileNodes.<span class="me1">Current</span>.<span class="me1">Value</span> <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>
<p>&nbsp;</p>
<p>I still seem to have problems with namespaces in some XML documents though (ones without them seem to make searching a hassle).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lastrayofhope.com/2009/07/04/csharp-xpath-and-converting-a-relative-path-to-absolute/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

