Saturday, January 31, 2009

Python 3.0 / Hex On!

This week, I've started playing with Python 3.0 (aka Py3k). The final 3.0 version had been released in December, so there is only a release candidate available in the Intrepid repositories, but I didn't care. In fact, it was dead easy to install - simply apt-get install python3 and you're all set!

Among the things, that amazed me the most, is a new fractions module (actually existing since Python 2.6), that provides support for rational numbers! What are rational numbers good for? Well, they're invaluable for pretty much anything, that involves just intonation, to say the least!

For example, here's how I've managed to code a simple Hexany generator in almost no time! Roughly speaking, a hexany is created from several prime (or not so prime) numbers, that are multiplied together in pairs to form a set. To get a scale out of it, the resulting numbers should be divided by a chosen "base note" and reduced to the octave range. So, first of all, we'll need a simple octave reduction function:
def octave_range(fr):
    if fr <= 0: raise ValueError("Invalid frequency ratio")
    elif fr > 2: return octave_range(fr / 2)
    elif fr < 1: return octave_range(fr * 2)
    else: return fr
Now, let's define a "base note" and use set comprehension (another Py3k feature) to fill the CPS with permutations of numbers from the Wikipedia example:
nums = [2, 3, 5, 7]
base = 5 * 7
cmps = {a * b for a in nums for b in nums if a != b}
And thanks to the fractions support, the final step is also going to be the easiest:
from fractions import Fraction
hexany = [octave_range(Fraction(note, base)) for note in cmps]
The code is also suitable for producing dekanies and other scales, based on Wilson's combination product sets. I still have to figure out the proper ways of using them in music though :P

Monday, January 26, 2009


There's not much to write about, but here are several quick updates to keep this feed alive... First of all, Sced source code has been moved to the SuperCollider SVN tree, so any further development is going to continue out there... Another good news is that our favourite text editor - gedit is probably going to have some kind of D-Bus interface for the 2.26 release. So things like ScedDocument (and other means of feedback from sclang) could be finally made possible.

I have moved the few Russian posts in this blog into, thanks to the Blogger import and export feature. It means, that from now on, I shall continue writing in English here and the secondary blog will be in Russian. Looks like it also makes the Blogger interface more convenient, 'cause the engine does not seem to respect the browser language anyway...

And yet one more thing, just in case you'd like to add a little Human look to your code... It can be done with a couple of Ubuntu-coloured themes for GtkSourceView. They're not quite polished yet, but already seem to nicely fit an all-human environment...