ESPHome  2024.4.1
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"
5 
6 namespace esphome {
7 namespace rtttl {
8 
9 static const char *const TAG = "rtttl";
10 
11 static const uint32_t DOUBLE_NOTE_GAP_MS = 10;
12 
13 // These values can also be found as constants in the Tone library (Tone.h)
14 static const uint16_t NOTES[] = {0, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
15  523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047,
16  1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217,
17  2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951};
18 
19 static const uint16_t I2S_SPEED = 1000;
20 
21 #undef HALF_PI
22 static const double HALF_PI = 1.5707963267948966192313216916398;
23 
24 inline double deg2rad(double degrees) {
25  static const double PI_ON_180 = 4.0 * atan(1.0) / 180.0;
26  return degrees * PI_ON_180;
27 }
28 
29 void Rtttl::dump_config() { ESP_LOGCONFIG(TAG, "Rtttl"); }
30 
31 void Rtttl::play(std::string rtttl) {
32  this->rtttl_ = std::move(rtttl);
33 
34  this->default_duration_ = 4;
35  this->default_octave_ = 6;
36  this->note_duration_ = 0;
37 
38  int bpm = 63;
39  uint8_t num;
40 
41  // Get name
42  this->position_ = rtttl_.find(':');
43 
44  // it's somewhat documented to be up to 10 characters but let's be a bit flexible here
45  if (this->position_ == std::string::npos || this->position_ > 15) {
46  ESP_LOGE(TAG, "Missing ':' when looking for name.");
47  return;
48  }
49 
50  auto name = this->rtttl_.substr(0, this->position_);
51  ESP_LOGD(TAG, "Playing song %s", name.c_str());
52 
53  // get default duration
54  this->position_ = this->rtttl_.find("d=", this->position_);
55  if (this->position_ == std::string::npos) {
56  ESP_LOGE(TAG, "Missing 'd='");
57  return;
58  }
59  this->position_ += 2;
60  num = this->get_integer_();
61  if (num > 0)
62  this->default_duration_ = num;
63 
64  // get default octave
65  this->position_ = this->rtttl_.find("o=", this->position_);
66  if (this->position_ == std::string::npos) {
67  ESP_LOGE(TAG, "Missing 'o=");
68  return;
69  }
70  this->position_ += 2;
71  num = get_integer_();
72  if (num >= 3 && num <= 7)
73  this->default_octave_ = num;
74 
75  // get BPM
76  this->position_ = this->rtttl_.find("b=", this->position_);
77  if (this->position_ == std::string::npos) {
78  ESP_LOGE(TAG, "Missing b=");
79  return;
80  }
81  this->position_ += 2;
82  num = get_integer_();
83  if (num != 0)
84  bpm = num;
85 
86  this->position_ = this->rtttl_.find(':', this->position_);
87  if (this->position_ == std::string::npos) {
88  ESP_LOGE(TAG, "Missing second ':'");
89  return;
90  }
91  this->position_++;
92 
93  // BPM usually expresses the number of quarter notes per minute
94  this->wholenote_ = 60 * 1000L * 4 / bpm; // this is the time for whole note (in milliseconds)
95 
96  this->output_freq_ = 0;
97  this->last_note_ = millis();
98  this->note_duration_ = 1;
99 
100 #ifdef USE_SPEAKER
101  this->samples_sent_ = 0;
102  this->samples_count_ = 0;
103 #endif
104 }
105 
106 void Rtttl::stop() {
107  this->note_duration_ = 0;
108 #ifdef USE_OUTPUT
109  if (this->output_ != nullptr) {
110  this->output_->set_level(0.0);
111  }
112 #endif
113 #ifdef USE_SPEAKER
114  if (this->speaker_ != nullptr) {
115  if (this->speaker_->is_running()) {
116  this->speaker_->stop();
117  }
118  }
119 #endif
120 }
121 
122 void Rtttl::loop() {
123  if (this->note_duration_ == 0)
124  return;
125 
126 #ifdef USE_SPEAKER
127  if (this->speaker_ != nullptr) {
128  if (this->samples_sent_ != this->samples_count_) {
129  SpeakerSample sample[SAMPLE_BUFFER_SIZE + 1];
130  int x = 0;
131  double rem = 0.0;
132 
133  while (true) {
134  // Try and send out the remainder of the existing note, one per loop()
135 
136  if (this->samples_per_wave_ != 0 && this->samples_sent_ >= this->samples_gap_) { // Play note//
137  rem = ((this->samples_sent_ << 10) % this->samples_per_wave_) * (360.0 / this->samples_per_wave_);
138 
139  int16_t val = (49152 * this->gain_) * sin(deg2rad(rem));
140 
141  sample[x].left = val;
142  sample[x].right = val;
143 
144  } else {
145  sample[x].left = 0;
146  sample[x].right = 0;
147  }
148 
149  if (x >= SAMPLE_BUFFER_SIZE || this->samples_sent_ >= this->samples_count_) {
150  break;
151  }
152  this->samples_sent_++;
153  x++;
154  }
155  if (x > 0) {
156  int send = this->speaker_->play((uint8_t *) (&sample), x * 4);
157  if (send != x * 4) {
158  this->samples_sent_ -= (x - (send / 4));
159  }
160  return;
161  }
162  }
163  }
164 #endif
165 #ifdef USE_OUTPUT
166  if (this->output_ != nullptr && millis() - this->last_note_ < this->note_duration_)
167  return;
168 #endif
169  if (!this->rtttl_[position_]) {
170  this->note_duration_ = 0;
171 #ifdef USE_OUTPUT
172  if (this->output_ != nullptr) {
173  this->output_->set_level(0.0);
174  }
175 #endif
176  ESP_LOGD(TAG, "Playback finished");
177  this->on_finished_playback_callback_.call();
178  return;
179  }
180 
181  // align to note: most rtttl's out there does not add and space after the ',' separator but just in case...
182  while (this->rtttl_[this->position_] == ',' || this->rtttl_[this->position_] == ' ')
183  this->position_++;
184 
185  // first, get note duration, if available
186  uint8_t num = this->get_integer_();
187 
188  if (num) {
189  this->note_duration_ = this->wholenote_ / num;
190  } else {
191  this->note_duration_ =
192  this->wholenote_ / this->default_duration_; // we will need to check if we are a dotted note after
193  }
194 
195  uint8_t note;
196 
197  switch (this->rtttl_[this->position_]) {
198  case 'c':
199  note = 1;
200  break;
201  case 'd':
202  note = 3;
203  break;
204  case 'e':
205  note = 5;
206  break;
207  case 'f':
208  note = 6;
209  break;
210  case 'g':
211  note = 8;
212  break;
213  case 'a':
214  note = 10;
215  break;
216  case 'b':
217  note = 12;
218  break;
219  case 'p':
220  default:
221  note = 0;
222  }
223  this->position_++;
224 
225  // now, get optional '#' sharp
226  if (this->rtttl_[this->position_] == '#') {
227  note++;
228  this->position_++;
229  }
230 
231  // now, get optional '.' dotted note
232  if (this->rtttl_[this->position_] == '.') {
233  this->note_duration_ += this->note_duration_ / 2;
234  this->position_++;
235  }
236 
237  // now, get scale
238  uint8_t scale = get_integer_();
239  if (scale == 0)
240  scale = this->default_octave_;
241  bool need_note_gap = false;
242 
243  // Now play the note
244  if (note) {
245  auto note_index = (scale - 4) * 12 + note;
246  if (note_index < 0 || note_index >= (int) sizeof(NOTES)) {
247  ESP_LOGE(TAG, "Note out of valid range");
248  this->note_duration_ = 0;
249  return;
250  }
251  auto freq = NOTES[note_index];
252  need_note_gap = freq == this->output_freq_;
253 
254  // Add small silence gap between same note
255  this->output_freq_ = freq;
256 
257  ESP_LOGVV(TAG, "playing note: %d for %dms", note, this->note_duration_);
258  } else {
259  ESP_LOGVV(TAG, "waiting: %dms", this->note_duration_);
260  this->output_freq_ = 0;
261  }
262 
263 #ifdef USE_OUTPUT
264  if (this->output_ != nullptr) {
265  if (need_note_gap) {
266  this->output_->set_level(0.0);
267  delay(DOUBLE_NOTE_GAP_MS);
268  this->note_duration_ -= DOUBLE_NOTE_GAP_MS;
269  }
270  if (this->output_freq_ != 0) {
271  this->output_->update_frequency(this->output_freq_);
272  this->output_->set_level(this->gain_);
273  } else {
274  this->output_->set_level(0.0);
275  }
276  }
277 #endif
278 #ifdef USE_SPEAKER
279  if (this->speaker_ != nullptr) {
280  this->samples_sent_ = 0;
281  this->samples_gap_ = 0;
282  this->samples_per_wave_ = 0;
283  this->samples_count_ = (this->sample_rate_ * this->note_duration_) / 1600; //(ms);
284  if (need_note_gap) {
285  this->samples_gap_ = (this->sample_rate_ * DOUBLE_NOTE_GAP_MS) / 1600; //(ms);
286  }
287  if (this->output_freq_ != 0) {
288  this->samples_per_wave_ = (this->sample_rate_ << 10) / this->output_freq_;
289 
290  // make sure there is enough samples to add a full last sinus.
291  uint16_t division = ((this->samples_count_ << 10) / this->samples_per_wave_) + 1;
292  uint16_t x = this->samples_count_;
293  this->samples_count_ = (division * this->samples_per_wave_);
294  ESP_LOGD(TAG, "play time old: %d div: %d new: %d %d", x, division, this->samples_count_, this->samples_per_wave_);
295  this->samples_count_ = this->samples_count_ >> 10;
296  }
297  // Convert from frequency in Hz to high and low samples in fixed point
298  }
299 #endif
300 
301  this->last_note_ = millis();
302 }
303 
304 } // namespace rtttl
305 } // namespace esphome
virtual size_t play(const uint8_t *data, size_t length)=0
uint16_t wholenote_
Definition: rtttl.h:63
bool is_running() const
Definition: speaker.h:23
const char * name
Definition: stm32flash.h:78
void play(std::string rtttl)
Definition: rtttl.cpp:31
uint16_t x
Definition: tt21100.cpp:17
num_t degrees(num_t rad)
Definition: sun.cpp:27
void dump_config() override
Definition: rtttl.cpp:29
void loop() override
Definition: rtttl.cpp:122
mopeka_std_values val[4]
CallbackManager< void()> on_finished_playback_callback_
Definition: rtttl.h:88
speaker::Speaker * speaker_
Definition: rtttl.h:79
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
uint32_t output_freq_
Definition: rtttl.h:69
void set_level(float state)
Set the level of this float output, this is called from the front-end.
uint8_t get_integer_()
Definition: rtttl.h:53
double deg2rad(double degrees)
Definition: rtttl.cpp:24
uint16_t note_duration_
Definition: rtttl.h:67
std::string rtttl_
Definition: rtttl.h:61
uint16_t default_duration_
Definition: rtttl.h:64
size_t position_
Definition: rtttl.h:62
output::FloatOutput * output_
Definition: rtttl.h:73
uint16_t default_octave_
Definition: rtttl.h:65
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
virtual void stop()=0
uint32_t last_note_
Definition: rtttl.h:66
virtual void update_frequency(float frequency)
Set the frequency of the output for PWM outputs.
Definition: float_output.h:67
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26