Loginskip to content

October 24th, 2006

Making Music With NXT Melodies

Introduction

I want to first thank Eric and Guy for letting me join the nxtasy team. I’ve been impressed for quite some time by the amazing content and community being built here and I wanted to be a small part of it.

And now on to the subject at hand - making music!

Playing Sounds

A very popular feature of the standard NXT firmware is the ability to play wave files which have been converted into NXT sound files (with an .rso extension). The NXT software comes with many sounds which can be used in an NXT-G or NBC program with the standard NXT firmware to make your robot sound cool. Using a wave file to NXT sound file conversion utility you can easily create additional sounds for use in NXT-G and NBC programs.

Unfortunately, even though NXT sound files are fairly low quality (8-bit, mono, 8k sample rate) sounds, they can take up large amounts of flash memory when they are used in a program. The “Writing Efficient NXT-G Programs” pdf document on the First Lego League website recommends that you “minimize use of Sound and Display blocks” in order to more optimally use the available flash memory. With flash memory limited to around 128 kilobytes it can be very important to pay close attention to how much space is taken up by sound files.

Size Matters

All of this brings me to a little known feature of the standard NXT firmware – melody files. What is a melody file? You might think of a melody file, in comparison to a sound file, as being something like a MIDI file in comparison to a wave file. Where sound and wave files contain sound samples (8 thousand of them per second in sound files) melody and MIDI files contain notes which are played for a specific duration. MIDI files are quite a bit more involved than melody files but hopefully the analogy helps explain the difference between sounds and melodies.

If a sound lasts for much more than a split second, the number of samples that are stored in the file causes the file size to grow rapidly. In the world of melody files, playing a single tone for 10 seconds takes only 4 bytes (2 bytes for the frequency or tone and 2 bytes for the duration). An uncompressed .rso sound file lasting 10 seconds would take something like 80,000 bytes.

Of course, melody files can’t be used to reproduce speech and the music that can be played using a melody file won’t be as fancy as that in a sound file. But the size of long melody files will be much smaller than the size of short sound files.

Creating a Melody File

Melody files can be created in a few different ways. Each has its pluses and minuses. Bricx Command Center (BricxCC) has a Brick Piano tool window which can be used to manually construct a series of tones. The resulting “song” can be saved in many different formats, one of which is the NXT melody file format. For simple melodies this is a pretty easy way to create a melody file.

Perhaps an easier way to create a complex melody file is to convert an existing MIDI file into a melody file. The trick with this approach is that many MIDI files use multiple tracks, each of which only play some of the notes which comprise the song. And without a MIDI editor or track viewer it may be hard to tell which track contains the melody as opposed to the bass guitar or drums or whatever. The MIDI Conversion tool in BricxCC can be used to convert MIDI files into the NXT melody file format (as well as several other formats). Using this tool you pick which track you want to convert and the format to use for the conversion.

The easiest way to quickly create a lot of complex melody files is to use the MIDI batch conversion utility which makes it possible to select many MIDI files and convert them all (either the first track or all tracks) into corresponding NXT melody files. This utility, unlike BricxCC, is available for multiple platforms, including Mac OSX, and Linux.

Currently, the MIDI conversion mechanism does not support combining multiple MIDI tracks into a single melody file. That would be a nice feature to have and it is on my list of enhancements that I hope to get to once I get NQC and other high level languages working with the standard NXT firmware.

Playing a Melody File

So you have created a melody file. Now, how on Earth can you hear what it sounds like?

First of all, you should know that the LEGO Mindstorms NXT software does not know how to play an NXT melody file. You can get melody files to show up in the NXT-G Sound block’s list of sounds by a) giving the file an .rso extension even though it is not a sound file, and b) copying the file to your engine\Sounds directory below the main Mindstorms NXT install directory. On my PC the correct directory is at C:\Program Files\LEGO Software\LEGO MINDSTORMS NXT\engine\Sounds.

You should also know that there aren’t any shareware NXT melody players out there for your favorite operating system (OS) like you can find for MIDI files. There is, however, a simple utility that I have written called RMDPlayer. It currently runs only on the Win32 platform. RMDPlayer lets you preview approximately what a melody file will sound like on the NXT. The player uses the computer’s sound card to play the notes in the melody file for the specified durations. Computers can handle a range of frequencies that extends beyond the range supported by the NXT, so a melody file that contains very low notes or very high notes will play differently on the NXT than on your computer. RMDPlayer also lets you save your melody file as a wave file. This means that you can convert from MIDI to NXT melody to Wave to NXT sound if you feel so inclined. :-)

Another way to hear a melody file – exactly as it will sound from a running program – is to use the NXT Explorer tool from within BricxCC. Using this window you can select a melody file on the NXT (and, soon, on your PC) and play it via the Play menu item or toolbar button. If you haven’t downloaded a melody file to the NXT yet then the NXT Explorer window is the perfect place to be since it also lets you drag and drop files from directories on your computer to your NXT. I’ll leave saying anything more about the NXT Explorer window to another post.

Playing Melodies on the NXT

If you write a program in NXT-G which uses a Sound block and you select a melody file from the list of sounds you won’t hear the melody play as you would a sound file. But when you download your program to the NXT and run it, the melody will play as it should. The NXT software will even transfer the melody file to the NXT just as it does for any sound files you select. Long melody files could be played in a Sound block on a parallel beam so that the music plays in the background while the robot moves around and responds to sensor input. NXT-G needs melody files to have a sound file extension (.rso) because the Sound block is hard-coded internally to append “.rso” to the end of the filename (without any extension) that you selected from the lists of sounds.

With NBC you have direct access to the system function for playing files. The function is called using the syscall opcode. The name of the function is SoundPlayFile. You pass this function a structure of type TSoundPlayFile. This structure has Loop, Filename, and Volume members which you set prior to the call to the SoundPlayFile system function. Here is some code which demonstrates how simple it is to play either a sound file or a melody file using NBC:

#include “NXTDefs.h”

thread main
precedes player, worker
exit
endt

thread player
dseg segment
spfArgs TSoundPlayFile
dseg ends
// this thread plays music in the background
mov spfArgs.Filename, ‘mysong.rmd’
set spfArgs.Loop, TRUE
set spfArgs.Volume, 3 // 75%
syscall SoundPlayFile, spfArgs
endt

thread worker
// this thread does all the “work”
endt

With NBC, either via the command-line or when using BricxCC, the files used in NBC programs, such as melody files, sound files, and icon files, are not currently transferred to the NXT automatically. This may change in a future version of NBC but for now you have to transfer these auxiliary files manually. These files can be copied to the NXT using the NXT Explorer tool in BricxCC, the NextTool command line utility, or via NBC with the -b command-line switch:

nbc –S=usb -d -b mysong.rmd
nexttool /COM=usb -download=mysong.rmd

So give NXT melodies a try and let your NXT sing!