ESPHome 2026.2.3
Loading...
Searching...
No Matches
rtttl.h
Go to the documentation of this file.
1#pragma once
2
5
6#ifdef USE_OUTPUT
8#endif // USE_OUTPUT
9
10#ifdef USE_SPEAKER
12#endif // USE_SPEAKER
13
14namespace esphome::rtttl {
15
16enum class State : uint8_t {
17 STOPPED = 0,
18 INIT,
20 RUNNING,
22};
23
24class Rtttl : public Component {
25 public:
26#ifdef USE_OUTPUT
27 void set_output(output::FloatOutput *output) { this->output_ = output; }
28#endif // USE_OUTPUT
29
30#ifdef USE_SPEAKER
31 void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
32#endif // USE_SPEAKER
33
34 void dump_config() override;
35 void loop() override;
36 void play(std::string rtttl);
37 void stop();
38
39 float get_gain() { return this->gain_; }
40 void set_gain(float gain) { this->gain_ = clamp(gain, 0.0f, 1.0f); }
41
42 bool is_playing() { return this->state_ != State::STOPPED; }
43
44 void add_on_finished_playback_callback(std::function<void()> callback) {
45 this->on_finished_playback_callback_.add(std::move(callback));
46 }
47
48 protected:
49 inline uint16_t get_integer_() {
50 uint16_t ret = 0;
51 while (isdigit(this->rtttl_[this->position_])) {
52 ret = (ret * 10) + (this->rtttl_[this->position_++] - '0');
53 }
54 return ret;
55 }
63 void finish_();
65
67 std::string rtttl_{""};
69 size_t position_{0};
71 uint16_t wholenote_;
77 uint32_t last_note_;
80
82 uint32_t output_freq_;
84 float gain_{0.6f};
87
88#ifdef USE_OUTPUT
91#endif // USE_OUTPUT
92
93#ifdef USE_SPEAKER
97 int sample_rate_{16000};
106#endif // USE_SPEAKER
107
110};
111
112template<typename... Ts> class PlayAction : public Action<Ts...> {
113 public:
114 PlayAction(Rtttl *rtttl) : rtttl_(rtttl) {}
115 TEMPLATABLE_VALUE(std::string, value)
116
117 void play(const Ts &...x) override { this->rtttl_->play(this->value_.value(x...)); }
118
119 protected:
121};
122
123template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Rtttl> {
124 public:
125 void play(const Ts &...x) override { this->parent_->stop(); }
126};
127
128template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Rtttl> {
129 public:
130 bool check(const Ts &...x) override { return this->parent_->is_playing(); }
131};
132
134 public:
135 explicit FinishedPlaybackTrigger(Rtttl *parent) {
136 parent->add_on_finished_playback_callback([this]() { this->trigger(); });
137 }
138};
139
140} // namespace esphome::rtttl
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:258
Helper class to easily give an object a parent of type T.
Definition helpers.h:1471
void trigger(const Ts &...x)
Definition automation.h:279
Base class for all output components that can output a variable level, like PWM.
bool check(const Ts &...x) override
Definition rtttl.h:130
PlayAction(Rtttl *rtttl)
Definition rtttl.h:114
TEMPLATABLE_VALUE(std::string, value) void play(const Ts &...x) override
Definition rtttl.h:115
int samples_per_wave_
The number of samples for one full cycle of a note's waveform, in Q10 fixed-point format.
Definition rtttl.h:99
uint16_t wholenote_
The duration of a whole note in milliseconds.
Definition rtttl.h:71
uint32_t last_note_
The time the last note was started.
Definition rtttl.h:77
int samples_sent_
The number of samples sent.
Definition rtttl.h:101
uint16_t note_duration_
The duration of the current note in milliseconds.
Definition rtttl.h:79
int samples_gap_
The number of samples for the gap between notes.
Definition rtttl.h:105
int sample_rate_
The sample rate of the speaker.
Definition rtttl.h:97
output::FloatOutput * output_
The output to write the sound to.
Definition rtttl.h:90
uint16_t get_integer_()
Definition rtttl.h:49
void dump_config() override
Definition rtttl.cpp:74
void set_state_(State state)
Definition rtttl.cpp:410
void finish_()
Finalizes the playback of the RTTTL string.
Definition rtttl.cpp:382
float gain_
The gain of the output.
Definition rtttl.h:84
uint32_t output_freq_
The frequency of the current note in Hz.
Definition rtttl.h:82
void loop() override
Definition rtttl.cpp:81
void add_on_finished_playback_callback(std::function< void()> callback)
Definition rtttl.h:44
uint16_t default_duration_
The default duration of a note (e.g. 4 for a quarter note).
Definition rtttl.h:73
size_t position_
The current position in the RTTTL string.
Definition rtttl.h:69
void set_gain(float gain)
Definition rtttl.h:40
uint16_t default_octave_
The default octave for a note.
Definition rtttl.h:75
State state_
The current state of the RTTTL player.
Definition rtttl.h:86
void set_speaker(speaker::Speaker *speaker)
Definition rtttl.h:31
int samples_count_
The total number of samples to send.
Definition rtttl.h:103
speaker::Speaker * speaker_
The speaker to write the sound to.
Definition rtttl.h:95
void set_output(output::FloatOutput *output)
Definition rtttl.h:27
CallbackManager< void()> on_finished_playback_callback_
The callback to call when playback is finished.
Definition rtttl.h:109
void play(std::string rtttl)
Definition rtttl.cpp:268
std::string rtttl_
The RTTTL string to play.
Definition rtttl.h:67
void play(const Ts &...x) override
Definition rtttl.h:125
bool state
Definition fan.h:2
AlsGain501 gain
uint16_t x
Definition tt21100.cpp:5