ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
rtttl.h
Go to the documentation of this file.
1#pragma once
2
5
6#ifdef USE_OUTPUT
8#endif
9
10#ifdef USE_SPEAKER
12#endif
13
14namespace esphome {
15namespace rtttl {
16
24
25#ifdef USE_SPEAKER
26static const size_t SAMPLE_BUFFER_SIZE = 2048;
27
29 int8_t left{0};
30 int8_t right{0};
31};
32#endif
33
34class Rtttl : public Component {
35 public:
36#ifdef USE_OUTPUT
37 void set_output(output::FloatOutput *output) { this->output_ = output; }
38#endif
39#ifdef USE_SPEAKER
40 void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
41#endif
42 float get_gain() { return gain_; }
43 void set_gain(float gain) { this->gain_ = clamp(gain, 0.0f, 1.0f); }
44 void play(std::string rtttl);
45 void stop();
46 void dump_config() override;
47
48 bool is_playing() { return this->state_ != State::STATE_STOPPED; }
49 void loop() override;
50
51 void add_on_finished_playback_callback(std::function<void()> callback) {
52 this->on_finished_playback_callback_.add(std::move(callback));
53 }
54
55 protected:
56 inline uint8_t get_integer_() {
57 uint8_t ret = 0;
58 while (isdigit(this->rtttl_[this->position_])) {
59 ret = (ret * 10) + (this->rtttl_[this->position_++] - '0');
60 }
61 return ret;
62 }
70 void finish_();
72
74 std::string rtttl_{""};
76 size_t position_{0};
78 uint16_t wholenote_;
84 uint32_t last_note_;
87
89 uint32_t output_freq_;
91 float gain_{0.6f};
94
95#ifdef USE_OUTPUT
98#endif
99
100#ifdef USE_SPEAKER
104 int sample_rate_{16000};
113
114#endif
115
118};
119
120template<typename... Ts> class PlayAction : public Action<Ts...> {
121 public:
122 PlayAction(Rtttl *rtttl) : rtttl_(rtttl) {}
123 TEMPLATABLE_VALUE(std::string, value)
124
125 void play(Ts... x) override { this->rtttl_->play(this->value_.value(x...)); }
126
127 protected:
129};
130
131template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<Rtttl> {
132 public:
133 void play(Ts... x) override { this->parent_->stop(); }
134};
135
136template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<Rtttl> {
137 public:
138 bool check(Ts... x) override { return this->parent_->is_playing(); }
139};
140
142 public:
143 explicit FinishedPlaybackTrigger(Rtttl *parent) {
144 parent->add_on_finished_playback_callback([this]() { this->trigger(); });
145 }
146};
147
148} // namespace rtttl
149} // namespace esphome
virtual void play(Ts... x)=0
Base class for all automation conditions.
Definition automation.h:124
Helper class to easily give an object a parent of type T.
Definition helpers.h:697
void trigger(Ts... x)
Definition automation.h:145
Base class for all output components that can output a variable level, like PWM.
bool check(Ts... x) override
Definition rtttl.h:138
PlayAction(Rtttl *rtttl)
Definition rtttl.h:122
TEMPLATABLE_VALUE(std::string, value) void play(Ts... x) override
Definition rtttl.h:123
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:106
uint16_t wholenote_
The duration of a whole note in milliseconds.
Definition rtttl.h:78
uint32_t last_note_
The time the last note was started.
Definition rtttl.h:84
int samples_sent_
The number of samples sent.
Definition rtttl.h:108
uint16_t note_duration_
The duration of the current note in milliseconds.
Definition rtttl.h:86
int samples_gap_
The number of samples for the gap between notes.
Definition rtttl.h:112
int sample_rate_
The sample rate of the speaker.
Definition rtttl.h:104
output::FloatOutput * output_
The output to write the sound to.
Definition rtttl.h:97
void dump_config() override
Definition rtttl.cpp:29
void set_state_(State state)
Definition rtttl.cpp:396
void finish_()
Finalizes the playback of the RTTTL string.
Definition rtttl.cpp:145
float gain_
The gain of the output.
Definition rtttl.h:91
uint32_t output_freq_
The frequency of the current note in Hz.
Definition rtttl.h:89
void loop() override
Definition rtttl.cpp:170
uint8_t get_integer_()
Definition rtttl.h:56
void add_on_finished_playback_callback(std::function< void()> callback)
Definition rtttl.h:51
uint16_t default_duration_
The default duration of a note (e.g. 4 for a quarter note).
Definition rtttl.h:80
size_t position_
The current position in the RTTTL string.
Definition rtttl.h:76
void set_gain(float gain)
Definition rtttl.h:43
uint16_t default_octave_
The default octave for a note.
Definition rtttl.h:82
State state_
The current state of the RTTTL player.
Definition rtttl.h:93
void set_speaker(speaker::Speaker *speaker)
Definition rtttl.h:40
int samples_count_
The total number of samples to send.
Definition rtttl.h:110
speaker::Speaker * speaker_
The speaker to write the sound to.
Definition rtttl.h:102
void set_output(output::FloatOutput *output)
Definition rtttl.h:37
CallbackManager< void()> on_finished_playback_callback_
The callback to call when playback is finished.
Definition rtttl.h:117
void play(std::string rtttl)
Definition rtttl.cpp:36
std::string rtttl_
The RTTTL string to play.
Definition rtttl.h:74
void play(Ts... x) override
Definition rtttl.h:133
bool state
Definition fan.h:0
AlsGain501 gain
@ STATE_STOPPING
Definition rtttl.h:22
@ STATE_RUNNING
Definition rtttl.h:21
@ STATE_STOPPED
Definition rtttl.h:18
@ STATE_STARTING
Definition rtttl.h:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5