libRtttl cplusplus library

libRtttl – A c++ library (with UI) for compressing/optimizing the RTTTL audio format

Introduction

The following article is about another library that I have written: libRtttl is a c++ library for manipulating the RTTTL audio format.

Skip to the download section for quick download.

See this post for a quick recall of the RTTTL format.

Edit: for a better understanding and examples of what libRTTTL can do, please see this post: How to convert arduino code to actual RTTTL melodies using libRtttl and AnyRtttl.

Purpose

RTTTL songs available on the internet are not optimized for the smallest size. Still they are readable by a human but they are far from optimized for the arduino embedded device. Most RTTTL song can be optimized (without quality loss) and have their size reduced by ~20%. Conversion to binary allows even better compression but it wont be human readable anymore.

The library also allows one to convert an RTTTL song to hardcoded function calls (tone() and delay() calls). This is really useful if your dynamic memory is almost completely full and you would like to move the load from dynamic memory to program storage space.

The UI (rtttlOptimizerGui) also features a basic RTTTL player. The quality of the playback is limited but it allows one to prove that a compression did not changed the song’s melody.

Library

Functionalities / Features

Possible use are:

  • Optimize an existing RTTTL melodies.
  • Convert RTTTL melodies to other format:
    • MIDI.
    • Arduino tone() and delay() function calls.
    • Binary format (10 bits per note) for maximum compression (minimum size).
    • Binary format (16 bits per note) for quick note offset addressing and minimum code footprint.
  • Convert arduino tone() and delay() function calls back to a valid RTTTL melody.
  • Allow relaxed RTTTL settings for easier conversions.

Note that RTTTL binary format requires an external arduino library for playback. The AnyRtttl library supports all common RTTTL format including the binary format generated by this library.

Requirements

The library has some compilation requirements and library dependencies which are:

libRtttl

  • Only Visual Studio 2008 projects files are available. Files can be manually moved to other platforms or IDE as required. Supporting other compilers is not currently planned.
  • Requires code from BitReader and AnyRtttl arduino libraries, libMidi and win32Arduino win32 library which are all already included in source code.
  • The Google Test v1.6 library is required for running test cases.

rtttlOptimizerGui

  • Requires the wxWidgets Cross-Platform GUI Library v2.8.12 for compiling.
  • The UI design files must be edited with wxFormBuilder  v3.0.57 (a RAD tool for wxWidgets GUI design). This tools is not required for compilation.
  • NSIS: Nullsoft Scriptable Install System is required for generating the installation package.
  • The Play RTTTL & RTTTL 2 MIDI functionalities require the Windows platform for runtime.
  • The code uses some compiler-dependent instructions for debugging the UI under Windows.
  • All non-portable code is protected with #ifdef _WIN32 to be cross-platform friendly.

Usage

All functionalities of the library are available through the UI.

Start the UI by launching rtttlOptimizerGui.exe.

rtttlOptimizerGui with default options

rtttlOptimizerGui with default options

Copy & paste the input data in the Command Input text area. Click on one of the available command buttons. The output result will be displayed in the Command Output area.

Optimize

Copy & paste a valid song in RTTTL format in the Command Input section.
Press the Optimize button to compress the RTTTL code to its shortest form.

During optimization, the library will convert the input RTTTL song to “raw” data and re-encode the result back to RTTTL using all possible combinations of default duration, default octave and BPM (Beats per minute).

From all combination possibilities, the library will select the shortest one. For example, the following RTTTL code from The Legend of Zelda :

can be optimized to the following code:

which reduces the original code length by 89 bytes (from 446 bytes to 357 bytes), that is a 19.95% size reduction.

Play RTTTL

Copy & paste a valid song in RTTTL format in the Command Input section.
Press the Play RTTTL button to play the melody.

Note that application will momentary freeze during the playback. The application will respond again once the melody is done playing.

RTTTL to MIDI

Copy & paste a valid song in RTTTL format in the Command Input section.
Press the RTTTL 2 MIDI button to convert the melody to the MIDI sound format and automatically play the melody.

Note that MIDI sound format offers much better playback capabilities and sound quality than beeping each tone. See my post about my MIDI library for more details: libMIDI – A C++ open-source library for generating single tone melodies in MIDI format.

tone() to RTTTL

Copy & paste a valid song in arduino code (tone() and delay() function calls) in the Command Input section.
Press the tone() 2 RTTTL button to encode the melody into RTTTL.

For example, the following arduino code:

is encoded into the following RTTTL code:

This feature is useful for moving data from program storage space to dynamic memory.

RTTTL to tone()

Copy & paste a valid song in RTTTL format in the Command Input section.
Press the RTTTL 2 tone() button to decode the input song to “raw” data (note with frequency and duration) and generate the arduino code required to play the song.

This process is really useful if your dynamic memory is almost completely full and you would like to move the load from dynamic memory to program storage space.

For instance, the RTTTL code from Super Mario Bros. – 1-up sound :

generates the following arduino code:

RTTTL to Binary (10 bits)

Copy & paste a valid song in RTTTL format in the Command Input section.
Press the RTTTL 2 Binary (10 bits) button to decode the input RTTTL song and encode the same melody into a binary RTTTL format where each note is encoded into 10 bits. The bits definition is already documented in section RTTTL binary format definition of AnyRtttl library post.

The AnyRtttl library can be used to play the compressed RTTTL data.

For example, the RTTTL code for playing Tetris theme song:

is compressed to the following :

which reduces the original code length by 104 bytes (from 159 bytes to 55 bytes), that is a 65% size reduction.

RTTTL to Binary (16 bits)

Copy & paste a valid song in RTTTL format in the Command Input section.
Press the RTTTL 2 Binary (16 bits) button to decode the input RTTTL song and encode the same melody into a binary RTTTL format where each note is encoded into 16 bits. The bits definition is already documented in section RTTTL binary format definition of AnyRtttl library post.

The AnyRtttl library can be used to play the compressed RTTTL data.

For example, the same RTTTL code for playing Tetris theme song:

is compressed to the following :

which reduces the original code length by 73 bytes (from 159 bytes to 86 bytes), that is a 46% size reduction.

License

libRTTTL Library / rtttlOptimizer
Copyright (C) 2016 Antoine Beauchamp
The code & updates for the library can be found on http://end2endzone.com

AUTHOR/LICENSE:
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  Lesser General Public License (LGPL-3.0) for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

DISCLAIMER:
This software is furnished “as is”, without technical support, and with no warranty, express or implied, as to its usefulness for any purpose.

Download

You can download the libRTTTL library / rtttlOptimizer by clicking on the following links:

Download “rtttlOptimizer v1.2.195 Source Code” rtttlOptimizer-v1.2.195-src.zip – Downloaded 554 times – 186 KB

Download “rtttlOptimizer v1.2.195 win32 portable” rtttlOptimizer-v1.2.195-win32-portable.zip – Downloaded 541 times – 720 KB

Download “rtttlOptimizer v1.2.195 win32 Setup” rtttlOptimizer-v1.2.195-win32-Setup.exe – Downloaded 649 times – 834 KB

Download “rtttlOptimizer v1.2.195 x64 portable” rtttlOptimizer-v1.2.195-x64-portable.zip – Downloaded 545 times – 847 KB

Download “rtttlOptimizer v1.2.195 x64 Setup” rtttlOptimizer-v1.2.195-x64-Setup.exe – Downloaded 664 times – 969 KB

Leave a Reply