<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Bluish Coder: pure</title>
 <link href="http://bluishcoder.co.nz/tag/pure/atom.xml" rel="self"/>
 <link href="http://bluishcoder.co.nz/"/>
 <updated>2020-07-10T16:25:05+12:00</updated>
 <id>http://bluishcoder.co.nz/</id>
 <author>
   <name>Bluishcoder</name>
   <email>admin@bluishcoder.co.nz</email>
 </author>

 
 <entry>
   <title>Vorbis Player implemented in Pure</title>
   <link href="http://bluishcoder.co.nz/2010/02/25/vorbis-player-written-in-pure.html"/>
   <updated>2010-02-25T14:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2010/02/25/vorbis-player-written-in-pure</id>
   <content type="html">&lt;p&gt;After writing the &lt;a href=&quot;http://bluishcoder.co.nz/2010/02/20/pure-preforking-echo-server-example.html&quot;&gt;preforking echo server example&lt;/a&gt; in &lt;a href=&quot;http://code.google.com/p/pure-lang/&quot;&gt;Pure&lt;/a&gt; I wanted to try something a bit more complex. I&#39;ve written Ogg players in various languages before so I decided to do one in Pure to compare. Here&#39;s some of the ones I&#39;ve written in the past:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/2009/03/oggplayer-simple-ogg-player-using.html&quot;&gt;oggplayer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/2009/06/26/decoding-vorbis-files-with-libvorbis.html&quot;&gt;plogg&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://tech.groups.yahoo.com/group/iolanguage/message/11610&quot;&gt;Io Language Addons&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://bluishcoder.co.nz/2007/05/ogg-theora-support-for-factor.html&quot;&gt;Factor Ogg Player&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;An unreleased Haskell Vorbis player&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I wrapped the libogg and libvorbis libraries using the Pure FFI. A program &#39;pure-gen&#39; is included with Pure that will generate FFI wrappers given a C include file. I ran this against libogg and libvorbis. The generated Pure wrappers look like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;extern int ogg_page_version(ogg_page*);
extern int ogg_page_continued(ogg_page*);
extern int ogg_page_bos(ogg_page*);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see Pure FFI definitions look very much like the C definitions.&lt;/p&gt;

&lt;p&gt;I decided to model an Ogg file as a list of physical pages. This is esentially a list containing elements of the libogg ogg_page object. I have a Pure function that will take this list of pages and return a list of packets for each logical bitstream in the file. For vorbis playback I take this list of packets for the vorbis bitstream and produce a list of decoded PCM data.&lt;/p&gt;

&lt;p&gt;Pure is a strict language by default but provides an &#39;&amp;amp;&#39; special form to perform lazy evaluation. I use this to make sure all the returned lists mentioned above are lazy. Playing the lazy list of decoded PCM data never loads the entire file in memory. The file is read, converted into packets and decoded as each element of the PCM data list is requested.&lt;/p&gt;

&lt;p&gt;For audio playback I use &lt;a href=&quot;http://connect.creativelabs.com/openal/default.aspx&quot;&gt;OpenAL&lt;/a&gt;. I queue the PCM data to an OpenAL source up to a limit of 50 queued items, removing them as they get processed and adding new ones when room becomes available.&lt;/p&gt;

&lt;p&gt;Here&#39;s what some of the API to process the Ogg files looks like in Pure. First load the player code. This will also load the Ogg, Vorbis and OpenAL wrappers:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ pure -i player.pure
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;From the Pure REPL, play a vorbis file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; play_vorbis (sources!0) &quot;test.ogg&quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Load an Ogg file getting a lazy list of all pages in the file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; using namespace ogg;
&amp;gt; let p = file_pages &quot;test.ogg&quot;;
&amp;gt; p;
ogg_page (#&amp;lt;pointer 0x9e61cc8&amp;gt;,[...]):#&amp;lt;thunk 0xb7259b38&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Get a list of all packets and the serial number of the first logical bitstream in the list of pages:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; let (stream1,serialno) = packets p;
&amp;gt; serialno;
1426627898
&amp;gt; stream1;
ogg_packet (#&amp;lt;pointer 0x9dd98f0&amp;gt;,[...]):#&amp;lt;thunk 0xb725bce0&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Get the next logical bitstream:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; let p2 = filter (\x-&amp;gt;ogg_page_serialno x ~= serialno) p;
&amp;gt; p2;
#&amp;lt;thunk 0xb725dc30&amp;gt;
&amp;gt; let (stream2,serialno2) = packets p2;
&amp;gt; serialno2;
629367739
&amp;gt; stream2;
ogg_packet (#&amp;lt;pointer 0x9cfcf08&amp;gt;,[...]):#&amp;lt;thunk 0xb725c988&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that all these results are lazily evaluated so the entire
file is not loaded into memory. If we look at the original list
of pages you&#39;ll see what has been read so far (two pages):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; p;
ogg_page (...):ogg_page (...):#&amp;lt;thunk 0xb725dd98&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Get all the streams in the file:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; let s = all_streams p;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Get all the vorbis streams:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; let v = vorbis_streams s;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Get the first vorbis stream. I ignore the serial number here (this is what the _ does):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; let (v1, _) = v!0;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Decode the vorbis header packets and intialize a vorbis decoder
for the first vorbis stream:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; let decoder = vorbis_header v1;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Get a lazy list of all the decoded PCM data from the vorbis stream:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; let pcm = pcm_data decoder;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The mechanics of getting the decoding floating point data from libvorbis into Pure and then back to the OpenAL C API is made easier due to it being possible to easily convert to and from raw C pointers and Pure matrix objects.&lt;/p&gt;

&lt;p&gt;For example, the data returned from the vorbis call to decode the data is an array of pointers pointing to an array of floats. To convert this to a Pure matrix with dimensions (c,n) where &#39;c&#39; is the number of channels and &#39;n&#39; is the number of samples:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;vorbis_synthesis_read dsp samples $$ matrix m
when
  f0 = get_pointer data;
  floats = [get_pointer (f0+(x*4))|x=0..(channels-1)];
  m = map (float_matrix samples) floats;
end;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code uses a list comprehension to return a list of &#39;pointer to array of floats&#39;. One pointer for each channel in the audio data. It then maps over this list producing a new list containing a matrix of float&#39;s. This is list of matrices each of which contains the audio data for a channel. The &#39;matrix m&#39; call converts this to a single matrix of dimensions (c,n).&lt;/p&gt;

&lt;p&gt;OpenAL expects the data as a pointer to a contiguous matrix of floats with dimensions (n,c) so when writing to OpenAL the matrix needs to be transposed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;play_pcm source rate pcm@(x:xs) =
  ....
  queue_sample source rate (transpose x) $$
  play source $$
  play_pcm source rate xs;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This transposed matrix is later converted from floats to 16 bit integers and &#39;packed&#39; so it is contiguous in memory, and a pointer to the raw memory passed to OpenAL:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;buffer16 s = pack (map clamp s);  
...
data = buffer16 ...pcmdata...;
alBufferData ... (cooked (short_pointer NULL data)) ((#data) * (sizeof sshort_t)) ...;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &#39;cooked&#39; call means the C memory has &#39;free&#39; called on it when nothing in Pure is holding onto it anymore.&lt;/p&gt;

&lt;p&gt;One downside with lazy lists is it can be easy to accidently read the entire file and decode everything before playing. This will cause issues with large Ogg files. I managed to avoid that and files play in constant memory. An &lt;a href=&quot;http://okmij.org/ftp/Streams.html&quot;&gt;iteratee&lt;/a&gt; style approach might be interesting to try as an alternative to lazy lists. I&#39;d like to try decoding Theora files and doing a/v sync in Pure at some point as well.&lt;/p&gt;

&lt;p&gt;I haven&#39;t used Pure much but it seemed to be fairly simple to put this together. I&#39;d be interested in feedback on the approach (using lazy lists) and Pure style issues in the code.&lt;/p&gt;

&lt;p&gt;The code for this is at &lt;a href=&quot;http://github.com/doublec/pure-ogg-player&quot;&gt;http://github.com/doublec/pure-ogg-player&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Pure Preforking Echo Server Example</title>
   <link href="http://bluishcoder.co.nz/2010/02/20/pure-preforking-echo-server-example.html"/>
   <updated>2010-02-20T23:50:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2010/02/20/pure-preforking-echo-server-example</id>
   <content type="html">&lt;p&gt;A few months back there were some examples of preforking servers implemented in various languages showing how posix API&#39;s could be used in those languages. Some examples were:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ruby: &lt;a href=&quot;http://tomayko.com/writings/unicorn-is-unix&quot;&gt;I like Unicorn because it&#39;s Unix&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Python: &lt;a href=&quot;http://jacobian.org/writing/python-is-unix/&quot;&gt;Python is Unix&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://gist.github.com/240095&quot;&gt;PHP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://gist.github.com/204301&quot;&gt;C&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;I&#39;m learning the &lt;a href=&quot;http://code.google.com/p/pure-lang/&quot;&gt;Pure programming language&lt;/a&gt; and took a stab at doing a Pure implementation. Pure comes with a good FFI for calling C functions and the needed posix calls are provided in the &#39;posix&#39; library that comes with it.&lt;/p&gt;

&lt;p&gt;It doesn&#39;t have any socket routines however but there is a library by Mike Maul called &lt;a href=&quot;http://code.google.com/p/pure-lang-extras/&quot;&gt;pure-lang-extras&lt;/a&gt; that provides this. With pure-lang-extras installed the preforking echo server looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;using ffi, system;
using posix, posix::socket;
using namespace posix, posix::socket;

let sockfd = socket AF_INET SOCK_STREAM 0;
let yes = {1};
setsockopt sockfd SOL_SOCKET SO_REUSEADDR (int_pointer yes) (sizeof sint_t) 0;
let ai = make_sockaddr AF_INET &quot;*&quot; 5000;
bind_socket sockfd ai;
listen sockfd 10;

fork_child f = if pid == 0 then f pid $$ exit 0 else pid when pid = fork end;

child pid  =  fprintf file &quot;Child %d echo&amp;gt; &quot; getpid $$
              fflush file $$
              fprintf file &quot;%s\n&quot; (fgets file) $$
              fclose file $$
              child pid 
              when
                client = accept_socket sockfd ai;
                file = fdopen client &quot;r+&quot;;
              end;

let children = [fork_child child|x=0..2];
do (printf &quot;Forked: %d\n&quot;) children;
do (\n-&amp;gt;(waitpid n NULL 0)) children;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This can be run (assuming it&#39;s in a file called &#39;test.pure&#39;) with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ pure -x test.pure
Forked: 10433
Forked: 10434
Forked: 10435
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It will fork three worker processes all waiting on the same socket provided by the main process. The main process waits until all three worker processes complete or are killed. You can test it out by using telnet to port 5000:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ telnet localhost 5000
Trying 127.0.0.1...
Connected to localhost.
Escape character is &#39;^]&#39;.
Child 10433 echo&amp;gt; test test test
test test test
Connection closed by foreign host.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you do this a few times you&#39;ll see it cycle through the worker processes.&lt;/p&gt;

&lt;p&gt;The example code mostly does posix calls and thin wrapper&#39;s around C functions so it doesn&#39;t show off much of the interesting parts of Pure. Pure has automatic currying (or partial evaluation) of functions by missing arguments from the function. For example, this code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;do (printf &quot;Forked: %d\n&quot;) children;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;printf&lt;/code&gt; is like the C &lt;code&gt;printf&lt;/code&gt; function. It takes a format string followed by a tuple containing arguments for each format directive in that string. In the example above it has one of these, &#39;%d&#39;. The
code &lt;code&gt;(printf &quot;Forked: %d\n&quot;)&lt;/code&gt; is missing the last argument expected to &lt;code&gt;printf&lt;/code&gt;. Thanks to partial evaluation this evaluates to a function that takes one argument. &lt;code&gt;do&lt;/code&gt; calls this function for each element in the &lt;code&gt;children&lt;/code&gt; list, passing the list element to the function resulting in the &lt;code&gt;printf&lt;/code&gt; call. It&#39;s equivalent to this more verbose code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;do (\n -&amp;gt; printf &quot;Forked: %d\n&quot; n) children;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Another Pure feature used is the ability to get raw pointers to matrices. The code &lt;code&gt;{1}&lt;/code&gt; is a one row matrix containing one element, the interger &#39;1&#39;. A later line gets a raw pointer to this, converting it to a array of integers if it isn&#39;t already in that format: &lt;code&gt;int_pointer yes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To fork the child processes I use a list comprehension:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let children = [fork_child child|x=0..2];
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;0..2&lt;/code&gt; is a short hand syntax to create a list of numbers from 0 to 2. The &lt;code&gt;[...|....]&lt;/code&gt; is a list comprehension. The left hand side is called for each element of the right hand side and the result accumulated in a list. For example, at the Pure REPL:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;gt; [x*2|x=0..5];
[0,2,4,6,8,10]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For more on Pure there is quite a bit of documentation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://pure-lang.googlecode.com/svn/docs/pure-intro/pure-intro.pdf&quot;&gt;The Pure Programming Language&lt;/a&gt; (PDF)&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://pure-lang.googlecode.com/svn/docs/pure.html&quot;&gt;The Pure Manual&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://pure-lang.googlecode.com/svn/docs/purelib.html&quot;&gt;The Pure Library Manual&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Dealing with my archived Blogger posts</title>
   <link href="http://bluishcoder.co.nz/2010/02/14/dealing-with-archived-blogger-posts.html"/>
   <updated>2010-02-14T16:54:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2010/02/14/dealing-with-archived-blogger-posts</id>
   <content type="html">&lt;p&gt;I&#39;ve &lt;a href=&quot;http://bluishcoder.co.nz/2010/02/13/moving-away-from-blogger.html&quot;&gt;stopped using Blogger&lt;/a&gt; for the weblog and have moved to using &lt;a href=&quot;http://github.com/mojombo/jekyll&quot;&gt;Jekyll&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I&#39;ve set things up so I can edit the posts on my local push, push to my server using &lt;a href=&quot;http://git-scm.com/&quot;&gt;git&lt;/a&gt; and it publishes the new post.&lt;/p&gt;

&lt;p&gt;Blogger provides a way to export all existing posts and comments as an XML file. I used this to manually import a few posts to test out Jekyll but then decided this was way to much work to convert everything. Instead I opted to create posts that link to my original Blogger ones so at least the archives and tags list in Jekyll will allow listing the posts and titles.&lt;/p&gt;

&lt;p&gt;To import the existing archived blogger posts I wrote a program to read from the Blogger export file, extract the title, post tags/categories and existing URL and create Jekyll equivalents. It seems to have worked ok and I&#39;ll manually fix up any problems if I find them.&lt;/p&gt;

&lt;p&gt;I wrote the importer in the &lt;a href=&quot;http://code.google.com/p/pure-lang/&quot;&gt;Pure Programming Language&lt;/a&gt; to have a play with that language. I dabbled with it when it first came out but this was my first time using it in anger. It worked out pretty well. The core of the code to write out the posts looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;write_tag_header fd [] = ();
write_tag_header fd (x:xs) = fprintf fd &quot;tags:\n&quot; ();

write_tags fd (x:xs) = fprintf fd &quot;  - %s\n&quot; x $$ 
                       write_tags_fd xs;
write_tags fd [] = ();

create_post post = fprintf fd &quot;---\n&quot; () $$
                   fprintf fd &quot;layout: post\n&quot; () $$
                   fprintf fd &quot;title: %s\n&quot; title $$
                   write_tag_header fd tags $$
                   write_tags fd tags $$
                   fprintf fd &quot;---\n&quot; () $$
                   fprintf fd &quot;Original Post [%s](%s)\n&quot; (title,href) $$
                   fclose fd
                   when
                       filename = post_filename post;
                       fd = fopen filename &quot;w&quot;;
                       title = article_title post;
                       href = article_url post;
                       tags = article_tags post;
                  end;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Basically I used the Pure XML library to get a list of posts (I use an XPATH expression to get the relevant parts of the post from the Blogger XML). For each post I then call create_post on it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;map create_post all_posts
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I didn&#39;t go crazy and convert the actual HTML content into the &lt;a href=&quot;http://daringfireball.net/projects/markdown/&quot;&gt;markdown&lt;/a&gt; format I&#39;m using fo the Jekyll posts and convert everything completely - maybe a task for another day.&lt;/p&gt;
</content>
 </entry>
 
 
</feed>
