<?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; Midi</title>
	<atom:link href="http://www.lastrayofhope.com/tag/midi/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lastrayofhope.com</link>
	<description>Home of Kaluriel Hargrove</description>
	<lastBuildDate>Wed, 27 Jan 2010 21:56:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</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&#8217;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>
<pre class="brush: csharp;">
//
//
using System;

//
//
namespace Midi
{
    class Endian
    {
        //
        //
        public static UInt16 SwapUInt16( UInt16 inValue )
        {
            byte[] byteArray = BitConverter.GetBytes(inValue);
            Array.Reverse(byteArray);
            return BitConverter.ToUInt16(byteArray,0);
        }

        //
        //
        public static UInt32 SwapUInt32( UInt32 inValue )
        {
            byte[] byteArray = BitConverter.GetBytes(inValue);
            Array.Reverse(byteArray);
            return BitConverter.ToUInt32(byteArray,0);
        }
    }
}
</pre>
<p>But this method is slower than just using a bitshift.</p>
<pre class="brush: csharp;">
//
//
using System;

//
//
namespace Midi
{
    class Endian
    {
        //
        //
        public static UInt16 SwapUInt16( UInt16 inValue )
        {
            return (UInt16)( ((inValue &amp; 0xff00) &gt;&gt; 8) |
                             ((inValue &amp; 0x00ff) &lt;&lt; 8) );
        }

        //
        //
        public static UInt32 SwapUInt32( UInt32 inValue )
        {
            return (UInt32)( ((inValue &amp; 0xff000000) &gt;&gt; 24) |
                             ((inValue &amp; 0x00ff0000) &gt;&gt; 8) |
                             ((inValue &amp; 0x0000ff00) &lt;&lt; 8) |
                             ((inValue &amp; 0x000000ff) &lt;&lt; 24) );
        }
    }
}
</pre>
<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 has 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 has 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, &#8220;Set Tempo&#8221; and &#8220;Time Signature&#8221; meta events need to be handled. Also remember that the raw denominator for &#8220;Time Signature&#8221; meta events needs to be used as the exponent for raising two to a power:</p>
<pre class="brush: cpp;">
const float kTimeSignatureDenominator = powf( 2, rawTimeSignatureDenominator );
</pre>
<p>This first page will cover correctly calculating &#8220;Beats Per Minute&#8221; (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&#8217;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 &#8220;Set Tempo&#8221; 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 &#8220;Set Tempo&#8221; 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>
<pre class="brush: cpp;">
const float kOneMinuteInMicroseconds = 60000000;

// This is wrong if the time signature is not 4/4
float BPM = kOneMinuteInMicroseconds / newMicrosecondsPerQuarterNote;
</pre>
<p>So how do we solve this? We use the new time signature to scale our BPM to the correct value.</p>
<pre class="brush: cpp;">
const float kOneMinuteInMicroseconds = 60000000;
const float kTimeSignatureNumerator = 4.0f; // For show only, use the actual time signature numerator
const float kTimeSignatureDenominator = 4.0f; // For show only, use the actual time signature denominator
// This is correct
float BPM = ( kOneMinuteInMicroseconds / newMicrosecondsPerQuarterNote ) * ( kTimeSignatureDenominator / 4.0f );
</pre>
<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 &#8220;8.0f&#8221;, 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&#8217;t set.</p>
<p>If it isn&#8217;t set on the initial test, the value from that byte will be used for the value of the VLQ.</p>
<pre class="brush: csharp;">
//
//
using System;

//
//
namespace Midi
{
    class VariableLengthQuantity
    {
        //
        // Functions
        public VariableLengthQuantity( byte[] inVariableData, int inOffset )
        {
            int offset = inOffset;

            // At least one byte is always used
            m_value = inVariableData[offset];
            m_numBytes = 1;
            ++offset;

            //
            if( ((m_value &amp; 0x80) != 0) )
            {
                UInt32 c;

                //
                m_value &amp;= 0x7F;

                //
                do
                {
                    //
                    c = inVariableData[offset];

                    //
                    m_value = (m_value &lt;&lt; 7) + (c &amp; 0x7F);

                    //
                    ++m_numBytes;
                    ++offset;
                } while( (c &amp; 0x80) != 0 );
            }
        }

        //
        //
        public UInt32 Value
        {
            get
            {
                return m_value;
            }
        }

        //
        //
        public UInt32 NumBytes
        {
            get
            {
                return m_numBytes;
            }
        }

        //
        // Attributes
        UInt32 m_value;
        UInt32 m_numBytes;
    }
}
</pre>
]]></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>
	</channel>
</rss>
