diff --git a/esphome/components/nabu_microphone/nabu_microphone.cpp b/esphome/components/nabu_microphone/nabu_microphone.cpp index cf3c43a..372dc6c 100644 --- a/esphome/components/nabu_microphone/nabu_microphone.cpp +++ b/esphome/components/nabu_microphone/nabu_microphone.cpp @@ -224,14 +224,21 @@ void NabuMicrophone::read_task_(void *params) { std::vector> channel_0_samples; std::vector> channel_1_samples; - if (this_microphone->channel_0_ != nullptr) - channel_0_samples.reserve(FRAMES_IN_ALL_DMA_BUFFERS); + size_t channel_0_reserved_samples = 0; + size_t channel_1_reserved_samples = 0; - if (this_microphone->channel_1_ != nullptr) - channel_1_samples.reserve(FRAMES_IN_ALL_DMA_BUFFERS); + if (this_microphone->channel_0_ != nullptr) { + channel_0_reserved_samples = FRAMES_IN_ALL_DMA_BUFFERS; + channel_0_samples.reserve(channel_0_reserved_samples); + } + + if (this_microphone->channel_1_ != nullptr) { + channel_1_reserved_samples = FRAMES_IN_ALL_DMA_BUFFERS; + channel_1_samples.reserve(channel_1_reserved_samples); + } - if ((buffer == nullptr) || (channel_0_samples.capacity() < FRAMES_IN_ALL_DMA_BUFFERS) || - (channel_1_samples.capacity() < FRAMES_IN_ALL_DMA_BUFFERS)) { + if ((buffer == nullptr) || (channel_0_samples.capacity() < channel_0_reserved_samples) || + (channel_1_samples.capacity() < channel_1_reserved_samples)) { event.type = TaskEventType::WARNING; event.err = ESP_ERR_NO_MEM; xQueueSend(this_microphone->event_queue_, &event, portMAX_DELAY); @@ -243,8 +250,10 @@ void NabuMicrophone::read_task_(void *params) { xQueueSend(this_microphone->event_queue_, &event, portMAX_DELAY); } else { // TODO: Is this the ideal spot to reset the ring buffers? - this_microphone->channel_0_->get_ring_buffer()->reset(); - this_microphone->channel_1_->get_ring_buffer()->reset(); + if (this_microphone->channel_0_ != nullptr) + this_microphone->channel_0_->get_ring_buffer()->reset(); + if (this_microphone->channel_1_ != nullptr) + this_microphone->channel_1_->get_ring_buffer()->reset(); event.type = TaskEventType::STARTED; xQueueSend(this_microphone->event_queue_, &event, portMAX_DELAY); @@ -272,20 +281,26 @@ void NabuMicrophone::read_task_(void *params) { const size_t frames_read = samples_read / NUMBER_OF_CHANNELS; // Left and right channel samples combine into 1 frame - const uint8_t channel_0_shift = 16 - this_microphone->channel_0_->get_amplify_shift(); - const uint8_t channel_1_shift = 16 - this_microphone->channel_1_->get_amplify_shift(); + uint8_t channel_0_shift = 16; + if (this_microphone->channel_0_ != nullptr) { + channel_0_shift -= this_microphone->channel_0_->get_amplify_shift(); + } + uint8_t channel_1_shift = 16; + if (this_microphone->channel_1_ != nullptr) { + channel_1_shift -= this_microphone->channel_1_->get_amplify_shift(); + } for (size_t i = 0; i < frames_read; i++) { int32_t channel_0_sample = 0; if ((this_microphone->channel_0_ != nullptr) && (!this_microphone->channel_0_->get_mute_state())) { channel_0_sample = buffer[NUMBER_OF_CHANNELS * i] >> channel_0_shift; - channel_0_samples[i] = (int16_t)clamp(channel_0_sample, INT16_MIN, INT16_MAX); + channel_0_samples[i] = (int16_t) clamp(channel_0_sample, INT16_MIN, INT16_MAX); } int32_t channel_1_sample = 0; if ((this_microphone->channel_1_ != nullptr) && (!this_microphone->channel_1_->get_mute_state())) { channel_1_sample = buffer[NUMBER_OF_CHANNELS * i + 1] >> channel_1_shift; - channel_1_samples[i] = (int16_t)clamp(channel_1_sample, INT16_MIN, INT16_MAX); + channel_1_samples[i] = (int16_t) clamp(channel_1_sample, INT16_MIN, INT16_MAX); } }