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;
33 static constexpr double PI_ON_180 = M_PI / 180.0;
34 return degrees * PI_ON_180;
38#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
40PROGMEM_STRING_TABLE(RtttlStateStrings,
"State::STOPPED",
"State::INIT",
"State::STARTING",
"State::RUNNING",
41 "State::STOPPING",
"UNKNOWN");
43static const LogString *state_to_string(
State state) {
44 return RtttlStateStrings::get_log_str(
static_cast<uint8_t
>(
state), RtttlStateStrings::LAST_INDEX);
48static uint8_t note_index_from_char(
char note) {
121 int16_t sample[SAMPLE_BUFFER_SIZE];
122 uint16_t sample_index = 0;
129 sample[sample_index] = INT16_MAX * sin(
deg2rad(rem));
131 sample[sample_index] = 0;
136 if (sample_index > 0) {
137 size_t bytes = sample_index *
sizeof(int16_t);
138 size_t sent_bytes = this->
speaker_->
play((uint8_t *) (&sample), bytes);
139 size_t samples_sent = sent_bytes /
sizeof(int16_t);
140 if (samples_sent != sample_index) {
152 if (c !=
',' && c !=
' ')
165 if (note_denominator) {
172 uint8_t note_index_in_octave = note_index_from_char(this->
rtttl_[this->
position_]);
177 if (this->
rtttl_[this->position_] ==
'#') {
178 note_index_in_octave++;
188 if (scale < MIN_OCTAVE || scale > MAX_OCTAVE) {
189 ESP_LOGE(TAG,
"Octave must be between %d and %d (it is %d)", MIN_OCTAVE, MAX_OCTAVE, scale);
195 if (this->
rtttl_[this->position_] ==
'.') {
196 this->note_duration_ += this->note_duration_ / 2;
200 bool need_note_gap =
false;
203 if (note_index_in_octave == 0) {
205 ESP_LOGVV(TAG,
"Waiting: %dms", this->note_duration_);
207 uint8_t note_index = (scale - MIN_OCTAVE) * SEMITONES_IN_OCTAVE + note_index_in_octave;
208 if (note_index >= NOTES_COUNT) {
209 ESP_LOGE(TAG,
"Note out of range (note: %d, scale: %d, index: %d, max: %d)", note_index_in_octave, scale,
210 note_index, NOTES_COUNT);
214 uint16_t freq = NOTES[note_index];
220 ESP_LOGVV(TAG,
"Playing note: %d for %dms", note_index_in_octave, this->note_duration_);
224 if (this->
output_ !=
nullptr) {
228 if (need_note_gap && this->note_duration_ > REPEATING_NOTE_GAP_MS) {
230 delay(REPEATING_NOTE_GAP_MS);
231 this->note_duration_ -= REPEATING_NOTE_GAP_MS;
246 this->
samples_gap_ = (SAMPLE_RATE * REPEATING_NOTE_GAP_MS) / 1000;
256 ESP_LOGVV(TAG,
"Calc play time: wish: %" PRIu32
" gets: %" PRIu32
" (div: %d spw: %" PRIu32
")", samples_wish,
269 size_t len = (pos != std::string::npos) ?
pos : this->
rtttl_.length();
270 ESP_LOGW(TAG,
"Already playing: %.*s", (
int)
len, this->
rtttl_.c_str());
274 this->
rtttl_ = std::move(rtttl);
280 uint16_t bpm = DEFAULT_BPM;
286 if (this->
position_ == std::string::npos) {
287 ESP_LOGE(TAG,
"Unable to determine name; missing ':'");
290 if (this->
position_ >= SONG_NAME_LENGTH_LIMIT) {
291 ESP_LOGE(TAG,
"Name is too long: length=%u, limit=%u",
static_cast<unsigned>(this->
position_),
292 static_cast<unsigned>(SONG_NAME_LENGTH_LIMIT));
295 ESP_LOGD(TAG,
"Playing song %.*s", (
int) this->
position_, this->
rtttl_.c_str());
298 this->position_ = this->
rtttl_.find(
"d=", this->position_);
299 if (this->position_ == std::string::npos) {
300 ESP_LOGE(TAG,
"Missing 'd='");
303 this->position_ += 2;
305 if (num == 1 || num == 2 || num == 4 || num == 8 || num == 16 || num == 32) {
308 ESP_LOGE(TAG,
"Invalid default duration: %d", num);
313 this->position_ = this->
rtttl_.find(
"o=", this->position_);
314 if (this->position_ == std::string::npos) {
315 ESP_LOGE(TAG,
"Missing 'o=");
318 this->position_ += 2;
320 if (num >= MIN_OCTAVE && num <= MAX_OCTAVE) {
323 ESP_LOGE(TAG,
"Invalid default octave: %d", num);
328 this->position_ = this->
rtttl_.find(
"b=", this->position_);
329 if (this->position_ == std::string::npos) {
330 ESP_LOGE(TAG,
"Missing b=");
333 this->position_ += 2;
338 ESP_LOGE(TAG,
"Invalid BPM: %d", num);
342 this->position_ = this->
rtttl_.find(
':', this->position_);
343 if (this->position_ == std::string::npos) {
344 ESP_LOGE(TAG,
"Missing second ':'");
357 if (this->
output_ !=
nullptr) {
373 if (this->
output_ !=
nullptr) {
393 ESP_LOGV(TAG,
"Rtttl::finish_()");
396 if (this->
output_ !=
nullptr) {
404 int16_t sample[2] = {0, 0};
405 this->
speaker_->
play((uint8_t *) (&sample),
sizeof(sample));
419 ESP_LOGV(TAG,
"State changed from %s to %s", LOG_STR_ARG(state_to_string(old_state)),
420 LOG_STR_ARG(state_to_string(
state)));
426 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.
virtual void set_volume(float volume)
void set_audio_stream_info(const audio::AudioStreamInfo &audio_stream_info)
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()