A while ago, I wrote a tutorial on how to get the NBC/NXC running on Linux in order to program an Mindstorm NXT brick. In the tutorial, I posted an example script that turns the robot into a dancer and plays some music. The only problem is that the music was just set of random frequencies and thus was unbearable to listen to. So I though, is there a way to make it less random without the need to make it play the same thing over and over again?
Experimenting with this idea lead me to write the libmidi library. MIDI files are a collection of notes, so it seemed as the best format to use. It allowed me to just generate random notes and tell the MIDI player to play them in some order. The library also includes an example called randommusic, but just does the same thing as the dancing robot. It just plays 128 random notes. This is what it sounds like:
Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.
I am no expert on music theory, but from what I understand most songs contain only notes in one scale. And the idea also works the other way, playing notes in a scale can result in a song. The song won’t be a masterpiece, but it will be bearable. Since I am too lazy to pre-program every possible scale, I decided that I will only limit notes to ones found in an existing song instead of a scale.
After analyzing Piano Sonata No.1, that I grabbed from Kunst der Fuge some time ago, I get the following result:
Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.
It sounds much better than before, but it still sounds like it is all over the place. To fix this, I applied one more change to how the next note is picked by looking at one more piece of information when analyzing the song; the note that was played previously. That is if note B never follows note A in the existing song, then it should never do so in the random music. Here is the result:
Audio clip: Adobe Flash Player (version 9 or above) is required to play this audio clip. Download the latest version here. You also need to have JavaScript enabled in your browser.
That was a big improvement! I think that is where I will stop working on this. But more tweaks could be done in the future:
- Add random rhythm
- Analyze multiple songs
Of course, the code still needs to be ported to NXT to make a much more improved dancer robot.
The script that I wrote to do this can be downloaded here and is released under the GPL3 license. To use it, you need to have libmidi and ROOT installed. This is how it works:
- Analyze the song
./analyze input.mid output.root - Generate a random tune.
- The first argument is the track of the orignal song to analyze, and should be set as track1, track2 and so on. Also you can set it to song, which uses all tracks.
- The second argument is the ROOT file with the analyzed song created in step 1
- The third argument is the output MIDI where the result will be saved
- The fourth argument is optional. Specifying it will ignore any previous note conditional probability.
./generate track input.root output.mid [unconditional]
Yesterday John Boswell released a new song in his Symphony Of Science series called The Poetry of Reality. This one stars scientists like Michael Shermer, Jacob Bronowski, Carl Sagan, Neil deGrasse Tyson, Richard Dawkins, Jill Tarter, Lawrence Krauss, Richard Feynman, Brian Greene, Stephen Hawking, Carolyn Porco, and PZ Myers. You can download the MP3 here.
Now someone just has to create that robot with an NXT brick inside it and have follow preprogrammed paths.
[via: Woodgears]
I spent the last few days of my winter vacation experimenting with generating random music. This was inspired by my simple NXT Dancer robot, which currently just plays random tones. The idea behind this latest experiment is that (1) I build a probability density function of played notes from well known music and (2) I use that PDF to pick the notes to be played next, based on the previous note. I will post more details about this in a few days. But the important part is that I used MIDI files, which contain commands like “play this note” and “use this volume” to recreate the actual music.
Being lazy, I decided not to search a lot (I trie a bit) for a pre-made library for reading MIDI files and instead I decided to create my own. The result is libmidi, a very simple MIDI reading and writing library. To parse a MIDI file, just do this:
MIDI::File file("/path/to/file.mid"); file.open();
Then the contents of that file are available as they were found in the MIDI file. For example, to get a track and print out all of the events (the commands that tell the MIDI synchronizer what to do), do this:
MIDI::Track* track=file.track(0); for(int j=0;j<track->numEvents();j++) { MIDI::Event* event=track->event(j); event->debug(); }
Also libmidi contains a basic write support. You can add new events to the end of a track and call the MIDI::File::write() function to save the file that the track represents. See the randommusic example, included in the libmidi-0.1 package, on how to do this.
The downside to using the libmidi library right now is that it is not well developed yet. For example, there are only a few functions to modify the MIDI file (ei: tracks can only be appended to the end of the track) and not all meta event types have a nice define yet. Of course, I plan to fix this in the future.
But for now, you can download version 0.1 or checkout the latest development code at GitHUB. The API documentation is available here.
For more information about MIDI files, check out these websites:
Over the last few days, I’ve noticed that Wordpress started converting all of the special HTML characters into their proper HTML entities (ei: & will be translated into &), even though I explicitly specified that I do want the character itself. This interfered with the wp-syntax plugin that I use when I post snippets of code, because the plugin assumes that non-HTML encoded code is being used. It applied the conversion to HTML entities after all that is done. But now because of Wordpress’ over-zealous compliance to XHTML specs, when the following is part of a post:
<pre lang=”php”>if(true && false) echo “Hello World”;</pre>
You will see the following:
if(true && false) echo "Hello World";
Luckily, the fix is quite simple. It does not even involve using the seemingly abandoned role-manager plugin as suggested by wp-syntax’s FAQ. Just add the escaped=”true” argument to the pre tag, and wp-syntax will decode the HTML entities for you automatically. Now
<pre lang=”php” escaped=”true”>if(true && false) echo “Hello World”;</pre>
turns into
if(true && false) echo "Hello World";
To make things easier, I suggest that you install the wp-syntax-button plugin. This plugin adds a “Code” button to the visual Wordpress editor, using which you can automatically highlight any code without having to switch to the HTML editor. And of course, the wp-syntax-button plugin automatically adds the escaped=”true” argument.

For Christmas, I’ve got a fun little toy: The LEGO MindStorm NXT 2.0 Kit. It came with a NXT brick and a CD containing software for programming it. However that software only runs on a Windows and Mac, neither of which I use regularly. But the MindStorm franchise has a large hobbyist community, so large that LEGO even saw it fit to to open source the firmware on the NXT brick! This made it easy for people to develop tools that run on Linux.
After some quick searching, I’ve decided to settle with Next Byte Codes (NCB) and Not eXactly C (NXC). These are two text-based (as opposed to the official graphics-based) programming languages. NCB is very similar to assembly, and NXC is very similar to C. Of the two, I use NXT, because I find that C is better at making complex code more readable. Of course, that is just my personal preference.
In this tutorial, I want to provide a quick guide about settting up and using the NCB/NXC compilers to write code for the NXT brick. This is not a programming guide, as several already exist and are listed on the NCB/NXC site (NXC Guide, NBC Guide, NBC Tutorial, and also a book can be purchased!). But those tutorial suppose that you are using the Windows-only bricxcc IDE to transfer code to the NXT brick. They do not document the steps that you have to take by using the tools directly, which is what you have to do on Linux.
There are several parts to this tutorial:
- Downloading and setting up the tools
- Transferring code over USB
- Transferring code over Bluetooth
This tutorial was written and tested using Ubuntu Karmic Koala, but it should work fine on other distributions. In fact I mainly use a Gentoo desktop to program my NXT brick, but it does not have Bluetooth so I would not be able to compile this entire tutorial on it.
Finally, during the tutorial I use a sample program called “dancer”, that can be downloaded here. The package contains a version written in NCB and a version written in NXC. It makes a car-bot, built by following the instructions in the booklet from the Mindstorm box, dance by following random instructions (move, turn, make sounds). The result are very boring, but very simple and thus serve their purpose in this tutorial.
If you have any questions about this tutorial or how to accomplish other things, do not hesitate to contact me.
Merry Christmas, and an even more merry Boxing Day tomorrow! But most importantly, take some time to remember that today (in Julian Calendar) is the birthday of a man without whom we wouldn’t have the world as we know it today.
[Image Source: Wikipedia]
A week after the 0.3 release of my AutoRotate daemon, a big bug was found (Thank you, Jonas). By default on Ubuntu systems (and probably others), system buses are not allowed to be created unless they are specified as a special exception in a file. And for 0.3, I’ve forgot to include this special file in the release so if you tried to run the auto-rotate.py program, you would get this error:
Traceback (most recent call last):
File “/usr/bin/auto-rotate.py”, line 50, in
main()
File “/usr/bin/auto-rotate.py”, line 40, in main
busname = dbus.service.BusName(“net.krizka.autorotate”,system_bus);
File “/usr/lib/pymodules/python2.6/dbus/service.py”, line 129, in __new__
retval = bus.request_name(name, name_flags)
File “/usr/lib/pymodules/python2.6/dbus/bus.py”, line 306, in request_name
’su’, (name, flags))
File “/usr/lib/pymodules/python2.6/dbus/connection.py”, line 620, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.AccessDenied: Connection “:1.148″ is not allowed to own the service “net.krizka.autorotate” due to security policies in the configuration file
This is now fixed in the 0.4 release of AutoRotate, the sources for which you can download here and follow these instructions to install it. Also, the Ubuntu package is currently being build in my PPA and will be installable soon, following these instructions.
Finally, there are a few other changes in this release. The most notable being the employment of libnotify, by the manual-rotate.py script, to notify you when the automatic rotation has been enabled/disabled.

The full change log is as follows:
- Added .conf file to allow creation of system bus
- manual-rotate.py notifies the user when automatic rotation is enabled/disabled via libnotify
- Check if autorotate daemon already running before starting another one
- Added dbus .service file describing the autorotate service
- Fixed the dependencies in the debian control file
One missing piece in Linux tablet support is an easy way to automatically rotate the screen, based on either the orientation of the laptop or other parameters. I’ve tried to fix this by writing a simple daemon that uses the hdaps driver to check the orientation of the tablet and calculates the rotation. This new version contains a lot of improvements over the previous hack-job, in that it:
- Can be controlled via dbus
- Directly determines laptop vs tablet mode from hardware, and no longer depends on ACPI events
- Much easier to install (using a deb from my PPA or a setup.py script)
The new version, 0.3, can be downloaded from here with installation instruction here (Ubuntu) and here. More up-to-date details can be found on the AutoRotate project page.
I’ve only tested it on my laptop, which is a ThinkPad X61T running Ubuntu Karmic Koala. But there is no reason why it shouldn’t work on other hardware that uses hdaps and other distributions.



