Yesterday I was ask to extend my Arduino AudioTools library to support RTTTL. To be honest, I had no clue what this is, so I had to look it up: RTTTL stands for Ring Tone Text Transfer Language and was developed by Nokia to transfer ringtones to cellphones.
It basically descibes the notes and durations in simple text form. My library supports different sound generators and also provides the frequencies of all notes: so all I needed was to provide a parser for RTTTL and link it up with the existing functionality.
The new class is called RTTTOutput and you just provide the generator and output device in the constructor and then you can play the audio by printing the RTTTS string.
Apart from the simple sound generators, we can use the STKGenerator class which provides access to all instruments provided by the STK framework.
You can find a big collections of ringtones on the internet. Here is the link to one extensive example collection.
The examples usually use a very high pitch, so I added the functionality to transpose the played notes by +-n octaves.
Example Arduino Sketch
Here is a simple example that uses an AI Thinker AudioKit as output device and the Saxophone provided by STK. The pitch of the example is too high, so we transpose it down by 2 octaves!
#include "AudioTools.h"
#include "AudioTools/AudioLibs/AudioBoardStream.h"
#include "AudioTools/AudioLibs/AudioSTK.h" // install https://github.com/pschatzmann/Arduino-STK
AudioInfo info(44100, 2, 16);
Saxofony instrument(440); // the stk clarinet instrument
STKGenerator<Instrmnt, int16_t> generator(instrument); // subclass of SoundGenerator
AudioBoardStream out(AudioKitEs8388V1);
RTTTLOutput<int16_t> rtttl(generator, out);
// Arduino Setup
void setup(void) {
// Open Serial
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Info);
// start I2S
Serial.println("starting I2S...");
auto config = out.defaultConfig(TX_MODE);
config.copyFrom(info);
out.begin(config);
// Setup generator
generator.begin(info);
Serial.println("started...");
}
// Arduino loop - play rtttl tune
void loop() {
rtttl.setTransposeOctaves(-2);
rtttl.begin(info);
rtttl.print("ZoVrolij:d=4,o=5,b=125:8g,8c6,8c6,8c6,8c6,8g,8g,8p,8g,8a,8a,8p,8a,8e,8e,8p,8e,8f,8d,8f,8d,8a,8g,8p,8g,8c,8d,8e,8g,c6,8p,8g,8c6,8c6,8c6,8c6,8g,8g,8p,8g,8a,8a,8p,8a,8e,8e,8p,8e,8f,8d,8f,8d,8a,8g,8p,8g,8c,8d,8e,8g,c6,8p,8c6,8c#6,8c#6,8c#6,8c#6,8g#,8g#,8p,8g#,8a#,8a#,8a#,8a#,8f,8f,8p,8f,8d#,8f,8f#,8d#,8a#,8g#,8p,8g#,8c#,8d#,8f,8g#,c#6,8p,8g#,8c#6,8c#6,8c#6,8c#6,8g#,8g#");
delay(3000);
}
Code language: PHP (php)
0 Comments