PX4 Firmware
PX4 Autopilot Software http://px4.io
|
Library for parsing tunes from melody-strings or dedicated tune messages. More...
#include <tunes.h>
Public Types | |
enum | NoteMode { NoteMode::NORMAL, NoteMode::LEGATO, NoteMode::STACCATO } |
Public Member Functions | |
Tunes (unsigned default_note_length=TUNE_DEFAULT_NOTE_LENGTH, NoteMode default_note_mode=NoteMode::NORMAL, unsigned default_octave=TUNE_DEFAULT_OCTAVE, unsigned default_tempo=TUNE_DEFAULT_TEMPO) | |
Constructor with the default parameters set to: default_tempo: TUNE_DEFAULT_TEMPO default_octave: TUNE_DEFAULT_OCTAVE default_note_length: TUNE_DEFAULT_NOTE_LENGTH default_mode: NORMAL. More... | |
~Tunes ()=default | |
int | set_control (const tune_control_s &tune_control) |
Set tune to be played using the message. More... | |
void | set_string (const char *const string, uint8_t volume) |
Set tune to be played using a string. More... | |
int | get_next_note (unsigned &frequency, unsigned &duration, unsigned &silence) |
Get next note in the current tune, which has been provided by either set_control or play_string. More... | |
int | get_next_note (unsigned &frequency, unsigned &duration, unsigned &silence, uint8_t &volume) |
Get next note in the current tune, which has been provided by either set_control or play_string. More... | |
unsigned int | get_default_tunes_size () const |
Get the number of default tunes. More... | |
unsigned int | get_maximum_update_interval () |
Private Member Functions | |
uint32_t | note_to_frequency (unsigned note) const |
Convert note to frequency. More... | |
unsigned | note_duration (unsigned &silence, unsigned note_length, unsigned dots) const |
Calculate the duration in microseconds of play and silence for a note given the current tempo, length and mode and the number of dots following in the play string. More... | |
unsigned | rest_duration (unsigned rest_length, unsigned dots) const |
Calculate the duration in microseconds of a rest corresponding to a given note length. More... | |
int | next_char () |
Find the next character in the string, discard any whitespace. More... | |
unsigned | next_number () |
Extract a number from the string, consuming all the digit characters. More... | |
unsigned | next_dots () |
Consume dot characters from the string. More... | |
void | reset (bool repeat_flag) |
Reset the tune parameters. More... | |
int | tune_end () |
int | tune_error () |
Private Attributes | |
const char * | _next_tune = nullptr |
next note in the string More... | |
const char * | _tune = nullptr |
current tune string More... | |
const char * | _tune_start_ptr = nullptr |
pointer to repeat tune More... | |
int | _current_tune_id = static_cast<int>(TuneID::NONE) |
bool | _repeat = false |
if true, tune restarts at end More... | |
unsigned int | _default_note_length = TUNE_DEFAULT_NOTE_LENGTH |
NoteMode | _default_note_mode = NoteMode::NORMAL |
unsigned int | _default_octave = TUNE_DEFAULT_OCTAVE |
unsigned int | _default_tempo = TUNE_DEFAULT_TEMPO |
unsigned int | _note_length = TUNE_DEFAULT_NOTE_LENGTH |
NoteMode | _note_mode = NoteMode::NORMAL |
unsigned int | _octave = TUNE_DEFAULT_OCTAVE |
unsigned int | _tempo = TUNE_DEFAULT_TEMPO |
unsigned int | _duration = 0 |
unsigned int | _frequency = 0 |
unsigned int | _silence = 0 |
uint8_t | _volume = 0 |
bool | _using_custom_msg = false |
Static Private Attributes | |
static const char *const | _default_tunes [] |
static const bool | _default_tunes_interruptable [] |
static const unsigned int | _default_tunes_size = sizeof(_default_tunes) / sizeof(_default_tunes[0]) |
static const uint8_t | _note_tab [] = {9, 11, 0, 2, 4, 5, 7} |
Library for parsing tunes from melody-strings or dedicated tune messages.
Needs to be instantiated as it keeps track of which tune is to be played next. Also handles repeated tunes.
|
strong |
Tunes::Tunes | ( | unsigned | default_note_length = TUNE_DEFAULT_NOTE_LENGTH , |
NoteMode | default_note_mode = NoteMode::NORMAL , |
||
unsigned | default_octave = TUNE_DEFAULT_OCTAVE , |
||
unsigned | default_tempo = TUNE_DEFAULT_TEMPO |
||
) |
|
default |
|
inline |
Get the number of default tunes.
This is useful for when a tune is requested via its tune ID.
Definition at line 121 of file tunes.h.
References _default_tunes_size.
Referenced by tune_control_main().
|
inline |
Definition at line 123 of file tunes.h.
References next_char(), next_dots(), next_number(), note_duration(), note_to_frequency(), reset(), rest_duration(), tune_end(), tune_error(), and TUNE_MAX_UPDATE_INTERVAL_US.
Referenced by ToneAlarm::next_note(), and tune_control_main().
int Tunes::get_next_note | ( | unsigned & | frequency, |
unsigned & | duration, | ||
unsigned & | silence | ||
) |
Get next note in the current tune, which has been provided by either set_control or play_string.
frequency | return frequency value (Hz) |
duration | return duration of the tone (us) |
silence | return silence duration (us) |
Definition at line 172 of file tunes.cpp.
References _duration, _frequency, _next_tune, _note_length, _note_mode, _note_tab, _octave, _repeat, _silence, _tempo, _tune, _using_custom_msg, LEGATO, next_char(), next_dots(), next_number(), NORMAL, note_duration(), note_to_frequency(), rest_duration(), STACCATO, TUNE_CONTINUE, tune_end(), and tune_error().
Referenced by get_next_note(), ToneAlarm::next_note(), and tune_control_main().
int Tunes::get_next_note | ( | unsigned & | frequency, |
unsigned & | duration, | ||
unsigned & | silence, | ||
uint8_t & | volume | ||
) |
Get next note in the current tune, which has been provided by either set_control or play_string.
frequency | return frequency value (Hz) |
duration | return duration of the note (us) |
silence | return silence duration (us) |
volume | return the volume level of the note (between 0-100) |
Definition at line 156 of file tunes.cpp.
References _volume, and get_next_note().
|
private |
Find the next character in the string, discard any whitespace.
Definition at line 441 of file tunes.cpp.
References _next_tune.
Referenced by get_maximum_update_interval(), get_next_note(), next_dots(), and next_number().
|
private |
Consume dot characters from the string.
Definition at line 467 of file tunes.cpp.
References _next_tune, and next_char().
Referenced by get_maximum_update_interval(), and get_next_note().
|
private |
Extract a number from the string, consuming all the digit characters.
Definition at line 450 of file tunes.cpp.
References _next_tune, and next_char().
Referenced by get_maximum_update_interval(), and get_next_note().
|
private |
Calculate the duration in microseconds of play and silence for a note given the current tempo, length and mode and the number of dots following in the play string.
silence | return silence duration (us) |
note_length | note length |
dots | extention of the note length |
Definition at line 384 of file tunes.cpp.
References _note_mode, _tempo, BEAT_TIME_CONVERSION, LEGATO, NORMAL, and STACCATO.
Referenced by get_maximum_update_interval(), and get_next_note().
|
private |
Convert note to frequency.
note | unsigned value of the semitone from C |
Definition at line 378 of file tunes.cpp.
References f().
Referenced by get_maximum_update_interval(), and get_next_note().
|
private |
Reset the tune parameters.
This is necessary when for example a tune moved one or more octaves up or down. reset() should always be called before (re)-starting a tune.
Definition at line 66 of file tunes.cpp.
References _default_note_length, _default_note_mode, _default_octave, _default_tempo, _next_tune, _note_length, _note_mode, _octave, _tempo, _tune, and _tune_start_ptr.
Referenced by get_maximum_update_interval(), set_control(), tune_end(), tune_error(), and Tunes().
|
private |
Calculate the duration in microseconds of a rest corresponding to a given note length.
rest_length | rest lenght in fraction of a note |
dots | number of extension dots |
Definition at line 421 of file tunes.cpp.
References _tempo, and BEAT_TIME_CONVERSION.
Referenced by get_maximum_update_interval(), and get_next_note().
int Tunes::set_control | ( | const tune_control_s & | tune_control | ) |
Set tune to be played using the message.
If a tune is already being played the call to this function will be ignored, unless the override flag is set or the tune being already played is a repeated tune.
tune_control | struct containig the uORB message |
Definition at line 85 of file tunes.cpp.
References _current_tune_id, _default_tunes, _default_tunes_interruptable, _default_tunes_size, _duration, _frequency, _next_tune, _repeat, _silence, _tune, _tune_start_ptr, _using_custom_msg, _volume, tune_control_s::duration, tune_control_s::frequency, OK, reset(), tune_control_s::silence, tune_control_s::tune_id, tune_control_s::tune_override, and tune_control_s::volume.
Referenced by ToneAlarm::orb_update(), and tune_control_main().
void Tunes::set_string | ( | const char *const | string, |
uint8_t | volume | ||
) |
Set tune to be played using a string.
Parses a tune string, formatted with the syntax of the Microsoft GWBasic/QBasic. Ownership of the string is NOT transferred. The string has to be kept in memory for the whole duration of the melody.
string | tune input string |
Definition at line 138 of file tunes.cpp.
References _next_tune, _tune, _tune_start_ptr, and _volume.
Referenced by test_tone(), and tune_control_main().
|
private |
Definition at line 356 of file tunes.cpp.
References _repeat, reset(), TUNE_CONTINUE, and TUNE_STOP.
Referenced by get_maximum_update_interval(), and get_next_note().
|
private |
Definition at line 370 of file tunes.cpp.
References _repeat, reset(), and TUNE_ERROR.
Referenced by get_maximum_update_interval(), and get_next_note().
|
private |
Definition at line 198 of file tunes.h.
Referenced by set_control().
|
private |
|
private |
|
private |
|
private |
|
staticprivate |
|
staticprivate |
|
staticprivate |
Definition at line 191 of file tunes.h.
Referenced by get_default_tunes_size(), and set_control().
|
private |
Definition at line 212 of file tunes.h.
Referenced by get_next_note(), and set_control().
|
private |
Definition at line 213 of file tunes.h.
Referenced by get_next_note(), and set_control().
|
private |
next note in the string
Definition at line 194 of file tunes.h.
Referenced by get_next_note(), next_char(), next_dots(), next_number(), reset(), set_control(), and set_string().
|
private |
Definition at line 207 of file tunes.h.
Referenced by get_next_note(), and reset().
|
private |
Definition at line 208 of file tunes.h.
Referenced by get_next_note(), note_duration(), and reset().
|
staticprivate |
Definition at line 192 of file tunes.h.
Referenced by get_next_note().
|
private |
Definition at line 209 of file tunes.h.
Referenced by get_next_note(), and reset().
|
private |
if true, tune restarts at end
Definition at line 200 of file tunes.h.
Referenced by get_next_note(), set_control(), tune_end(), and tune_error().
|
private |
Definition at line 214 of file tunes.h.
Referenced by get_next_note(), and set_control().
|
private |
Definition at line 210 of file tunes.h.
Referenced by get_next_note(), note_duration(), reset(), and rest_duration().
|
private |
current tune string
Definition at line 195 of file tunes.h.
Referenced by get_next_note(), reset(), set_control(), and set_string().
|
private |
pointer to repeat tune
Definition at line 196 of file tunes.h.
Referenced by reset(), set_control(), and set_string().
|
private |
Definition at line 217 of file tunes.h.
Referenced by get_next_note(), and set_control().
|
private |
Definition at line 215 of file tunes.h.
Referenced by get_next_note(), set_control(), and set_string().