mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-03 16:41:50 +00:00 
			
		
		
		
	Compare commits
	
		
			12 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					5b062a222c | ||
| 
						 | 
					664ee56dc5 | ||
| 
						 | 
					388b2c2de0 | ||
| 
						 | 
					ce4a3d9950 | ||
| 
						 | 
					ac9f57600d | ||
| 
						 | 
					3fe2fc9b56 | ||
| 
						 | 
					4cd4b168b4 | ||
| 
						 | 
					f07479419c | ||
| 
						 | 
					54b51269ab | ||
| 
						 | 
					d72ab25d46 | ||
| 
						 | 
					af755380b7 | ||
| 
						 | 
					04db724295 | 
@@ -19,10 +19,27 @@ void I2SAudioSpeaker::setup() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Setting up I2S Audio Speaker...");
 | 
			
		||||
 | 
			
		||||
  this->buffer_queue_ = xQueueCreate(BUFFER_COUNT, sizeof(DataEvent));
 | 
			
		||||
  if (this->buffer_queue_ == nullptr) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed to create buffer queue");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->event_queue_ = xQueueCreate(BUFFER_COUNT, sizeof(TaskEvent));
 | 
			
		||||
  if (this->event_queue_ == nullptr) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed to create event queue");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void I2SAudioSpeaker::start() { this->state_ = speaker::STATE_STARTING; }
 | 
			
		||||
void I2SAudioSpeaker::start() {
 | 
			
		||||
  if (this->is_failed()) {
 | 
			
		||||
    ESP_LOGE(TAG, "Cannot start audio, speaker failed to setup");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  this->state_ = speaker::STATE_STARTING;
 | 
			
		||||
}
 | 
			
		||||
void I2SAudioSpeaker::start_() {
 | 
			
		||||
  if (!this->parent_->try_lock()) {
 | 
			
		||||
    return;  // Waiting for another i2s component to return lock
 | 
			
		||||
@@ -141,6 +158,8 @@ void I2SAudioSpeaker::player_task(void *params) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void I2SAudioSpeaker::stop() {
 | 
			
		||||
  if (this->is_failed())
 | 
			
		||||
    return;
 | 
			
		||||
  if (this->state_ == speaker::STATE_STOPPED)
 | 
			
		||||
    return;
 | 
			
		||||
  if (this->state_ == speaker::STATE_STARTING) {
 | 
			
		||||
@@ -200,6 +219,10 @@ void I2SAudioSpeaker::loop() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
size_t I2SAudioSpeaker::play(const uint8_t *data, size_t length) {
 | 
			
		||||
  if (this->is_failed()) {
 | 
			
		||||
    ESP_LOGE(TAG, "Cannot play audio, speaker failed to setup");
 | 
			
		||||
    return 0;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->state_ != speaker::STATE_RUNNING && this->state_ != speaker::STATE_STARTING) {
 | 
			
		||||
    this->start();
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -109,6 +109,7 @@ void ImprovSerialComponent::write_data_(std::vector<uint8_t> &data) {
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
    case logger::UART_SELECTION_USB_SERIAL_JTAG:
 | 
			
		||||
      usb_serial_jtag_write_bytes((char *) data.data(), data.size(), 20 / portTICK_PERIOD_MS);
 | 
			
		||||
      usb_serial_jtag_ll_txfifo_flush();  // fixes for issue in IDF 4.4.7
 | 
			
		||||
      break;
 | 
			
		||||
#endif  // USE_ESP32_VARIANT_ESP32C3 || USE_ESP32_VARIANT_ESP32S3
 | 
			
		||||
    default:
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,7 @@
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32C3) || defined(USE_ESP32_VARIANT_ESP32C6) || defined(USE_ESP32_VARIANT_ESP32S3) || \
 | 
			
		||||
    defined(USE_ESP32_VARIANT_ESP32H2)
 | 
			
		||||
#include <driver/usb_serial_jtag.h>
 | 
			
		||||
#include <hal/usb_serial_jtag_ll.h>
 | 
			
		||||
#endif
 | 
			
		||||
#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
#include <esp_private/usb_console.h>
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@ static const char *const TAG = "voice_assistant";
 | 
			
		||||
 | 
			
		||||
static const size_t SAMPLE_RATE_HZ = 16000;
 | 
			
		||||
static const size_t INPUT_BUFFER_SIZE = 32 * SAMPLE_RATE_HZ / 1000;  // 32ms * 16kHz / 1000ms
 | 
			
		||||
static const size_t BUFFER_SIZE = 1024 * SAMPLE_RATE_HZ / 1000;
 | 
			
		||||
static const size_t BUFFER_SIZE = 512 * SAMPLE_RATE_HZ / 1000;
 | 
			
		||||
static const size_t SEND_BUFFER_SIZE = INPUT_BUFFER_SIZE * sizeof(int16_t);
 | 
			
		||||
static const size_t RECEIVE_SIZE = 1024;
 | 
			
		||||
static const size_t SPEAKER_BUFFER_SIZE = 16 * RECEIVE_SIZE;
 | 
			
		||||
@@ -71,6 +71,12 @@ void VoiceAssistant::setup() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Setting up Voice Assistant...");
 | 
			
		||||
 | 
			
		||||
  global_voice_assistant = this;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool VoiceAssistant::allocate_buffers_() {
 | 
			
		||||
  if (this->send_buffer_ != nullptr) {
 | 
			
		||||
    return true;  // Already allocated
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_SPEAKER
 | 
			
		||||
  if (this->speaker_ != nullptr) {
 | 
			
		||||
@@ -78,8 +84,7 @@ void VoiceAssistant::setup() {
 | 
			
		||||
    this->speaker_buffer_ = speaker_allocator.allocate(SPEAKER_BUFFER_SIZE);
 | 
			
		||||
    if (this->speaker_buffer_ == nullptr) {
 | 
			
		||||
      ESP_LOGW(TAG, "Could not allocate speaker buffer");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
      return;
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
@@ -88,8 +93,7 @@ void VoiceAssistant::setup() {
 | 
			
		||||
  this->input_buffer_ = allocator.allocate(INPUT_BUFFER_SIZE);
 | 
			
		||||
  if (this->input_buffer_ == nullptr) {
 | 
			
		||||
    ESP_LOGW(TAG, "Could not allocate input buffer");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP_ADF
 | 
			
		||||
@@ -99,17 +103,71 @@ void VoiceAssistant::setup() {
 | 
			
		||||
  this->ring_buffer_ = RingBuffer::create(BUFFER_SIZE * sizeof(int16_t));
 | 
			
		||||
  if (this->ring_buffer_ == nullptr) {
 | 
			
		||||
    ESP_LOGW(TAG, "Could not allocate ring buffer");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ExternalRAMAllocator<uint8_t> send_allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
 | 
			
		||||
  this->send_buffer_ = send_allocator.allocate(SEND_BUFFER_SIZE);
 | 
			
		||||
  if (send_buffer_ == nullptr) {
 | 
			
		||||
    ESP_LOGW(TAG, "Could not allocate send buffer");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  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_() {
 | 
			
		||||
@@ -138,14 +196,13 @@ void VoiceAssistant::loop() {
 | 
			
		||||
    }
 | 
			
		||||
    this->continuous_ = false;
 | 
			
		||||
    this->signal_stop_();
 | 
			
		||||
    this->clear_buffers_();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  switch (this->state_) {
 | 
			
		||||
    case State::IDLE: {
 | 
			
		||||
      if (this->continuous_ && this->desired_state_ == State::IDLE) {
 | 
			
		||||
        this->idle_trigger_->trigger();
 | 
			
		||||
 | 
			
		||||
        this->ring_buffer_->reset();
 | 
			
		||||
#ifdef USE_ESP_ADF
 | 
			
		||||
        if (this->use_wake_word_) {
 | 
			
		||||
          this->set_state_(State::START_MICROPHONE, State::WAIT_FOR_VAD);
 | 
			
		||||
@@ -161,8 +218,15 @@ void VoiceAssistant::loop() {
 | 
			
		||||
    }
 | 
			
		||||
    case State::START_MICROPHONE: {
 | 
			
		||||
      ESP_LOGD(TAG, "Starting Microphone");
 | 
			
		||||
      memset(this->send_buffer_, 0, SEND_BUFFER_SIZE);
 | 
			
		||||
      memset(this->input_buffer_, 0, INPUT_BUFFER_SIZE * sizeof(int16_t));
 | 
			
		||||
      if (!this->allocate_buffers_()) {
 | 
			
		||||
        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->high_freq_.start();
 | 
			
		||||
      this->set_state_(State::STARTING_MICROPHONE);
 | 
			
		||||
@@ -343,10 +407,9 @@ void VoiceAssistant::loop() {
 | 
			
		||||
        this->speaker_->stop();
 | 
			
		||||
        this->cancel_timeout("speaker-timeout");
 | 
			
		||||
        this->cancel_timeout("playing");
 | 
			
		||||
        this->speaker_buffer_size_ = 0;
 | 
			
		||||
        this->speaker_buffer_index_ = 0;
 | 
			
		||||
        this->speaker_bytes_received_ = 0;
 | 
			
		||||
        memset(this->speaker_buffer_, 0, SPEAKER_BUFFER_SIZE);
 | 
			
		||||
 | 
			
		||||
        this->clear_buffers_();
 | 
			
		||||
 | 
			
		||||
        this->wait_for_stream_end_ = false;
 | 
			
		||||
        this->stream_ended_ = false;
 | 
			
		||||
 | 
			
		||||
@@ -507,7 +570,6 @@ void VoiceAssistant::request_start(bool continuous, bool silence_detection) {
 | 
			
		||||
  if (this->state_ == State::IDLE) {
 | 
			
		||||
    this->continuous_ = continuous;
 | 
			
		||||
    this->silence_detection_ = silence_detection;
 | 
			
		||||
    this->ring_buffer_->reset();
 | 
			
		||||
#ifdef USE_ESP_ADF
 | 
			
		||||
    if (this->use_wake_word_) {
 | 
			
		||||
      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; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  bool allocate_buffers_();
 | 
			
		||||
  void clear_buffers_();
 | 
			
		||||
  void deallocate_buffers_();
 | 
			
		||||
 | 
			
		||||
  int read_microphone_();
 | 
			
		||||
  void set_state_(State state);
 | 
			
		||||
  void set_state_(State state, State desired_state);
 | 
			
		||||
 
 | 
			
		||||
@@ -37,4 +37,4 @@ async def to_code(config):
 | 
			
		||||
            cg.add_library("FS", None)
 | 
			
		||||
            cg.add_library("Update", None)
 | 
			
		||||
        # https://github.com/esphome/ESPAsyncWebServer/blob/master/library.json
 | 
			
		||||
        cg.add_library("esphome/ESPAsyncWebServer-esphome", "3.2.0")
 | 
			
		||||
        cg.add_library("esphome/ESPAsyncWebServer-esphome", "3.2.2")
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
"""Constants used by esphome."""
 | 
			
		||||
 | 
			
		||||
__version__ = "2024.5.2"
 | 
			
		||||
__version__ = "2024.5.5"
 | 
			
		||||
 | 
			
		||||
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
 | 
			
		||||
VALID_SUBSTITUTIONS_CHARACTERS = (
 | 
			
		||||
 
 | 
			
		||||
@@ -433,6 +433,10 @@ int8_t step_to_accuracy_decimals(float step) {
 | 
			
		||||
  return str.length() - dot_pos - 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const std::string BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 | 
			
		||||
                                        "abcdefghijklmnopqrstuvwxyz"
 | 
			
		||||
                                        "0123456789+/";
 | 
			
		||||
 | 
			
		||||
static inline bool is_base64(char c) { return (isalnum(c) || (c == '+') || (c == '/')); }
 | 
			
		||||
 | 
			
		||||
std::string base64_encode(const std::vector<uint8_t> &buf) { return base64_encode(buf.data(), buf.size()); }
 | 
			
		||||
 
 | 
			
		||||
@@ -435,10 +435,6 @@ std::string value_accuracy_to_string(float value, int8_t accuracy_decimals);
 | 
			
		||||
/// Derive accuracy in decimals from an increment step.
 | 
			
		||||
int8_t step_to_accuracy_decimals(float step);
 | 
			
		||||
 | 
			
		||||
static const std::string BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 | 
			
		||||
                                        "abcdefghijklmnopqrstuvwxyz"
 | 
			
		||||
                                        "0123456789+/";
 | 
			
		||||
 | 
			
		||||
std::string base64_encode(const uint8_t *buf, size_t buf_len);
 | 
			
		||||
std::string base64_encode(const std::vector<uint8_t> &buf);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -58,7 +58,7 @@ lib_deps =
 | 
			
		||||
    SPI                                                   ; spi (Arduino built-in)
 | 
			
		||||
    Wire                                                  ; i2c (Arduino built-int)
 | 
			
		||||
    heman/AsyncMqttClient-esphome@1.0.0                   ; mqtt
 | 
			
		||||
    esphome/ESPAsyncWebServer-esphome@3.2.0               ; web_server_base
 | 
			
		||||
    esphome/ESPAsyncWebServer-esphome@3.2.2               ; web_server_base
 | 
			
		||||
    fastled/FastLED@3.3.2                                 ; fastled_base
 | 
			
		||||
    mikalhart/TinyGPSPlus@1.0.2                           ; gps
 | 
			
		||||
    freekode/TM1651@1.0.1                                 ; tm1651
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user