Non-blocking playback of RTTTL melodies
RTTTL is Nokia's Ring Tone Text Transfer Language, from when mobile phones could only play a single, squeaky note at a time. People could text each other ringtones like this example...
Combine the term "RTTTL" and your favourite old-skool tune in a google search for more examples.
This Arduino library provides non-blocking playback of RTTTL melodies using a Piezoelectric transducer. Non-blocking means it can update the tones being played by an ATMEGA chip (controlled by hardware timers) to match with a loaded melody, allowing other functions continue to be performed by the microcontroller alongside the melody.
Although based on ponty's arduino-rttl-player changing it to non-blocking playback leaves very little left (@ponty's was a blocking library). Thanks to @ponty for all the RTTTL decoding logic, and the stimulus to create this library.
- pollSong() since the hardware handles timer-driven playback, you can continue to service other microcontroller functions between calls to pollSong(), which will only change the tone output if it is time to do so (according to the melody) and returns immediately.
- stepSong() this progresses the melody, but also blocks until a single note is finished. It is useful, for example, to write simple code which couples lighting effects to notes.
- finishSong() this plays out the whole melody once, blocking until it's finished. It's useful if a tune needs to be completed before the next action should be taken (e.g. playout when a game has ended and before it resets).
N.B. If you use this non-blocking strategy,
pollSong() should be called regularly alongside the other operations in your sketch to ensure the melody timing is correct.