ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
rtttl.h
Go to the documentation of this file.
1#pragma once
2
7
8#ifdef USE_OUTPUT
10#endif // USE_OUTPUT
11
12#ifdef USE_SPEAKER
14#endif // USE_SPEAKER
15
16namespace esphome::rtttl {
17
18namespace testing {
19class RtttlParserAccess;
20} // namespace testing
21
22inline constexpr uint8_t DEFAULT_NOTE_DENOMINATOR = 4; // Default note-denominator (quarter note)
23inline constexpr uint8_t DEFAULT_OCTAVE =
24 6; // Default octave for a note (see: `MIN_OCTAVE`, `MAX_OCTAVE` in `rtttl.cpp`)
25
26enum class State : uint8_t {
27 STOPPED = 0,
28 INIT,
30 RUNNING,
32};
33
38
39class Rtttl final : public Component {
40 public:
41#ifdef USE_OUTPUT
42 void set_output(output::FloatOutput *output) { this->output_ = output; }
43#endif // USE_OUTPUT
44
45#ifdef USE_SPEAKER
46 void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
47#endif // USE_SPEAKER
48
49 void dump_config() override;
50 void loop() override;
52 void play(std::string rtttl);
54 void play(StaticSong song);
55 void stop();
56
57 float get_gain() { return this->gain_; }
58 void set_gain(float gain) { this->gain_ = clamp(gain, 0.0f, 1.0f); }
59
60 bool is_playing() { return this->state_ != State::STOPPED; }
61
62#ifdef USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
63 template<typename F> void add_on_finished_playback_callback(F &&callback) {
64 this->on_finished_playback_callback_.add(std::forward<F>(callback));
65 }
66#endif
67
68 protected:
69 inline uint16_t get_integer_() {
70 uint16_t ret = 0;
71 while (isdigit(this->song_at_(this->position_))) {
72 ret = (ret * 10) + (this->song_at_(this->position_++) - '0');
73 }
74 return ret;
75 }
77 char song_at_(size_t pos) const {
78 if (pos >= this->song_len_)
79 return '\0';
80 return static_cast<char>(progmem_read_byte(reinterpret_cast<const uint8_t *>(this->song_) + pos));
81 }
83 size_t song_find_(char c, size_t from, size_t end) const;
85 size_t song_find_control_(char key, size_t from, size_t end) const;
87 void song_name_(char *buf, size_t size, size_t name_len) const;
89 void set_song_(const char *song, size_t len);
91 void start_();
93 bool is_busy_() const;
101 void finish_();
102 void set_state_(State state);
103
105 const char *song_{nullptr};
107 std::string owned_song_;
113 float gain_{0.6f};
115 uint16_t song_len_{0};
117 uint16_t position_{0};
119 uint16_t note_duration_{0};
128
129#ifdef USE_OUTPUT
132#endif // USE_OUTPUT
133
134#ifdef USE_SPEAKER
145#endif // USE_SPEAKER
146
147#ifdef USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
150#endif
151
152 friend class testing::RtttlParserAccess;
153};
154
155} // namespace esphome::rtttl
Base class for all output components that can output a variable level, like PWM.
uint32_t samples_per_wave_
The number of samples for one full cycle of a note's waveform, in Q10 fixed-point format.
Definition rtttl.h:138
std::string owned_song_
Backing store for play(std::string).
Definition rtttl.h:107
size_t song_find_(char c, size_t from, size_t end) const
Index of the first c in [from, end), or end when absent.
Definition rtttl.cpp:305
uint32_t last_note_start_time_
The time in milliseconds since microcontroller boot when the last note was started.
Definition rtttl.h:109
uint8_t default_note_denominator_
The default duration of a note (e.g. 4 for a quarter note).
Definition rtttl.h:123
uint16_t position_
The current position in the song.
Definition rtttl.h:117
bool is_busy_() const
Logs and returns true when a song is still playing, so a new one must wait.
Definition rtttl.cpp:295
uint16_t note_duration_
The duration of the current note in milliseconds.
Definition rtttl.h:119
uint16_t song_len_
Length of the song in bytes; songs are a few hundred bytes, so 16 bits is ample.
Definition rtttl.h:115
void start_()
Parses the header and starts playback of the song the view points at.
Definition rtttl.cpp:331
uint32_t samples_sent_
The number of samples sent.
Definition rtttl.h:140
output::FloatOutput * output_
The output to write the sound to.
Definition rtttl.h:131
uint16_t get_integer_()
Definition rtttl.h:69
void dump_config() override
Definition rtttl.cpp:80
void set_state_(State state)
Definition rtttl.cpp:476
void finish_()
Finalizes the playback of the RTTTL string.
Definition rtttl.cpp:452
void add_on_finished_playback_callback(F &&callback)
Definition rtttl.h:63
float gain_
The gain of the output.
Definition rtttl.h:113
uint8_t default_octave_
The default octave for a note.
Definition rtttl.h:125
uint32_t output_freq_
The frequency of the current note in Hz.
Definition rtttl.h:111
void loop() override
Definition rtttl.cpp:87
uint32_t samples_gap_
The number of samples for the gap between notes.
Definition rtttl.h:144
char song_at_(size_t pos) const
The byte of the song at pos, or NUL past its end; the read is flash safe for a PROGMEM song on ESP826...
Definition rtttl.h:77
void set_gain(float gain)
Definition rtttl.h:58
uint32_t samples_count_
The total number of samples to send.
Definition rtttl.h:142
uint16_t wholenote_duration_
The duration of a whole note in milliseconds.
Definition rtttl.h:121
const char * song_
The song being played; points at owned_song_ or at static storage.
Definition rtttl.h:105
size_t song_find_control_(char key, size_t from, size_t end) const
Index of key followed by '=' in [from, end), or end when absent.
Definition rtttl.cpp:313
void song_name_(char *buf, size_t size, size_t name_len) const
Copies the first name_len bytes of the song, its name, into buf for logging.
Definition rtttl.cpp:323
State state_
The current state of the RTTTL player.
Definition rtttl.h:127
void set_speaker(speaker::Speaker *speaker)
Definition rtttl.h:46
speaker::Speaker * speaker_
The speaker to write the sound to.
Definition rtttl.h:136
void set_output(output::FloatOutput *output)
Definition rtttl.h:42
CallbackManager< void()> on_finished_playback_callback_
The callback to call when playback is finished.
Definition rtttl.h:149
void set_song_(const char *song, size_t len)
Points the view at a song, clamped to what song_len_ can hold.
Definition rtttl.cpp:289
void play(std::string rtttl)
Play a song; the string is copied so it may come from anywhere.
Definition rtttl.cpp:269
const char * key
bool state
Definition fan.h:2
int ret
AlsGain501 gain
constexpr uint8_t DEFAULT_OCTAVE
Definition rtttl.h:23
constexpr uint8_t DEFAULT_NOTE_DENOMINATOR
Definition rtttl.h:22
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:34
size_t size_t pos
Definition helpers.h:1255
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
const __FlashStringHelper * ProgmemStr
Definition progmem.h:31
static void uint32_t
A song in storage that outlives playback, such as a literal from generated code; PROGMEM on ESP8266.
Definition rtttl.h:35
uint8_t end[39]
Definition sun_gtil2.cpp:17