<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version='2.0' xmlns:lj='http://www.livejournal.org/rss/lj/1.0/'>
	<channel>
		<title>The Corelatus Blog - Entries from February 2009</title>
		<description>Entries from February 2009</description>
                <link>../../../</link>

	
	<item>
		<title>Erlang example code for GTH</title>
		<link>../../../Erlang_example_code_for_GTH.html</link>        
		<guid isPermaLink="true">../../../Erlang_example_code_for_GTH.html</guid>
                <pubDate>Sun, 1 Feb 2009 16:55:04 GMT</pubDate>
		<description>&lt;p&gt;
Years ago, I wrote a few bits of example code to help customers get
started with controlling a &lt;a
href=&quot;http://www.corelatus.com/&quot;&gt;GTH&lt;/a&gt;.  If you were kind, you&#39;d
call them minimal, e.g. the Erlang one was about 20 lines long.
&lt;/p&gt;

&lt;p&gt; None of our customers that use Erlang were very impressed by that,
so I&#39;d hand out part of the code we use to do API testing (a cut-down
XML parser and a little library to handle sockets) along with sternly
issued instructions that this was just example code and that live
systems needed something more robust. Not great, but I had other
things to do.  &lt;/p&gt;

&lt;p&gt;
I finally got around to completing and releasing a proper native
Erlang API for the GTH. No (visible) XML. It&#39;s a gen_server, so it&#39;s
well-behaved and simple enough so that you can do almost anything from
the Erlang shell, e.g. to turn on an E1:
&lt;/p&gt;

&lt;pre&gt;
&lt;code&gt;
&lt;span class=&quot;synConstant&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;synStatement&quot;&gt;&amp;gt;&lt;/span&gt; {ok, A} &lt;span class=&quot;synStatement&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;synIdentifier&quot;&gt;gth:start_link&lt;/span&gt;(&lt;span class=&quot;synConstant&quot;&gt;&amp;quot;172.16.2.7&amp;quot;&lt;/span&gt;)&lt;span class=&quot;synSpecial&quot;&gt;.&lt;/span&gt;
{ok, &lt;span class=&quot;synStatement&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;synConstant&quot;&gt;0.44&lt;/span&gt;&lt;span class=&quot;synSpecial&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;synConstant&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;synStatement&quot;&gt;&amp;gt;&lt;/span&gt;}
&lt;span class=&quot;synConstant&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;synStatement&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;synIdentifier&quot;&gt;gth:set&lt;/span&gt;(A, &lt;span class=&quot;synConstant&quot;&gt;&amp;quot;pcm1B&amp;quot;&lt;/span&gt;, [{&lt;span class=&quot;synConstant&quot;&gt;&amp;quot;status&amp;quot;&lt;/span&gt;, &lt;span class=&quot;synConstant&quot;&gt;&amp;quot;enabled&amp;quot;&lt;/span&gt;}])&lt;span class=&quot;synSpecial&quot;&gt;.&lt;/span&gt;
ok
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;
and put an audio file on a timeslot:
&lt;/p&gt;

&lt;pre&gt;
&lt;code&gt;
&lt;span class=&quot;synConstant&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;synStatement&quot;&gt;&amp;gt;&lt;/span&gt; {ok, Bin} &lt;span class=&quot;synStatement&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;synIdentifier&quot;&gt;file:read_file&lt;/span&gt;(&lt;span class=&quot;synConstant&quot;&gt;&amp;quot;/tmp/resample_q_alaw.raw&amp;quot;&lt;/span&gt;)&lt;span class=&quot;synSpecial&quot;&gt;.&lt;/span&gt;
{ok,&lt;span class=&quot;synStatement&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;synSpecial&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;synStatement&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;}
&lt;span class=&quot;synConstant&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;synStatement&quot;&gt;&amp;gt;&lt;/span&gt; {ok, _ID, P_socket} &lt;span class=&quot;synStatement&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;synIdentifier&quot;&gt;gth:new_tcp_player&lt;/span&gt;(A, &lt;span class=&quot;synConstant&quot;&gt;&amp;quot;1B&amp;quot;&lt;/span&gt;, &lt;span class=&quot;synConstant&quot;&gt;2&lt;/span&gt;)&lt;span class=&quot;synSpecial&quot;&gt;.&lt;/span&gt;
{ok,&lt;span class=&quot;synConstant&quot;&gt;&amp;quot;strp1&amp;quot;&lt;/span&gt;,#Port&lt;span class=&quot;synStatement&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;synConstant&quot;&gt;0.1097&lt;/span&gt;&lt;span class=&quot;synStatement&quot;&gt;&amp;gt;&lt;/span&gt;}
&lt;span class=&quot;synConstant&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;synStatement&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;synIdentifier&quot;&gt;gen_tcp:send&lt;/span&gt;(P_socket, Bin)&lt;span class=&quot;synSpecial&quot;&gt;.&lt;/span&gt;
ok
&lt;span class=&quot;synConstant&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;synStatement&quot;&gt;&amp;gt;&lt;/span&gt; flush()&lt;span class=&quot;synSpecial&quot;&gt;.&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;
There&#39;s a direct mapping between GTH commands and function names in
the gth.erl module, e.g. the XML &amp;lt;set&amp;gt; command becomes
gth:set(). While writing the gth.erl module, I also rewrote our entire
API test suite (60 modules, 30kloc) to use the gth.erl interface. It
ended up under 20kloc. I&#39;d guess half of that reduction comes from
gth.erl being nicer to use and the other half comes from cleaning up
&quot;while I was at it&quot;.
&lt;/p&gt;

&lt;p&gt;
The code, with examples: &lt;a
href=&quot;http://www.corelatus.com/gth/api/gth_erlang_api.zip&quot;&gt;gth_erlang_api.zip&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
While writing gth.erl, I had a go at doing something similar in
Python. But that&#39;s another story.
&lt;/p&gt;
</description>
	</item>
	
	<item>
		<title>Moving to wordpress</title>
		<link>../../../Moving_to_wordpress.html</link>        
		<guid isPermaLink="true">../../../Moving_to_wordpress.html</guid>
                <pubDate>Thu, 19 Feb 2009 11:40:19 GMT</pubDate>
		<description>&lt;p&gt;
I tired of maintaining my personal homepage in straight HTML. So I&#39;ve
moved it to a wordpress account. We&#39;ll see how that works out.
&lt;/p&gt;

&lt;p&gt;
Edit: 21. January 2010. After a year, I tired of wordpress. It worked OK,
      but was often frustrating, especially when formatting code examples.
      So now I&#39;m using a blog compiler (chronicle) instead. We&#39;ll see
      how that works out.
&lt;/p&gt;
</description>
	</item>
	
	<item>
		<title>SOX parameters for downsampling to 8kHz Alaw</title>
		<link>../../../SOX_parameters_for_downsampling_to_8kHz_Alaw.html</link>        
		<guid isPermaLink="true">../../../SOX_parameters_for_downsampling_to_8kHz_Alaw.html</guid>
                <pubDate>Thu, 19 Feb 2009 16:50:16 GMT</pubDate>
		<description>&lt;p&gt; A technician working for an operator mailed me a few days ago
wondering why the recorded voice clips they use for their IVR sound so
bad, &quot;like they&#39;re coming from the bottom of a deep well&quot;. It turned
out that the clips actually sounded OK on a telephone, just not
through his laptop&#39;s speaker. He asked if I recommend any specific
filter parameters when converting audio from 44.1kHz wav to 8kHz Alaw
voice clips.
&lt;/p&gt;

&lt;h4&gt;An example&lt;/h4&gt;

&lt;p&gt;
I took this audio snippet from the introduction to an audio book. It
was originally a .ogg file. I converted it to a .wav file with a
44.1kHz sampling rate and 16 bits per sample. For my purposes any
artefacts from ogg vorbis are negligible.
&lt;/p&gt;

&lt;a href=&quot;http://www.corelatus.com/%7Ematthias/1_mono.wav&quot;&gt;1_mono.wav&lt;/a&gt; (44.1kHz, 16 bit linear samples)

&lt;p&gt;
Next, I converted it to 8kHz Alaw using &lt;a
href=&quot;http://sox.sourceforge.net/&quot;&gt;sox&lt;/a&gt;. 8kHz Alaw is what runs on
the fixed telephone network in most of the world.  (The US uses a
minor variant, &amp;#956;law):
&lt;/p&gt;

&lt;pre&gt;sox 1_mono.wav -A -r 8000 2_8kHz_alaw.wav&lt;/pre&gt;

&lt;a href=&quot;http://www.corelatus.com/%7Ematthias/2_8kHz_alaw.wav&quot;&gt;2_8kHz_alaw.wav&lt;/a&gt; (8kHz, 8 bit Alaw samples)

&lt;p&gt; That sounds a bit less clear than the original, but it&#39;s OK. It&#39;s
what you&#39;d expect coming out of a telephone. There&#39;s some weirdness
though. The audible difference between the two files varies from one
PC to another and even one playback program to another. Why? Because
laptop speakers vary in quality and because playback programs usually
quietly convert everything back to 48kHz or 44.1kHz sampling rates,
and they do it with different approaches. For fun, I resampled to
44.1kHz:
&lt;/p&gt;

&lt;pre&gt;sox 2_8kHz_alaw.wav -r 44100 -s 3_resampled.wav&lt;/pre&gt;

&lt;a href=&quot;http://www.corelatus.com/%7Ematthias/3_resampled.wav&quot;&gt;3_resampled.wav&lt;/a&gt; (44.1kHz, 16 bit linear samples)

&lt;p&gt; 2_8kHz_alaw.wav and 3_resampled.wav should sound almost the
same. But on some PCs they sound markedly different.  
&lt;/p&gt;

&lt;h5&gt;The GTH just plays octets (bytes)&lt;/h5&gt; 

&lt;p&gt;
The GTH has a simple approach to playing
back audio. It just copies the bytes you give it to the destination
timeslot. No format or rate conversion happens, though the GTH does
make sure the data is played out at the E1&#39;s frame rate (8000Hz). The
downside of that is that you have to convert all the files for your
IVR system before giving them to a GTH, e.g. using sox. The upside is
that it&#39;s simple. Nothing happens behind your back.
&lt;/p&gt;

&lt;h4&gt;What are the best SOX options to use?&lt;/h4&gt; 

&lt;p&gt;
I don&#39;t know. I used to suggest the following as a reasonable starting
point:
&lt;/p&gt;

&lt;pre&gt;sox original.wav -r 8000 -c 1 -A -t raw gth.raw resample -q&lt;/pre&gt;

&lt;p&gt;
As of a few years ago, sox improved and the &#39;resample&#39; effect got
deprecated. So now I suggest just letting sox do what it thinks is
best:
&lt;/p&gt;

&lt;pre&gt;sox original.wav -r 8000 -c 1 -A -t raw gth.raw&lt;/pre&gt;

&lt;p&gt; At the time of writing, it uses its &#39;rate&#39; effect with reasonable
default parameters for the bandwidth and filter characteristics. I
experimented a bit with the -m, -h, -v and -s switches for the &#39;rate&#39;
effect. I could not reliably hear a difference, let alone decide that
one sounded better.
&lt;/p&gt;

&lt;h4&gt;Why does the phone system use 8kHz anyway?&lt;/h4&gt;

&lt;p&gt;
There&#39;s a certain sound quality level expected in telephone networks,
and part of that is that the network carries everything up to about
3500Hz. Analog local loop specifications mention that, and pretty much
all digital telephone systems use an 8kHz sampling rate, which is what
you need to be able to carry audio up to 3.5kHz. Even the GSM and AMR
codecs start off with the assumption that the incoming audio is
limited to 3500Hz.
&lt;/p&gt;

&lt;p&gt;
So the bar is set pretty low. I haven&#39;t come across any systems which
set out to provide higher quality, e.g. even skype compresses the hell
out of the audio to save bandwidth. Even when both parties in a
conversation have huge amounts of it. Surprising, why not aim for VOIP
to sound much better than a regular telephone?
&lt;/p&gt;
</description>
	</item>
	
        </channel>
</rss>

