Category Archives: Programming

soundprint

As some astute observers may be aware, free software isn’t my only nerdy obsession. A quick perusal of my Flickr photos may reveal some of my other interests. If you guessed “taking poor pictures of wildlife”, you’d be pretty close.

Zonotrichia albicollis

Yes, I watch birds.

To make a long story short, a couple of years ago I became quite interested in the vocalizations of birds: learning their calls and songs, learning to identify a bird by ear. It turns out that in order to really internalize a sound, it’s actually very helpful to be able to visualize it. This is generally done with a plot of frequency vs time. To generate these spectrograms, I’ve been using a slightly modified version of spek, which is a great little program. However, I’ve also found myself wishing I could have an easy visual overview of all of the files in a folder so that I could see at a glance what sort of a sound it was.

By happy coincidence, I have just learned some basics of gstreamer, so I thought it would be a nice opportunity to kill a couple of birds with a single stone[1]. So after a couple of hours of hacking, I’ve pushed a git repository for a little utility I’ve tentatively called ‘soundprint’. It generates a sort of fingerprint for sound files: a spectrogram of the first 5 seconds of audio. It also installs a .thumbnailer file so that nautilus can use it to generate thumbnails for audio files.  It’s quite simplistic, but it does what I want it to do.

Spectrogram thumbnails for audio files

I admit that it’s a bit of a niche application. Spectrograms work best on audio that consists of relatively pure tones; music files tend to end up looking fairly similar to eachother. But in the hope that it may be useful to somebody else, there it is.

[1] no birds were actually killed during this process.

How to waste an evening debugging the internals of glib’s qsort implementation

Working on a personal itch-scratching project, I found myself wanting a sortable treeview. So I stuffed my treemodel into a TreeModelSort and set up my sort functions, and everything was happy. Except, when running my application, I kept getting strange segfaults within glib’s qsort implementation. I had set the following function as my sort_func (yes, it’s C++, but it should be fairly self-explanatory):

static int
compare_foo (const Gtk::TreeModel::iterator& a, const Gtk::TreeModel::iterator& b)
{
    std::tr1::shared_ptr<Foo> l1, l2;
    l1 = a->get_value (COLUMN_FOO);
    l2 = b->get_value (COLUMN_FOO);

    if (!l1)
        return -1;

    if (!l2)
        return 1;

    return l1->get_id () - l2->get_id ();
}

Can you spot the error? Running it under valgrind indicated that I was reading data from *before* the start of the array that was being sorted. In fact, the following lines of code in g_qsort_with_data() were causing tmp_ptr to be decreased back past the start of the array:

        while ((*compare_func) ((void *) run_ptr, (void *) tmp_ptr, user_data) < 0)
          tmp_ptr -= size;

Hmm. Why is there no guard to prevent tmp_ptr from being decreased past the start? Then I noticed the following comment up a few lines:

    /* Find smallest element in first threshold and place it at the
       array's beginning.  This is the smallest array element,
       and the operation speeds up insertion sort's inner loop. */

So at this point, the code is assuming that the very first element of the array is the smallest, so compare_func should always return >= 0 when it reaches the start of the array. Aha! So the problem is my compare_func. It turns out I forgot to handle the single case of both values being NULL.

    l1 = a->get_value (COLUMN_FOO);
    l2 = b->get_value (COLUMN_FOO);
+
+   if (!l1 && !l2)
+       return 0;

    if (!l1)
        return -1;

So there’s not really a big lesson to be learned here, but if you ever hit strange segfaults deep inside glib’s qsort while sorting a treemodel, do yourself a favor and double-check that your sort function is sane first. Also, do yourself a favor and run it under valgrind as soon as you get a strange segfault. Knowing the exact point of the invalid read is infinitely more helpful than waiting until that invalid read causes a segfault.

Two Day

We celebrated r’s 2nd birthday a bit over a week ago. She’s now old enough to understand the concept of a birthday, and it’s been really fun to watch her get excited about it.

DSC_9620

She’s also getting to the age where it’s a bit easier to find somebody to watch her while we go out for an evening. In the past couple weeks we were able to go see a theatre show and our first music concert in quite a while (a cd release show for one of my favorite local bands).

It feels like spring has finally arrived and Minneapolis is awaking from a long hibernation. This is definitely the best time of the year. It’s as if everybody’s just been huddled inside waiting for spring and then there’s just the sudden burst of activity. Everybody’s outside without coats (even though it’s still only in the 40s or 50s F), you see neighbors you’ve hardly seen for months. Of course, now that I say that, we’ll probably get hit with another big snowstorm…

DSC_9841

I found a bit of time to hack on the nemiver debugger again lately. Since 0.6.5 I think I’ve closed about 10 bugs, getting the bug count below the century mark once again, which feels nice. I’m hoping I’ll have time to work on new features again in addition to just bugfixes, since there are still a few features missing that I think are really necessary for more widespread adoption.