Platonic Music Engine logo

Why I Chose Lua for the Platonic Music Engine or the Battle of Five Languages

A question I’m never asked though I wish someone would because I have this neato long-winded answer and I really like to talk about the Platonic Music Engine every single chance I get is why did I choose Lua as the language for the PME?

For the question that no one asks and that I can’t imagine anyone really cares about I have an answer. It is below.

But first, Lua wasn’t the only language I considered so let’s meet the contenders:

The Contenders

Lua: It is a scripting language and that’s all I really knew about it. The reason it was on the list is that besides being a composer of strange modernist/postmodernist music I’m also something of an amateur typesetter. My weapon of choice being the super nerdy LaTeX. Or more specifically LuaLaTeX. LuaLaTeX is, by most counts, the most advanced and progressive of all the many versions of TeX and family that you can use. What makes it further interesting is that it has Lua as a built in scripting language that allows you to do all sorts of nifty programmy things to your LaTeX files. Since I use LuaLaTeX anyway it seemed like it would be a good idea to learn Lua so that I can do some of those nifty programmy things to my own LaTeX files. Made sense.

Scheme: Similar to Lua above Scheme (a dialect of the venerable Lisp) is the scripting language that the music engraving program Lilypond uses. I use Lilypond to generate my sheet music so it seemed reasonable to learn Scheme so that I can do all sorts of groovy programmy bits in my Lilypond files.

Ruby: According to a developer friend of mine, Ruby is ideal for web-based applications. The PME will eventually be a web-based application. One and one is two or something therefore Ruby gets on the list.

Python & Perl: These are both obvious choices for any application that is using a scripting language. They both have tons of libraries and huge user communities. Perl seems to be losing in popularity these days while Python remains very popular.

Round 1: Noob Friendliness

This was a very simple round. For each language I found the first tutorial I could for someone who had never programmed before and attempted to make it through said tutorial. The results are as follows.

Lua: Super easy for the super beginner! This doesn’t mean that the language is easier than the others just that it’s easier to get started than the others.

Scheme: Wow. Some super hard stuff. Difficult to wrap one’s mind around how you’d actually put together entire programs. Tricky.

Ruby: The first program of the first tutorial I found for Ruby that claimed it was for people who had never programmed before was not the canonical first program “hello world” program. It was 20 lines long. The explanation accompanying it talked about functional programming and inheriting classes. I was lost and so did Ruby.

Python & Perl: Both are easy enough to get started with. Not quite as easy as Lua but both did well.

The ranking after round 1:

1. Lua

2 & 3. Python & Perl

4. Scheme

Loser. Ruby (out of the running)

Round 2: MIDI Library

I had already decided that I was going to use MIDI as the way to represent music within the software. If you don’t know what MIDI is don’t worry, I didn’t either. I just knew that it was numbers representing the notes used in music and that it should be well supported. This round came down to finding a library for each language that would make it easy for me to manipulate MIDI. A library is like a bunch of extra commands that some kind soul provides to make it easy to do something that wasn’t part of a language’s original design.

Lua: I found a library. I did not understand the syntax at all. In fact I still don’t even though I’m using it. But the important thing is that I was able to change the values to create the notes I wanted and that the creator of the library added all sorts of other features that makes MIDI more intuitive for non-programmer musician types like me. Good show.

Scheme: I had a much tougher time finding a MIDI library for Scheme and the one I found had none of the nice extra stuff of the Lua one above and was also like 15 years old. Not a good sign for a language that I was already struggling with.

Python & Perl: Libraries galore! But, none that had all the extra features that the Lua one did. But I would have been fine with either one in the long run. It’s too bad for them I wanted a language that I would be fine with in the short run.

Rankings for round 2:

1. Lua

2 & 3. Python & Perl

4. Scheme

And the Winner Is ...

Lua.

Thoughts From Having Lived With This Decision for 9 Months/2400 Hours of Programming

Turns out that Lua is a perfectly fine language. It doesn’t have as many commands built into it as most other languages but that’s by design. I think one of the benefits of this is that it’s really fast. And it is. And if I were able to use LuaJIT instead of the main Lua branch then it would be insanely fast. I still might change.

One issue that does plague Lua is the size of its user base and the fact that it does have a major fork. This raises very real questions concerning long-time viability. Of course this is an issue for most other languages (with the exception of C, perhaps) but for Lua it’s a bit more concerning than say for Python.

I kind of wish I had gone with Scheme. I have yet to have a need to do any nifty programmy things in my LuaLaTeX files but I’ve needed to do nifty programmy things in Lilypond. Instead of doing these things myself I’ve had to rely on the kindness of others to do them. And when no one kindly steps forward then I just do without. But of course the question remains: would I have been able to have gotten good enough at this rather difficult language to have done those nifty programmy things anyway?

In round 2 above I said that I the MIDI library I found for Python didn’t have all the extra cool features that the one for Lua did. There’s a great irony at work here. That exact same library written by the exact same person does exist for Python. But because Python is soooo popular and has so many libraries (MIDI or otherwise), this particular MIDI library did not show up early enough in my searches for me to use it. Had it then Python might have been the winner. Basically Python’s popularity worked against it. Still, it would have been a good choice. I think.

But in the end I’m happy with Lua. And there’s another benefit to using it that I wasn’t aware of in the beginning. It turns out that Lua is used as a scripting language for video gamers to use to mod a lot of video games. So theoretically there might be this whole big user base that is already familiar with Lua and who then would be able to create their own style algorithms for the PME. Of course the real truth of the matter is that’s probably only like two people who are video gamely and musically nerdy enough to match that description.

So for reasons that are only somewhat unmuddied, Lua is the language for the PME.

Copyright 2015 David Bellows

Colophon