Archive for January, 2008

Team Viamentis will be at FOSSConf 08

Thursday, January 31st, 2008

Hey guys, hope you all know about FossConf. For the first time, Chennai has its’ very own FOSS conference, and we are very excited to be attending it. I am quite proud to be associated with it as a organizing committee member, though my contribution is quite negligible compared to others on the committee. As you can see, the line up of talks are cover the entire gamut of expertise, from beginner to guru-level.

It is quite refreshing to see a conference take pains to be beginner-friendly, and very student oriented. FossConf, just like ILUGC, is about real community, and real contributions. There is nothing quite like ILUGC when it comes to active grassroots FOSS advocacy. I have always been a great supporter of technical events with a strong community, and FossConf, organized by ILUGC, as per my expectation, will turn out to be a gem of an event.

If you are in Chennai, or can make it, do come down, and it is a great place to meet us, just drop us a mail with your contact, and one of us will catch up with you at the event. See you there!

Listening and Talking to your Linux Box

Friday, January 25th, 2008

Traditional ways in which our computer applications ‘talks’ to us is by displaying lines of text or may be images or animations/videos. Apart from the occasional beep/alert sounds, the normal computer applications try to communicate to the user only visually. I wanted to go a few steps ahead and write some applications that would literally ‘talk’ to the user.

A tool that could translate text to speech was what i needed and i found a brilliant open source tool called Festival.

Installing Festival

sudo apt-get install festival

sudo apt-get install festvox-don

Turn on your speakers and try the following :

$ echo “Hello World” | festival –tts

Its so simple..!

Festival and the Shell

Festival when combined with the capabilities of the powerful Unix Shell gives awesome results.

$ cat filename.txt | festival –tts

$ man festival | festival –tts

$ echo “Hello $(whoami), $(who | wc -l) people are logged into $(hostname) now” | festival –tts
Festival and Ruby

I wouldn’t be satifisfied with anything unless i mix it with Ruby..!

Laptop lid program

A program that says “come back soon” when you close your laptop’s lid, and says “Welcome back” when you open it back.

Linux’s elegant /proc interface provides an easy way to sense closing and opening of laptop lid.

Try “cat /proc/acpi/button/lid/LID/state”

My ruby script polls this file for ’state’ change, and uses festival to speak out the corresponding message, whenever lid is opened/closed.

Festival and IM

I couldn’t stop before i tried using Festival with my Instant messenger programs :)

Idea was to make the IM programs read the received messages aloud, rather than me having to read it. It was fairly straight forward.

Voice chat

The Reverse process : Reading data from PC’s microphone

I wanted to read raw sound data from my laptop’s microphone and dump it into a disk file. In other words, i wanted to write a sound recording program.

In Linux systems, PC’s soundcard is represented by /dev/dsp (or /dev/dsp or /dev/sound). Reading from /dev/dsp, we will get raw audio data from the ‘line-in’ or the ‘built-in microphone’. We can dump raw digital audio data to /dev/dsp and it will be played on the speakers.

The sound input circuitry of the soundcard is an Analog to Digital Converter (ADC), which digitize the analog audio signals.

Programming the soundcard’s ADC

The ADC has a few parameters which needs to be set before reading data from it, namely Sampling Rate and Sample Size.

  • Sampling rate decides the number of samples to be taken from the analog input per second.
  • Sample size is the precision with which each of these samples needs to be saved in digital format(to be precise, its the number of bits used per sample of data)

Ioctl() system call is used to set these paramters after opening “/dev/dsp”. Ruby has a wrapper for ioctl(), but it dint work satisfactorily. So I used my all time favorite, the mighty C.

Recorder Program

You can compile the program using

cc record.c -o recorder

Then run it as

./recorder

It starts recording and records 10 seconds of audio (duration can be changed by editing the DURATION parameter in the code) and saves at sound.pcm in the current directory.

We cant use the usual media players to play it since its raw digital data. But we can simply dump into the “/dev/dsp” !!

cat sound.pcm > /dev/dsp

Its a demonstration of the beauty and power of the Unix architecture.

Necessary data for programming the soundcard were obtained from here and here

Some more fun with Ruby and Instant messaging

Tuesday, January 22nd, 2008

AI used to be one of my favorite subjects right from college days. I was searching for some AI algorithms and happened to find a ‘chat-bot’ called Eliza in between. ‘She’ was good enough, even though she doesn’t perfectly simulate a human user, she does a pretty good job. The next moment i wanted to use Eliza to talk to my friends on behalf of me through my Instant messenger.

Since i already had some ruby programs which could talk to IM servers, an interface to Eliza was just what i needed. I visted the Eliza’s webpage, one of the simplest webpages ever. It just had a plain html form, with a textfield and a submit button. The page source said that textfield’s name was “Entry1″. That was all i needed !

Here’s my Ruby interface for Eliza :

def eliza(str)
response = Net::HTTP.post_form(URI.parse("http://www-ai.ijs.si/eliza-cgi-bin/eliza_script"),{'Entry1'=>str})
return response.body.split("</strong>\n").last.split("\n").first
end

Hats off to Ruby…!

Now whenever a chat message arrives on my IM program, i simply call this ‘eliza’ method, get a response string from Eliza, and send it as reply in the IM program.

Here’s the full Ruby code for the Eliza chat program.

Plotting Mathematical functions with Gnuplot

Friday, January 18th, 2008

Complex math always scares me. Mathematical functions written on the paper never made any sense to me. But i used to find it interesting to plot these functions and see the shapes they produce.But plotting them with pencil and paper was painful and took lot of time. Later after I learnt some elementary programming in C, I generated datasets with simple programs and tried using
putpixel(x,y) like functions, to plot them on screen. Even that wasn’t easy and dint get smooth curves.

Now i am trying to do the same exercise using a powerful tool of the GNU tool chain - Gnuplot. Its available free with almost all Linux distros or can be downloaded for free from here. Debian users can use

sudo apt-get install gnuplot

Getting started with gnuplot

gnuplot is extremely user/programmer friendly. Just type ‘gnuplot’ to get the gnuplot shell. I wanted to see my favorite Sine wave first.

gnuplot> plot sin(x)
Thats it and i got my first function plotted on gnuplot. It cant get anymore simple..!

sine.png

Saving output to file

Next I wanted to save the graph as an image file.

gnuplot> set terminal png
gnuplot> set output “sine.png”
gnuplot> replot

Now the output is dumped into “sine.png” in the current directory.

‘replot’ simply repeats the last ‘plot’ call.

Plotting multiple functions in the same graph

Intuitive ways work perfectly on gnuplot. To plot sine and cos waves on the same graph :
gnuplot> plot sin(x),cos(x)
OR
gnuplot> plot sin(x)
gnuplot> replot cos(x)

replot adds its arguments to the previous plot’s argument list and then calls plot again.

multiple.png

Plotting parametric equations

1. Line

x = constant
y = t

gnuplot> set parametric
gnuplot> plot 3,t

vertical_line.png

gnuplot> plot t,3

horizontal_line.png

2. Square

Basically 2 vertical and 2 horizontal striaght lines.

gnuplot> set parametric
gnuplot> set xrange[-2:8]
gnuplot> set yrange[-2:8]
gnuplot> set trange[0:4]
gnuplot> plot 0,t
gnuplot> replot 4,t
gnuplot> replot t,0
gnuplot> replot t,4

3. Circle
Circle of radius r and centered at (a,b) :

x = r * Cos(t) + a
y = r * Sin(t) + b

gnuplot> set parametric
gnuplot> set angle degree
gnuplot> set trange [0:360]
gnuplot> set size square
gnuplot> r = 4
gnuplot> plot r*cos(t), r*sin(t)

circle.png

4. Spiral
In the circle’s equation, instead of keeping radius constant, if we make it a function of t, we get a spiral (when r(t) = t)

x = r(t) * Cos(t) + a
y = r(t) * Sin(t) + br(t) = t

gnuplot> set parametric
gnuplot> set trange [0:10*pi]
gnuplot> set xrange[-10*pi:10*pi]
gnuplot> set yrange[-10*pi:10*pi]
gnuplot> set samples 1000
gnuplot> plot t*cos(t), t*sin(t)

spiral.png

5. Cardioid
As its name indicates, Cardioid is a heart shaped curve. In spiral’s parametric equation, if we put r(t) = 1 + Cos(t) , the result is a cardioid.

gnuplot> set parametric
gnuplot> r(t) = 1 + cos(t)
gnuplot> plot r(t)*cos(t), r(t)*sin(t)

cardioid.png

Plotting Styles
plot command can take different options like lines, impulses, boxes, points, linespoints etc

gnuplot> plot sin(x) with impulses

sine_impulse.png

3D Plotting

splot is the command used for 3-dimensional plotting.

gnuplot> splot sin(x)

Click and drag with mouse to rotate the figure 3-dimensionally.

3d_sine.png

i. Sphere

Parametric equations are:

x = cos(u)*sin(v)
y = sin(u)*sin(v)
z = cos(v)

gnuplot> set parametric
gnuplot> set urange [0:2*pi]
gnuplot> set vrange [0:pi]
gnuplot> set isosample 40
gnuplot> splot cos(u)*sin(v),sin(u)*sin(v),cos(v)

sphere.png

ii. Cone

Parametric equations are:

x = r cos(t)
y = r sin(t)
z = t

gnuplot> set parametric
gnuplot> set urange [-2:2]
gnuplot> set vrange [0:2*pi]
gnuplot> set isosample 40
gnuplot> set view 75,30
gnuplot> splot u*cos(v),u*sin(v),u

cone.png

Parametric equations for 3d figures were obtained from Introduction to gnuplot for Vector Calculus