9static const char *
const TAG =
"rtttl";
11static const uint32_t DOUBLE_NOTE_GAP_MS = 10;
14static 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};
19static const uint16_t I2S_SPEED = 1000;
22static const double HALF_PI = 1.5707963267948966192313216916398;
25 static const double PI_ON_180 = 4.0 * atan(1.0) / 180.0;
26 return degrees * PI_ON_180;
38 int pos = this->
rtttl_.find(
':');
39 auto name = this->
rtttl_.substr(0, pos);
40 ESP_LOGW(TAG,
"Already playing: %s", name.c_str());
44 this->
rtttl_ = std::move(rtttl);
58 ESP_LOGE(TAG,
"Unable to determine name; missing ':'");
63 ESP_LOGD(TAG,
"Playing song %s", name.c_str());
67 if (this->
position_ == std::string::npos) {
68 ESP_LOGE(TAG,
"Missing 'd='");
78 if (this->
position_ == std::string::npos) {
79 ESP_LOGE(TAG,
"Missing 'o=");
84 if (num >= 3 && num <= 7)
89 if (this->
position_ == std::string::npos) {
90 ESP_LOGE(TAG,
"Missing b=");
99 if (this->
position_ == std::string::npos) {
100 ESP_LOGE(TAG,
"Missing second ':'");
120 if (this->
output_ !=
nullptr) {
128 if (this->
output_ !=
nullptr) {
195 int send = this->
speaker_->
play((uint8_t *) (&sample),
x * 2);
214 while (this->
rtttl_[this->position_] ==
',' || this->
rtttl_[this->position_] ==
' ')
221 this->note_duration_ = this->
wholenote_ / num;
223 this->note_duration_ =
229 switch (this->
rtttl_[this->position_]) {
259 if (this->
rtttl_[this->position_] ==
'#') {
265 if (this->
rtttl_[this->position_] ==
'.') {
266 this->note_duration_ += this->note_duration_ / 2;
275 if (scale < 4 || scale > 7) {
276 ESP_LOGE(TAG,
"Octave must be between 4 and 7 (it is %d)", scale);
280 bool need_note_gap =
false;
284 auto note_index = (scale - 4) * 12 + note;
285 if (note_index < 0 || note_index >= (
int)
sizeof(NOTES)) {
286 ESP_LOGE(TAG,
"Note out of range (note: %d, scale: %d, index: %d, max: %d)", note, scale, note_index,
287 (
int)
sizeof(NOTES));
291 auto freq = NOTES[note_index];
297 ESP_LOGVV(TAG,
"playing note: %d for %dms", note, this->note_duration_);
299 ESP_LOGVV(TAG,
"waiting: %dms", this->note_duration_);
304 if (this->
output_ !=
nullptr) {
307 delay(DOUBLE_NOTE_GAP_MS);
308 this->note_duration_ -= DOUBLE_NOTE_GAP_MS;
337 ESP_LOGVV(TAG,
"- Calc play time: wish: %d gets: %d (div: %d spw: %d)", samples_wish, this->
samples_count_,
349 if (this->
output_ !=
nullptr) {
369 ESP_LOGD(TAG,
"Playback finished");
372static const LogString *state_to_string(
State state) {
375 return LOG_STR(
"STATE_STOPPED");
377 return LOG_STR(
"STATE_STARTING");
379 return LOG_STR(
"STATE_RUNNING");
381 return LOG_STR(
"STATE_STOPPING");
383 return LOG_STR(
"STATE_INIT");
385 return LOG_STR(
"UNKNOWN");
392 ESP_LOGV(TAG,
"State changed from %s to %s", LOG_STR_ARG(state_to_string(old_state)),
393 LOG_STR_ARG(state_to_string(
state)));