Monday, September 26, 2016

Space Chem: A brilliant game about Chemistry and Parallel computing

If you like programming, you might want to play SpaceChem, a delightful game.

The world doesn't need another platformer, another action RPG, or another First Person Shooter.  These genres have been explored beyond the point of creativity.  I played a game recently that blew my mind: it was a puzzle game that allowed you to write code to create molecules.  If you are left scratching your head, it is because the game truly is a mind-bender.

Spacechem bills itself as a puzzle game about chemical synthesis, but it is really about programming.  You build reactors using blocks which follow simple programming rules.  You can control two devices called Waldos (one Red, one Blue) that run the commands, and allow you to combine or uncombine molecules into elements.  Elements follow their true chemical properties: Oxygen can make two bonds, Hydrogen can only make a single bond.

The game board is limited, and the input and output areas are clearly delineated.  This constrains your programs, as objects cannot overlap on the game board.  And only one instruction can be placed for a single waldo: you cannot have two red instructions in a square.

Since there are two Waldos, sometimes you need to synchronize them to accomplish a task.  This forces you to reckon with parallel programming concepts.  Accomplishing the task is sufficient to advance to the next level, though you are given a graph of time and complexity (reactors used, and symbols used).  These correspond to time spent and space complexity of parallel programs.  To accomplish the goal quickly or with a small complexity, you quickly learn to keep your waldos busy as much as possible, and synchronize little.  This corresponds to high CPU utilization and reduced global barriers in parallel computing.

All put together, this is one of the most innovative games I have played.  There is a story-line which, while innovative,  pales compared to the beauty and elegance of the game.  The combination of Chemistry and Computing and puzzle solving is truly unmatched.

SpaceChem stands out as a wonderful example of gaming both as a creative art form, and a splendid way to motivate and teach programming.

Here is a program I made which creates Titanium Dioxide and Zinc Oxide from Oxygen, Zinc and Titanium.

Monday, September 19, 2016

Shutter Actuations for Nikon camera through exiv2

Digital SLR cameras have a physical shutter that wears out.  Shutter actuation count is a good measure of the age of the camera.  If you want to estimate how much life is still remaining in your shutter, or to estimate what to pay for a used camera, the actuation count can give you a good idea. Consumer cameras last about 100k actuations, and professional cameras often last much longer.

Exiv2 is a versatile tool for manipulating image metadata, and is often installed by default on modern Linux distributions.

To find shutter count from a photograph, a simple exiv2 command will do:

exiv2 -pa _DSC0901.NEF | grep Nikon3\.ShutterCount

The same command works for JPG files as well
exiv2 -pa _DSC0901.JPG | grep Nikon3\.ShutterCount

You can remove the grep filter to learn other parameters for the picture:
  • Exif.Nikon3.LensType: The lens used (D: DX, VR: Vibration Reduction)
  • Exif.Nikon3.Lens: Focal length and aperture specs of the lens used.
  • Exif.Nikon3.SerialNumber: Unique serial number of the camera body printed in the bottom of the camera.
  • Exif.Nikon3.ISOSpeed: ISO sensitivity of the sensor

Saturday, September 17, 2016

Kids jukebox using Python, Gst and Gtk

I had blogged earlier about an alphabet program for little children. That program allowed my children to recognize alphabets. Recently, a friend gifted us a children's CD that has an animal song for each letter of the alphabet. So I modified that program to play a song for each letter of the alphabet in addition to showing the alphabet.  Since it has been a while since I wrote the original program, I had to learn GObject, and gst-1.0.

The songs are hardcoded in the directory /home/dev/music/X.ogg.  You can change that location below.  The program is great for giving children control of a computer where they can play music.    You could either record your own voice for each alphabet, or a song that corresponds to the letter.  This program will also play songs for each number key 0.ogg, 1.ogg.  I have only tested this on X Windows on recent Ubuntu versions.  You can use this as a starting point for Mac or Windows assuming you can get gstreamer, GObject, Gtk and all dependencies installed.  It is easy to change this to handle MP3 files by changing the Vorbis decoder 'vorbisdec' to 'mpg123'.  To find out which decoders are installed, you can run the command 'gst-inspect-1.0 |grep dec |grep mp3'

This code is also up on github for collaboration.


import datetime, string, os

# Audio handling in Gstreamer 1.0 and GObject
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, Gtk, Pango,Gdk, GObject

class AudioPlayer():
    """ A class that plays OGG Vorbis files. """
    def __init__(self):
        self.started = False
        # Change this location to indicate where the songs are stored.
        self.music_path = os.path.dirname(os.path.realpath(__file__)) + "/music/"
        print self.music_path

    def start(self, alphabet):
        """Starts playing music for the alphabet indicated."""
        if (self.started):
        self.pipeline ="pipe")

        source = Gst.ElementFactory.make('filesrc')
        demux = Gst.ElementFactory.make('oggdemux')
        # The demux does not expose any pads till it has a file. Attach
        # a callback when pads are added
        demux.connect("pad-added", self.demuxer_callback)
        self.decoder = Gst.ElementFactory.make('vorbisdec')
        converter = Gst.ElementFactory.make('audioconvert')
        sink = Gst.ElementFactory.make('autoaudiosink')

        # Attach all the elements to the pipeline

        # Attach source -> demux & decoder  -> converter -> sink
        # demux -> decoder is done in the demuxer_callback

        # Attach a signal handler for being turned on
        bus = self.pipeline.get_bus()
        bus.connect("message", self.on_message)

        # Specify <current_dir>/music/S.ogg as the file to play when
        # the letter 's' or 'S' is pressed.
        filename = self.music_path + ("%s.ogg" % alphabet)

        # Set this as the source filename
        source.set_property("location", filename)

        # Start playing the pipeline
        self.started = True

    def on_message(self, bus, message):
        """ Graciously handle End Of Stream and Error cases."""
        t = message.type
        if t == Gst.MessageType.EOS:
            self.started = False
        elif t == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print "Error: %s" % err, debug
            self.started = False

    def demuxer_callback(self, demuxer, pad):
        "Connects the demux to the vorbis decoder"
        # Get the decoder pad that will accept the demultiplexed output
        decoder_pad = self.decoder.get_static_pad("sink")
        # And connect the newly formed pad to it, thereby joining the
        # demultiplexer to the decoder.

    def stop(self):
        if (self.started):
        self.started = False

class BigChar():
    """ Create a Gtk window for a single giant textview that accepts
        all keyboard input. """
    def on_key_press(self, widget, data=None):
        """ Intercept all keypress events and show ascii
            characters. This requires the CAPS_LOCK to be off.  We
            don't intercept CAPS NUM or SCROLL lock, probably
        # Set the time, even if the keypress is irrelevant

        ascii_value = data.keyval
        # Print the keycode received
        # print ascii_value


        # Uppercase and lowercase letters
        if (ascii_value >= 97 and ascii_value <= 122):
            self.display_alphabet(ascii_value - 97)
        if (ascii_value >= 65 and ascii_value <= 90):
            self.display_alphabet(ascii_value - 65)
        # Numbers
        if (ascii_value >= 48 and ascii_value <= 57):
            self.display_number(ascii_value - 48)
        # Number pad
        if (ascii_value >= 65456 and ascii_value <= 65466):
            self.display_number(ascii_value - 65456)
        # Special characters on the number pad.
        if (ascii_value == 65450):
        if (ascii_value == 65451):
        if (ascii_value == 65454):
        if (ascii_value == 65453):
        # Backspace should produce a left-pointing arrow.
        if (ascii_value == 65288):
        if (ascii_value == 65515):

    def display(self, text):
        """ Show the text in the textbox."""
        start = self.textBuffer.get_start_iter()
        end = self.textBuffer.get_end_iter()
        self.textBuffer.apply_tag_by_name("real_big", start, end)

    def display_alphabet(self, index):
        """ Show the English alphabet (CAPS and lower) at 0 indexed
            position 'A a' = 0, 'B b' = 1, ...
            Also plays the song corresponding to the alphabet. """
        big = string.ascii_uppercase[index]
        small = string.ascii_lowercase[index]
        self.display(big + " " + small)

    def display_number(self, number):
        """ Show the Number and play the song corresponding to it"""
        self.display("%d" % number)

    def realize_handler(self, widget):
        pixmap = GdkPixbuf.Pixbuf(None, 1, 1, 1)
        color = Gdk.Color(0, 0, 0)
        cursor = Gdk.Cursor(pixmap, color, 0, 0)

    def set_time(self):
        """Set the progress indicator to the current time.  Shows time
           in a horizontal access with the morning being near the left
           edge and night being near the right edge."""
        current_time =
        # Total hours past since (assume children wake up at 6am)
        minutes_past = ((current_time.hour - 6)
                        * 60.0 + current_time.minute)
        if (minutes_past < self.day_end):
            fraction = minutes_past / self.day_end
            fraction = 1.0

    def __init__(self):
        """ Create a window with a single giant text view. Disables
            all chrome. """
        # Foreground and background color are read from here.
        #background_color = "black"
        #foreground_color = "#1111ff"
        background_color = "green"
        foreground_color = "black"

        self.audio_player = AudioPlayer()

        self.w = Gtk.Window(Gtk.WindowType.TOPLEVEL)
        # No border
        # Take over the entire screen

        # Connect the callback on_key_press to the signal key_press.
        self.w.connect("key_press_event", self.on_key_press)
        # self.w.connect("realize", self.realize_handler)
        # Make the widget aware of the signal to catch.

        # Add a text view to show the key pressed
        textView = Gtk.TextView()
        # Disable a cursor in the text view.
        # Show the single character in the middle
        # This is the place we will write the character to
        self.textBuffer = textView.get_buffer()
        # Make the text view huge and bold
        fontdesc = Pango.FontDescription("monospace bold 400")

        # Creates a tag that is applied to the text every time
        tag = self.textBuffer.create_tag(
            , background=background_color
            , foreground=foreground_color)
        # The progress bar shows the current proportion of awake-time
        # for a child.
        # Minutes are capped at 8am, which is when kids go to
        # bed. Expressed as minutes after 6am.
        self.day_end = ((20 - 6) * 60.0)
        self.progress = Gtk.ProgressBar()

        # Make the text view take the entire window
        vbox = Gtk.VBox(homogeneous=False, spacing=0)
        color = Gdk.Color.parse(background_color)[1]

        self.w.modify_bg(Gtk.StateType.NORMAL, color)
        self.progress.modify_bg(Gtk.StateType.NORMAL, color)
        textView.modify_bg(Gtk.StateType.NORMAL, color)

        vbox.pack_start(textView, fill=True, expand=False, padding=0)
        vbox.pack_start(self.progress, fill=True, expand=True, padding=0)


    def show(self):
        """ Show the window"""

if __name__ == '__main__':
    # Create a bigchar window, and show it.
    bigchar = BigChar()

Monday, November 30, 2015

Eric Clapton: Clawing back a life

Read Eric Clapton's autobiography to learn how to enjoy life.

I am a fan of Eric Clapton's music.  I grew up listening to the Rainbow Concert and Bluesbreakers on endless repeat.  I sang the praise of Clapton through school years, debating his prowess over other guitarists.  Clapton was a role-model for my guitar playing: both for a choice of instruments, and the choice of music.

So it is surprising that I held off reading his biography till much later.  A few months ago, to be precise, I started reading it.   I knew about much of his life, I had memorized early details of his career.  So I was pleasantly surprised that there was much to learn.  Now that I am a father, I found myself identifying with his later years.  Clapton's love of life, and his joy in family struck me more than his prowess with the guitar.

I started out loving Clapton, the guitar God.  Now I am in awe of Clapton, the daddy.

Clapton's book goes in chronological order through his life.  His early years, a tough childhood, and his early success at music.  The writing style is easy, and his honesty comes through.  He is honest on his academic failure, and his eagerness to regain a life through music.  He is honest about his shaky relationship with women, and his hesitation in approaching them.  He is honest about the success of other musicians around him, and how much they helped him.

The most gripping parts were his middle years, "Lost Years" as he calls it.  While I knew that Clapton had struggled with substance abuse, the extent of the abuse shocked me.  The damage it caused to his family and friends, and his life was horrifying.    And his recovery and relapse into alcohol addiction was equally saddening.  One of the most brilliant guitarists of our age, a God, struggling with such a mortal weakness!  Clapton's frequent references to his diary are impressive.  Having kept a journal, he was able to dig back into those years and paint a clear picture.  Deep in the midst of his substance abuse, he frequently forgets entire days and even concerts that he played at.

Clapton's recent years are the most mysterious to me.  In the recent past, I started listening to more blues, B.B. King, more jazz, Louis Armstrong.  Over time, my own musical interests have changed.  While I still enjoy the older albums, I find the recent Clapton albums much more to my taste.  "Riding with the King" with B.B. King and Clapton was stellar, as was "Reptile" and "Me and Mr. Johnson".  I found myself identifying more with the cleaner blues sound.  However, by this time I had lost touch with Clapton's life, and didn't idolize him as much.  So it was a complete surprise to find that Eric Clapton had not just recovered from substance abuse, but had found a satisfying family life.  There was an adorable picture of Clapton playing guitar in his children's room.  The mother is sitting on a couch reading a book as the children play by.  It nearly brought a tear to my eye.  I identified with the joy of playing the simplest melody to my kids, and reflecting in their wonder.  I could see my own daughter sing happily at the top of her voice while I struggle to play along.  I could see my son asking me to strum his favorite song, no matter how poorly.

I found that Clapton was a new role model to me.  Not a guitar god who is aloof from humanity.  But a gentler, kinder man.  A person who has prevailed over harsh times.  A parent who provides comfort.  A parent who is glad just to be around the children.

A guitarist who plays for himself and his kids.

Image, courtesy Amazon.

Friday, July 31, 2015

Book Review: Lead Guitar Harvey Vinson

I'm learning Lead guitar from Harvey Vinson's book.  I came across this book in a used bookstore, and it came with flexible record.  Apparently, this is was how music was shipped in the Analogue age.  The technology involved is quite impressive: you can stamp these flexible records out quite cheaply, they are flexible and light.  And you can play it without electricity, if you have a hand-cranked turntable.

So I was talking about this book with my friends, and one of them offered to transcode the record into a format for the digital age.  Mr. DM took the book from me, and returned me three files containing both sides of the record. Side One has a short tuning prelude, and then a rhythm backing track for standard blues in G.  Side Two has tuning, a triplet blues backing track in G, and a track for turnaround in G.

The choice of G is interesting: I much prefer the key of A.  While you are learning, it doesn't matter.

The book goes through the pentatonic scale, the blues variation, and shows you how to build lead for a standard 12 bar blues song.  With the backing track, you can practice and make your own song.  This format works very well: you are learning something basic, and then the book gives you enough understanding to start making your own music.  To a student, this is the most fulfilling part of learning: creating something new.  Many guitar books start the student out on basic tunes.  For a while this is sufficient, but many students tire out of playing dull tunes.  It is exciting to be able to make your own music, to record it, and share it with friends.  This book gives you enough insight that you can start learning the basic of 12 bar blues, and gives you enough starting points to develop your own solos, and your own riffs.

Here are the audio files, in case you have this obscure book, and are stuck with a record that you cannot play any more.  You can download these files and play them locally as backing tracks while you are using the book.  You can also add the audio track to your existing audio project (Logic Pro or Audacity) and record your lead guitar in a separate track.

Side One
Side Two Triplet Blues in G
Side Two Turnaround in G 

 Courtesy: DM for doing an amazing job at converting the record.

Thursday, July 30, 2015

Limiting the rate of ssh connections

The internet is a wild place.  I have an SSH server that is open.  The machine is locked down with very few accounts, all with long passwords, but that doesn't deter attackers from trying to get into the machine.  Most attacks are against the root account, which is futile since the root password is hopelessly long.  And it only accepts public key authentication on that account.

Here is a script I use to limit the number of ssh connections.  As a sample, I show how to rate-limit connections to two ports (222 and 2222) down to one connection in a 60 second window.  Most automated attack scripts back off very rapidly when they notice that they don't get through.  So this easy remedy is enough to thwart a majority of the bot-infested machines.


# Clear all chains
/sbin/iptables -F
/sbin/iptables -L -v -n

# Create a new chain to log and then to drop
/sbin/iptables -N LOGDROP
/sbin/iptables -A LOGDROP -j LOG
/sbin/iptables -A LOGDROP -j DROP

# The external ports 222 and 2222 need to be rate limited.
iptables -I INPUT -p tcp --dport 222 -i eth0 -m state --state NEW -m recent --set --name FIRST
iptables -I INPUT -p tcp --dport 2222 -i eth0 -m state --state NEW -m recent --set --name SECOND

# One connection in a 60 second window.
iptables -I INPUT -p tcp --dport 222 -i eth0 -m state --state NEW -m recent  --update  --name FIRST --seconds 60 --hitcount 1 -j LOGDR
iptables -I INPUT -p tcp --dport 2222 -i eth0 -m state --state NEW -m recent  --update  --name SECOND --seconds 60 --hitcount 1 -j LOG