Sunday, October 29, 2006

Pi Music

I must have spent most of my life facing one keyboard or another. Over a year ago, while facing a musical kind, I was in a weird whimsical mood and I wrote a tune based on the digits of pi.

It did not surprise me when I discovered I was on a well-beaten path, as a search on Google quickly reveals. After all, many pi-inspired feats have been accomplished, such as setting Edgar Allen Poe's poem "The Raven" to the digits of pi. See also the honourable mention in this ray-tracing competition.

The pi music I found was quite abstract, whereas I want mine to sound more standard. I chose to have the digits 1-7 represent the tones of the C minor scale, altered if necessary, while 8 and 9 wrap around, that is, I encoded 8 and 1 the same way, as well as 9 and 2. Accordingly, I would have treated a 0 as a 7 but the tune ends just before reaching the first 0. (Thus 31 decimal places are encoded, this number fortunately corresponding with the first two digits of pi.)

This is akin to jianpu, a numbered musical notation system which I see in many Chinese books, but have never encountered in the Western world though supposedly it has seem some use in Europe. My scheme differs in that with jianpu, one would normally notate a minor key starting from 6, not 1.

It's a shame jianpu is not more widespread. It is well-suited for quickly and concisely jotting down simple melodies. A blank piece of paper suffices: there is no need to rule a music staff first. Writing numbers and dashes can be done extremely fast and most people are accustomed to this, unlike drawing geometric figures. More sloppiness is tolerated, as it is easier to identify scrawled digits and dashes than it is to figure out which line or space a hastily-scribbled note lies on, or whether a certain dot belongs to a note or was just a slip of then pen. Jianpu is also easy to learn due to its logic and simplicity. (Of course, jianpu performs terribly for even a slightly complex piece.)

(To be precise, jianpu-like notation is in fact employed in Western music. For example, when one writes "C6" to denote a chord, the "6" refers to the sixth note of the major scale.)

I added extra notes in order to produce a repeated motif typical of tunes. Following the digits exactly produces a melody that sounds a little too random for my tastes.

I chose conventional chord progressions. The A section uses a chromatically descending bassline, and the bridge mostly follows the cycle of fifths.

The time signature is 3/4, as this resembles 3.14 superficially. Also, in early (Western) musical notation, this time signature was represented with a circle.

I had intended to post it only once I had made some recordings (ideally after working on Bliss) but I have enough on my plate as it is. Maybe next Pi Day.

Anyway, it feels like an auspicious time to release it, as I happened to have recently released version 0.3.14 of the PBC library. And not too long ago Akira Haraguchi broke the world record after memorizing 100,000 digits of pi.

The lead sheets were created using Lilypond. The Lilypond website convincingly argues that their software automatically produces better sheet music than their major competitors, including commercial ones. It's also easy to integrate its output with HTML:

Incidently, around the same time I also penned a tune that loosely encodes a friend's name, but using a different method:

Wednesday, September 27, 2006

CMake and Autotools

I'm very grateful that somebody (Joe Cooley) contributed Autoconf and Automake files for the PBC library that I maintain. The GNU Autotools are almost essential because many people expect its presence and are used to compiling packages on Unix by typing ./configure followed by make. For the same reason, the Autotools give the project a more mature appearance. And they really do allow painless compilation on many systems.

But there is a dark side to using the GNU build system. The PBC source packages increased by several hundred kilobytes. I notice this extra weight because I often use wireless networks. Additionally, build times are at least twice as slow. On one run, it took 37 seconds to compile PBC, compared to 13 seconds with the original handwritten Makefile. And that's not counting the 13 seconds for ./configure and the 4 seconds for ./setup.

Furthermore, one has to be fluent in m4 and various configuration file formats, and understand how the toolchain works to create and maintain the build system. I was never motivated enough to learn all this, and I haven't changed. Any modifications I make to the Autoconf and Automake files are based on what I see there already, or a brief Google search.

It seems some people were sufficiently dissatisfied with the Autotools that they offered a substantial sum to anyone who wrote a better build tool. The winning project was SCons, which I experimented with last time I needed a build tool with more features than make.

But now a different tool has caught my attention, ever since I read this article. The KDE project ran into trouble with the Autotools, and began searching for alternatives. Like me, they found SCons promising, but after further investigation decided to switch to CMake.

Porting KDE to different platforms is highly nontrivial, so I reasoned that if CMake worked for such a substantial project, odds are good that it would work for my tiny library. I'm following by example I guess, and it certainly isn't the first time. I use git because it works for managing the Linux kernel source, and I use C because it works for countless important parts of the system (including of course the kernel), and for many well-respected programmers across several generations.

While researching CMake I found more arguments for switching: autoconf/automake is insane, glowing reviews of CMake (in the comments section), another endorsement and an interview with a CMake author.

I had a CMakeLists.txt file up and running fairly quickly, as the syntax is quite simple. Running times are much better: CMake took a couple of seconds to generate a Makefile, which in turn only took a second longer than my handwritten one to compile PBC. I was also pleased by the clean and colourful output.

Unfortunately, CMake is not installed on many systems. And it must be relatively untested. How does it compare to the Autotools on less mainstream platforms? And what about the more obscure tasks that the Autotools excel at? For example, is it difficult to get CMake to cross-compile a project?

For now, I'll have to maintain two sets of build configuration files in parallel, but I hope one day CMake will be equally well-known and widespread so I can jettison the autoconf/automake baggage.

Thursday, September 21, 2006

A Tale of Two Hacks

Years ago I wrote rcenter which has long been superseded by the LIRC project. I finally switched to LIRC myself. I wonder if anyone out there is still using rcenter.

It took me a while to figure out that LIRC does not work with OSS drivers unless Stephen Beahm's midi poll patch has been applied. I decided to switch to the ALSA drivers to avoid this issue. But then I had to set some other module options: snd-emu10k1 enable_ir=1 extin="0x3fc3" extout="0x1fff".

I wrote xmmspipe almost simultaneously, and in contrast, this project isn't obsolete yet. In fact, I recently discovered it has been an official Gentoo Linux package for some time when I received a bugfix for it.

It's times like these when I feel warm and fuzzy. I get to experience first-hand some of the touted benefits of free software. Having the source open means thankfully someone else can solve the problem. Even if I managed to fix the bug myself, I probably would've spent hours doing so.

On the other hand, I wouldn't mind if xmmspipe were obsolete and named pipes came with XMMS by default. I'm reminded of a quote from Doug McIlroy:

This is the Unix philosophy. Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface.

and one from Rob Pike:

There has been much talk about component architectures, but only one true success: Unix pipes.

Thursday, September 14, 2006

Mental Feats

Michael Curtis (who commented on a previous posting) has written several articles on mental feats involving memory and mathematics.

Coincidently, I too read about the Trachtenberg system (a method for performing mental arithmetic) many years ago.

Reading his site reminded me of a time when I'd relieve boredom during occasions such as high school assemblies by squaring 2-digit numbers in my head using the techniques I had read about.

Today, I'd still use Trachtenberg's method for numbers ending in 5, based on the equation (10 a + 5)^2 = 100 a (a+1) + 25. However I have since found faster methods for other numbers, which I haven't seen described on the web. They only require additions and subtractions, but one has more to memorize.

Let n be the 2-digit number to be squared. Then if n lies in the range:

  1. 0-25: Memorize these answers.
  2. 25-50: Work out how far n is from 50 and how far n is from 25. Then the answer is 100(n - 25) + (50 - n)^2.
  3. 50-75: Compute 100((n-50) + 25) + (n-50)^2. (This is also Trachtenberg's method for squaring fifty-somethings.)
  4. 75-100: Compute 100(100 - 2(100-n)) + (100-n)^2

While I'm at it, I'll record a method for finding square roots (of squares of 2-digit numbers):

  1. Remove the last two digits of the square. Then the first digit of the answer is the largest digit whose square is less than this number.
  2. The last digit of the square tells us what the last digit of the answer could be. If it is 0 or 5, then so is the last digit of the answer and we are done, otherwise:
  3. Let the first digit of the answer is a. Compare the square with the square of 10a+5. If larger, then the last digit of the answer is between 6 and 9, and if smaller, it is between 1 and 4. Luckily, in base 10, the squares of 1 to 4 have distinct last digits. Also the squares of a and (10-a) end in the same digit, so it is now easy to determine the last digit of the answer.

The corresponding algorithm for cube roots is much simpler, because the cube of each digit has a distinct last digit (and similarly with other odd powers).

Friday, August 18, 2006

Slideshows in Firefox

Like many geeks, I frequently use text-based interfaces where normal people use GUIs, with a sense of smug self-satisfaction. Instead of a WYSIAYG word processor I use typesetting software like LaTeX. No fancy website creators for me, I use gvim to edit HTML files. Spreadsheets? I keep data in flat text files and write scripts to process them.

How about presentations? I had used MagicPoint for a few, but I wasn't completely satisfied. For instance, equations were fiddly: I had to write a script that would run TeX to render the equations to encapsulated PostScript and embed the resulting image in the slideshow. I briefly thought about writing my own program. Very briefly. Then I thought about exploiting existing programs instead.

I had come across PinPoint which uses GIMP to produce great-looking slides from a few lines. GIMP was designed to manipulate and display text and images, and is scriptable. But for live presentations, and for certain features I wanted, other programs or scripts would be needed, requiring a fair amount of work.

An idea hit me. MagicPoint can convert slides to HTML. How difficult would it be to modify things slightly so that presentations can be done in a web browser? After all, web browsers also manipulate and display text and images from a simple language. Not only that, they were designed to show different pages in succession. They are also ubiquitous.

One would just need to display pages in fullscreen, and perhaps using Javascript, have certain keypresses cause certain actions such as changing slides and triggering animations and other effects. Soon after experimenting with this, I discovered I was definitely not the first to think about web-based presentations.

The Opera browser has long had a slide show feature (the Opera Show Format), but unfortunately it is not supported by other browsers. I want it to work on Firefox.

Luckily, an alternative, the S5 project, has surfaced, which creates slideshows from a few lines of XHTML, and should work on any standards-compliant browser.

S5 was just what I was looking for. Webpages can contain images, text, visual effects, animations, and so on, and in theory S5 presentations should be able to as well.

MathML in S5

I want to display equations via MathML, but at present one cannot simply embed MathML (or SVG) and change the MIME type of S5 slides accordingly, though a fix exists and will be released.

As a workaround, I use ASCIIMathML. Perhaps this is a good thing. I had intended to put LaTeX style equations in the middle of the HTML and use itex2mml to convert it to MathML, but since ASCIIMathML converts to MathML on-the-fly using JavaScript, I can skip the compilation step. (Other tools to convert human-friendly text to MathML are blahtex, TexToMathML, and TtM.)

There's still the matter of getting the equations to display on Firefox. Until the STIX Fonts are ready, extra mathematical fonts have to be manually installed.

Also, for months now, MathML does not display correctly on certain Linux systems, though a workaround exists [also described here]. And for some reason, S5 is extremely slow on my Debian system, but runs fine on the Windows build of Firefox.

Monday, July 17, 2006

Mnemonic Major Systems

I discovered two things when I tried using the net to brush up on mnemonic major systems (aka phonetic number systems). (If you have no idea what they are read the article before continuing!) Firstly, the only encoding from digits to sounds I've seen online so far is the one published by Harry Lorayne. Secondly, it turned out I didn't need any brushing up at all. The mapping I learnt many years ago was so easy to remember that, not only can I still recall it, but the memory is so strong I cannot use Lorayne's system without confusion.

I came across the major system I use in a book by Jean Hugard. I argue that it is more natural and easier to learn. There were only a few rules to remember:

1,2,3 correspond to the consonants l,n,m respectively. This is easy to remember since the letters require 1,2, and 3 strokes to write. If you happen to know the British sign language alphabet, observe that you place 1, 2 or 3 fingers on the other hand's palm when signing l, n and m respectively.

Some mappings are based on the way you pronounce digits in English. 4 is r (think “fourrr”), 5 is f or v (think “five”). 0 is s or z (think “zero”).

Then there are the digits that look like letters. 6 is b or p, 7 is t, th or d. 9 is k or g. With sufficiently bad handwriting (or fonts), 6 and b are indistinguishable, as are 9 and g (or q, which in English is always pronounced starting with a k or g sound, and always as a k sound in several European languages). 7 and T are also similar.

The only rule I never liked much was the one for 8 (which is not a problem since being the odd one out makes it easy to remember!): “Eight” sounds like “aitch”, which hopefully helps you remember that the sh, ch (and j) sounds correspond to 8.

Digit Consonant(s) Reason
1 l strokes
2 n strokes
3 m strokes
4 r sound
5 f, v sound
6 b, p shape
7 d, t, th shape
8 ch, j, sh special
9 k, g, q shape
0 s, z sound

I also mostly prefer the mapping from playing cards to words as presented by Hugard. Although I dislike the aces been treated specially and agree with Lorayne assigning the names of the suits to the jacks, I believe thinking KH as a groom and QH as a bride for example is easier to learn.

However, there is at least one practical benefit to Lorayne's system. By Benford's law, it is more likely that a number one wishes to memorize begins with a 1, and it is easier to think of a word starting with d, t or th than to think of one starting with l.

Friday, May 26, 2006

Firefox and Manpages

I prefer to read manpages in a web browser rather than in a terminal or a text editor. Konqueror formats manpages very nicely [screenshots here], but I use Firefox, and do not use KDE.

I wish there were a Firefox extension to handle manpages in a similar way to Konqueror. Instead, I use man2html. I setup a Smart Keyword so that typing man firefox for example retrieves the manpage for Firefox.

Unfortunately I feel its output is not as pleasing as Konqueror's. As a stopgap measure, I use the Greasemonkey Firefox extension to add some CSS to make the pages more to my liking.

  • script.js: Greasemonkey script that activates every time man2html is invoked on localhost. Adds a CSS file to the page.
  • man2html.css: CSS file that changes the look of the manpages.