ESPHome 2026.6.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
18inline constexpr uint8_t DEFAULT_NOTE_DENOMINATOR = 4; // Default note-denominator (quarter note)
19inline constexpr uint8_t DEFAULT_OCTAVE =
20 6; // Default octave for a note (see: `MIN_OCTAVE`, `MAX_OCTAVE` in `rtttl.cpp`)
21
22enum class State : uint8_t {
23 STOPPED = 0,
24 INIT,
26 RUNNING,
28};
29
30class Rtttl : public Component {
31 public:
32#ifdef USE_OUTPUT
33 void set_output(output::FloatOutput *output) { this->output_ = output; }
34#endif // USE_OUTPUT
35
36#ifdef USE_SPEAKER
37 void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
38#endif // USE_SPEAKER
39
40 void dump_config() override;
41 void loop() override;
42 void play(std::string rtttl);
43 void stop();
44
45 float get_gain() { return this->gain_; }
46 void set_gain(float gain) { this->gain_ = clamp(gain, 0.0f, 1.0f); }
47
48 bool is_playing() { return this->state_ != State::STOPPED; }
49
50#ifdef USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
51 template<typename F> void add_on_finished_playback_callback(F &&callback) {
52 this->on_finished_playback_callback_.add(std::forward<F>(callback));
53 }
54#endif
55
56 protected:
57 inline uint16_t get_integer_() {
58 uint16_t ret = 0;
59 while (isdigit(this->rtttl_[this->position_])) {
60 ret = (ret * 10) + (this->rtttl_[this->position_++] - '0');
61 }
62 return ret;
63 }
71 void finish_();
73
75 std::string rtttl_;
77 size_t position_{0};
83 uint16_t note_duration_{0};
91 float gain_{0.6f};
94
95#ifdef USE_OUTPUT
98#endif // USE_OUTPUT
99
100#ifdef USE_SPEAKER
111#endif // USE_SPEAKER
112
113#ifdef USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
116#endif
117};
118
119template<typename... Ts> class PlayAction : public Action<Ts...> {
120 public:
121 PlayAction(Rtttl *rtttl) : rtttl_(rtttl) {}
122 TEMPLATABLE_VALUE(std::string, value)
123
124 void play(const Ts &...x) override { this->rtttl_->play(this->value_.value(x...)); }
125
126 protected:
128};
129
130template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Rtttl> {
131 public:
132 void play(const Ts &...x) override { this->parent_->stop(); }
133};
134
135template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Rtttl> {
136 public:
137 bool check(const Ts &...x) override { return this->parent_->is_playing(); }
138};
139
140} // namespace esphome::rtttl
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:438
Helper class to easily give an object a parent of type T.
Definition helpers.h:1861
Base class for all output components that can output a variable level, like PWM.
bool check(const Ts &...x) override
Definition rtttl.h:137
PlayAction(Rtttl *rtttl)
Definition rtttl.h:121
TEMPLATABLE_VALUE(std::string, value) void play(const Ts &...x) override
Definition rtttl.h:122
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:104
uint32_t last_note_start_time_
The time in milliseconds since microcontroller boot when the last note was started.
Definition rtttl.h:87
uint8_t default_note_denominator_
The default duration of a note (e.g. 4 for a quarter note).
Definition rtttl.h:79
uint16_t note_duration_
The duration of the current note in milliseconds.
Definition rtttl.h:83
uint32_t samples_sent_
The number of samples sent.
Definition rtttl.h:106
output::FloatOutput * output_
The output to write the sound to.
Definition rtttl.h:97
uint16_t get_integer_()
Definition rtttl.h:57
void dump_config() override
Definition rtttl.cpp:77
void set_state_(State state)
Definition rtttl.cpp:418
void finish_()
Finalizes the playback of the RTTTL string.
Definition rtttl.cpp:394
void add_on_finished_playback_callback(F &&callback)
Definition rtttl.h:51
float gain_
The gain of the output.
Definition rtttl.h:91
uint8_t default_octave_
The default octave for a note.
Definition rtttl.h:81
uint32_t output_freq_
The frequency of the current note in Hz.
Definition rtttl.h:89
void loop() override
Definition rtttl.cpp:84
uint32_t samples_gap_
The number of samples for the gap between notes.
Definition rtttl.h:110
size_t position_
The current position in the RTTTL string.
Definition rtttl.h:77
void set_gain(float gain)
Definition rtttl.h:46
uint32_t samples_count_
The total number of samples to send.
Definition rtttl.h:108
uint16_t wholenote_duration_
The duration of a whole note in milliseconds.
Definition rtttl.h:85
State state_
The current state of the RTTTL player.
Definition rtttl.h:93
void set_speaker(speaker::Speaker *speaker)
Definition rtttl.h:37
speaker::Speaker * speaker_
The speaker to write the sound to.
Definition rtttl.h:102
void set_output(output::FloatOutput *output)
Definition rtttl.h:33
CallbackManager< void()> on_finished_playback_callback_
The callback to call when playback is finished.
Definition rtttl.h:115
void play(std::string rtttl)
Definition rtttl.cpp:266
std::string rtttl_
The RTTTL string to play.
Definition rtttl.h:75
void play(const Ts &...x) override
Definition rtttl.h:132
bool state
Definition fan.h:2
AlsGain501 gain
constexpr uint8_t DEFAULT_OCTAVE
Definition rtttl.h:19
constexpr uint8_t DEFAULT_NOTE_DENOMINATOR
Definition rtttl.h:18
static void uint32_t
uint16_t x
Definition tt21100.cpp:5