Cleaning up code

This commit is contained in:
X-Ryl669 2024-04-05 14:25:39 +02:00
parent 3d5c6224e3
commit 1e14dbf2ce
4 changed files with 118 additions and 233 deletions

View file

@ -5,7 +5,7 @@ import esphome.codegen as cg
import esphome.final_validate as fv import esphome.final_validate as fv
from esphome.components import esp32 from esphome.components import esp32
from esphome.components.i2c import I2CDevice, i2c_ns, I2CBus from esphome.components.i2c import i2c_ns, I2CBus
from esphome.const import CONF_ID, CONF_BOARD from esphome.const import CONF_ID, CONF_BOARD
@ -91,9 +91,8 @@ async def to_code(config):
cg.add_define("USE_ESP_ADF_BOARD") cg.add_define("USE_ESP_ADF_BOARD")
esp32.add_idf_sdkconfig_option(SUPPORTED_BOARDS[board], True) esp32.add_idf_sdkconfig_option(SUPPORTED_BOARDS[board], True)
#esp32.add_idf_sdkconfig_option("CONFIG_SPIRAM_BOOT_INIT", True) # esp32.add_idf_sdkconfig_option("CONFIG_SPIRAM_BOOT_INIT", True)
#esp32.add_idf_sdkconfig_option("CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY", True) # esp32.add_idf_sdkconfig_option("CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY", True)
esp32.add_extra_script( esp32.add_extra_script(
"pre", "pre",
@ -105,16 +104,22 @@ async def to_code(config):
"https://github.com/X-Ryl669/esp-adf/raw/with-i2c-cb/idf_patches/idf_v4.4_freertos.patch", "https://github.com/X-Ryl669/esp-adf/raw/with-i2c-cb/idf_patches/idf_v4.4_freertos.patch",
) )
# I2C Bus below # I2C Bus below
ADFI2CBus = i2c_ns.class_("ADFI2CBus", I2CBus, cg.Component) ADFI2CBus = i2c_ns.class_("ADFI2CBus", I2CBus, cg.Component)
# Patch the I2C config schema to use the right I2C bus # Patch the I2C config schema to use the right I2C bus
def _patch_idfi2cbus(config): def _patch_idfi2cbus(config):
from esphome.cpp_generator import MockObjClass from esphome.cpp_generator import MockObjClass
if "i2c" in fv.full_config.get(): if "i2c" in fv.full_config.get():
for i2c_inst in fv.full_config.get()["i2c"]: for i2c_inst in fv.full_config.get()["i2c"]:
i2c_inst["id"].type = MockObjClass("i2c::ADFI2CBus", parents = i2c_inst["id"].type._parents) i2c_inst["id"].type = MockObjClass(
"i2c::ADFI2CBus", parents=i2c_inst["id"].type._parents
)
return config return config
FINAL_VALIDATE_SCHEMA = _patch_idfi2cbus FINAL_VALIDATE_SCHEMA = _patch_idfi2cbus

View file

@ -8,16 +8,12 @@
#include <cstring> #include <cstring>
#include <cinttypes> #include <cinttypes>
namespace esphome { namespace esphome {
namespace i2c { namespace i2c {
static const char *const TAG = "i2c.adf"; static const char *const TAG = "i2c.adf";
static void recover_i2c_hard(i2c_port_t, void* bus) { static void recover_i2c_hard(i2c_port_t, void *bus) { ((ADFI2CBus *) bus)->recover_(); }
((ADFI2CBus*)bus)->recover_();
}
void ADFI2CBus::setup() { void ADFI2CBus::setup() {
ESP_LOGCONFIG(TAG, "Setting up I2C bus..."); ESP_LOGCONFIG(TAG, "Setting up I2C bus...");
@ -30,12 +26,14 @@ void ADFI2CBus::setup() {
next_port = I2C_NUM_MAX; next_port = I2C_NUM_MAX;
#endif #endif
if (port == I2C_NUM_MAX) { if (port == I2C_NUM_MAX) {
ESP_LOGE(TAG, "Too many I2C buses configured"); this->mark_failed(); return; ESP_LOGE(TAG, "Too many I2C buses configured");
this->mark_failed();
return;
} }
// Recovery is disabled by default in adf since any esp-adf component that needed the I2C bus // Recovery is disabled by default in adf since any esp-adf component that needed the I2C bus
// might have run its setup and create the bus already. In that case, it would break the bus // might have run its setup and create the bus already. In that case, it would break the bus
// this->recover_(); // this->recover_();
this->recovery_result_ = RECOVERY_COMPLETED; this->recovery_result_ = RECOVERY_COMPLETED;
i2c_config_t conf{}; i2c_config_t conf{};
@ -52,12 +50,12 @@ void ADFI2CBus::setup() {
this->mark_failed(); this->mark_failed();
return; return;
} }
/* if (i2c_bus_run_cb(this->handle_, &recover_i2c_hard, this) != ESP_OK) { /* if (i2c_bus_run_cb(this->handle_, &recover_i2c_hard, this) != ESP_OK) {
ESP_LOGW(TAG, "i2c_bus_recover failed"); ESP_LOGW(TAG, "i2c_bus_recover failed");
this->mark_failed(); this->mark_failed();
return; return;
} }
*/ */
if (this->scan_) { if (this->scan_) {
ESP_LOGV(TAG, "Scanning i2c bus for active devices..."); ESP_LOGV(TAG, "Scanning i2c bus for active devices...");
this->i2c_scan_(); this->i2c_scan_();
@ -95,15 +93,13 @@ void ADFI2CBus::dump_config() {
} }
} }
struct ReadVCmd {
struct ReadVCmd
{
uint8_t address; uint8_t address;
ReadBuffer * buffers; ReadBuffer *buffers;
size_t cnt; size_t cnt;
ErrorCode code { ERROR_UNKNOWN }; ErrorCode code{ERROR_UNKNOWN};
ReadVCmd(uint8_t address, ReadBuffer * buffers, size_t cnt) : address(address), buffers(buffers), cnt(cnt) {} ReadVCmd(uint8_t address, ReadBuffer *buffers, size_t cnt) : address(address), buffers(buffers), cnt(cnt) {}
ErrorCode read(i2c_port_t port) { ErrorCode read(i2c_port_t port) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_cmd_handle_t cmd = i2c_cmd_link_create();
@ -167,11 +163,10 @@ struct ReadVCmd
return ERROR_OK; return ERROR_OK;
} }
}; };
// Calling stub for I2C port // Calling stub for I2C port
static void i2c_readv(i2c_port_t port, void* arg) { static void i2c_readv(i2c_port_t port, void *arg) {
ReadVCmd * args = (ReadVCmd*)arg; ReadVCmd *args = (ReadVCmd *) arg;
args->code = args->read(port); args->code = args->read(port);
} }
@ -182,7 +177,7 @@ ErrorCode ADFI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
ESP_LOGVV(TAG, "i2c bus not initialized!"); ESP_LOGVV(TAG, "i2c bus not initialized!");
return ERROR_NOT_INITIALIZED; return ERROR_NOT_INITIALIZED;
} }
ReadVCmd cmd { address, buffers, cnt }; ReadVCmd cmd{address, buffers, cnt};
if (i2c_bus_run_cb(this->handle_, &i2c_readv, &cmd) != ESP_OK || cmd.code != ERROR_OK) { if (i2c_bus_run_cb(this->handle_, &i2c_readv, &cmd) != ESP_OK || cmd.code != ERROR_OK) {
ESP_LOGVV(TAG, "i2c readv failed!"); ESP_LOGVV(TAG, "i2c readv failed!");
return cmd.code; return cmd.code;
@ -190,16 +185,15 @@ ErrorCode ADFI2CBus::readv(uint8_t address, ReadBuffer *buffers, size_t cnt) {
return ERROR_OK; return ERROR_OK;
} }
struct WriteVCmd struct WriteVCmd {
{
uint8_t address; uint8_t address;
WriteBuffer * buffers; WriteBuffer *buffers;
size_t cnt; size_t cnt;
bool stop; bool stop;
ErrorCode code { ERROR_UNKNOWN }; ErrorCode code{ERROR_UNKNOWN};
WriteVCmd(uint8_t address, WriteBuffer * buffers, size_t cnt, bool stop) : address(address), buffers(buffers), cnt(cnt), stop(stop) {}
WriteVCmd(uint8_t address, WriteBuffer *buffers, size_t cnt, bool stop)
: address(address), buffers(buffers), cnt(cnt), stop(stop) {}
ErrorCode write(i2c_port_t port) { ErrorCode write(i2c_port_t port) {
#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE #ifdef ESPHOME_LOG_HAS_VERY_VERBOSE
@ -266,8 +260,8 @@ struct WriteVCmd
}; };
// Calling stub for I2C port // Calling stub for I2C port
static void i2c_writev(i2c_port_t port, void* arg) { static void i2c_writev(i2c_port_t port, void *arg) {
WriteVCmd * args = (WriteVCmd*)arg; WriteVCmd *args = (WriteVCmd *) arg;
args->code = args->write(port); args->code = args->write(port);
} }
@ -279,7 +273,7 @@ ErrorCode ADFI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cnt, b
return ERROR_NOT_INITIALIZED; return ERROR_NOT_INITIALIZED;
} }
WriteVCmd cmd { address, buffers, cnt, stop }; WriteVCmd cmd{address, buffers, cnt, stop};
if (i2c_bus_run_cb(this->handle_, &i2c_writev, &cmd) != ESP_OK || cmd.code != ERROR_OK) { if (i2c_bus_run_cb(this->handle_, &i2c_writev, &cmd) != ESP_OK || cmd.code != ERROR_OK) {
ESP_LOGVV(TAG, "i2c writev failed!"); ESP_LOGVV(TAG, "i2c writev failed!");
return cmd.code; return cmd.code;
@ -287,14 +281,12 @@ ErrorCode ADFI2CBus::writev(uint8_t address, WriteBuffer *buffers, size_t cnt, b
return ERROR_OK; return ERROR_OK;
} }
/// Perform I2C bus recovery, see: /// Perform I2C bus recovery, see:
/// https://www.nxp.com/docs/en/user-guide/UM10204.pdf /// https://www.nxp.com/docs/en/user-guide/UM10204.pdf
/// https://www.analog.com/media/en/technical-documentation/application-notes/54305147357414AN686_0.pdf /// https://www.analog.com/media/en/technical-documentation/application-notes/54305147357414AN686_0.pdf
void ADFI2CBus::recover_() { void ADFI2CBus::recover_() {
ESP_LOGI(TAG, "Performing I2C bus recovery"); ESP_LOGI(TAG, "Performing I2C bus recovery");
const gpio_num_t scl_pin = static_cast<gpio_num_t>(scl_pin_); const gpio_num_t scl_pin = static_cast<gpio_num_t>(scl_pin_);
const gpio_num_t sda_pin = static_cast<gpio_num_t>(sda_pin_); const gpio_num_t sda_pin = static_cast<gpio_num_t>(sda_pin_);
@ -402,7 +394,6 @@ void ADFI2CBus::recover_() {
recovery_result_ = RECOVERY_COMPLETED; recovery_result_ = RECOVERY_COMPLETED;
} }
} // namespace i2c } // namespace i2c
} // namespace esphome } // namespace esphome

View file

@ -75,50 +75,18 @@ void ESPADFMicrophone::read_task(void *params) {
event.type = TaskEventType::STARTING; event.type = TaskEventType::STARTING;
xQueueSend(this_mic->read_event_queue_, &event, portMAX_DELAY); xQueueSend(this_mic->read_event_queue_, &event, portMAX_DELAY);
/* /*
audio_pipeline_cfg_t pipeline_cfg = { audio_pipeline_cfg_t pipeline_cfg = {
.rb_size = 8 * 1024, .rb_size = 8 * 1024,
}; };
*/ */
audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG(); audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
audio_pipeline_handle_t pipeline = audio_pipeline_init(&pipeline_cfg); audio_pipeline_handle_t pipeline = audio_pipeline_init(&pipeline_cfg);
/*
i2s_driver_config_t i2s_config = {};
i2s_config.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX);
i2s_config.sample_rate = 16000;
i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
i2s_config.communication_format = I2S_COMM_FORMAT_STAND_I2S;
i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2 | ESP_INTR_FLAG_IRAM;
i2s_config.dma_buf_count = 8;
i2s_config.dma_buf_len = 128;
i2s_config.use_apll = false;
i2s_config.tx_desc_auto_clear = true;
i2s_config.fixed_mclk = 0;
i2s_config.mclk_multiple = I2S_MCLK_MULTIPLE_256;
i2s_config.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT;
i2s_stream_cfg_t i2s_cfg = {};
i2s_cfg.type = AUDIO_STREAM_READER;
i2s_cfg.i2s_config = i2s_config;
i2s_cfg.i2s_port = static_cast<i2s_port_t>(CODEC_ADC_I2S_PORT);
i2s_cfg.use_alc = false;
i2s_cfg.volume = 0;
i2s_cfg.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE;
i2s_cfg.task_stack = I2S_STREAM_TASK_STACK;
i2s_cfg.task_core = I2S_STREAM_TASK_CORE;
i2s_cfg.task_prio = I2S_STREAM_TASK_PRIO;
i2s_cfg.stack_in_ext = false;
i2s_cfg.multi_out_num = 0;
i2s_cfg.uninstall_drv = true;
i2s_cfg.need_expand = false;
i2s_cfg.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT;
*/
//i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT_WITH_PARA(CODEC_ADC_I2S_PORT, 48000, 16, AUDIO_STREAM_READER);
i2s_stream_cfg_t i2s_cfg = {}; i2s_stream_cfg_t i2s_cfg = {};
memset(&i2s_cfg, 0, sizeof(i2s_cfg)); memset(&i2s_cfg, 0, sizeof(i2s_cfg));
i2s_cfg.type = AUDIO_STREAM_READER; i2s_cfg.type = AUDIO_STREAM_READER;
i2s_cfg.i2s_port = (i2s_port_t)CODEC_ADC_I2S_PORT; i2s_cfg.i2s_port = (i2s_port_t) CODEC_ADC_I2S_PORT;
i2s_cfg.use_alc = false; i2s_cfg.use_alc = false;
i2s_cfg.volume = 0; i2s_cfg.volume = 0;
i2s_cfg.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE; i2s_cfg.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE;
@ -131,7 +99,7 @@ void ESPADFMicrophone::read_task(void *params) {
i2s_cfg.need_expand = false; i2s_cfg.need_expand = false;
i2s_cfg.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT; i2s_cfg.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT;
i2s_cfg.buffer_len = I2S_STREAM_BUF_SIZE; i2s_cfg.buffer_len = I2S_STREAM_BUF_SIZE;
i2s_cfg.i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX); i2s_cfg.i2s_config.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX);
i2s_cfg.i2s_config.sample_rate = 16000; i2s_cfg.i2s_config.sample_rate = 16000;
i2s_cfg.i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT; i2s_cfg.i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
i2s_cfg.i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT; i2s_cfg.i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
@ -143,30 +111,9 @@ void ESPADFMicrophone::read_task(void *params) {
i2s_cfg.i2s_config.tx_desc_auto_clear = true; i2s_cfg.i2s_config.tx_desc_auto_clear = true;
i2s_cfg.i2s_config.fixed_mclk = 0; i2s_cfg.i2s_config.fixed_mclk = 0;
audio_element_handle_t i2s_stream_reader = i2s_stream_init(&i2s_cfg); audio_element_handle_t i2s_stream_reader = i2s_stream_init(&i2s_cfg);
/*
rsp_filter_cfg_t rsp_cfg = {
.src_rate = 16000,
.src_ch = 2,
.dest_rate = 16000,
.dest_bits = 16,
.dest_ch = 1,
.src_bits = I2S_BITS_PER_SAMPLE_16BIT,
.mode = RESAMPLE_DECODE_MODE,
.max_indata_bytes = RSP_FILTER_BUFFER_BYTE,
.out_len_bytes = RSP_FILTER_BUFFER_BYTE,
.type = ESP_RESAMPLE_TYPE_AUTO,
.complexity = 2,
.down_ch_idx = 0,
.prefer_flag = ESP_RSP_PREFER_TYPE_SPEED,
.out_rb_size = RSP_FILTER_RINGBUFFER_SIZE,
.task_stack = RSP_FILTER_TASK_STACK,
.task_core = RSP_FILTER_TASK_CORE,
.task_prio = RSP_FILTER_TASK_PRIO,
.stack_in_ext = true,
};
*/
rsp_filter_cfg_t rsp_cfg = {}; rsp_filter_cfg_t rsp_cfg = {};
memset(&rsp_cfg, 0, sizeof(rsp_cfg)); //DEFAULT_RESAMPLE_FILTER_CONFIG(); memset(&rsp_cfg, 0, sizeof(rsp_cfg)); // DEFAULT_RESAMPLE_FILTER_CONFIG();
rsp_cfg.src_rate = 16000; rsp_cfg.src_rate = 16000;
rsp_cfg.src_ch = 2; rsp_cfg.src_ch = 2;
rsp_cfg.src_bits = 16; rsp_cfg.src_bits = 16;
@ -187,30 +134,31 @@ void ESPADFMicrophone::read_task(void *params) {
rsp_cfg.stack_in_ext = true; rsp_cfg.stack_in_ext = true;
audio_element_handle_t filter = rsp_filter_init(&rsp_cfg); audio_element_handle_t filter = rsp_filter_init(&rsp_cfg);
algorithm_stream_cfg_t algo_cfg = { /*
.input_type = ALGORITHM_STREAM_INPUT_TYPE1, algorithm_stream_cfg_t algo_cfg = {
.task_stack = 10 * 1024, .input_type = ALGORITHM_STREAM_INPUT_TYPE1,
.task_prio = ALGORITHM_STREAM_TASK_PERIOD, .task_stack = 10 * 1024,
.task_core = ALGORITHM_STREAM_PINNED_TO_CORE, .task_prio = ALGORITHM_STREAM_TASK_PERIOD,
.out_rb_size = ALGORITHM_STREAM_RINGBUFFER_SIZE, .task_core = ALGORITHM_STREAM_PINNED_TO_CORE,
.stack_in_ext = true, .out_rb_size = ALGORITHM_STREAM_RINGBUFFER_SIZE,
.rec_linear_factor = 1, .stack_in_ext = true,
.ref_linear_factor = 1, .rec_linear_factor = 1,
.debug_input = false, .ref_linear_factor = 1,
.swap_ch = false, .debug_input = false,
// .algo_mask = ALGORITHM_STREAM_USE_AGC, .swap_ch = false,
// .algo_mask = (ALGORITHM_STREAM_USE_AEC | ALGORITHM_STREAM_USE_AGC | ALGORITHM_STREAM_USE_NS), // .algo_mask = ALGORITHM_STREAM_USE_AGC,
// .algo_mask = (ALGORITHM_STREAM_USE_AGC | ALGORITHM_STREAM_USE_NS), // .algo_mask = (ALGORITHM_STREAM_USE_AEC | ALGORITHM_STREAM_USE_AGC | ALGORITHM_STREAM_USE_NS),
.algo_mask = (ALGORITHM_STREAM_USE_AEC | ALGORITHM_STREAM_USE_NS), // .algo_mask = (ALGORITHM_STREAM_USE_AGC | ALGORITHM_STREAM_USE_NS),
// .algo_mask = (ALGORITHM_STREAM_USE_NS), .algo_mask = (ALGORITHM_STREAM_USE_AEC | ALGORITHM_STREAM_USE_NS),
.sample_rate = 16000, // .algo_mask = (ALGORITHM_STREAM_USE_NS),
.mic_ch = 1, .sample_rate = 16000,
.agc_gain = 10, .mic_ch = 1,
.aec_low_cost = false, .agc_gain = 10,
}; .aec_low_cost = false,
};
// audio_element_handle_t algo_stream = algo_stream_init(&algo_cfg);
audio_element_handle_t algo_stream = algo_stream_init(&algo_cfg);
*/
raw_stream_cfg_t raw_cfg = { raw_stream_cfg_t raw_cfg = {
.type = AUDIO_STREAM_READER, .type = AUDIO_STREAM_READER,
.out_rb_size = 8 * 1024, .out_rb_size = 8 * 1024,
@ -230,18 +178,15 @@ void ESPADFMicrophone::read_task(void *params) {
}; };
audio_pipeline_link(pipeline, &link_tag[0], 3); audio_pipeline_link(pipeline, &link_tag[0], 3);
/* // Set the event listener for the pipeline, else it'll fill itself and crash /*
audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG(); // Set the event listener for the pipeline, else it'll fill itself and crash
audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg); audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
audio_pipeline_set_listener(pipeline, evt); audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG(); audio_pipeline_set_listener(pipeline, evt);
esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg); esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
// Initialize SD Card peripheral ESP_LOGW(TAG, "audio pipeline listener installed");
// audio_board_sdcard_init(set, SD_MODE_1_LINE); */
audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt);
ESP_LOGW(TAG, "audio pipeline listener installed");
*/
audio_pipeline_run(pipeline); audio_pipeline_run(pipeline);
@ -280,21 +225,21 @@ void ESPADFMicrophone::read_task(void *params) {
} }
rb_write(this_mic->ring_buffer_, (char *) buffer, bytes_read, 0); rb_write(this_mic->ring_buffer_, (char *) buffer, bytes_read, 0);
/* // Pipeline listener for events too /* // Pipeline listener for events too
audio_event_iface_msg_t msg; audio_event_iface_msg_t msg;
esp_err_t ret = audio_event_iface_listen(evt, &msg, 0); esp_err_t ret = audio_event_iface_listen(evt, &msg, 0);
if (ret == ESP_OK) { if (ret == ESP_OK) {
ESP_LOGW(TAG, "msg srct:%d, src:%p, cmd:%d, data:%d", msg.source_type, msg.source, msg.cmd, (int)msg.data); ESP_LOGW(TAG, "msg srct:%d, src:%p, cmd:%d, data:%d", msg.source_type, msg.source, msg.cmd, (int)msg.data);
// Stop when the last pipeline element (raw_read in this case) receives stop event // Stop when the last pipeline element (raw_read in this case) receives stop event
if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) raw_read if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) raw_read
&& msg.cmd == AEL_MSG_CMD_REPORT_STATUS && msg.cmd == AEL_MSG_CMD_REPORT_STATUS
&& (((int)msg.data == AEL_STATUS_STATE_STOPPED) || ((int)msg.data == AEL_STATUS_STATE_FINISHED))) { && (((int)msg.data == AEL_STATUS_STATE_STOPPED) || ((int)msg.data == AEL_STATUS_STATE_FINISHED))) {
// Stop from the microphone (upon error?) // Stop from the microphone (upon error?)
break; break;
} }
} }
*/ */
} }
allocator.deallocate(buffer, BUFFER_SIZE / sizeof(int16_t)); allocator.deallocate(buffer, BUFFER_SIZE / sizeof(int16_t));

View file

@ -16,7 +16,7 @@
namespace esphome { namespace esphome {
namespace esp_adf { namespace esp_adf {
static const size_t BUFFER_COUNT = 20; static const size_t BUFFER_COUNT = 25;
static const char *const TAG = "esp_adf.speaker"; static const char *const TAG = "esp_adf.speaker";
@ -50,41 +50,6 @@ void ESPADFSpeaker::player_task(void *params) {
}; };
audio_pipeline_handle_t pipeline = audio_pipeline_init(&pipeline_cfg); audio_pipeline_handle_t pipeline = audio_pipeline_init(&pipeline_cfg);
/* i2s_driver_config_t i2s_config = {};
i2s_config.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_TX);
i2s_config.sample_rate = 16000;
i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT;
i2s_config.communication_format = I2S_COMM_FORMAT_STAND_I2S;
i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2 | ESP_INTR_FLAG_IRAM;
i2s_config.dma_buf_count = 8;
i2s_config.dma_buf_len = 1024;
i2s_config.use_apll = false;
i2s_config.tx_desc_auto_clear = true;
i2s_config.fixed_mclk = 0;
i2s_config.mclk_multiple = I2S_MCLK_MULTIPLE_256;
i2s_config.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT;
i2s_stream_cfg_t i2s_cfg = {};
i2s_cfg.type = AUDIO_STREAM_WRITER;
i2s_cfg.i2s_config = i2s_config;
i2s_cfg.i2s_port = I2S_NUM_0;
i2s_cfg.use_alc = false;
i2s_cfg.volume = 0;
i2s_cfg.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE;
i2s_cfg.task_stack = I2S_STREAM_TASK_STACK;
i2s_cfg.task_core = I2S_STREAM_TASK_CORE;
i2s_cfg.task_prio = I2S_STREAM_TASK_PRIO;
i2s_cfg.stack_in_ext = false;
i2s_cfg.multi_out_num = 0;
i2s_cfg.uninstall_drv = true;
i2s_cfg.need_expand = false;
i2s_cfg.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT;
audio_element_handle_t i2s_stream_writer = i2s_stream_init(&i2s_cfg);
*/
i2s_stream_cfg_t i2s_cfg = {}; i2s_stream_cfg_t i2s_cfg = {};
memset(&i2s_cfg, 0, sizeof(i2s_cfg)); memset(&i2s_cfg, 0, sizeof(i2s_cfg));
i2s_cfg.type = AUDIO_STREAM_WRITER; i2s_cfg.type = AUDIO_STREAM_WRITER;
@ -101,7 +66,7 @@ void ESPADFSpeaker::player_task(void *params) {
i2s_cfg.need_expand = false; i2s_cfg.need_expand = false;
i2s_cfg.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT; i2s_cfg.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT;
i2s_cfg.buffer_len = I2S_STREAM_BUF_SIZE; i2s_cfg.buffer_len = I2S_STREAM_BUF_SIZE;
i2s_cfg.i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX); i2s_cfg.i2s_config.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_TX);
i2s_cfg.i2s_config.sample_rate = 16000; i2s_cfg.i2s_config.sample_rate = 16000;
i2s_cfg.i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT; i2s_cfg.i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
i2s_cfg.i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT; i2s_cfg.i2s_config.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT;
@ -115,49 +80,28 @@ void ESPADFSpeaker::player_task(void *params) {
i2s_cfg.i2s_config.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT; i2s_cfg.i2s_config.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT;
audio_element_handle_t i2s_stream_writer = i2s_stream_init(&i2s_cfg); audio_element_handle_t i2s_stream_writer = i2s_stream_init(&i2s_cfg);
/*
rsp_filter_cfg_t rsp_cfg = {}; rsp_filter_cfg_t rsp_cfg = {};
/* rsp_cfg.src_rate = 16000;
.src_rate = 16000, rsp_cfg.src_ch = 1;
.src_ch = 1, rsp_cfg.src_bits = 16;
.dest_rate = 16000, rsp_cfg.dest_rate = 16000;
.dest_bits = 16, rsp_cfg.dest_ch = 2;
.dest_ch = 2, rsp_cfg.dest_bits = 16;
.src_bits = 16, rsp_cfg.mode = RESAMPLE_DECODE_MODE;
.mode = RESAMPLE_DECODE_MODE, rsp_cfg.max_indata_bytes = RSP_FILTER_BUFFER_BYTE;
.max_indata_bytes = RSP_FILTER_BUFFER_BYTE, rsp_cfg.out_len_bytes = RSP_FILTER_BUFFER_BYTE;
.out_len_bytes = RSP_FILTER_BUFFER_BYTE, rsp_cfg.type = ESP_RESAMPLE_TYPE_AUTO;
.type = ESP_RESAMPLE_TYPE_AUTO, rsp_cfg.complexity = 2;
.complexity = 2, rsp_cfg.down_ch_idx = 0;
.down_ch_idx = 0, rsp_cfg.prefer_flag = ESP_RSP_PREFER_TYPE_SPEED;
.prefer_flag = ESP_RSP_PREFER_TYPE_SPEED, rsp_cfg.out_rb_size = RSP_FILTER_RINGBUFFER_SIZE;
.out_rb_size = RSP_FILTER_RINGBUFFER_SIZE, rsp_cfg.task_stack = RSP_FILTER_TASK_STACK;
.task_stack = RSP_FILTER_TASK_STACK, rsp_cfg.task_core = RSP_FILTER_TASK_CORE;
.task_core = RSP_FILTER_TASK_CORE, rsp_cfg.task_prio = RSP_FILTER_TASK_PRIO;
.task_prio = RSP_FILTER_TASK_PRIO, rsp_cfg.stack_in_ext = true;
.stack_in_ext = true, audio_element_handle_t filter = rsp_filter_init(&rsp_cfg);
};
*/ */
rsp_cfg.src_rate = 16000;
rsp_cfg.src_ch = 1;
rsp_cfg.src_bits = 16;
rsp_cfg.dest_rate = 16000;
rsp_cfg.dest_ch = 2;
rsp_cfg.dest_bits = 16;
rsp_cfg.mode = RESAMPLE_DECODE_MODE;
rsp_cfg.max_indata_bytes = RSP_FILTER_BUFFER_BYTE;
rsp_cfg.out_len_bytes = RSP_FILTER_BUFFER_BYTE;
rsp_cfg.type = ESP_RESAMPLE_TYPE_AUTO;
rsp_cfg.complexity = 2;
rsp_cfg.down_ch_idx = 0;
rsp_cfg.prefer_flag = ESP_RSP_PREFER_TYPE_SPEED;
rsp_cfg.out_rb_size = RSP_FILTER_RINGBUFFER_SIZE;
rsp_cfg.task_stack = RSP_FILTER_TASK_STACK;
rsp_cfg.task_core = RSP_FILTER_TASK_CORE;
rsp_cfg.task_prio = RSP_FILTER_TASK_PRIO;
rsp_cfg.stack_in_ext = true;
audio_element_handle_t filter = rsp_filter_init(&rsp_cfg);
raw_stream_cfg_t raw_cfg = { raw_stream_cfg_t raw_cfg = {
.type = AUDIO_STREAM_WRITER, .type = AUDIO_STREAM_WRITER,
.out_rb_size = 8 * 1024, .out_rb_size = 8 * 1024,
@ -165,7 +109,7 @@ void ESPADFSpeaker::player_task(void *params) {
audio_element_handle_t raw_write = raw_stream_init(&raw_cfg); audio_element_handle_t raw_write = raw_stream_init(&raw_cfg);
audio_pipeline_register(pipeline, raw_write, "raw"); audio_pipeline_register(pipeline, raw_write, "raw");
audio_pipeline_register(pipeline, filter, "filter"); // audio_pipeline_register(pipeline, filter, "filter");
audio_pipeline_register(pipeline, i2s_stream_writer, "i2s"); audio_pipeline_register(pipeline, i2s_stream_writer, "i2s");
const char *link_tag[3] = { const char *link_tag[3] = {