Skip to content

Commit

Permalink
more consistent base microphone class
Browse files Browse the repository at this point in the history
  • Loading branch information
kahrendt committed Sep 5, 2024
1 parent 23abaec commit 181da7b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions esphome/components/microphone/microphone.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#pragma once

#include "esphome/core/entity_base.h"
#include <cstddef>
#include <cstdint>
#include <freertos/FreeRTOS.h>
#include <functional>
#include <vector>
#include "esphome/core/helpers.h"

namespace esphome {
namespace microphone {

// TODO: The mute state should belong to the microphone, not the parent nabu_microphone

enum State : uint8_t {
STATE_STOPPED = 0,
STATE_STARTING,
Expand All @@ -23,12 +25,12 @@ class Microphone {
void add_data_callback(std::function<void(const std::vector<int16_t> &)> &&data_callback) {
this->data_callbacks_.add(std::move(data_callback));
}
virtual size_t read(int16_t *buf, size_t len, TickType_t ticks_to_wait = 0) = 0;
virtual size_t read(int16_t *buf, size_t len) = 0;

// How many bytes are available in the ring buffer
virtual size_t available() { return 0; }
/// @brief Reads from the microphone blocking ticks_to_wait FreeRTOS ticks. Intended for use in tasks.
virtual size_t read(int16_t *buf, size_t len, TickType_t ticks_to_wait) { return this->read(buf, len); }

// Reset the ring buffer
/// @brief If the microphone implementation uses a ring buffer, this will reset it - discarding all the stored data
virtual void reset() {}

virtual void set_mute_state(bool mute_state) {};
Expand All @@ -44,4 +46,4 @@ class Microphone {
};

} // namespace microphone
} // namespace esphome
} // namespace esphome
2 changes: 1 addition & 1 deletion esphome/components/nabu_microphone/nabu_microphone.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class NabuMicrophoneChannel : public microphone::Microphone, public Component {
size_t read(int16_t *buf, size_t len, TickType_t ticks_to_wait = 0) override {
return this->ring_buffer_->read((void *) buf, len, ticks_to_wait);
};
size_t available() override { return this->ring_buffer_->available(); }
size_t read(int16_t *buf, size_t len) override { return this->ring_buffer_->read((void *) buf, len); };
void reset() override { this->ring_buffer_->reset(); }

RingBuffer *get_ring_buffer() { return this->ring_buffer_.get(); }
Expand Down

0 comments on commit 181da7b

Please sign in to comment.