ESPHome 2026.10.2
Loading...
Searching...
No Matches
rtttl.h
Go to the documentation of this file.
1#pragma once
2
6
7#ifdef USE_OUTPUT
9#endif // USE_OUTPUT
10
11#ifdef USE_SPEAKER
13#endif // USE_SPEAKER
14
15namespace esphome::rtttl {
16
17inline constexpr uint8_t DEFAULT_NOTE_DENOMINATOR = 4; // Default note-denominator (quarter note)
18inline constexpr uint8_t DEFAULT_OCTAVE =
19 6; // Default octave for a note (see: `MIN_OCTAVE`, `MAX_OCTAVE` in `rtttl.cpp`)
20
21enum class State : uint8_t {
22 STOPPED = 0,
23 INIT,
25 RUNNING,
27};
28
29class Rtttl final : public Component {
30 public:
31#ifdef USE_OUTPUT
32 void set_output(output::FloatOutput *output) { this->output_ = output; }
33#endif // USE_OUTPUT
34
35#ifdef USE_SPEAKER
36 void set_speaker(speaker::Speaker *speaker) { this->speaker_ = speaker; }
37#endif // USE_SPEAKER
38
39 void dump_config() override;
40 void loop() override;
41 void play(std::string rtttl);
42 void stop();
43
44 float get_gain() { return this->gain_; }
45 void set_gain(float gain) { this->gain_ = clamp(gain, 0.0f, 1.0f); }
46
47 bool is_playing() { return this->state_ != State::STOPPED; }
48
49#ifdef USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
50 template<typename F> void add_on_finished_playback_callback(F &&callback) {
51 this->on_finished_playback_callback_.add(std::forward<F>(callback));
52 }
53#endif
54
55 protected:
56 inline uint16_t get_integer_() {
57 uint16_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};
82 uint16_t note_duration_{0};
92 float gain_{0.6f};
93
94#ifdef USE_OUTPUT
97#endif // USE_OUTPUT
98
99#ifdef USE_SPEAKER
110#endif // USE_SPEAKER
111
112#ifdef USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
115#endif
116};
117
118} // 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:103
uint32_t last_note_start_time_
The time in milliseconds since microcontroller boot when the last note was started.
Definition rtttl.h:88
uint8_t default_note_denominator_
The default duration of a note (e.g. 4 for a quarter note).
Definition rtttl.h:78
uint16_t note_duration_
The duration of the current note in milliseconds.
Definition rtttl.h:82
uint32_t samples_sent_
The number of samples sent.
Definition rtttl.h:105
output::FloatOutput * output_
The output to write the sound to.
Definition rtttl.h:96
uint16_t get_integer_()
Definition rtttl.h:56
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:50
float gain_
The gain of the output.
Definition rtttl.h:92
uint8_t default_octave_
The default octave for a note.
Definition rtttl.h:80
uint32_t output_freq_
The frequency of the current note in Hz.
Definition rtttl.h:90
void loop() override
Definition rtttl.cpp:84
uint32_t samples_gap_
The number of samples for the gap between notes.
Definition rtttl.h:109
size_t position_
The current position in the RTTTL string.
Definition rtttl.h:76
void set_gain(float gain)
Definition rtttl.h:45
uint32_t samples_count_
The total number of samples to send.
Definition rtttl.h:107
uint16_t wholenote_duration_
The duration of a whole note in milliseconds.
Definition rtttl.h:84
State state_
The current state of the RTTTL player.
Definition rtttl.h:86
void set_speaker(speaker::Speaker *speaker)
Definition rtttl.h:36
speaker::Speaker * speaker_
The speaker to write the sound to.
Definition rtttl.h:101
void set_output(output::FloatOutput *output)
Definition rtttl.h:32
CallbackManager< void()> on_finished_playback_callback_
The callback to call when playback is finished.
Definition rtttl.h:114
void play(std::string rtttl)
Definition rtttl.cpp:266
std::string rtttl_
The RTTTL string to play.
Definition rtttl.h:74
bool state
Definition fan.h:2
int ret
AlsGain501 gain
constexpr uint8_t DEFAULT_OCTAVE
Definition rtttl.h:18
constexpr uint8_t DEFAULT_NOTE_DENOMINATOR
Definition rtttl.h:17
static void uint32_t