9static const char *
const TAG =
"rtttl";
11static constexpr uint8_t SONG_NAME_LENGTH_LIMIT = 64;
12static constexpr uint8_t SEMITONES_IN_OCTAVE = 12;
14static constexpr uint8_t MIN_OCTAVE = 4;
15static constexpr uint8_t MAX_OCTAVE = 7;
17static constexpr uint8_t DEFAULT_BPM = 63;
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]));
26static constexpr uint8_t REPEATING_NOTE_GAP_MS = 10;
29static constexpr uint16_t SAMPLE_BUFFER_SIZE = 2048;
30static constexpr uint16_t SAMPLE_RATE = 16000;
38 static constexpr double PI_ON_180 = M_PI / 180.0;
39 return degrees * PI_ON_180;
43#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
45PROGMEM_STRING_TABLE(RtttlStateStrings,
"State::STOPPED",
"State::INIT",
"State::STARTING",
"State::RUNNING",
46 "State::STOPPING",
"UNKNOWN");
48static const LogString *state_to_string(
State state) {
49 return RtttlStateStrings::get_log_str(
static_cast<uint8_t
>(
state), RtttlStateStrings::LAST_INDEX);
53static uint8_t note_index_from_char(
char note) {
123 SpeakerSample sample[SAMPLE_BUFFER_SIZE + 2];
124 uint16_t sample_index = 0;
134 sample[sample_index].left =
val;
135 sample[sample_index].right =
val;
137 sample[sample_index].left = 0;
138 sample[sample_index].right = 0;
147 if (sample_index > 0) {
148 size_t bytes_to_send = sample_index *
sizeof(SpeakerSample);
149 size_t send = this->
speaker_->
play((uint8_t *) (&sample), bytes_to_send);
150 if (send != bytes_to_send) {
151 this->
samples_sent_ -= (sample_index - (send /
sizeof(SpeakerSample)));
172 if (note_denominator) {
179 uint8_t note_index_in_octave = note_index_from_char(this->
rtttl_[this->
position_]);
184 if (this->
rtttl_[this->position_] ==
'#') {
185 note_index_in_octave++;
195 if (scale < MIN_OCTAVE || scale > MAX_OCTAVE) {
196 ESP_LOGE(TAG,
"Octave must be between %d and %d (it is %d)", MIN_OCTAVE, MAX_OCTAVE, scale);
202 if (this->
rtttl_[this->position_] ==
'.') {
203 this->note_duration_ += this->note_duration_ / 2;
207 bool need_note_gap =
false;
210 if (note_index_in_octave == 0) {
212 ESP_LOGVV(TAG,
"Waiting: %dms", this->note_duration_);
214 uint8_t note_index = (scale - MIN_OCTAVE) * SEMITONES_IN_OCTAVE + note_index_in_octave;
215 if (note_index >= NOTES_COUNT) {
216 ESP_LOGE(TAG,
"Note out of range (note: %d, scale: %d, index: %d, max: %d)", note_index_in_octave, scale,
217 note_index, NOTES_COUNT);
221 uint16_t freq = NOTES[note_index];
227 ESP_LOGVV(TAG,
"Playing note: %d for %dms", note_index_in_octave, this->note_duration_);
231 if (this->
output_ !=
nullptr) {
235 if (need_note_gap && this->note_duration_ > REPEATING_NOTE_GAP_MS) {
237 delay(REPEATING_NOTE_GAP_MS);
238 this->note_duration_ -= REPEATING_NOTE_GAP_MS;
253 this->
samples_gap_ = (SAMPLE_RATE * REPEATING_NOTE_GAP_MS) / 1000;
263 ESP_LOGVV(TAG,
"Calc play time: wish: %" PRIu32
" gets: %" PRIu32
" (div: %d spw: %" PRIu32
")", samples_wish,
276 size_t len = (pos != std::string::npos) ?
pos : this->
rtttl_.length();
277 ESP_LOGW(TAG,
"Already playing: %.*s", (
int)
len, this->
rtttl_.c_str());
281 this->
rtttl_ = std::move(rtttl);
287 uint16_t bpm = DEFAULT_BPM;
293 if (this->
position_ == std::string::npos) {
294 ESP_LOGE(TAG,
"Unable to determine name; missing ':'");
297 if (this->
position_ >= SONG_NAME_LENGTH_LIMIT) {
298 ESP_LOGE(TAG,
"Name is too long: length=%u, limit=%u",
static_cast<unsigned>(this->
position_),
299 static_cast<unsigned>(SONG_NAME_LENGTH_LIMIT));
302 ESP_LOGD(TAG,
"Playing song %.*s", (
int) this->
position_, this->
rtttl_.c_str());
305 this->position_ = this->
rtttl_.find(
"d=", this->position_);
306 if (this->position_ == std::string::npos) {
307 ESP_LOGE(TAG,
"Missing 'd='");
310 this->position_ += 2;
312 if (num == 1 || num == 2 || num == 4 || num == 8 || num == 16 || num == 32) {
315 ESP_LOGE(TAG,
"Invalid default duration: %d", num);
320 this->position_ = this->
rtttl_.find(
"o=", this->position_);
321 if (this->position_ == std::string::npos) {
322 ESP_LOGE(TAG,
"Missing 'o=");
325 this->position_ += 2;
327 if (num >= MIN_OCTAVE && num <= MAX_OCTAVE) {
330 ESP_LOGE(TAG,
"Invalid default octave: %d", num);
335 this->position_ = this->
rtttl_.find(
"b=", this->position_);
336 if (this->position_ == std::string::npos) {
337 ESP_LOGE(TAG,
"Missing b=");
340 this->position_ += 2;
345 ESP_LOGE(TAG,
"Invalid BPM: %d", num);
349 this->position_ = this->
rtttl_.find(
':', this->position_);
350 if (this->position_ == std::string::npos) {
351 ESP_LOGE(TAG,
"Missing second ':'");
364 if (this->
output_ !=
nullptr) {
380 if (this->
output_ !=
nullptr) {
400 ESP_LOGV(TAG,
"Rtttl::finish_()");
403 if (this->
output_ !=
nullptr) {
411 SpeakerSample sample[2];
416 this->
speaker_->
play((uint8_t *) (&sample),
sizeof(sample));
430 ESP_LOGV(TAG,
"State changed from %s to %s", LOG_STR_ARG(state_to_string(old_state)),
431 LOG_STR_ARG(state_to_string(
state)));
437 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.
uint32_t samples_per_wave_
The number of samples for one full cycle of a note's waveform, in Q10 fixed-point format.
uint32_t last_note_start_time_
The time in milliseconds since microcontroller boot when the last note was started.
uint8_t default_note_denominator_
The default duration of a note (e.g. 4 for a quarter note).
uint16_t note_duration_
The duration of the current note in milliseconds.
uint32_t samples_sent_
The number of samples sent.
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.
uint8_t default_octave_
The default octave for a note.
uint32_t output_freq_
The frequency of the current note in Hz.
uint32_t samples_gap_
The number of samples for the gap between notes.
size_t position_
The current position in the RTTTL string.
uint32_t samples_count_
The total number of samples to send.
uint16_t wholenote_duration_
The duration of a whole note in milliseconds.
State state_
The current state of the RTTTL player.
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)
constexpr uint8_t DEFAULT_OCTAVE
constexpr uint8_t DEFAULT_NOTE_DENOMINATOR
PROGMEM_STRING_TABLE(RtttlStateStrings, "State::STOPPED", "State::INIT", "State::STARTING", "State::RUNNING", "State::STOPPING", "UNKNOWN")
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()