[voice_assistant] Bugfix: Fix crash on start (#7662)

This commit is contained in:
Kevin Ahrendt 2024-10-23 13:25:31 -04:00 committed by GitHub
parent dd8d25e43f
commit fdebf04196
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 26 deletions

View file

@ -433,6 +433,7 @@ void VoiceAssistant::loop() {
#ifdef USE_SPEAKER #ifdef USE_SPEAKER
void VoiceAssistant::write_speaker_() { void VoiceAssistant::write_speaker_() {
if ((this->speaker_ != nullptr) && (this->speaker_buffer_ != nullptr)) {
if (this->speaker_buffer_size_ > 0) { if (this->speaker_buffer_size_ > 0) {
size_t write_chunk = std::min<size_t>(this->speaker_buffer_size_, 4 * 1024); size_t write_chunk = std::min<size_t>(this->speaker_buffer_size_, 4 * 1024);
size_t written = this->speaker_->play(this->speaker_buffer_, write_chunk); size_t written = this->speaker_->play(this->speaker_buffer_, write_chunk);
@ -445,6 +446,7 @@ void VoiceAssistant::write_speaker_() {
ESP_LOGV(TAG, "Speaker buffer full, trying again next loop"); ESP_LOGV(TAG, "Speaker buffer full, trying again next loop");
} }
} }
}
} }
#endif #endif
@ -772,16 +774,20 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
} }
case api::enums::VOICE_ASSISTANT_TTS_STREAM_START: { case api::enums::VOICE_ASSISTANT_TTS_STREAM_START: {
#ifdef USE_SPEAKER #ifdef USE_SPEAKER
if (this->speaker_ != nullptr) {
this->wait_for_stream_end_ = true; this->wait_for_stream_end_ = true;
ESP_LOGD(TAG, "TTS stream start"); ESP_LOGD(TAG, "TTS stream start");
this->defer([this] { this->tts_stream_start_trigger_->trigger(); }); this->defer([this] { this->tts_stream_start_trigger_->trigger(); });
}
#endif #endif
break; break;
} }
case api::enums::VOICE_ASSISTANT_TTS_STREAM_END: { case api::enums::VOICE_ASSISTANT_TTS_STREAM_END: {
#ifdef USE_SPEAKER #ifdef USE_SPEAKER
if (this->speaker_ != nullptr) {
this->stream_ended_ = true; this->stream_ended_ = true;
ESP_LOGD(TAG, "TTS stream end"); ESP_LOGD(TAG, "TTS stream end");
}
#endif #endif
break; break;
} }
@ -802,6 +808,7 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
void VoiceAssistant::on_audio(const api::VoiceAssistantAudio &msg) { void VoiceAssistant::on_audio(const api::VoiceAssistantAudio &msg) {
#ifdef USE_SPEAKER // We should never get to this function if there is no speaker anyway #ifdef USE_SPEAKER // We should never get to this function if there is no speaker anyway
if ((this->speaker_ != nullptr) && (this->speaker_buffer_ != nullptr)) {
if (this->speaker_buffer_index_ + msg.data.length() < SPEAKER_BUFFER_SIZE) { if (this->speaker_buffer_index_ + msg.data.length() < SPEAKER_BUFFER_SIZE) {
memcpy(this->speaker_buffer_ + this->speaker_buffer_index_, msg.data.data(), msg.data.length()); memcpy(this->speaker_buffer_ + this->speaker_buffer_index_, msg.data.data(), msg.data.length());
this->speaker_buffer_index_ += msg.data.length(); this->speaker_buffer_index_ += msg.data.length();
@ -811,6 +818,7 @@ void VoiceAssistant::on_audio(const api::VoiceAssistantAudio &msg) {
} else { } else {
ESP_LOGE(TAG, "Cannot receive audio, buffer is full"); ESP_LOGE(TAG, "Cannot receive audio, buffer is full");
} }
}
#endif #endif
} }

View file

@ -250,7 +250,7 @@ class VoiceAssistant : public Component {
#ifdef USE_SPEAKER #ifdef USE_SPEAKER
void write_speaker_(); void write_speaker_();
speaker::Speaker *speaker_{nullptr}; speaker::Speaker *speaker_{nullptr};
uint8_t *speaker_buffer_; uint8_t *speaker_buffer_{nullptr};
size_t speaker_buffer_index_{0}; size_t speaker_buffer_index_{0};
size_t speaker_buffer_size_{0}; size_t speaker_buffer_size_{0};
size_t speaker_bytes_received_{0}; size_t speaker_bytes_received_{0};
@ -282,8 +282,8 @@ class VoiceAssistant : public Component {
float volume_multiplier_; float volume_multiplier_;
uint32_t conversation_timeout_; uint32_t conversation_timeout_;
uint8_t *send_buffer_; uint8_t *send_buffer_{nullptr};
int16_t *input_buffer_; int16_t *input_buffer_{nullptr};
bool continuous_{false}; bool continuous_{false};
bool silence_detection_; bool silence_detection_;