<?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>Karol Krizka &#187; Projects</title>
	<atom:link href="http://www.krizka.net/category/projects/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.krizka.net</link>
	<description>A geek's view of the world...</description>
	<lastBuildDate>Wed, 30 Jun 2010 19:36:27 +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>Generating Random Music</title>
		<link>http://www.krizka.net/2010/03/09/generating-random-music/</link>
		<comments>http://www.krizka.net/2010/03/09/generating-random-music/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 19:23:13 +0000</pubDate>
		<dc:creator>Karol Krizka</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[libmidi]]></category>

		<guid isPermaLink="false">http://www.krizka.net/?p=1123</guid>
		<description><![CDATA[A while ago, I wrote a tutorial on how to get the NBC/NXC running on Linux in order to program an Mindstorm NXT brick. In the tutorial, I posted an example script that turns the robot into a dancer and plays some music. The only problem is that the music was just set of random [...]]]></description>
			<content:encoded><![CDATA[<div class="KonaBody">
<p>A while ago, I wrote a <a href="http://www.krizka.net/2009/12/27/starting-mindstorm-nxt-2-0-development-on-linux/">tutorial on how to get the NBC/NXC running on Linux in order to program an Mindstorm NXT brick</a>. In the tutorial, I posted an example script that turns the robot into a dancer and plays some music. The only problem is that the music was just set of random frequencies and thus was unbearable to listen to. So I though, is there a way to make it less random without the need to make it play the same thing over and over again?</p>
<p>Experimenting with this idea lead me to write the <a href="http://www.krizka.net/projects/libmidi/">libmidi library</a>. MIDI files are a collection of notes, so it seemed as the best format to use. It allowed me to just generate random notes and tell the MIDI player to play them in some order. The library also includes an example called <em>randommusic</em>, but just does the same thing as the dancing robot. It just plays 128 random notes. This is what it sounds like:</p>
<p>I am no expert on music theory, but from what I understand most songs contain only notes in one scale. And the idea also works the other way, playing notes in a scale can result in a song. The song won&#8217;t be a masterpiece, but it will be bearable. Since I am too lazy to pre-program every possible scale, I decided that I will only limit notes to ones found in an existing song instead of a scale.</p>
<p>After analyzing Piano Sonata No.1, that I grabbed from <a href="http://www.kunstderfuge.com/">Kunst der Fuge</a> some time ago, I get the following result:</p>
<p>It sounds much better than before, but it still sounds like it is all over the place. To fix this, I applied one more change to how the next note is picked by looking at one more piece of information when analyzing the song; the note that was played previously. That is if note B never follows note A in the existing song, then it should never do so in the random music. Here is the result:</p>
<p>That was a big improvement! I think that is where I will stop working on this. But more tweaks could be done in the future:</p>
<ul>
<li>Add random rhythm</li>
<li>Analyze multiple songs</li>
</ul>
<p>Of course, the code still needs to be ported to NXT to make a much more improved dancer robot.</p>
<p>The script that I wrote to do this can be downloaded here and is released under the GPL3 license. To use it, you need to have <a href="http://www.krizka.net/projects/libmidi/">libmidi</a> and ROOT installed. This is how it works:</p>
<ol>
<li>Analyze the song</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>analyze input.mid output.root</pre></div></div>

</li>
<li>Generate a random tune.
<ul>
<li>The first argument is the track of the orignal song to analyze, and should be set as <em>track1</em>, <em>track2</em> and so on. Also you can set it to <em>song</em>, which uses all tracks.</li>
<li>The second argument is the ROOT file with the analyzed song created in step 1</li>
<li>The third argument is the output MIDI where the result will be saved</li>
<li>The fourth argument is optional. Specifying it will ignore any previous note conditional probability.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>generate track input.root output.mid <span style="color: #7a0874; font-weight: bold;">&#91;</span>unconditional<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.krizka.net/2010/03/09/generating-random-music/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://www.krizka.net/wp-content/uploads/2010/02/conditional.mp3" length="3130215" type="audio/mpeg" />
<enclosure url="http://www.krizka.net/wp-content/uploads/2010/02/unconditional.mp3" length="3110520" type="audio/mpeg" />
<enclosure url="http://www.krizka.net/wp-content/uploads/2010/02/totallyrandommusic.mp3" length="471409" type="audio/mpeg" />
		</item>
		<item>
		<title>Announcing libmidi, A Very Simple MIDI File Library</title>
		<link>http://www.krizka.net/2010/01/05/announcing-libmidi-a-very-simple-midi-file-library/</link>
		<comments>http://www.krizka.net/2010/01/05/announcing-libmidi-a-very-simple-midi-file-library/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 01:13:38 +0000</pubDate>
		<dc:creator>Karol Krizka</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[libmidi]]></category>

		<guid isPermaLink="false">http://www.krizka.net/?p=1106</guid>
		<description><![CDATA[I spent the last few days of my winter vacation experimenting with generating random music. This was inspired by my simple NXT Dancer robot, which currently just plays random tones. The idea behind this latest experiment is that (1) I build a probability density function of played notes from well known music and (2) I [...]]]></description>
			<content:encoded><![CDATA[<div class="KonaBody">
<p>I spent the last few days of my winter vacation experimenting with generating random music. This was inspired by my simple <a href="http://www.krizka.net/2009/12/27/starting-mindstorm-nxt-2-0-development-on-linux/">NXT Dancer robot</a>, which currently just plays random tones. The idea behind this latest experiment is that (1) I build a probability density function of played notes from well known music and (2) I use that PDF to pick the notes to be played next, based on the previous note. I will post more details about this in a few days. But the important part is that I used MIDI files, which contain commands like &#8220;play this note&#8221; and &#8220;use this volume&#8221; to recreate the actual music.</p>
<p>Being lazy, I decided not to search a lot (I trie a bit) for a pre-made library for reading MIDI files and instead I decided to create my own. The result is <a href="http://www.krizka.net/projects/libmidi/">libmidi</a>, a very simple MIDI reading and writing library. To parse a MIDI file, just do this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">MIDI<span style="color: #008080;">::</span><span style="color: #007788;">File</span> file<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;/path/to/file.mid&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
file.<span style="color: #007788;">open</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Then the contents of that file are available as they were found in the MIDI file. For example, to get a track and print out all of the events (the commands that tell the <em>MIDI synchronizer</em> what to do), do this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">MIDI<span style="color: #008080;">::</span><span style="color: #007788;">Track</span><span style="color: #000040;">*</span> track<span style="color: #000080;">=</span>file.<span style="color: #007788;">track</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>j<span style="color: #000080;">&lt;</span>track<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>numEvents<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>j<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   MIDI<span style="color: #008080;">::</span><span style="color: #007788;">Event</span><span style="color: #000040;">*</span> event<span style="color: #000080;">=</span>track<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>event<span style="color: #008000;">&#40;</span>j<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   event<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>debug<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Also <a href="http://www.krizka.net/projects/libmidi/">libmidi</a> contains a basic write support. You can add new events to the end of a track and call the MIDI::File::write() function to save the file that the track represents. See the <em>randommusic</em> example, included in the libmidi-0.1 package, on how to do this.</p>
<p>The downside to using the <a href="http://www.krizka.net/projects/libmidi/">libmidi</a> library right now is that it is not well developed yet. For example, there are only a few functions to modify the MIDI file (ei: tracks can only be appended to the end of the track) and not all meta event types have a nice define yet. Of course, I plan to fix this in the future.</p>
<p>But for now, you can <a href="http://krizka.net/download/libmidi-0.1.tar.bz2">download version 0.1</a> or checkout the <a href="http://github.com/kkrizka/libmidi">latest development code at GitHUB</a>. The <a href="http://www.krizka.net/docs/libmidi-0.1/">API documentation is available here</a>.</p>
<p>For more information about MIDI files, check out these websites:</p>
<ul>
<li><a href="http://253.ccarh.org/handout/smf/">http://253.ccarh.org/handout/smf/</a></li>
<li><a href="http://www.piclist.com/tecHREF/io/serial/midi/midifile.html">http://www.piclist.com/tecHREF/io/serial/midi/midifile.html</a></li>
<li><a href="http://www.sonicspot.com/guide/midifiles.html">http://www.sonicspot.com/guide/midifiles.html</a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.krizka.net/2010/01/05/announcing-libmidi-a-very-simple-midi-file-library/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.241 seconds -->
