ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
rtttl.cpp
Go to the documentation of this file.
1#include "rtttl.h"
2#include <cmath>
3#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
6
7namespace esphome::rtttl {
8
9static const char *const TAG = "rtttl";
10
11static constexpr uint8_t SONG_NAME_LENGTH_LIMIT = 64;
12static constexpr uint8_t SEMITONES_IN_OCTAVE = 12;
13
14static constexpr uint8_t MIN_OCTAVE = 4;
15static constexpr uint8_t MAX_OCTAVE = 7;
16
17static constexpr uint8_t DEFAULT_BPM = 63; // Default beats per minute
18
19// These values can also be found as constants in the Tone library (Tone.h)
20static constexpr uint16_t NOTES[] = {0, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
21 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047,
22 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217,
23 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951};
24static constexpr uint8_t NOTES_COUNT = static_cast<uint8_t>(sizeof(NOTES) / sizeof(NOTES[0]));
25
26static constexpr uint8_t REPEATING_NOTE_GAP_MS = 10;
27
28#ifdef USE_SPEAKER
29static constexpr uint16_t SAMPLE_BUFFER_SIZE = 2048;
30static constexpr uint16_t SAMPLE_RATE = 16000;
31
32inline double deg2rad(double degrees) {
33 static constexpr double PI_ON_180 = M_PI / 180.0;
34 return degrees * PI_ON_180;
35}
36#endif // USE_SPEAKER
37
38#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
39// RTTTL state strings indexed by State enum (0-4): STOPPED, INIT, STARTING, RUNNING, STOPPING, plus UNKNOWN fallback
40PROGMEM_STRING_TABLE(RtttlStateStrings, "State::STOPPED", "State::INIT", "State::STARTING", "State::RUNNING",
41 "State::STOPPING", "UNKNOWN");
42
43static const LogString *state_to_string(State state) {
44 return RtttlStateStrings::get_log_str(static_cast<uint8_t>(state), RtttlStateStrings::LAST_INDEX);
45}
46#endif // ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
47
48static uint8_t note_index_from_char(char note) {
49 switch (note) {
50 case 'c':
51 return 1;
52 // 'c#': 2
53 case 'd':
54 return 3;
55 // 'd#': 4
56 case 'e':
57 return 5;
58 case 'f':
59 return 6;
60 // 'f#': 7
61 case 'g':
62 return 8;
63 // 'g#': 9
64 case 'a':
65 return 10;
66 // 'a#': 11
67 // Support both 'b' (English notation for B natural) and 'h' (German notation for B natural)
68 case 'b':
69 case 'h':
70 return 12;
71 case 'p':
72 default:
73 return 0;
74 }
75}
76
78 ESP_LOGCONFIG(TAG,
79 "Rtttl:\n"
80 " Gain: %f",
81 this->gain_);
82}
83
85 if (this->state_ == State::STOPPED) {
86 this->disable_loop();
87 return;
88 }
89
90#ifdef USE_OUTPUT
91 if (this->output_ != nullptr && millis() - this->last_note_start_time_ < this->note_duration_) {
92 return;
93 }
94#endif // USE_OUTPUT
95
96#ifdef USE_SPEAKER
97 if (this->speaker_ != nullptr) {
98 if (this->state_ == State::STOPPING) {
99 if (this->speaker_->is_stopped()) {
101 } else {
102 return;
103 }
104 } else if (this->state_ == State::INIT) {
105 if (this->speaker_->is_stopped()) {
106 audio::AudioStreamInfo audio_stream_info = audio::AudioStreamInfo(16, 1, SAMPLE_RATE);
107 this->speaker_->set_audio_stream_info(audio_stream_info);
108 this->speaker_->set_volume(this->gain_);
109 this->speaker_->start();
111 }
112 } else if (this->state_ == State::STARTING) {
113 if (this->speaker_->is_running()) {
115 }
116 }
117 if (!this->speaker_->is_running()) {
118 return;
119 }
120 if (this->samples_sent_ != this->samples_count_) {
121 int16_t sample[SAMPLE_BUFFER_SIZE];
122 uint16_t sample_index = 0;
123 double rem = 0.0;
124
125 while (sample_index < SAMPLE_BUFFER_SIZE && this->samples_sent_ < this->samples_count_) {
126 // Try and send out the remainder of the existing note, one per `loop()`
127 if (this->samples_per_wave_ != 0 && this->samples_sent_ >= this->samples_gap_) { // Play note
128 rem = ((this->samples_sent_ << 10) % this->samples_per_wave_) * (360.0 / this->samples_per_wave_);
129 sample[sample_index] = INT16_MAX * sin(deg2rad(rem));
130 } else {
131 sample[sample_index] = 0;
132 }
133 this->samples_sent_++;
134 sample_index++;
135 }
136 if (sample_index > 0) {
137 size_t bytes = sample_index * sizeof(int16_t);
138 size_t sent_bytes = this->speaker_->play((uint8_t *) (&sample), bytes);
139 size_t samples_sent = sent_bytes / sizeof(int16_t);
140 if (samples_sent != sample_index) {
141 this->samples_sent_ -= (sample_index - samples_sent);
142 }
143 return;
144 }
145 }
146 }
147#endif // USE_SPEAKER
148
149 // Align to note: most rtttl's out there does not add any space after the ',' separator but just in case
150 while (this->position_ < this->rtttl_.length()) {
151 char c = this->rtttl_[this->position_];
152 if (c != ',' && c != ' ')
153 break;
154 this->position_++;
155 }
156
157 if (this->position_ >= this->rtttl_.length()) {
158 this->finish_();
159 return;
160 }
161
162 // First, get note duration, if available
163 uint8_t note_denominator = this->get_integer_();
164
165 if (note_denominator) {
166 this->note_duration_ = this->wholenote_duration_ / note_denominator;
167 } else {
168 // We will need to check if we are a dotted note after
169 this->note_duration_ = this->wholenote_duration_ / this->default_note_denominator_;
170 }
171
172 uint8_t note_index_in_octave = note_index_from_char(this->rtttl_[this->position_]);
173
174 this->position_++;
175
176 // Now, get optional '#' sharp
177 if (this->rtttl_[this->position_] == '#') {
178 note_index_in_octave++;
179 this->position_++;
180 }
181
182 // Now, get scale
183 uint8_t scale = this->get_integer_();
184 if (scale == 0) {
185 scale = this->default_octave_;
186 }
187
188 if (scale < MIN_OCTAVE || scale > MAX_OCTAVE) {
189 ESP_LOGE(TAG, "Octave must be between %d and %d (it is %d)", MIN_OCTAVE, MAX_OCTAVE, scale);
190 this->finish_();
191 return;
192 }
193
194 // Now, get optional '.' dotted note
195 if (this->rtttl_[this->position_] == '.') {
196 this->note_duration_ += this->note_duration_ / 2; // Duration +50%
197 this->position_++;
198 }
199
200 bool need_note_gap = false;
201
202 // Now play the note
203 if (note_index_in_octave == 0) {
204 this->output_freq_ = 0;
205 ESP_LOGVV(TAG, "Waiting: %dms", this->note_duration_);
206 } else {
207 uint8_t note_index = (scale - MIN_OCTAVE) * SEMITONES_IN_OCTAVE + note_index_in_octave;
208 if (note_index >= NOTES_COUNT) {
209 ESP_LOGE(TAG, "Note out of range (note: %d, scale: %d, index: %d, max: %d)", note_index_in_octave, scale,
210 note_index, NOTES_COUNT);
211 this->finish_();
212 return;
213 }
214 uint16_t freq = NOTES[note_index];
215 need_note_gap = freq == this->output_freq_;
216
217 // Add small silence gap between same note
218 this->output_freq_ = freq;
219
220 ESP_LOGVV(TAG, "Playing note: %d for %dms", note_index_in_octave, this->note_duration_);
221 }
222
223#ifdef USE_OUTPUT
224 if (this->output_ != nullptr) {
225 if (this->output_freq_ == 0) {
226 this->output_->set_level(0.0);
227 } else {
228 if (need_note_gap && this->note_duration_ > REPEATING_NOTE_GAP_MS) {
229 this->output_->set_level(0.0);
230 delay(REPEATING_NOTE_GAP_MS);
231 this->note_duration_ -= REPEATING_NOTE_GAP_MS;
232 }
234 this->output_->set_level(this->gain_);
235 }
236 }
237#endif // USE_OUTPUT
238
239#ifdef USE_SPEAKER
240 if (this->speaker_ != nullptr) {
241 this->samples_sent_ = 0;
242 this->samples_gap_ = 0;
243 this->samples_per_wave_ = 0;
244 this->samples_count_ = (SAMPLE_RATE * this->note_duration_) / 1000;
245 if (need_note_gap) {
246 this->samples_gap_ = (SAMPLE_RATE * REPEATING_NOTE_GAP_MS) / 1000;
247 }
248 if (this->output_freq_ != 0) {
249 // Make sure there is enough samples to add a full last sinus.
250 uint32_t samples_wish = this->samples_count_;
251 this->samples_per_wave_ = (SAMPLE_RATE << 10) / this->output_freq_;
252
253 uint16_t division = ((this->samples_count_ << 10) / this->samples_per_wave_) + 1;
254
255 this->samples_count_ = (division * this->samples_per_wave_) >> 10;
256 ESP_LOGVV(TAG, "Calc play time: wish: %" PRIu32 " gets: %" PRIu32 " (div: %d spw: %" PRIu32 ")", samples_wish,
257 this->samples_count_, division, this->samples_per_wave_);
258 }
259 // Convert from frequency in Hz to high and low samples in fixed point
260 }
261#endif // USE_SPEAKER
262
264}
265
266void Rtttl::play(std::string rtttl) {
267 if (this->state_ != State::STOPPED && this->state_ != State::STOPPING) {
268 size_t pos = this->rtttl_.find(':');
269 size_t len = (pos != std::string::npos) ? pos : this->rtttl_.length();
270 ESP_LOGW(TAG, "Already playing: %.*s", (int) len, this->rtttl_.c_str());
271 return;
272 }
273
274 this->rtttl_ = std::move(rtttl);
275
278 this->note_duration_ = 0;
279
280 uint16_t bpm = DEFAULT_BPM;
281 uint16_t num; // Used for: default note-denominator, default octave, BPM
282
283 // Get name
284 this->position_ = this->rtttl_.find(':');
285
286 if (this->position_ == std::string::npos) {
287 ESP_LOGE(TAG, "Unable to determine name; missing ':'");
288 return;
289 }
290 if (this->position_ >= SONG_NAME_LENGTH_LIMIT) {
291 ESP_LOGE(TAG, "Name is too long: length=%u, limit=%u", static_cast<unsigned>(this->position_),
292 static_cast<unsigned>(SONG_NAME_LENGTH_LIMIT));
293 return;
294 }
295 ESP_LOGD(TAG, "Playing song %.*s", (int) this->position_, this->rtttl_.c_str());
296
297 size_t name_end_position = this->position_;
298 size_t control_end = this->rtttl_.find(':', name_end_position + 1);
299 if (control_end == std::string::npos) {
300 ESP_LOGE(TAG, "Missing second ':'");
301 return;
302 }
303
304 // Get default duration
305 size_t pos = this->rtttl_.find("d=", name_end_position);
306 if (pos == std::string::npos || pos >= control_end) {
307 ESP_LOGW(TAG, "Missing 'd='; use default duration %d", this->default_note_denominator_);
308 } else {
309 this->position_ = pos + 2;
310 num = this->get_integer_();
311 if (num == 1 || num == 2 || num == 4 || num == 8 || num == 16 || num == 32) {
312 this->default_note_denominator_ = num;
313 } else {
314 ESP_LOGE(TAG, "Invalid default duration: %d", num);
315 return;
316 }
317 }
318
319 // Get default octave
320 pos = this->rtttl_.find("o=", name_end_position);
321 if (pos == std::string::npos || pos >= control_end) {
322 ESP_LOGW(TAG, "Missing 'o='; use default octave %d", this->default_octave_);
323 } else {
324 this->position_ = pos + 2;
325 num = this->get_integer_();
326 if (num >= MIN_OCTAVE && num <= MAX_OCTAVE) {
327 this->default_octave_ = num;
328 } else {
329 ESP_LOGE(TAG, "Invalid default octave: %d", num);
330 return;
331 }
332 }
333
334 // Get BPM
335 pos = this->rtttl_.find("b=", name_end_position);
336 if (pos == std::string::npos || pos >= control_end) {
337 ESP_LOGW(TAG, "Missing 'b='; use default BPM %d", bpm);
338 } else {
339 this->position_ = pos + 2;
340 num = this->get_integer_();
341 if (num >= 4) { // Below 4 is not realistic and would cause a integer overflow
342 bpm = num;
343 } else {
344 ESP_LOGE(TAG, "Invalid BPM: %d", num);
345 return;
346 }
347 }
348
349 this->position_ = control_end + 1;
350
351 // BPM usually expresses the number of quarter notes per minute
352 this->wholenote_duration_ = 60 * 1000L * 4 / bpm; // This is the time for whole note (in milliseconds)
353
354 this->output_freq_ = 0;
356 this->note_duration_ = 1;
357
358#ifdef USE_OUTPUT
359 if (this->output_ != nullptr) {
361 }
362#endif // USE_OUTPUT
363
364#ifdef USE_SPEAKER
365 if (this->speaker_ != nullptr) {
366 this->set_state_(State::INIT);
367 this->samples_sent_ = 0;
368 this->samples_count_ = 0;
369 }
370#endif // USE_SPEAKER
371}
372
374#ifdef USE_OUTPUT
375 if (this->output_ != nullptr) {
376 this->output_->set_level(0.0);
378 }
379#endif // USE_OUTPUT
380
381#ifdef USE_SPEAKER
382 if (this->speaker_ != nullptr) {
383 if (this->speaker_->is_running()) {
384 this->speaker_->stop();
385 }
387 }
388#endif // USE_SPEAKER
389
390 this->position_ = this->rtttl_.length();
391 this->note_duration_ = 0;
392}
393
395 ESP_LOGV(TAG, "Rtttl::finish_()");
396
397#ifdef USE_OUTPUT
398 if (this->output_ != nullptr) {
399 this->output_->set_level(0.0);
401 }
402#endif // USE_OUTPUT
403
404#ifdef USE_SPEAKER
405 if (this->speaker_ != nullptr) {
406 int16_t sample[2] = {0, 0};
407 this->speaker_->play((uint8_t *) (&sample), sizeof(sample));
408 this->speaker_->finish();
410 }
411#endif // USE_SPEAKER
412
413 // Ensure no more notes are played in case finish_() is called for an error.
414 this->position_ = this->rtttl_.length();
415 this->note_duration_ = 0;
416}
417
419 State old_state = this->state_;
420 this->state_ = state;
421 ESP_LOGV(TAG, "State changed from %s to %s", LOG_STR_ARG(state_to_string(old_state)),
422 LOG_STR_ARG(state_to_string(state)));
423
424 // Clear loop_done when transitioning from `State::STOPPED` to any other state
425 if (state == State::STOPPED) {
426 this->disable_loop();
427#ifdef USE_RTTTL_FINISHED_PLAYBACK_CALLBACK
429#endif
430 ESP_LOGD(TAG, "Playback finished");
431 } else if (old_state == State::STOPPED) {
432 this->enable_loop();
433 }
434}
435
436} // namespace esphome::rtttl
void enable_loop()
Enable this component's loop.
Definition component.h:246
void disable_loop()
Disable this component's loop.
void set_level(float state)
Set the level of this float output, this is called from the front-end.
virtual void update_frequency(float frequency)
Set the frequency of the output for PWM outputs.
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
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
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
speaker::Speaker * speaker_
The speaker to write the sound to.
Definition rtttl.h:102
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
virtual size_t play(const uint8_t *data, size_t length)=0
Plays the provided audio data.
bool is_running() const
Definition speaker.h:65
virtual void set_volume(float volume)
Definition speaker.h:70
void set_audio_stream_info(const audio::AudioStreamInfo &audio_stream_info)
Definition speaker.h:98
virtual void start()=0
virtual void finish()
Definition speaker.h:57
bool is_stopped() const
Definition speaker.h:66
virtual void stop()=0
bool state
Definition fan.h:2
double deg2rad(double degrees)
Definition rtttl.cpp:32
constexpr uint8_t DEFAULT_OCTAVE
Definition rtttl.h:19
constexpr uint8_t DEFAULT_NOTE_DENOMINATOR
Definition rtttl.h:18
PROGMEM_STRING_TABLE(RtttlStateStrings, "State::STOPPED", "State::INIT", "State::STARTING", "State::RUNNING", "State::STOPPING", "UNKNOWN")
const void size_t len
Definition hal.h:64
size_t size_t pos
Definition helpers.h:1038
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t