9 static const char *
const TAG =
"rtttl";
11 static const uint32_t DOUBLE_NOTE_GAP_MS = 10;
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};
19 static const uint16_t I2S_SPEED = 1000;
22 static 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;
33 int pos = this->
rtttl_.find(
':');
35 ESP_LOGW(TAG,
"RTTTL Component is already playing: %s",
name.c_str());
39 this->
rtttl_ = std::move(rtttl);
53 ESP_LOGE(TAG,
"Missing ':' when looking for name.");
58 ESP_LOGD(TAG,
"Playing song %s",
name.c_str());
62 if (this->position_ == std::string::npos) {
63 ESP_LOGE(TAG,
"Missing 'd='");
72 this->position_ = this->
rtttl_.find(
"o=", this->position_);
73 if (this->position_ == std::string::npos) {
74 ESP_LOGE(TAG,
"Missing 'o=");
79 if (num >= 3 && num <= 7)
83 this->position_ = this->
rtttl_.find(
"b=", this->position_);
84 if (this->position_ == std::string::npos) {
85 ESP_LOGE(TAG,
"Missing b=");
93 this->position_ = this->
rtttl_.find(
':', this->position_);
94 if (this->position_ == std::string::npos) {
95 ESP_LOGE(TAG,
"Missing second ':'");
115 if (this->
output_ !=
nullptr) {
123 if (this->
output_ !=
nullptr) {
190 int send = this->
speaker_->
play((uint8_t *) (&sample), x * 2);
209 while (this->
rtttl_[this->position_] ==
',' || this->
rtttl_[this->position_] ==
' ')
224 switch (this->
rtttl_[this->position_]) {
254 if (this->
rtttl_[this->position_] ==
'#') {
260 if (this->
rtttl_[this->position_] ==
'.') {
270 if (scale < 4 || scale > 7) {
271 ESP_LOGE(TAG,
"Octave out of valid range. Should be between 4 and 7. (Octave: %d)", scale);
275 bool need_note_gap =
false;
279 auto note_index = (scale - 4) * 12 + note;
280 if (note_index < 0 || note_index >= (
int)
sizeof(NOTES)) {
281 ESP_LOGE(TAG,
"Note out of valid range (note: %d, scale: %d, index: %d, max: %d)", note, scale, note_index,
282 (
int)
sizeof(NOTES));
286 auto freq = NOTES[note_index];
292 ESP_LOGVV(TAG,
"playing note: %d for %dms", note, this->
note_duration_);
299 if (this->
output_ !=
nullptr) {
302 delay(DOUBLE_NOTE_GAP_MS);
332 ESP_LOGVV(TAG,
"- Calc play time: wish: %d gets: %d (div: %d spw: %d)", samples_wish, this->
samples_count_,
344 if (this->
output_ !=
nullptr) {
364 ESP_LOGD(TAG,
"Playback finished");
367 static const LogString *state_to_string(
State state) {
370 return LOG_STR(
"STATE_STOPPED");
372 return LOG_STR(
"STATE_STARTING");
374 return LOG_STR(
"STATE_RUNNING");
376 return LOG_STR(
"STATE_STOPPING");
378 return LOG_STR(
"STATE_INIT");
380 return LOG_STR(
"UNKNOWN");
387 ESP_LOGD(TAG,
"State changed from %s to %s", LOG_STR_ARG(state_to_string(old_state)),
388 LOG_STR_ARG(state_to_string(state)));
virtual size_t play(const uint8_t *data, size_t length)=0
void play(std::string rtttl)
void dump_config() override
CallbackManager< void()> on_finished_playback_callback_
speaker::Speaker * speaker_
uint32_t IRAM_ATTR HOT millis()
void set_level(float state)
Set the level of this float output, this is called from the front-end.
void set_state_(State state)
double deg2rad(double degrees)
uint16_t default_duration_
output::FloatOutput * output_
Implementation of SPI Controller mode.
virtual void update_frequency(float frequency)
Set the frequency of the output for PWM outputs.
void IRAM_ATTR HOT delay(uint32_t ms)