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