mirror of
https://github.com/esphome/esphome.git
synced 2026-05-27 11:56:11 +08:00
[rtttl] Fix speaker playback bugs (#14280)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
Jesse Hills
parent
da930310b1
commit
a39be5a461
@@ -139,9 +139,10 @@ void Rtttl::loop() {
|
|||||||
x++;
|
x++;
|
||||||
}
|
}
|
||||||
if (x > 0) {
|
if (x > 0) {
|
||||||
int send = this->speaker_->play((uint8_t *) (&sample), x * 2);
|
size_t bytes_to_send = x * sizeof(SpeakerSample);
|
||||||
if (send != x * 4) {
|
size_t send = this->speaker_->play((uint8_t *) (&sample), bytes_to_send);
|
||||||
this->samples_sent_ -= (x - (send / 2));
|
if (send != bytes_to_send) {
|
||||||
|
this->samples_sent_ -= (x - (send / sizeof(SpeakerSample)));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -201,9 +202,9 @@ void Rtttl::loop() {
|
|||||||
bool need_note_gap = false;
|
bool need_note_gap = false;
|
||||||
if (note) {
|
if (note) {
|
||||||
auto note_index = (scale - 4) * 12 + note;
|
auto note_index = (scale - 4) * 12 + note;
|
||||||
if (note_index < 0 || note_index >= (int) sizeof(NOTES)) {
|
if (note_index < 0 || note_index >= (int) (sizeof(NOTES) / sizeof(NOTES[0]))) {
|
||||||
ESP_LOGE(TAG, "Note out of range (note: %d, scale: %d, index: %d, max: %d)", note, scale, note_index,
|
ESP_LOGE(TAG, "Note out of range (note: %d, scale: %d, index: %d, max: %d)", note, scale, note_index,
|
||||||
(int) sizeof(NOTES));
|
(int) (sizeof(NOTES) / sizeof(NOTES[0])));
|
||||||
this->finish_();
|
this->finish_();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -221,7 +222,7 @@ void Rtttl::loop() {
|
|||||||
|
|
||||||
#ifdef USE_OUTPUT
|
#ifdef USE_OUTPUT
|
||||||
if (this->output_ != nullptr) {
|
if (this->output_ != nullptr) {
|
||||||
if (need_note_gap) {
|
if (need_note_gap && this->note_duration_ > DOUBLE_NOTE_GAP_MS) {
|
||||||
this->output_->set_level(0.0);
|
this->output_->set_level(0.0);
|
||||||
delay(DOUBLE_NOTE_GAP_MS);
|
delay(DOUBLE_NOTE_GAP_MS);
|
||||||
this->note_duration_ -= DOUBLE_NOTE_GAP_MS;
|
this->note_duration_ -= DOUBLE_NOTE_GAP_MS;
|
||||||
@@ -240,9 +241,9 @@ void Rtttl::loop() {
|
|||||||
this->samples_sent_ = 0;
|
this->samples_sent_ = 0;
|
||||||
this->samples_gap_ = 0;
|
this->samples_gap_ = 0;
|
||||||
this->samples_per_wave_ = 0;
|
this->samples_per_wave_ = 0;
|
||||||
this->samples_count_ = (this->sample_rate_ * this->note_duration_) / 1600; //(ms);
|
this->samples_count_ = (this->sample_rate_ * this->note_duration_) / 1000;
|
||||||
if (need_note_gap) {
|
if (need_note_gap) {
|
||||||
this->samples_gap_ = (this->sample_rate_ * DOUBLE_NOTE_GAP_MS) / 1600; //(ms);
|
this->samples_gap_ = (this->sample_rate_ * DOUBLE_NOTE_GAP_MS) / 1000;
|
||||||
}
|
}
|
||||||
if (this->output_freq_ != 0) {
|
if (this->output_freq_ != 0) {
|
||||||
// make sure there is enough samples to add a full last sinus.
|
// make sure there is enough samples to add a full last sinus.
|
||||||
@@ -279,7 +280,7 @@ void Rtttl::play(std::string rtttl) {
|
|||||||
this->note_duration_ = 0;
|
this->note_duration_ = 0;
|
||||||
|
|
||||||
int bpm = 63;
|
int bpm = 63;
|
||||||
uint8_t num;
|
uint16_t num;
|
||||||
|
|
||||||
// Get name
|
// Get name
|
||||||
this->position_ = this->rtttl_.find(':');
|
this->position_ = this->rtttl_.find(':');
|
||||||
@@ -395,7 +396,7 @@ void Rtttl::finish_() {
|
|||||||
sample[0].right = 0;
|
sample[0].right = 0;
|
||||||
sample[1].left = 0;
|
sample[1].left = 0;
|
||||||
sample[1].right = 0;
|
sample[1].right = 0;
|
||||||
this->speaker_->play((uint8_t *) (&sample), 8);
|
this->speaker_->play((uint8_t *) (&sample), sizeof(sample));
|
||||||
this->speaker_->finish();
|
this->speaker_->finish();
|
||||||
this->set_state_(State::STOPPING);
|
this->set_state_(State::STOPPING);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ class Rtttl : public Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
inline uint8_t get_integer_() {
|
inline uint16_t get_integer_() {
|
||||||
uint8_t ret = 0;
|
uint16_t ret = 0;
|
||||||
while (isdigit(this->rtttl_[this->position_])) {
|
while (isdigit(this->rtttl_[this->position_])) {
|
||||||
ret = (ret * 10) + (this->rtttl_[this->position_++] - '0');
|
ret = (ret * 10) + (this->rtttl_[this->position_++] - '0');
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@ class Rtttl : public Component {
|
|||||||
|
|
||||||
#ifdef USE_OUTPUT
|
#ifdef USE_OUTPUT
|
||||||
/// The output to write the sound to.
|
/// The output to write the sound to.
|
||||||
output::FloatOutput *output_;
|
output::FloatOutput *output_{nullptr};
|
||||||
#endif // USE_OUTPUT
|
#endif // USE_OUTPUT
|
||||||
|
|
||||||
#ifdef USE_SPEAKER
|
#ifdef USE_SPEAKER
|
||||||
|
|||||||
Reference in New Issue
Block a user