How to convert arduino code to actual RTTTL melodies using libRtttl and AnyRtttl

Introduction

The following article is written in order to demonstrate how my arduino libraries libRtttl & AnyRtttl can be used to convert arduino code (tone() and delay() functions calls) into actual RTTTL melodies.

This article shows how to convert ‘Game of Throne theme‘, ‘Star Wars – The imperial March‘ and most Super Mario Bros. sound themes (Main theme, 1-up life sound, Flagpole fanfare, Death and Game Over sounds) into actual playable RTTTL melodies.

Methodology

The basic idea is to convert the code to their lowest common denominator which is tone frequencies and duration (delays). The libRtttl gui program has build-it functionnality to extract the frequencies and delays from arduino code. Using the application, one can convert any melody to RTTTL.

Handling structured or logical code

Some programs are written in an optimized fashion where reapeated sections of a melody are extracted into a function. ie: playRefrain(). The function is then called multiple times to play the whole melody. This method is an effective way to reduce code size but libRtttl gui is unable to parse this code.

Other programs could also use tone() or delay() calls using Constants. A typical usage of this method is to use predefined note names for frequencies which makes the code easier to read. However, constants are also hard to extract by libRtttl gui which does not yet support this feature.

The following code shows the usage of note names as arguments to the tone() function:

To hande these kind of program, the following code can be added at the beginning of the program. This code will output all calls to the tone() and delay() functions to the serial port.

Note that the code above is later refered to as “Arduino tone and delay functions overrides“.

Samples

The following section shows example of using libRtttl gui to convert arduino code (tone() and delay() functions calls) into actual RTTTL melodies.

Game of Throne

Source code:

Modify the code by adding the “Arduino tone and delay functions overrides” to the code. Once done, the code should be looking like this:

which result in the following data outputted on the serial port when executed:

Using libRtttl gui, the above code can be converted to the following RTTTL melody:

Settings: force default, enable tone() octave offsetting.

Game of Throne RTTTL:

or the following (by enabling unofficial BPM values) which help have accurate delays between notes:

Star Wars – The imperial March

Source code:

Modify the code by adding the “Arduino tone and delay functions overrides” to the code. Once done, the code should be looking like this:

which result in the following data outputted on the serial port:

Using libRtttl gui, the above code can be converted to the following RTTTL melody:

Settings: force default, enable tone() octave offsetting

Star Wars – The imperial March RTTTL:

or the following (by enabling unofficial BPM values) which help have accurate delays between notes:

Super Mario Bros. themes

Source code:

Modify the code by adding the “Arduino tone and delay functions overrides” to the code. Once done, the code should be looking like this:

which result in the following data outputted on the serial port:

Note that output contains the following function calls: tone(12,0,something);. Unfortunately, the libRtttl does not properly identify these lines as “silence pin 12” but they can safely be removed from the output without any issue.

Using libRtttl gui, the above code can be converted to the following RTTTL melodies:

Settings: force default, enable tone() octave offsetting

Super Mario Bros. themes RTTTL:

or the following (by enabling unofficial BPM values) which help have accurate delays between notes:

Leave a Reply