<?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>There's Something About Code</title>
	<atom:link href="http://blog.somethingaboutcode.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://blog.somethingaboutcode.com</link>
	<description>Tools, languages and nifty libraries. Occasional rants.</description>
	<lastBuildDate>Wed, 17 Jun 2009 18:16:50 +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>Twisted HTTP proxy</title>
		<link>http://blog.somethingaboutcode.com/?p=155</link>
		<comments>http://blog.somethingaboutcode.com/?p=155#comments</comments>
		<pubDate>Wed, 17 Jun 2009 18:15:15 +0000</pubDate>
		<dc:creator>Knut Eldhuset</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[twisted]]></category>

		<guid isPermaLink="false">http://blog.somethingaboutcode.com/?p=155</guid>
		<description><![CDATA[A couple of years ago I wrote an HTTP proxy that would save all viewed web pages. It was written in Java and used a relational database as storage. It had a streaming architecture, which meant that complete requests and responses were not kept in memory, but rather flowed through the proxy. This meant that [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of years ago I wrote an HTTP proxy that would save all viewed web pages. It was written in Java and used a relational database as storage. It had a streaming architecture, which meant that complete requests and responses were not kept in memory, but rather flowed through the proxy. This meant that it could handle responses of any size, including large file downloads, without using much memory. You could write filters that would modify requests and responses, as well as block them completely. Storing web pages was implemented as a special filter. </p>
<p>I never got around to implementing all the features I wanted. Hoping to change that, I am taking a shot at implementing an HTTP proxy in Python. Obviously, socket programming is needed on some level. While my original Java proxy used asynchronous sockets directly, I am thinking about using the Twisted framework in Python. <a href="http://twistedmatrix.com/trac/">Twisted</a> is &#8220;an event-driven networking engine written in Python.&#8221;  It provides lots of different protocol implementations, including HTTP. In fact, there is a simple HTTP proxy, too. Starting a proxy instance is easy:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> http
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span>.<span style="color: black;">proxy</span> <span style="color: #ff7700;font-weight:bold;">import</span> Proxy
&nbsp;
factory = http.<span style="color: black;">HTTPFactory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
factory.<span style="color: black;">protocol</span> = Proxy
&nbsp;
reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">8000</span>, factory<span style="color: black;">&#41;</span>
reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>However, a this proxy isn&#8217;t very interesting. If we would like to print every site that is accessed through the proxy, we could subclass it like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> http
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span>.<span style="color: black;">proxy</span> <span style="color: #ff7700;font-weight:bold;">import</span> Proxy, ProxyRequest
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> VerboseProxyRequest<span style="color: black;">&#40;</span>ProxyRequest<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> process<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">self</span>.<span style="color: black;">uri</span>
        ProxyRequest.<span style="color: black;">process</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> VerboseProxy<span style="color: black;">&#40;</span>Proxy<span style="color: black;">&#41;</span>:
    requestFactory = VerboseProxyRequest
&nbsp;
factory = http.<span style="color: black;">HTTPFactory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
factory.<span style="color: black;">protocol</span> = VerboseProxy
&nbsp;
reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">8000</span>, factory<span style="color: black;">&#41;</span>
reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The <code>ProxyRequest</code> implementation of <code>process</code> makes a new HTTP client, an instance of the class <code>ProxyRequestClient</code>. The HTTP client will simply forward anything it receives to the response transport channel of the parent <code>ProxyRequest</code> instance. Creating a simple ad blocker is easy:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> http
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span>.<span style="color: black;">proxy</span> <span style="color: #ff7700;font-weight:bold;">import</span> Proxy, ProxyRequest
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> BlockingProxyRequest<span style="color: black;">&#40;</span>ProxyRequest<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> process<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">&quot;ads&quot;</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">uri</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Blocked:&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">uri</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;HTTP/1.0 404 Not found<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Content-Type: text/html<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span><span style="color: #483d8b;">'&lt;H1&gt;Resource not found&lt;/H1&gt;'</span><span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">transport</span>.<span style="color: black;">loseConnection</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        ProxyRequest.<span style="color: black;">process</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> BlockingProxy<span style="color: black;">&#40;</span>Proxy<span style="color: black;">&#41;</span>:
    requestFactory = BlockingProxyRequest
&nbsp;
factory = http.<span style="color: black;">HTTPFactory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
factory.<span style="color: black;">protocol</span> = BlockingProxy
&nbsp;
reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">8000</span>, factory<span style="color: black;">&#41;</span>
reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>This code will block any request that has the string &#8220;ads&#8221; in the URI. This may be overly aggressive for a real ad blocker, though.</p>
<p>Twisted handles all the details of the HTTP requests and responses, as well as the underlying transport protocol. One drawback is that the provided HTTP client implementation does not support HTTP 1.1 yet. This may affect performance, but will not be an issue for my uses.</p>
<p>To get at the response data, we need to subclass the <code>ProxyClient</code> class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> http
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span>.<span style="color: black;">proxy</span> <span style="color: #ff7700;font-weight:bold;">import</span> Proxy, ProxyRequest, ProxyClientFactory, ProxyClient
<span style="color: #ff7700;font-weight:bold;">from</span> ImageFile <span style="color: #ff7700;font-weight:bold;">import</span> Parser
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">StringIO</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">StringIO</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> InterceptingProxyClient<span style="color: black;">&#40;</span>ProxyClient<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        ProxyClient.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">image_parser</span> = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> handleHeader<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, value<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> key == <span style="color: #483d8b;">&quot;Content-Type&quot;</span> <span style="color: #ff7700;font-weight:bold;">and</span> value <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;image/jpeg&quot;</span>, <span style="color: #483d8b;">&quot;image/gif&quot;</span>, <span style="color: #483d8b;">&quot;image/png&quot;</span><span style="color: black;">&#93;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">image_parser</span> = Parser<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> key == <span style="color: #483d8b;">&quot;Content-Length&quot;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">self</span>.<span style="color: black;">image_parser</span>:
            <span style="color: #ff7700;font-weight:bold;">pass</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            ProxyClient.<span style="color: black;">handleHeader</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, value<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> handleEndHeaders<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">image_parser</span>:
            <span style="color: #ff7700;font-weight:bold;">pass</span> <span style="color: #808080; font-style: italic;">#Need to calculate and send Content-Length first</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            ProxyClient.<span style="color: black;">handleEndHeaders</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> handleResponsePart<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, buffer<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">image_parser</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">image_parser</span>.<span style="color: black;">feed</span><span style="color: black;">&#40;</span>buffer<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            ProxyClient.<span style="color: black;">handleResponsePart</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, buffer<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> handleResponseEnd<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">image_parser</span>:
            image = <span style="color: #008000;">self</span>.<span style="color: black;">image_parser</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                format = image.<span style="color: black;">format</span>
                image = image.<span style="color: black;">rotate</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">180</span><span style="color: black;">&#41;</span>
                s = <span style="color: #dc143c;">StringIO</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                image.<span style="color: black;">save</span><span style="color: black;">&#40;</span>s, format<span style="color: black;">&#41;</span>
                buffer = s.<span style="color: black;">getvalue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span>:
                buffer = <span style="color: #483d8b;">&quot;&quot;</span>
            ProxyClient.<span style="color: black;">handleHeader</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">&quot;Content-Length&quot;</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>buffer<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            ProxyClient.<span style="color: black;">handleEndHeaders</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
            ProxyClient.<span style="color: black;">handleResponsePart</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, buffer<span style="color: black;">&#41;</span>
        ProxyClient.<span style="color: black;">handleResponseEnd</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> InterceptingProxyClientFactory<span style="color: black;">&#40;</span>ProxyClientFactory<span style="color: black;">&#41;</span>:
    protocol = InterceptingProxyClient
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> InterceptingProxyRequest<span style="color: black;">&#40;</span>ProxyRequest<span style="color: black;">&#41;</span>:
    protocols = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'http'</span>: InterceptingProxyClientFactory<span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> InterceptingProxy<span style="color: black;">&#40;</span>Proxy<span style="color: black;">&#41;</span>:
    requestFactory = InterceptingProxyRequest
&nbsp;
factory = http.<span style="color: black;">HTTPFactory</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
factory.<span style="color: black;">protocol</span> = InterceptingProxy
&nbsp;
reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">8000</span>, factory<span style="color: black;">&#41;</span>
reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>This proxy will rotate JPEG, GIF and PNG files 180 degrees, turning a Google image search into this:<br />
<img src="http://blog.somethingaboutcode.com/wp-content/uploads/2009/06/picture-51.png" alt="Upside-down Google image search" title="Upside-down Google image search" width="839" height="415" class="alignnone size-full wp-image-170" /><br />
Fun, but not very useful. It would have been just as easy to store the image, as well as other content, to disk or a database, but that is a topic for another blog post.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.somethingaboutcode.com/?feed=rss2&amp;p=155</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python audio output</title>
		<link>http://blog.somethingaboutcode.com/?p=10</link>
		<comments>http://blog.somethingaboutcode.com/?p=10#comments</comments>
		<pubDate>Mon, 01 Jun 2009 08:01:24 +0000</pubDate>
		<dc:creator>Knut Eldhuset</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[audioop]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[pyglet]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.somethingaboutcode.com/?p=10</guid>
		<description><![CDATA[Playing MOD files requires outputting digital sound at sample rates proportional to the desired pitch. In the Amiga, this was accomplished by setting the sample rate of the hardware channels. Using high level audio interfaces may require setting a fixed sample rate for the lifetime of the audio channel. Converting the MOD file to a [...]]]></description>
			<content:encoded><![CDATA[<p>Playing MOD files requires outputting digital sound at sample rates proportional to the desired pitch. In the Amiga, this was accomplished by setting the sample rate of the hardware channels. Using high level audio interfaces may require setting a fixed sample rate for the lifetime of the audio channel. Converting the MOD file to a WAV file would also require a fixed sample rate. Thus, sample rate conversion is needed.</p>
<p>The most basic MOD files have four channel sequencing. When played on stereo hardware, two sequencer channels are output to each hardware channel. The sound samples need to be converted from mono to stereo, then mixed together with the other channels.</p>
<p>In the Amiga, the sequencer played a new sample each vertical blanking interval. The screen refresh rate in the PAL version of the computer was 50Hz. I have used the <a href="http://www.pyglet.org/">pyglet</a> library to play audio. By subclassing the <code>StreamingSource</code> class and providing an implementation of the <code>_get_audio_data</code> method, the timing of the sequencer takes care of itself automatically. The <code>_get_audio_data</code> method returns an audio chunk equivalent to what the Amiga played per vertical blanking interval. Pyglet will simply request more data when needed.</p>
<p>The code for the MOD player can be found <a href="http://github.com/knutel/PyModPlayer/blob/8d04e6152d2bf28b8fc9e3ee589f0ca4b191a87d/ModLib/src/modlib/player/player.py">here</a>. The code excerpts below are taken from the file player.py.</p>
<p>The Python Multimedia Services library contains functions for doing the necessary raw audio operations. The audio operations are located in the <a href="http://www.python.org/doc/2.5.1/lib/module-audioop.html">audioop</a> module. The <code>ratecv</code> function takes care of sample rate conversion:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">ratecv<span style="color: black;">&#40;</span>fragment, width, nchannels, inrate, outrate, state<span style="color: black;">&#91;</span>, weightA<span style="color: black;">&#91;</span>, weightB<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It takes an audio fragment as input, and returns the fragment converted to the desired sample rate, as well as the new state. The new state is passed as input the next time the function is invoked. Here is how it looks in the MOD player:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">def</span> _ratecv<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, sounds<span style="color: black;">&#41;</span>:
        output = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> n, <span style="color: black;">&#40;</span>sound, state<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span><span style="color: #008000;">zip</span><span style="color: black;">&#40;</span>sounds, <span style="color: #008000;">self</span>.<span style="color: black;">ratecv_state</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
                o, state = ratecv<span style="color: black;">&#40;</span>sound, <span style="color: #008000;">self</span>.<span style="color: #dc143c;">bytes</span>, <span style="color: #ff4500;">1</span>, 
                              <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">round</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>sound<span style="color: black;">&#41;</span> / <span style="color: #008000;">self</span>.<span style="color: black;">tick_time</span><span style="color: black;">&#41;</span> / <span style="color: #008000;">self</span>.<span style="color: #dc143c;">bytes</span><span style="color: black;">&#41;</span>, 
                              <span style="color: #008000;">self</span>.<span style="color: black;">rate</span>, 
                              state<span style="color: black;">&#41;</span>
                <span style="color: #808080; font-style: italic;">#Length may be off by one, so process until OK</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>o<span style="color: black;">&#41;</span> == <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">round</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">rate</span> <span style="color: #66cc66;">*</span> <span style="color: #008000;">self</span>.<span style="color: black;">tick_time</span> <span style="color: #66cc66;">*</span> <span style="color: #008000;">self</span>.<span style="color: #dc143c;">bytes</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
                    <span style="color: #ff7700;font-weight:bold;">break</span>
            output.<span style="color: black;">append</span><span style="color: black;">&#40;</span>o<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">ratecv_state</span><span style="color: black;">&#91;</span>n<span style="color: black;">&#93;</span> = state
        <span style="color: #ff7700;font-weight:bold;">return</span> output</pre></td></tr></table></div>

<p><code>Self.bytes</code> is the number of bytes per sample. The number of channels is 1, since sound is a mono sample. The <code>inrate</code> will vary according to the pitch at which the sample fragment is played back. This is controlled by the sequencer by varying the length of the fragment. The inrate is then calculated based on the fact that this particular fragment fills <code>self.tick_time</code> seconds. The state is stored in an array for later use.</p>
<p>When mixing several channels into one, one needs to make sure that there will be no clipping of the resulting sound sample. This is done by dividing by the number of channels that are to be mixed:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>63
64
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">   <span style="color: #ff7700;font-weight:bold;">def</span> _scale<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, output<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>mul<span style="color: black;">&#40;</span>o, <span style="color: #008000;">self</span>.<span style="color: #dc143c;">bytes</span>, <span style="color: #ff4500;">1.0</span> / <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>output<span style="color: black;">&#41;</span> / <span style="color: #008000;">self</span>.<span style="color: black;">channels</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> o <span style="color: #ff7700;font-weight:bold;">in</span> output<span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>The <code>mul</code> function takes care of the scaling. As all the other audioop functions, it needs to know how many bytes are user per sample.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">mul<span style="color: black;">&#40;</span>fragment, width, factor<span style="color: black;">&#41;</span></pre></div></div>

<p>The <code>tostereo</code> function takes a mono sample and returns a stereo sample. One can supply scaling factors for each of the left and right channels.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">tostereo<span style="color: black;">&#40;</span>fragment, width, lfactor, rfactor<span style="color: black;">&#41;</span></pre></div></div>

<p>This is used below to put every even numbered sequencer channel in the left stereo channel, and every odd numbered sequencer channel in the right stereo channel.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>66
67
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">def</span> _tostereo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, output<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span>tostereo<span style="color: black;">&#40;</span>o, <span style="color: #008000;">self</span>.<span style="color: #dc143c;">bytes</span>, n <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span>, <span style="color: black;">&#40;</span>n + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> n, o <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>output<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p>Putting all this together, the variable sample rate sequencer channels can be transformed into a constant sample rate stereo output:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>69
70
71
72
73
74
75
76
77
78
79
80
81
82
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">def</span> _get_audio_data<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, num_bytes<span style="color: black;">&#41;</span>:
        sound = <span style="color: #008000;">self</span>.<span style="color: black;">sequencer</span>.<span style="color: black;">tick</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> sound <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
            pyglet.<span style="color: black;">app</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span>
        <span style="color: #008000;">self</span>._mute<span style="color: black;">&#40;</span>sound<span style="color: black;">&#41;</span>
        sound = <span style="color: black;">&#91;</span>lin2lin<span style="color: black;">&#40;</span>s, <span style="color: #ff4500;">1</span>, <span style="color: #008000;">self</span>.<span style="color: #dc143c;">bytes</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> s <span style="color: #ff7700;font-weight:bold;">in</span> sound<span style="color: black;">&#93;</span>
        output = <span style="color: #008000;">self</span>._ratecv<span style="color: black;">&#40;</span>sound<span style="color: black;">&#41;</span>
        output = <span style="color: #008000;">self</span>._scale<span style="color: black;">&#40;</span>output<span style="color: black;">&#41;</span>
        output = <span style="color: #008000;">self</span>._tostereo<span style="color: black;">&#40;</span>output<span style="color: black;">&#41;</span>
        stereo = mix<span style="color: black;">&#40;</span>output, <span style="color: #008000;">self</span>.<span style="color: #dc143c;">bytes</span><span style="color: black;">&#41;</span>
        audio = AudioData<span style="color: black;">&#40;</span>stereo, <span style="color: #008000;">self</span>.<span style="color: black;">audio_length</span>, <span style="color: #008000;">self</span>.<span style="color: black;">timestamp</span>, <span style="color: #008000;">self</span>.<span style="color: black;">tick_time</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">timestamp</span> += <span style="color: #008000;">self</span>.<span style="color: black;">tick_time</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> audio</pre></td></tr></table></div>

<p>For a standard MOD file, the sequencer returns 4 samples per tick. These are converted from 8 bit to 16 bit using the <code>lin2lin</code> function:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">lin2lin<span style="color: black;">&#40;</span>fragment, width, newwidth<span style="color: black;">&#41;</span></pre></div></div>

<p>The samples are then rate converted, scaled and converted to stereo. The <code>mix</code> function is a helper function to mix a list of samples. The audioop module only provides an <code>add</code> function to mix two channels, so I made the helper function to mix an arbitrary number of channels. The method ends with creating an AudioData object with the parameters needed for pyglet to play the sound.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.somethingaboutcode.com/?feed=rss2&amp;p=10</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Parsing binary files using Construct</title>
		<link>http://blog.somethingaboutcode.com/?p=8</link>
		<comments>http://blog.somethingaboutcode.com/?p=8#comments</comments>
		<pubDate>Tue, 19 May 2009 10:00:57 +0000</pubDate>
		<dc:creator>Knut Eldhuset</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[construct]]></category>
		<category><![CDATA[mod]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.somethingaboutcode.com/?p=8</guid>
		<description><![CDATA[Someone over at stackoverflow recommended using the Python library Construct for parsing binary files. Python already provides the struct module for parsing binary string data, but this gives a flat view of the data and does not reflect structure. Construct promises a more intuitive way of making a parser. Always happy to discover a new, [...]]]></description>
			<content:encoded><![CDATA[<p>Someone over at <a href="http://stackoverflow.com/">stackoverflow</a> <a href="http://stackoverflow.com/questions/5415/convert-bytes-to-floating-point-numbers-in-python/74095#74095">recommended</a> using the Python library <a href="http://construct.wikispaces.com/">Construct</a> for parsing binary files. Python already provides the struct module for parsing binary string data, but this gives a flat view of the data and does not reflect structure. Construct promises a more intuitive way of making a parser. Always happy to discover a new, shiny hammer, I decided to try it out on a real life, albeit &#8220;old skool&#8221;, problem. </p>
<p>Back in the days when I taught myself programming Turbo Pascal, I was very fascinated by the demo scene. An integral part of the scene, were the <a href="http://en.wikipedia.org/wiki/MOD_(file_format)">MOD</a>, or Module, files. MOD is a file format for sequenced music, much like MIDI files, except MOD files also contain sampled instruments. The file format is more or less specified <a href="http://mediasrv.ns.ac.yu/extra/fileformat/modules/mod/mod-form.txt">here</a>. I&#8217;ll describe the relevant parts as we move along.</p>
<p>The files <a href="http://github.com/knutel/PyModPlayer/blob/e1efea418a19034f96964d6254d0c34e2053ecde/ModLib/src/modlib/parsers/mod/mod.py">mod.py</a> and <a href="http://github.com/knutel/PyModPlayer/blob/e1efea418a19034f96964d6254d0c34e2053ecde/ModLib/src/modlib/parsers/mod/utils.py">utils.py</a> can be found at <a href="http://github.com/knutel/PyModPlayer/tree/master">GitHub</a>.</p>
<p>To better understand how the parser is built, it&#8217;s best to start with the most high level construct first. The overall structure of a MOD file is as follows:</p>
<p>EDIT: This is from the file mod.py.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>90
91
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">mod = Struct<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mod&quot;</span>, 
             String<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;title&quot;</span>, <span style="color: #ff4500;">20</span>, padchar=<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\x</span>00&quot;</span><span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>The String construct denotes a fixed length string, in this case 20 characters, named title. The string is padded with zeroes. Construct also provides PascalString for length prefixed string and CString for null terminated strings.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>92
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">             StrictRepeater<span style="color: black;">&#40;</span><span style="color: #ff4500;">31</span>, sample_info<span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>The StrictRepeater is used to specify that the sample_info sub construct is to be repeated an exact number of times. A MOD file has 31 samples, but if less samples are needed, the sample_info contains a length of zero for the unused ones.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>93
94
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">             UBInt8<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;num_positions&quot;</span><span style="color: black;">&#41;</span>,
             Padding<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>UBInt8 specifies an unsigned 8 bit integer in big endian format. The value num_positions is the number of patterns to play.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>95
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">             StrictRepeater<span style="color: black;">&#40;</span><span style="color: #ff4500;">128</span>, ULInt8<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;pattern_table&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>The pattern table is a list of bytes that describe the order in which the patterns are to be played. I mistakenly used ULInt8 for these bytes, but with a byte length of 8 bits it doesn&#8217;t matter if the little endian or big endian interpretation is used.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>96
97
98
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">             OneOf<span style="color: black;">&#40;</span>String<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;signature&quot;</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>, signature_vs_channels.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
             Value<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;num_channels&quot;</span>, 
                   <span style="color: #ff7700;font-weight:bold;">lambda</span> ctx: signature_vs_channels<span style="color: black;">&#91;</span>ctx.<span style="color: black;">signature</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>The OneOf construct is used to specify that the sub construct must be equal to exactly one of a list of candidate values. The signature field tells us how many audio channels the MOD file has. I put the mapping from signatures to number of channels into a dictionary, so I gave the keys as the list of possible values for the OneOf construct. The Value construct can be used to compute a value from the previously parsed constructs. Value takes a function with a single parameter, the parsing context, applies the function and assigns the resulting value to the given name. Here, the value for num_channels is simply looked up in the signature vs number of channels dictionary.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>99
100
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">             MetaRepeater<span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> ctx: <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">max</span><span style="color: black;">&#40;</span>ctx.<span style="color: black;">pattern_table</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">1</span>,
                          patterns<span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>The MetaRepeater differs from the StrictRepeater in that the number of repetitions is given as a function of the parsing context. A MOD file can contain up to 128 patterns. The number of actual patterns is the maximum value from the pattern table plus one, so the number of repetitions is simply given as a function that calculates this value.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>101
102
103
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">             MetaRepeater<span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> ctx: <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>ctx.<span style="color: black;">sample_info</span><span style="color: black;">&#41;</span>,
                          Bytes<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;samples&quot;</span>, sample_length<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
             <span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The samples&#8217; data is stored at the end of the file. The number of samples is given as the length of sample_info, but will 31 with the current parser. The sample data is stored as an array of bytes. This is where some ugliness creeps in. For each sample to be read, its length must be looked up in the sample_info array, but in Construct itself, there is no way to access the current index in the Repeater constructs. The function sample_length, defined in utils.py, takes care of looking up the length and keeping track of which sample that is read.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">signature_vs_channels = <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;M.K.&quot;</span>: <span style="color: #ff4500;">4</span>, 
                         <span style="color: #483d8b;">&quot;M!K!&quot;</span>: <span style="color: #ff4500;">4</span>, 
                         <span style="color: #483d8b;">&quot;6CHN&quot;</span>: <span style="color: #ff4500;">6</span>, 
                         <span style="color: #483d8b;">&quot;8CHN&quot;</span>: <span style="color: #ff4500;">8</span>, 
                         <span style="color: #483d8b;">&quot;12CH&quot;</span>: <span style="color: #ff4500;">12</span>, 
                         <span style="color: #483d8b;">&quot;28CH&quot;</span>: <span style="color: #ff4500;">28</span>, 
                         <span style="color: #483d8b;">&quot;FLT4&quot;</span>: <span style="color: #ff4500;">4</span><span style="color: black;">&#125;</span></pre></td></tr></table></div>

<p>A simple mapping between signatures and number of channels.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>14
15
16
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">sample_info = Struct<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sample_info&quot;</span>,
                String<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;name&quot;</span>, <span style="color: #ff4500;">22</span>, padchar=<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\x</span>00&quot;</span><span style="color: black;">&#41;</span>,
                WordsToBytesAdapter<span style="color: black;">&#40;</span>UBInt16<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;length&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>The sample_info sub structure is repeated once for each sample. Nothing new here except the WordsToBytesAdapter, which is defined in utils.py. The length value stored in the file is the number of 16 bit words in the sample. The WordsToBytesAdapter simply doubles the length to give the number of bytes instead.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>17
18
19
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">                Embed<span style="color: black;">&#40;</span>BitStruct<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>,
                          Padding<span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>,
                          BitField<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;finetune&quot;</span>, <span style="color: #ff4500;">4</span>, signed=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>One of the strengths of Construct is the ability to easily handle variable length bit fields. Normally, the parser operates on bytes. To operate on bits, a BitStruct is needed. In this case the BitStruct is enclosed in an Embed construct. This means that the members of the BitStruct will be included in the enclosing struct.</p>
<p>The finetune field is the lower 4 bits of a byte value, interpreted as a signed 4 bit integer, so we first insert a Padding struct to get rid of the upper 4 bits.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>20
21
22
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">                UBInt8<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;volume&quot;</span><span style="color: black;">&#41;</span>,
                WordsToBytesAdapter<span style="color: black;">&#40;</span>UBInt16<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;repeat_offset&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
                WordsToBytesAdapter<span style="color: black;">&#40;</span>UBInt16<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;repeat_length&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Samples loop forever if the repeat_length field is positive. When the end of a sample is reached, playback is continued from the repeat_offset. Once repeat_offset + repeat_length is reached, playback is once again continued from the repeat_offset.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>85
86
87
88
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">patterns = Struct<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;patterns&quot;</span>,
                 StrictRepeater<span style="color: black;">&#40;</span><span style="color: #ff4500;">64</span>,
                                MetaRepeater<span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> ctx: ctx._.<span style="color: black;">num_channels</span>, 
                                             channel_data<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Patterns consist of 64 divisions, each with 4 bytes of data per channel.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>75
76
77
78
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">channel_data = BitStruct<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;channel_data&quot;</span>,
                         Nibble<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sample_hi&quot;</span><span style="color: black;">&#41;</span>,
                         BitField<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;period&quot;</span>, <span style="color: #ff4500;">12</span><span style="color: black;">&#41;</span>,
                         Nibble<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sample_lo&quot;</span><span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>For some reason the sample number is split into two nibbles, separated by the period, or pitch, so play the sample at. I have used the Value construct to join the nibbles into a byte below. This still leaves sample_hi and sample_lo hanging around and there is, as far as I can tell, no way to get rid of them.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>79
80
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">                         major_effect,
                         Embed<span style="color: black;">&#40;</span>efx_and_params<span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>The effect to be applied for this particular channel and division is a 4 bit integer in the range 0-15. An effect has two 4 bit parameters, x and y, that follow. However, an effect value of 14 is an extended effect. The x then specifies which extended effect, and the y is its parameter.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>81
82
83
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">                         Value<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;sample&quot;</span>, 
                               <span style="color: #ff7700;font-weight:bold;">lambda</span> ctx: ctx.<span style="color: black;">sample_hi</span> <span style="color: #66cc66;">&lt;&lt;</span> <span style="color: #ff4500;">4</span> | ctx.<span style="color: black;">sample_lo</span><span style="color: black;">&#41;</span>
                        <span style="color: black;">&#41;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">major_effect = Enum<span style="color: black;">&#40;</span>Nibble<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;major_effect&quot;</span><span style="color: black;">&#41;</span>,
                    Arpeggio = <span style="color: #ff4500;">0</span>,
                    SlideUp = <span style="color: #ff4500;">1</span>,
                    SlideDown = <span style="color: #ff4500;">2</span>,
                    SlideToNote = <span style="color: #ff4500;">3</span>,
                    Vibrato = <span style="color: #ff4500;">4</span>,
                    ContinueSlideToNotePlusVolumeSlide = <span style="color: #ff4500;">5</span>,
                    ContinueVibratoPlusVolumeSlide = <span style="color: #ff4500;">6</span>,
                    Tremolo = <span style="color: #ff4500;">7</span>,
                    SetSampleOffset = <span style="color: #ff4500;">9</span>,
                    VolumeSlide = <span style="color: #ff4500;">10</span>,
                    PositionJump = <span style="color: #ff4500;">11</span>,
                    SetVolume = <span style="color: #ff4500;">12</span>,
                    PatternBreak = <span style="color: #ff4500;">13</span>,
                    extended_effect = <span style="color: #ff4500;">14</span>, <span style="color: #808080; font-style: italic;">#This is never seen outside parser</span>
                    SetSpeed = <span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span>
&nbsp;
extended_effect = Enum<span style="color: black;">&#40;</span>Nibble<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;extended_effect&quot;</span><span style="color: black;">&#41;</span>,
                       ToggleFilter = <span style="color: #ff4500;">0</span>,
                       FineslideUp = <span style="color: #ff4500;">1</span>,
                       FineslideDown = <span style="color: #ff4500;">2</span>,
                       ToggleGlissando = <span style="color: #ff4500;">3</span>,
                       SetVibratoWaveform = <span style="color: #ff4500;">4</span>,
                       SetFinetuneValue = <span style="color: #ff4500;">5</span>,
                       LoopPattern = <span style="color: #ff4500;">6</span>,
                       SetTremoloWaveform = <span style="color: #ff4500;">7</span>,
                       RetriggerSample = <span style="color: #ff4500;">9</span>,
                       FineVolumeSlideUp = <span style="color: #ff4500;">10</span>,
                       FineVolumeSlideDown = <span style="color: #ff4500;">11</span>,
                       CutSample = <span style="color: #ff4500;">12</span>,
                       DelaySample = <span style="color: #ff4500;">13</span>,
                       DelayPattern = <span style="color: #ff4500;">14</span>,
                       InvertLoop = <span style="color: #ff4500;">15</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Enums are used to map from integers to symbols.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>70
71
72
73
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">efx_and_params = IfThenElse<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, 
                            <span style="color: #ff7700;font-weight:bold;">lambda</span> ctx: ctx.<span style="color: black;">major_effect</span> == <span style="color: #483d8b;">&quot;extended_effect&quot;</span>,
                            efx_params,
                            fx_params<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The IfThenElse construct allows for conditional parsing. Here we check if the effect is really an extended effect. If it is, the sub construct efx_params is tried, otherwise fx_params.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>65
66
67
68
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">fx_params = Struct<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, 
                   Nibble<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;x&quot;</span><span style="color: black;">&#41;</span>, 
                   Nibble<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;y&quot;</span><span style="color: black;">&#41;</span>, 
                   Alias<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;effect&quot;</span>, <span style="color: #483d8b;">&quot;major_effect&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>An effect has two parameters, x and y. The Alias construct creates an additional name for a symbol.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>59
60
61
62
63
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">efx_params = Struct<span style="color: black;">&#40;</span><span style="color: #008000;">None</span>, 
                    extended_effect, 
                    Nibble<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;x&quot;</span><span style="color: black;">&#41;</span>, 
                    Value<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;y&quot;</span>, <span style="color: #ff7700;font-weight:bold;">lambda</span> ctx: <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>, 
                    Alias<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;effect&quot;</span>, <span style="color: #483d8b;">&quot;extended_effect&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>As mentioned, if the effect is an extended effect, its first parameter, or nibble, tells us which extended effect. The second parameter is the one and only extended effect parameter. Since the extended effect doesn&#8217;t have a second paramter, we simply give it a value of None using the Value construct. This time, the alias effect is assigned the value contained in extended_effect.</p>
<p>To clarify how the channel data will look when parsed, we can do a test with GLiPy:<br />
<img src="http://blog.somethingaboutcode.com/wp-content/uploads/2009/05/picture-1.png" alt="channel_data" title="channel_data" width="780" height="552" class="alignnone size-full wp-image-109" /></p>
<p>We can try it out on a file as well. I chose the file <a href="http://modarchive.org/data/downloads.php?moduleid=37444#demomod3.mod">demomod3.mod</a>. The following screenshot shows how to access parts of the parsed file:<br />
<img src="http://blog.somethingaboutcode.com/wp-content/uploads/2009/05/picture-4.png" alt="module properties" title="module properties" width="780" height="552" class="alignnone size-full wp-image-116" /><br />
Printing the module object looks like this, except I have edited the output to a reasonable length:</p>
<pre>
Container:
    title = 'demomodul#3'
    sample_info = [
        Container:
            name = "by laxity/kefrens '9"
            length = 9444
            finetune = 0
            volume = 64
            repeat_offset = 0
            repeat_length = 2
        Container:
            name = ''
            length = 4780
            finetune = 0
            volume = 60
            repeat_offset = 458
            repeat_length = 3862
&lt;29 more&gt;
    ]
    num_positions = 40
    pattern_table = [
        2
        1
        3
        4
        14
        0
        7
        8
&lt;120 more&gt;
    ]
    signature = 'M.K.'
    num_channels = 4
    patterns = [
        Container:
            channel_data = [
                [
                    Container:
                        sample_hi = 1
                        period = 226
                        sample_lo = 7
                        major_effect = 'SetSpeed'
                        x = 0
                        y = 6
                        effect = 'SetSpeed'
                        sample = 23
                    Container:
                        sample_hi = 0
                        period = 254
                        sample_lo = 11
                        major_effect = 'Arpeggio'
                        x = 0
                        y = 0
                        effect = 'Arpeggio'
                        sample = 11
                    Container:
                        sample_hi = 0
                        period = 254
                        sample_lo = 1
                        major_effect = 'Arpeggio'
                        x = 0
                        y = 0
                        effect = 'Arpeggio'
                        sample = 1
                    Container:
                        sample_hi = 0
                        period = 0
                        sample_lo = 0
                        major_effect = 'Arpeggio'
                        x = 0
                        y = 0
                        effect = 'Arpeggio'
                        sample = 0
                ]
&lt;63 more&gt;
            ]
        Container:
            channel_data = [
                [
                    Container:
                        sample_hi = 0
                        period = 0
                        sample_lo = 0
                        major_effect = 'Arpeggio'
                        x = 0
                        y = 0
                        effect = 'Arpeggio'
                        sample = 0
                    Container:
                        sample_hi = 0
                        period = 0
                        sample_lo = 0
                        major_effect = 'Arpeggio'
                        x = 0
                        y = 0
                        effect = 'Arpeggio'
                        sample = 0
                    Container:
                        sample_hi = 0
                        period = 254
                        sample_lo = 1
                        major_effect = 'Arpeggio'
                        x = 0
                        y = 0
                        effect = 'Arpeggio'
                        sample = 1
                    Container:
                        sample_hi = 0
                        period = 0
                        sample_lo = 0
                        major_effect = 'extended_effect'
                        extended_effect = 'FineVolumeSlideDown'
                        x = 1
                        y = None
                        effect = 'FineVolumeSlideDown'
                        sample = 0
                ]
&lt;63 more&gt;
            ]
&lt;32 more&gt;
    ]
    samples = [
        "\x00\x00\x00\x00\x00\x00\x02\r\x16\x1b\x1b\x1c\x1c\x1c\x1c\x1c\x1c\x1c\x1b\x1b\x17\x16\x16\x13\r\x03\x00\x00\xed..."
&lt;30 more&gt;
    ]
</pre>
<p>Now, that was quite easy! Try it out for yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.somethingaboutcode.com/?feed=rss2&amp;p=8</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GLiPy &#8211; The OpenGL IPython Terminal</title>
		<link>http://blog.somethingaboutcode.com/?p=62</link>
		<comments>http://blog.somethingaboutcode.com/?p=62#comments</comments>
		<pubDate>Sat, 16 May 2009 10:23:59 +0000</pubDate>
		<dc:creator>Knut Eldhuset</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[glipy]]></category>
		<category><![CDATA[ipython]]></category>
		<category><![CDATA[pyglet]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.somethingaboutcode.com/?p=62</guid>
		<description><![CDATA[For those of you that use the standard Python shell when trying out stuff, IPython is an enhanced shell with lots of useful features, including tab completion and command history across sessions. GLiPy is an enhancement over IPython. It provides plotting of numpy structures using OpenGL, as shown below. A side effect of the graphical [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you that use the standard Python shell when trying out stuff, <a href="http://ipython.scipy.org/moin/">IPython</a> is an enhanced shell with lots of useful features, including tab completion and command history across sessions. GLiPy is an enhancement over IPython. It provides plotting of numpy structures using OpenGL, as shown below.<img src="http://blog.somethingaboutcode.com/wp-content/uploads/2009/05/picture-2.png" alt="GLiPy and numpy" title="GLiPy and numpy" width="780" height="552" class="alignnone size-full wp-image-63" /><br />
A side effect of the graphical nature of GLiPy and the fact that it has a GUI, is that it has a message pump going. This means you can experiment with <del datetime="2009-05-16T10:35:15+00:00">Wx or</del> pyglet without starting additional threads to get an application object up and running. The short example below shows me loading the Sine source from pyglet and playing a short sine wave.<img src="http://blog.somethingaboutcode.com/wp-content/uploads/2009/05/picture-3.png" alt="GLiPy and pyglet" title="GLiPy and pyglet" width="780" height="552" class="alignnone size-full wp-image-64" /><br />
The difference between running these three lines in GLiPy and IPython, is that the sound plays for 4 seconds when run in GLiPy, while using IPython you only get a short beep. The reason for this is that there is no timer set up to get the next chunk of data from the sine source. To get the rest of the sine wave, you have to do <code>pyglet.app.run()</code> which then blocks your main thread.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.somethingaboutcode.com/?feed=rss2&amp;p=62</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When True is not None</title>
		<link>http://blog.somethingaboutcode.com/?p=33</link>
		<comments>http://blog.somethingaboutcode.com/?p=33#comments</comments>
		<pubDate>Fri, 15 May 2009 12:16:14 +0000</pubDate>
		<dc:creator>Knut Eldhuset</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[boolean]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.somethingaboutcode.com/?p=33</guid>
		<description><![CDATA[Sometimes, when you try to explain something using an example, people will pick on the details of your example instead of seeing the greater picture. Scott Hanselman got some flack for daring to write something == true. Whether or not he was justified in writing his code that way, there certainly are good reasons to [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes, when you try to explain something using an example, people will pick on the details of your example instead of seeing the greater picture. <a href="http://www.hanselman.com/blog/ELMAHAndExceptionDrivenDevelopmentFTW.aspx">Scott Hanselman got some flack</a> for daring to write <code>something == true</code>. Whether or not he was justified in writing his code that way, there certainly are good reasons to check for explicit truth values, especially in Python.</p>
<p>When using keyword arguments with a default value of <code>None</code>, it&#8217;s easy to check if the argument was supplied or not:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> do_expensive_thing<span style="color: black;">&#40;</span>times<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>times + <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Index: &quot;</span>, n
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> do_it<span style="color: black;">&#40;</span>argument_for_expensive_operation=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> argument_for_expensive_operation:
        do_expensive_thing<span style="color: black;">&#40;</span>argument_for_expensive_operation<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;It's done&quot;</span></pre></div></div>

<p>Let&#8217;s try it out with some different parameters:</p>
<pre>
In [3]: do_it()
It's done

In [4]: do_it(2)
Index:  0
Index:  1
Index:  2
Index:  3
It's done
</pre>
<p>Works nicely. Let&#8217;s try a third time:</p>
<pre>
In [5]: do_it(0)
It's done
</pre>
<p>Whooah! What happened this time? Conveniently, not only <code>None</code> gives a truth value of <code>False</code>. Other examples are the integer value zero and the empty list. This explains why the above example fails. The correct way is to explicitly check if the supplied argument is <code>None</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> do_it_right<span style="color: black;">&#40;</span>argument_for_expensive_operation=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> argument_for_expensive_operation <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span>:
        do_expensive_thing<span style="color: black;">&#40;</span>argument_for_expensive_operation<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;It's done&quot;</span></pre></div></div>

<p>Now it prints what we would expect:</p>
<pre>
In [7]: do_it_right(0)
Index:  0
Index:  1
It's done
</pre>
<p>The <a href="http://docs.python.org/library/stdtypes.html">Python documentation</a> covers truth value testing in detail.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.somethingaboutcode.com/?feed=rss2&amp;p=33</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Swallowed by a Python</title>
		<link>http://blog.somethingaboutcode.com/?p=21</link>
		<comments>http://blog.somethingaboutcode.com/?p=21#comments</comments>
		<pubDate>Fri, 15 May 2009 11:57:26 +0000</pubDate>
		<dc:creator>Knut Eldhuset</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[gotcha]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.somethingaboutcode.com/?p=21</guid>
		<description><![CDATA[Programming Python is mostly straightforward. Not thinking, however, can lead to subtle bugs, and this one had me stumped for a while. If a program raises an exception that is not caught further up the call stack, I expect the interpreter to exit with a whimper. Consider the following code: def throwing_function&#40;&#41;: try: print 1 [...]]]></description>
			<content:encoded><![CDATA[<p>Programming Python is mostly straightforward. Not thinking, however, can lead to subtle bugs, and this one had me stumped for a while. If a program raises an exception that is not caught further up the call stack, I expect the interpreter to exit with a whimper. Consider the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> throwing_function<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #ff4500;">1</span> / <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">finally</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Finally&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Useful string&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> throwing_function<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Running this will print</p>
<pre>
Finally
Useful string
</pre>
<p>If you can spot the bug immediately, good for you, but I didn&#8217;t. I expected this code to fail with a ZeroDivisionError exception. Not so. It turns out that the return statement hides the exception.</p>
<p>The correct code should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> throwing_function<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #ff4500;">1</span> / <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">finally</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Finally&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Useful string&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> throwing_function<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Now it behaves like expected:</p>
<pre>
Finally
Traceback (most recent call last):
  File "swallowed_exception_fixed.py", line 8, in <module>
    print throwing_function()
  File "swallowed_exception_fixed.py", line 3, in throwing_function
    print 1 / 0
ZeroDivisionError: integer division or modulo by zero
</pre>
<p>A good rule of thumb would be to never put a return statement inside a finally clause.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.somethingaboutcode.com/?feed=rss2&amp;p=21</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Coding is Power</title>
		<link>http://blog.somethingaboutcode.com/?p=41</link>
		<comments>http://blog.somethingaboutcode.com/?p=41#comments</comments>
		<pubDate>Fri, 15 May 2009 11:56:07 +0000</pubDate>
		<dc:creator>Knut Eldhuset</dc:creator>
				<category><![CDATA[Rant]]></category>

		<guid isPermaLink="false">http://blog.somethingaboutcode.com/?p=41</guid>
		<description><![CDATA[There&#8217;s something about code. Programming a computer to do what I want is one of the most rewarding intellectual experiences I know of. With the right tools and language, it&#8217;s possible to accomplish anything. You could build The Next Big Thing, make an operating system, a music player, a blog engine, Facebook, Twitter, or anything [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s something about code. Programming a computer to do what I want is one of the most rewarding intellectual experiences I know of. With the right tools and language, it&#8217;s possible to accomplish anything. You could build The Next Big Thing, make an operating system, a music player, a blog engine, Facebook, Twitter, or anything else that you can think of. The tools to do so are at your finger tips. One (wo)man, his computer and the Internet, being able to disrupt the order of things on a global scale. That is what I call power!</p>
<p>This blog will be about my experience as a software developer and coder. I&#8217;ll cover tools that I use, code that I&#8217;ve written, bugs that have bit me and languages that are fascinating to me. I can&#8217;t promise perfect code in my examples. If you spot mistakes or know of better ways to accomplish things, please leave a comment.</p>
<p>Why should you listen to me? Well, to <a href="http://www.codinghorror.com/blog/archives/001124.html">quote Jeff Atwood</a>:</p>
<blockquote><p>There&#8217;s absolutely no reason any of you should listen to me!</p></blockquote>
<p>I started out coding BASIC and some assembly on my C64 back in the beginning of the nineties, then switched to a PC and did some Pascal, Visual Basic and PHP. Then I got my MSc in Computer Science, and software development has been my bread and butter since 2001. I have been coding C++, Java, C# and Python, but do not claim to be an all-knowing expert in any of these languages. So that&#8217;s my experience; take my ramblings for what they are.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.somethingaboutcode.com/?feed=rss2&amp;p=41</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
