ESPHome 2026.5.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 // Get default duration
298 this->position_ = this->rtttl_.find("d=", this->position_);
299 if (this->position_ == std::string::npos) {
300 ESP_LOGE(TAG, "Missing 'd='");
301 return;
302 }
303 this->position_ += 2;
304 num = this->get_integer_();
305 if (num == 1 || num == 2 || num == 4 || num == 8 || num == 16 || num == 32) {
306 this->default_note_denominator_ = num;
307 } else {
308 ESP_LOGE(TAG, "Invalid default duration: %d", num);
309 return;
310 }
311
312 // Get default octave
313 this->position_ = this->rtttl_.find("o=", this->position_);
314 if (this->position_ == std::string::npos) {
315 ESP_LOGE(TAG, "Missing 'o=");
316 return;
317 }
318 this->position_ += 2;
319 num = this->get_integer_();
320 if (num >= MIN_OCTAVE && num <= MAX_OCTAVE) {
321 this->default_octave_ = num;
322 } else {
323 ESP_LOGE(TAG, "Invalid default octave: %d", num);
324 return;
325 }
326
327 // Get BPM
328 this->position_ = this->rtttl_.find("b=", this->position_);
329 if (this->position_ == std::string::npos) {
330 ESP_LOGE(TAG, "Missing b=");
331 return;
332 }
333 this->position_ += 2;
334 num = this->get_integer_();
335 if (num >= 4) { // Below 4 is not realistic and would cause a integer overflow
336 bpm = num;
337 } else {
338 ESP_LOGE(TAG, "Invalid BPM: %d", num);
339 return;
340 }
341
342 this->position_ = this->rtttl_.find(':', this->position_);
343 if (this->position_ == std::string::npos) {
344 ESP_LOGE(TAG, "Missing second ':'");
345 return;
346 }
347 this->position_++;
348
349 // BPM usually expresses the number of quarter notes per minute
350 this->wholenote_duration_ = 60 * 1000L * 4 / bpm; // This is the time for whole note (in milliseconds)
351
352 this->output_freq_ = 0;
354 this->note_duration_ = 1;
355
356#ifdef USE_OUTPUT
357 if (this->output_ != nullptr) {
359 }
360#endif // USE_OUTPUT
361
362#ifdef USE_SPEAKER
363 if (this->speaker_ != nullptr) {
364 this->set_state_(State::INIT);
365 this->samples_sent_ = 0;
366 this->samples_count_ = 0;
367 }
368#endif // USE_SPEAKER
369}
370
372#ifdef USE_OUTPUT
373 if (this->output_ != nullptr) {
374 this->output_->set_level(0.0);
376 }
377#endif // USE_OUTPUT
378
379#ifdef USE_SPEAKER
380 if (this->speaker_ != nullptr) {
381 if (this->speaker_->is_running()) {
382 this->speaker_->stop();
383 }
385 }
386#endif // USE_SPEAKER
387
388 this->position_ = this->rtttl_.length();
389 this->note_duration_ = 0;
390}
391
393 ESP_LOGV(TAG, "Rtttl::finish_()");
394
395#ifdef USE_OUTPUT
396 if (this->output_ != nullptr) {
397 this->output_->set_level(0.0);
399 }
400#endif // USE_OUTPUT
401
402#ifdef USE_SPEAKER
403 if (this->speaker_ != nullptr) {
404 int16_t sample[2] = {0, 0};
405 this->speaker_->play((uint8_t *) (&sample), sizeof(sample));
406 this->speaker_->finish();
408 }
409#endif // USE_SPEAKER
410
411 // Ensure no more notes are played in case finish_() is called for an error.
412 this->position_ = this->rtttl_.length();
413 this->note_duration_ = 0;
414}
415
417 State old_state = this->state_;
418 this->state_ = state;
419 ESP_LOGV(TAG, "State changed from %s to %s", LOG_STR_ARG(state_to_string(old_state)),
420 LOG_STR_ARG(state_to_string(state)));
421
422 // Clear loop_done when transitioning from `State::STOPPED` to any other state
423 if (state == State::STOPPED) {
424 this->disable_loop();
426 ESP_LOGD(TAG, "Playback finished");
427 } else if (old_state == State::STOPPED) {
428 this->enable_loop();
429 }
430}
431
432} // namespace esphome::rtttl
void enable_loop()
Enable this component's loop.
Definition component.h:258
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:77
void set_state_(State state)
Definition rtttl.cpp:416
void finish_()
Finalizes the playback of the RTTTL string.
Definition rtttl.cpp:392
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:84
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:266
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 set_volume(float volume)
Definition speaker.h:71
void set_audio_stream_info(const audio::AudioStreamInfo &audio_stream_info)
Definition speaker.h:99
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
double deg2rad(double degrees)
Definition rtttl.cpp:32
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:1045
size_t size_t pos
Definition helpers.h:1082
void HOT delay(uint32_t ms)
Definition core.cpp:28
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:26
static void uint32_t