mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[voice_assistant] Don't allocate buffers until starting the microphone for the first time (#6800)
This commit is contained in:
		| @@ -71,6 +71,12 @@ void VoiceAssistant::setup() { | |||||||
|   ESP_LOGCONFIG(TAG, "Setting up Voice Assistant..."); |   ESP_LOGCONFIG(TAG, "Setting up Voice Assistant..."); | ||||||
|  |  | ||||||
|   global_voice_assistant = this; |   global_voice_assistant = this; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool VoiceAssistant::allocate_buffers_() { | ||||||
|  |   if (this->send_buffer_ != nullptr) { | ||||||
|  |     return true;  // Already allocated | ||||||
|  |   } | ||||||
|  |  | ||||||
| #ifdef USE_SPEAKER | #ifdef USE_SPEAKER | ||||||
|   if (this->speaker_ != nullptr) { |   if (this->speaker_ != nullptr) { | ||||||
| @@ -78,8 +84,7 @@ void VoiceAssistant::setup() { | |||||||
|     this->speaker_buffer_ = speaker_allocator.allocate(SPEAKER_BUFFER_SIZE); |     this->speaker_buffer_ = speaker_allocator.allocate(SPEAKER_BUFFER_SIZE); | ||||||
|     if (this->speaker_buffer_ == nullptr) { |     if (this->speaker_buffer_ == nullptr) { | ||||||
|       ESP_LOGW(TAG, "Could not allocate speaker buffer"); |       ESP_LOGW(TAG, "Could not allocate speaker buffer"); | ||||||
|       this->mark_failed(); |       return false; | ||||||
|       return; |  | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
| #endif | #endif | ||||||
| @@ -88,8 +93,7 @@ void VoiceAssistant::setup() { | |||||||
|   this->input_buffer_ = allocator.allocate(INPUT_BUFFER_SIZE); |   this->input_buffer_ = allocator.allocate(INPUT_BUFFER_SIZE); | ||||||
|   if (this->input_buffer_ == nullptr) { |   if (this->input_buffer_ == nullptr) { | ||||||
|     ESP_LOGW(TAG, "Could not allocate input buffer"); |     ESP_LOGW(TAG, "Could not allocate input buffer"); | ||||||
|     this->mark_failed(); |     return false; | ||||||
|     return; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
| #ifdef USE_ESP_ADF | #ifdef USE_ESP_ADF | ||||||
| @@ -99,17 +103,71 @@ void VoiceAssistant::setup() { | |||||||
|   this->ring_buffer_ = RingBuffer::create(BUFFER_SIZE * sizeof(int16_t)); |   this->ring_buffer_ = RingBuffer::create(BUFFER_SIZE * sizeof(int16_t)); | ||||||
|   if (this->ring_buffer_ == nullptr) { |   if (this->ring_buffer_ == nullptr) { | ||||||
|     ESP_LOGW(TAG, "Could not allocate ring buffer"); |     ESP_LOGW(TAG, "Could not allocate ring buffer"); | ||||||
|     this->mark_failed(); |     return false; | ||||||
|     return; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   ExternalRAMAllocator<uint8_t> send_allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); |   ExternalRAMAllocator<uint8_t> send_allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); | ||||||
|   this->send_buffer_ = send_allocator.allocate(SEND_BUFFER_SIZE); |   this->send_buffer_ = send_allocator.allocate(SEND_BUFFER_SIZE); | ||||||
|   if (send_buffer_ == nullptr) { |   if (send_buffer_ == nullptr) { | ||||||
|     ESP_LOGW(TAG, "Could not allocate send buffer"); |     ESP_LOGW(TAG, "Could not allocate send buffer"); | ||||||
|     this->mark_failed(); |     return false; | ||||||
|     return; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   return true; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void VoiceAssistant::clear_buffers_() { | ||||||
|  |   if (this->send_buffer_ != nullptr) { | ||||||
|  |     memset(this->send_buffer_, 0, SEND_BUFFER_SIZE); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   if (this->input_buffer_ != nullptr) { | ||||||
|  |     memset(this->input_buffer_, 0, INPUT_BUFFER_SIZE * sizeof(int16_t)); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   if (this->ring_buffer_ != nullptr) { | ||||||
|  |     this->ring_buffer_->reset(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | #ifdef USE_SPEAKER | ||||||
|  |   if (this->speaker_buffer_ != nullptr) { | ||||||
|  |     memset(this->speaker_buffer_, 0, SPEAKER_BUFFER_SIZE); | ||||||
|  |  | ||||||
|  |     this->speaker_buffer_size_ = 0; | ||||||
|  |     this->speaker_buffer_index_ = 0; | ||||||
|  |     this->speaker_bytes_received_ = 0; | ||||||
|  |   } | ||||||
|  | #endif | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void VoiceAssistant::deallocate_buffers_() { | ||||||
|  |   ExternalRAMAllocator<uint8_t> send_deallocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); | ||||||
|  |   send_deallocator.deallocate(this->send_buffer_, SEND_BUFFER_SIZE); | ||||||
|  |   this->send_buffer_ = nullptr; | ||||||
|  |  | ||||||
|  |   if (this->ring_buffer_ != nullptr) { | ||||||
|  |     this->ring_buffer_.reset(); | ||||||
|  |     this->ring_buffer_ = nullptr; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  | #ifdef USE_ESP_ADF | ||||||
|  |   if (this->vad_instance_ != nullptr) { | ||||||
|  |     vad_destroy(this->vad_instance_); | ||||||
|  |     this->vad_instance_ = nullptr; | ||||||
|  |   } | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  |   ExternalRAMAllocator<int16_t> input_deallocator(ExternalRAMAllocator<int16_t>::ALLOW_FAILURE); | ||||||
|  |   input_deallocator.deallocate(this->input_buffer_, INPUT_BUFFER_SIZE); | ||||||
|  |   this->input_buffer_ = nullptr; | ||||||
|  |  | ||||||
|  | #ifdef USE_SPEAKER | ||||||
|  |   if (this->speaker_buffer_ != nullptr) { | ||||||
|  |     ExternalRAMAllocator<uint8_t> speaker_deallocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); | ||||||
|  |     speaker_deallocator.deallocate(this->speaker_buffer_, SPEAKER_BUFFER_SIZE); | ||||||
|  |     this->speaker_buffer_ = nullptr; | ||||||
|  |   } | ||||||
|  | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
| int VoiceAssistant::read_microphone_() { | int VoiceAssistant::read_microphone_() { | ||||||
| @@ -138,14 +196,13 @@ void VoiceAssistant::loop() { | |||||||
|     } |     } | ||||||
|     this->continuous_ = false; |     this->continuous_ = false; | ||||||
|     this->signal_stop_(); |     this->signal_stop_(); | ||||||
|  |     this->clear_buffers_(); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   switch (this->state_) { |   switch (this->state_) { | ||||||
|     case State::IDLE: { |     case State::IDLE: { | ||||||
|       if (this->continuous_ && this->desired_state_ == State::IDLE) { |       if (this->continuous_ && this->desired_state_ == State::IDLE) { | ||||||
|         this->idle_trigger_->trigger(); |         this->idle_trigger_->trigger(); | ||||||
|  |  | ||||||
|         this->ring_buffer_->reset(); |  | ||||||
| #ifdef USE_ESP_ADF | #ifdef USE_ESP_ADF | ||||||
|         if (this->use_wake_word_) { |         if (this->use_wake_word_) { | ||||||
|           this->set_state_(State::START_MICROPHONE, State::WAIT_FOR_VAD); |           this->set_state_(State::START_MICROPHONE, State::WAIT_FOR_VAD); | ||||||
| @@ -161,8 +218,15 @@ void VoiceAssistant::loop() { | |||||||
|     } |     } | ||||||
|     case State::START_MICROPHONE: { |     case State::START_MICROPHONE: { | ||||||
|       ESP_LOGD(TAG, "Starting Microphone"); |       ESP_LOGD(TAG, "Starting Microphone"); | ||||||
|       memset(this->send_buffer_, 0, SEND_BUFFER_SIZE); |       if (!this->allocate_buffers_()) { | ||||||
|       memset(this->input_buffer_, 0, INPUT_BUFFER_SIZE * sizeof(int16_t)); |         this->status_set_error("Failed to allocate buffers"); | ||||||
|  |         return; | ||||||
|  |       } | ||||||
|  |       if (this->status_has_error()) { | ||||||
|  |         this->status_clear_error(); | ||||||
|  |       } | ||||||
|  |       this->clear_buffers_(); | ||||||
|  |  | ||||||
|       this->mic_->start(); |       this->mic_->start(); | ||||||
|       this->high_freq_.start(); |       this->high_freq_.start(); | ||||||
|       this->set_state_(State::STARTING_MICROPHONE); |       this->set_state_(State::STARTING_MICROPHONE); | ||||||
| @@ -343,10 +407,9 @@ void VoiceAssistant::loop() { | |||||||
|         this->speaker_->stop(); |         this->speaker_->stop(); | ||||||
|         this->cancel_timeout("speaker-timeout"); |         this->cancel_timeout("speaker-timeout"); | ||||||
|         this->cancel_timeout("playing"); |         this->cancel_timeout("playing"); | ||||||
|         this->speaker_buffer_size_ = 0; |  | ||||||
|         this->speaker_buffer_index_ = 0; |         this->clear_buffers_(); | ||||||
|         this->speaker_bytes_received_ = 0; |  | ||||||
|         memset(this->speaker_buffer_, 0, SPEAKER_BUFFER_SIZE); |  | ||||||
|         this->wait_for_stream_end_ = false; |         this->wait_for_stream_end_ = false; | ||||||
|         this->stream_ended_ = false; |         this->stream_ended_ = false; | ||||||
|  |  | ||||||
| @@ -507,7 +570,6 @@ void VoiceAssistant::request_start(bool continuous, bool silence_detection) { | |||||||
|   if (this->state_ == State::IDLE) { |   if (this->state_ == State::IDLE) { | ||||||
|     this->continuous_ = continuous; |     this->continuous_ = continuous; | ||||||
|     this->silence_detection_ = silence_detection; |     this->silence_detection_ = silence_detection; | ||||||
|     this->ring_buffer_->reset(); |  | ||||||
| #ifdef USE_ESP_ADF | #ifdef USE_ESP_ADF | ||||||
|     if (this->use_wake_word_) { |     if (this->use_wake_word_) { | ||||||
|       this->set_state_(State::START_MICROPHONE, State::WAIT_FOR_VAD); |       this->set_state_(State::START_MICROPHONE, State::WAIT_FOR_VAD); | ||||||
|   | |||||||
| @@ -151,6 +151,10 @@ class VoiceAssistant : public Component { | |||||||
|   void set_wake_word(const std::string &wake_word) { this->wake_word_ = wake_word; } |   void set_wake_word(const std::string &wake_word) { this->wake_word_ = wake_word; } | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|  |   bool allocate_buffers_(); | ||||||
|  |   void clear_buffers_(); | ||||||
|  |   void deallocate_buffers_(); | ||||||
|  |  | ||||||
|   int read_microphone_(); |   int read_microphone_(); | ||||||
|   void set_state_(State state); |   void set_state_(State state); | ||||||
|   void set_state_(State state, State desired_state); |   void set_state_(State state, State desired_state); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user