Skip to content

Commit

Permalink
[nabu_microphone] handle case where only one channel is configured (#150
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kahrendt committed Sep 27, 2024
1 parent 4616882 commit d9ba928
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions esphome/components/nabu_microphone/nabu_microphone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,21 @@ void NabuMicrophone::read_task_(void *params) {
std::vector<int16_t, ExternalRAMAllocator<int16_t>> channel_0_samples;
std::vector<int16_t, ExternalRAMAllocator<int16_t>> 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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<int32_t>(channel_0_sample, INT16_MIN, INT16_MAX);
channel_0_samples[i] = (int16_t) clamp<int32_t>(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<int32_t>(channel_1_sample, INT16_MIN, INT16_MAX);
channel_1_samples[i] = (int16_t) clamp<int32_t>(channel_1_sample, INT16_MIN, INT16_MAX);
}
}

Expand Down

0 comments on commit d9ba928

Please sign in to comment.