mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Clean up RAMAllocators in audio related code (#9140)
This commit is contained in:
		| @@ -86,7 +86,7 @@ bool AudioTransferBuffer::reallocate(size_t new_buffer_size) { | |||||||
| bool AudioTransferBuffer::allocate_buffer_(size_t buffer_size) { | bool AudioTransferBuffer::allocate_buffer_(size_t buffer_size) { | ||||||
|   this->buffer_size_ = buffer_size; |   this->buffer_size_ = buffer_size; | ||||||
|  |  | ||||||
|   RAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); |   RAMAllocator<uint8_t> allocator; | ||||||
|  |  | ||||||
|   this->buffer_ = allocator.allocate(this->buffer_size_); |   this->buffer_ = allocator.allocate(this->buffer_size_); | ||||||
|   if (this->buffer_ == nullptr) { |   if (this->buffer_ == nullptr) { | ||||||
| @@ -101,7 +101,7 @@ bool AudioTransferBuffer::allocate_buffer_(size_t buffer_size) { | |||||||
|  |  | ||||||
| void AudioTransferBuffer::deallocate_buffer_() { | void AudioTransferBuffer::deallocate_buffer_() { | ||||||
|   if (this->buffer_ != nullptr) { |   if (this->buffer_ != nullptr) { | ||||||
|     RAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); |     RAMAllocator<uint8_t> allocator; | ||||||
|     allocator.deallocate(this->buffer_, this->buffer_size_); |     allocator.deallocate(this->buffer_, this->buffer_size_); | ||||||
|     this->buffer_ = nullptr; |     this->buffer_ = nullptr; | ||||||
|     this->data_start_ = nullptr; |     this->data_start_ = nullptr; | ||||||
|   | |||||||
| @@ -484,7 +484,7 @@ bool I2SAudioSpeaker::send_esp_err_to_event_group_(esp_err_t err) { | |||||||
| esp_err_t I2SAudioSpeaker::allocate_buffers_(size_t data_buffer_size, size_t ring_buffer_size) { | esp_err_t I2SAudioSpeaker::allocate_buffers_(size_t data_buffer_size, size_t ring_buffer_size) { | ||||||
|   if (this->data_buffer_ == nullptr) { |   if (this->data_buffer_ == nullptr) { | ||||||
|     // Allocate data buffer for temporarily storing audio from the ring buffer before writing to the I2S bus |     // Allocate data buffer for temporarily storing audio from the ring buffer before writing to the I2S bus | ||||||
|     ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); |     RAMAllocator<uint8_t> allocator; | ||||||
|     this->data_buffer_ = allocator.allocate(data_buffer_size); |     this->data_buffer_ = allocator.allocate(data_buffer_size); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -698,7 +698,7 @@ void I2SAudioSpeaker::delete_task_(size_t buffer_size) { | |||||||
|   this->audio_ring_buffer_.reset();  // Releases ownership of the shared_ptr |   this->audio_ring_buffer_.reset();  // Releases ownership of the shared_ptr | ||||||
|  |  | ||||||
|   if (this->data_buffer_ != nullptr) { |   if (this->data_buffer_ != nullptr) { | ||||||
|     ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); |     RAMAllocator<uint8_t> allocator; | ||||||
|     allocator.deallocate(this->data_buffer_, buffer_size); |     allocator.deallocate(this->data_buffer_, buffer_size); | ||||||
|     this->data_buffer_ = nullptr; |     this->data_buffer_ = nullptr; | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -27,7 +27,7 @@ void VADModel::log_model_config() { | |||||||
| } | } | ||||||
|  |  | ||||||
| bool StreamingModel::load_model_() { | bool StreamingModel::load_model_() { | ||||||
|   RAMAllocator<uint8_t> arena_allocator(RAMAllocator<uint8_t>::ALLOW_FAILURE); |   RAMAllocator<uint8_t> arena_allocator; | ||||||
|  |  | ||||||
|   if (this->tensor_arena_ == nullptr) { |   if (this->tensor_arena_ == nullptr) { | ||||||
|     this->tensor_arena_ = arena_allocator.allocate(this->tensor_arena_size_); |     this->tensor_arena_ = arena_allocator.allocate(this->tensor_arena_size_); | ||||||
| @@ -96,7 +96,7 @@ bool StreamingModel::load_model_() { | |||||||
| void StreamingModel::unload_model() { | void StreamingModel::unload_model() { | ||||||
|   this->interpreter_.reset(); |   this->interpreter_.reset(); | ||||||
|  |  | ||||||
|   RAMAllocator<uint8_t> arena_allocator(RAMAllocator<uint8_t>::ALLOW_FAILURE); |   RAMAllocator<uint8_t> arena_allocator; | ||||||
|  |  | ||||||
|   if (this->tensor_arena_ != nullptr) { |   if (this->tensor_arena_ != nullptr) { | ||||||
|     arena_allocator.deallocate(this->tensor_arena_, this->tensor_arena_size_); |     arena_allocator.deallocate(this->tensor_arena_, this->tensor_arena_size_); | ||||||
|   | |||||||
| @@ -85,7 +85,7 @@ bool VoiceAssistant::start_udp_socket_() { | |||||||
| bool VoiceAssistant::allocate_buffers_() { | bool VoiceAssistant::allocate_buffers_() { | ||||||
| #ifdef USE_SPEAKER | #ifdef USE_SPEAKER | ||||||
|   if ((this->speaker_ != nullptr) && (this->speaker_buffer_ == nullptr)) { |   if ((this->speaker_ != nullptr) && (this->speaker_buffer_ == nullptr)) { | ||||||
|     ExternalRAMAllocator<uint8_t> speaker_allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); |     RAMAllocator<uint8_t> speaker_allocator; | ||||||
|     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"); | ||||||
| @@ -103,7 +103,7 @@ bool VoiceAssistant::allocate_buffers_() { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   if (this->send_buffer_ == nullptr) { |   if (this->send_buffer_ == nullptr) { | ||||||
|     ExternalRAMAllocator<uint8_t> send_allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); |     RAMAllocator<uint8_t> send_allocator; | ||||||
|     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"); | ||||||
| @@ -136,7 +136,7 @@ void VoiceAssistant::clear_buffers_() { | |||||||
|  |  | ||||||
| void VoiceAssistant::deallocate_buffers_() { | void VoiceAssistant::deallocate_buffers_() { | ||||||
|   if (this->send_buffer_ != nullptr) { |   if (this->send_buffer_ != nullptr) { | ||||||
|     ExternalRAMAllocator<uint8_t> send_deallocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); |     RAMAllocator<uint8_t> send_deallocator; | ||||||
|     send_deallocator.deallocate(this->send_buffer_, SEND_BUFFER_SIZE); |     send_deallocator.deallocate(this->send_buffer_, SEND_BUFFER_SIZE); | ||||||
|     this->send_buffer_ = nullptr; |     this->send_buffer_ = nullptr; | ||||||
|   } |   } | ||||||
| @@ -147,7 +147,7 @@ void VoiceAssistant::deallocate_buffers_() { | |||||||
|  |  | ||||||
| #ifdef USE_SPEAKER | #ifdef USE_SPEAKER | ||||||
|   if ((this->speaker_ != nullptr) && (this->speaker_buffer_ != nullptr)) { |   if ((this->speaker_ != nullptr) && (this->speaker_buffer_ != nullptr)) { | ||||||
|     ExternalRAMAllocator<uint8_t> speaker_deallocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE); |     RAMAllocator<uint8_t> speaker_deallocator; | ||||||
|     speaker_deallocator.deallocate(this->speaker_buffer_, SPEAKER_BUFFER_SIZE); |     speaker_deallocator.deallocate(this->speaker_buffer_, SPEAKER_BUFFER_SIZE); | ||||||
|     this->speaker_buffer_ = nullptr; |     this->speaker_buffer_ = nullptr; | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -14,7 +14,7 @@ static const char *const TAG = "ring_buffer"; | |||||||
| RingBuffer::~RingBuffer() { | RingBuffer::~RingBuffer() { | ||||||
|   if (this->handle_ != nullptr) { |   if (this->handle_ != nullptr) { | ||||||
|     vRingbufferDelete(this->handle_); |     vRingbufferDelete(this->handle_); | ||||||
|     RAMAllocator<uint8_t> allocator(RAMAllocator<uint8_t>::ALLOW_FAILURE); |     RAMAllocator<uint8_t> allocator; | ||||||
|     allocator.deallocate(this->storage_, this->size_); |     allocator.deallocate(this->storage_, this->size_); | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @@ -24,7 +24,7 @@ std::unique_ptr<RingBuffer> RingBuffer::create(size_t len) { | |||||||
|  |  | ||||||
|   rb->size_ = len; |   rb->size_ = len; | ||||||
|  |  | ||||||
|   RAMAllocator<uint8_t> allocator(RAMAllocator<uint8_t>::ALLOW_FAILURE); |   RAMAllocator<uint8_t> allocator; | ||||||
|   rb->storage_ = allocator.allocate(rb->size_); |   rb->storage_ = allocator.allocate(rb->size_); | ||||||
|   if (rb->storage_ == nullptr) { |   if (rb->storage_ == nullptr) { | ||||||
|     return nullptr; |     return nullptr; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user