Theremin-eqsue HTML5

I’ve spent the last few days thinking about theremins, the musical instrument that you play without touching. When I went to see a band called Spaceships Are Cool the other day it triggered my mind. One night I suddenly decided it would be possible to make a theremin that worked using only one hand using a different design.

I plan to make one at some point in the future, when I get the parts in one place… For now I decided that I would build my concept in the browser. I started out making it with a bunch of div tags, which was frustrating, and then I realised that I could use the <canvas> tag from HTML5 to do it.

I threw together a quick page and then began thinking about the sound. I found a page detailing generating sound in Javascript in Firefox 4 and decided to go with that. I installed Firefox 4 and then started playing around with scripting everything. I started out by picking apart Paul Irish’s harmony.js file (view that site in Google Chrome and scroll to the bottom). Then I added in the sound stuff once I had values for the cursor position.

What I ended up with was a very plain page that generates sound (in Firefox 4) with the pitch determined by the cursor position on the X axis, and the volume on the Y axis. The sample time is 50ms, and at the moment there’s a bit of an annoying click. I think I might be able to get rid of that by keeping the audio element playing at 0 volume or something like that, but that’s debugging for another day.

Anyway, if you’re viewing this on my page in Firefox 4 then you should see the finished product underneath. If you’re viewing this in a feed or on Facebook then you’ll want to go here. If you’re not using Firefox 4, or you’re not sure what browser you’re using then this probably won’t work for you and you’re probably very bored of reading this by now.

Browser frustrations

Earlier on I was writing some code that inserted some text into a textarea on a webpage. After the text has been inserted I needed the cursor to be at the end of the textarea so that the user could then type more text in afterwards without any messing around.

I thought it would be a simple matter, but it turned out to be quite frustrating.

Needed for all
var t = document.getElementById('add-comment');

Gecko (Firefox, Mozilla, etc.)
t.focus();

Webkit (Chrome, Safari, etc.)
t.selectionStart(t.value.length);
t.selectionEnd(t.value.length);

Internet Explorer
t.setSelectionRange(t.value.length, t.value.length);
t.focus();

Is there some standard way of doing this that I’m unaware of? I think jQuery would be overkill for this one purpose.

Funny comments in open source software

Whenever I’m looking through code at work I come across various little nuggets of wisdom from whoever wrote what I happen to be working on at the time. Usually it’s just something fairly innocuous and helpful, but sometimes it’s just downright funny.

Based on this I decided to take a look around various pieces of open source software to see what kind of things their developers leave behind for future coders. I’ve gone through and come up with a list of some of the better ones. Enjoy:

/* most drives spin up by 10sec */
/* > 99% working drives spin up before 20sec */
/* give > 30 secs of idleness for retarded devices */

Linux Kernel – drivers/ata/libata-eh.c

/*
* Now, we have to map the power management section to write
* a bit which enables access to the GPIO registers.
* What lunatic came up with this shit?
*/

Linux Kernel – arch/x86/kernel/visws_quirks.c

"""
This insane behavior probably doesn't matter, but we're compatible just for shits and giggles.
"""

Mozilla – source/build/pymake/pymake/data.py

/* There are, alas, devices in this world too dumb
* to read their own hardware colormaps. Sick, but
* true. */

X.org Server – dix/colormap.c

/* Well, I wonder, rfc1812 was written by Cisco employee,
what the hell these idiots break standrads established
by themself???
*/

Linux Kernel – net/ipv4/ip_gre.c

/* NB: this is conceptually wrong, the string returned by setlocale should
* be taken as opaque -- but then we would be in deep shit^Wtrouble. This
* seems to actually happen on Win32.
*/

XBMC – sources/enca-1.9/src/locale_detect.c