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) {
146 ESP_LOGV(TAG,
"Rtttl::finish_()");
148 if (this->
output_ !=
nullptr) {
225 int send = this->
speaker_->
play((uint8_t *) (&sample),
x * 2);
251 this->note_duration_ = this->
wholenote_ / num;
253 this->note_duration_ =
289 if (this->
rtttl_[this->position_] ==
'#') {
295 if (this->
rtttl_[this->position_] ==
'.') {
296 this->note_duration_ += this->note_duration_ / 2;
305 if (scale < 4 || scale > 7) {
306 ESP_LOGE(TAG,
"Octave must be between 4 and 7 (it is %d)", scale);
310 bool need_note_gap =
false;
314 auto note_index = (scale - 4) * 12 + note;
315 if (note_index < 0 || note_index >= (
int)
sizeof(NOTES)) {
316 ESP_LOGE(TAG,
"Note out of range (note: %d, scale: %d, index: %d, max: %d)", note, scale, note_index,
317 (
int)
sizeof(NOTES));
321 auto freq = NOTES[note_index];
327 ESP_LOGVV(TAG,
"playing note: %d for %dms", note, this->note_duration_);
329 ESP_LOGVV(TAG,
"waiting: %dms", this->note_duration_);
334 if (this->
output_ !=
nullptr) {
337 delay(DOUBLE_NOTE_GAP_MS);
338 this->note_duration_ -= DOUBLE_NOTE_GAP_MS;
367 ESP_LOGVV(TAG,
"- Calc play time: wish: %d gets: %d (div: %d spw: %d)", samples_wish, this->
samples_count_,
377#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
378static const LogString *state_to_string(
State state) {
381 return LOG_STR(
"STATE_STOPPED");
383 return LOG_STR(
"STATE_STARTING");
385 return LOG_STR(
"STATE_RUNNING");
387 return LOG_STR(
"STATE_STOPPING");
389 return LOG_STR(
"STATE_INIT");
391 return LOG_STR(
"UNKNOWN");
399 ESP_LOGV(TAG,
"State changed from %s to %s", LOG_STR_ARG(state_to_string(old_state)),
400 LOG_STR_ARG(state_to_string(
state)));
406 ESP_LOGD(TAG,
"Playback finished");
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.
int samples_per_wave_
The number of samples for one full cycle of a note's waveform, in Q10 fixed-point format.
uint16_t wholenote_
The duration of a whole note in milliseconds.
uint32_t last_note_
The time the last note was started.
int samples_sent_
The number of samples sent.
uint16_t note_duration_
The duration of the current note in milliseconds.
int samples_gap_
The number of samples for the gap between notes.
int sample_rate_
The sample rate of the speaker.
output::FloatOutput * output_
The output to write the sound to.
void dump_config() override
void set_state_(State state)
void finish_()
Finalizes the playback of the RTTTL string.
float gain_
The gain of the output.
uint32_t output_freq_
The frequency of the current note in Hz.
uint16_t default_duration_
The default duration of a note (e.g. 4 for a quarter note).
size_t position_
The current position in the RTTTL string.
uint16_t default_octave_
The default octave for a note.
State state_
The current state of the RTTTL player.
int samples_count_
The total number of samples to send.
speaker::Speaker * speaker_
The speaker to write the sound to.
CallbackManager< void()> on_finished_playback_callback_
The callback to call when playback is finished.
void play(std::string rtttl)
std::string rtttl_
The RTTTL string to play.
virtual size_t play(const uint8_t *data, size_t length)=0
Plays the provided audio data.
double deg2rad(double degrees)
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()