@geastack/targets 0.1.87 → 0.1.89

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/package.json +7 -7
  2. package/targets/esp32/app_project.cmake +18 -1
  3. package/targets/esp32/chip_bindings/audio/echo_cancellation.cpp +229 -0
  4. package/targets/esp32/chip_bindings/audio/echo_cancellation.h +13 -0
  5. package/targets/esp32/chip_bindings/audio/es8311.cpp +293 -45
  6. package/targets/esp32/chip_bindings/displays/rgb_panel.cpp +115 -12
  7. package/targets/esp32/chip_bindings/expanders/io_expander.h +2 -0
  8. package/targets/esp32/chip_bindings/expanders/tca9554.cpp +74 -0
  9. package/targets/esp32/chip_bindings/touch/ft3168.cpp +23 -1
  10. package/targets/esp32/chip_bindings/touch/ft3168_controller.h +1 -0
  11. package/targets/esp32/chip_bindings/touch/gt911.cpp +4 -3
  12. package/targets/esp32/components/gea_framework/CMakeLists.txt +8 -1
  13. package/targets/esp32/components/gea_juice/CMakeLists.txt +46 -0
  14. package/targets/esp32/components/gea_juice/README.md +20 -0
  15. package/targets/esp32/components/gea_juice/juice_memory.c +19 -0
  16. package/targets/esp32/components/gea_juice/juice_memory.h +16 -0
  17. package/targets/esp32/components/gea_juice/patch_threads.py +50 -0
  18. package/targets/esp32/components/gea_vp8_peer/CMakeLists.txt +16 -0
  19. package/targets/esp32/components/gea_vp8_peer/README.md +39 -0
  20. package/targets/esp32/components/gea_vp8_peer/gea_vp8_peer.h +21 -0
  21. package/targets/esp32/components/gea_vp8_peer/vp8_peer.cpp +538 -0
  22. package/targets/esp32/components/gea_vpx/CMakeLists.txt +235 -0
  23. package/targets/esp32/components/gea_vpx/README.md +133 -0
  24. package/targets/esp32/components/gea_vpx/i420_rgb565_s3.S +169 -0
  25. package/targets/esp32/components/gea_vpx/vp8_blocks_s3.c +42 -0
  26. package/targets/esp32/components/gea_vpx/vp8_copy_s3.S +39 -0
  27. package/targets/esp32/components/gea_vpx/vp8_entropy_s3.c +76 -0
  28. package/targets/esp32/components/gea_vpx/vp8_filter_s3.S +58 -0
  29. package/targets/esp32/components/gea_vpx/vp8_idct_s3.S +269 -0
  30. package/targets/esp32/components/gea_vpx/vp8_intrapred_s3.S +65 -0
  31. package/targets/esp32/components/gea_vpx/vp8_loopfilter_s3.S +333 -0
  32. package/targets/esp32/components/gea_vpx/vp8_loopfilter_s3.c +62 -0
  33. package/targets/esp32/components/gea_vpx/vp8_profile_s3.c +22 -0
  34. package/targets/esp32/components/gea_vpx/vp8_profile_s3.h +20 -0
  35. package/targets/esp32/components/gea_vpx/vp8_s3.c +120 -0
  36. package/targets/esp32/components/gea_vpx/vp8_s3.h +58 -0
  37. package/targets/esp32/components/gea_vpx/vp8_workspace_s3.c +14 -0
  38. package/targets/esp32/components/gea_vpx/vpx_memory.c +41 -0
  39. package/targets/esp32/connectivity/diagnostics_transport.cpp +18 -0
  40. package/targets/esp32/connectivity/wifi.cpp +30 -13
  41. package/targets/esp32/crypto/srtp_hmac_psa.c +118 -0
  42. package/targets/esp32/display.cpp +21 -0
  43. package/targets/esp32/gea_framework.cmake +7 -0
  44. package/targets/esp32/host_sources.cmake +2 -0
  45. package/targets/esp32/i2c_completion_order.cmake +57 -0
  46. package/targets/esp32/patch_streaming_dependencies.py +233 -0
  47. package/targets/esp32/services/device_control.cpp +209 -7
  48. package/targets/esp32/services/frame_scheduler.cpp +36 -1
  49. package/targets/esp32/services/opus_benchmark.h +82 -0
  50. package/targets/esp32/services/pcm_simd_check.h +73 -0
  51. package/targets/esp32/services/srtp_benchmark.h +106 -0
  52. package/targets/esp32/services/video_simd_benchmark.h +477 -0
  53. package/targets/esp32/services/vp8_benchmark.h +147 -0
  54. package/targets/esp32/streaming_dependency_patches.cmake +103 -0
  55. package/targets/esp32-m5stack-m5paper/main/idf_component.yml +2 -0
  56. package/targets/esp32-p4-m5stack-tab5/main/idf_component.yml +2 -0
  57. package/targets/esp32-p4-waveshare-touch-lcd-3.5/main/idf_component.yml +2 -0
  58. package/targets/esp32-p4-waveshare-touch-lcd-7/main/idf_component.yml +2 -0
  59. package/targets/esp32-s3-elecrow-rotary-2.1/main/idf_component.yml +2 -0
  60. package/targets/esp32-s3-epaper-1.54/main/idf_component.yml +2 -0
  61. package/targets/esp32-s3-lilygo-t-display-s3-long/main/idf_component.yml +2 -0
  62. package/targets/esp32-s3-m5stack-papers3/main/idf_component.yml +2 -0
  63. package/targets/esp32-s3-m5stack-sticks3/main/idf_component.yml +2 -0
  64. package/targets/esp32-s3-touch-amoled-1.75/main/idf_component.yml +2 -0
  65. package/targets/esp32-s3-touch-amoled-1.8/dependencies.lock +125 -2
  66. package/targets/esp32-s3-touch-amoled-1.8/main/CMakeLists.txt +29 -3
  67. package/targets/esp32-s3-touch-amoled-1.8/main/app_main.cpp +6 -3
  68. package/targets/esp32-s3-touch-amoled-1.8/main/idf_component.yml +15 -0
  69. package/targets/esp32-s3-touch-amoled-1.8/main/include/board.h +12 -0
  70. package/targets/esp32-s3-touch-amoled-1.8/sdkconfig.defaults +9 -1
  71. package/targets/esp32-s3-touch-amoled-2.06/CMakeLists.txt +2 -0
  72. package/targets/esp32-s3-touch-amoled-2.06/dependencies.lock +172 -8
  73. package/targets/esp32-s3-touch-amoled-2.06/main/CMakeLists.txt +9 -1
  74. package/targets/esp32-s3-touch-amoled-2.06/main/app_main.cpp +10 -3
  75. package/targets/esp32-s3-touch-amoled-2.06/main/idf_component.yml +21 -1
  76. package/targets/esp32-s3-touch-amoled-2.06/main/include/board.h +10 -0
  77. package/targets/esp32-s3-touch-amoled-2.06/main/touch.cpp +7 -0
  78. package/targets/esp32-s3-touch-amoled-2.41/dependencies.lock +57 -1
  79. package/targets/esp32-s3-touch-amoled-2.41/main/idf_component.yml +10 -0
  80. package/targets/esp32-s3-waveshare-touch-lcd-4b.json +504 -0
  81. package/targets.json +12 -1
@@ -1,20 +1,27 @@
1
1
  #define GEA_AUDIO_DRIVER_INTERNAL 1
2
2
  #include "audio.h"
3
3
  #include "board.h"
4
+ #if GEA_BOARD_HAS_EXPANDER
5
+ #include "chip_bindings/expanders/io_expander.h"
6
+ #endif
4
7
  #include "audio/es8311/es8311.h"
8
+
5
9
  #include "i2c.h"
6
10
 
7
11
  #include <algorithm>
8
12
  #include <array>
13
+ #include <atomic>
9
14
  #include <cmath>
10
15
  #include <cstddef>
11
16
  #include <cstdint>
12
17
  #include <mutex>
18
+ #include <stdexcept>
13
19
  #include <vector>
14
20
 
15
21
  #include "driver/gpio.h"
16
22
  #include "driver/i2c_master.h"
17
23
  #include "driver/i2s_std.h"
24
+ #include "driver/i2s_tdm.h"
18
25
  #include "esp_codec_dev.h"
19
26
  #include "esp_codec_dev_defaults.h"
20
27
  #include "esp_log.h"
@@ -30,8 +37,14 @@
30
37
  #endif
31
38
  static_assert(GEA_AUDIO_FULL_DUPLEX == 0 || GEA_AUDIO_FULL_DUPLEX == 1);
32
39
 
33
- // Capture is raw microphone PCM. Full duplex does not imply acoustic echo
34
- // cancellation; the application must supply any required echo processing.
40
+ #ifndef GEA_AUDIO_ECHO_CANCELLATION
41
+ #define GEA_AUDIO_ECHO_CANCELLATION 0
42
+ #endif
43
+ #if GEA_AUDIO_ECHO_CANCELLATION
44
+ static_assert(GEA_AUDIO_FULL_DUPLEX, "Echo cancellation requires full duplex");
45
+ #include "echo_cancellation.h"
46
+ #include "esp_heap_caps.h"
47
+ #endif
35
48
 
36
49
  // Applications with a continuous low-latency source can use smaller DMA queues.
37
50
  #ifndef GEA_AUDIO_DMA_DESCRIPTORS
@@ -55,6 +68,22 @@ constexpr gpio_num_t kWsPin = gea::platform::board::audio.ws;
55
68
  constexpr gpio_num_t kDoutPin = gea::platform::board::audio.dout;
56
69
  constexpr gpio_num_t kDinPin = gea::platform::board::audio.din;
57
70
  constexpr gpio_num_t kPaPin = gea::platform::board::audio.powerAmplifier;
71
+
72
+ esp_err_t setAmplifierEnabled(bool enabled) {
73
+ #if GEA_BOARD_SPEAKER_POWER
74
+ return gea::platform::board::setSpeakerPower(enabled) ? ESP_OK : ESP_FAIL;
75
+ #endif
76
+ if constexpr (kPaPin != GPIO_NUM_NC) return gpio_set_level(kPaPin, enabled);
77
+ #if GEA_BOARD_HAS_EXPANDER
78
+ constexpr int pin = gea::platform::board::expander.powerAmplifier;
79
+ if constexpr (pin >= 0) {
80
+ auto &io = gea::platform::esp32::chip_bindings::expanders::ioExpander();
81
+ return io.writePin(pin, enabled) && io.setInput(pin, false) ? ESP_OK : ESP_FAIL;
82
+ }
83
+ #endif
84
+ return ESP_OK;
85
+ }
86
+
58
87
  constexpr int kCodecDataPort = 0;
59
88
 
60
89
  // Boards whose microphones sit on an ES7210 ADC sharing this I2S bus (Waveshare AMOLED 2.06) name its
@@ -68,6 +97,41 @@ constexpr int micAdcAddressOf(const AudioConfig &audio) {
68
97
  }
69
98
  }
70
99
  constexpr int kMicAdcAddress = micAdcAddressOf(gea::platform::board::audio);
100
+ template <typename AudioConfig>
101
+ constexpr int referenceMicOf(const AudioConfig &audio) {
102
+ if constexpr (requires { audio.es7210ReferenceMic; }) return audio.es7210ReferenceMic;
103
+ return 0;
104
+ }
105
+ constexpr bool kHardwareAecReference = GEA_AUDIO_ECHO_CANCELLATION && kMicAdcAddress != 0 &&
106
+ referenceMicOf(gea::platform::board::audio) == 3;
107
+
108
+ template <typename AudioConfig>
109
+ constexpr float aecMicGainOf(const AudioConfig &audio) {
110
+ if constexpr (requires { audio.aecMicGainDb; }) return audio.aecMicGainDb;
111
+ return 12.0f;
112
+ }
113
+ template <typename AudioConfig>
114
+ constexpr float aecOutputGainOf(const AudioConfig &audio) {
115
+ if constexpr (requires { audio.aecOutputGain; }) return audio.aecOutputGain;
116
+ return 1.0f;
117
+ }
118
+ constexpr float kAecMicGain = aecMicGainOf(gea::platform::board::audio);
119
+ constexpr float kAecOutputGain = aecOutputGainOf(gea::platform::board::audio);
120
+ template <typename AudioConfig>
121
+ constexpr bool aecAggressiveNlpOf(const AudioConfig &audio) {
122
+ if constexpr (requires { audio.aecAggressiveNlp; }) return audio.aecAggressiveNlp;
123
+ return true;
124
+ }
125
+ constexpr bool kAecAggressiveNlp = aecAggressiveNlpOf(gea::platform::board::audio);
126
+ template <typename AudioConfig>
127
+ constexpr bool codecReferenceOf(const AudioConfig &audio) {
128
+ if constexpr (requires { audio.aecCodecReference; }) return audio.aecCodecReference;
129
+ return false;
130
+ }
131
+ // ES8311's ADCL + DACR mode returns mic and DAC reference in one RX frame.
132
+ // This changes AEC pairing, not the ES7210-specific TDM/clock configuration.
133
+ constexpr bool kPairedAecReference = kHardwareAecReference ||
134
+ (GEA_AUDIO_ECHO_CANCELLATION && codecReferenceOf(gea::platform::board::audio));
71
135
 
72
136
  class AudioOutputDriver {
73
137
  public:
@@ -84,6 +148,9 @@ public:
84
148
  std::lock_guard<std::mutex> captureLock(readMutex_);
85
149
  const gea::chips::es8311::OutputFormat format(sampleRate, channels, bitsPerSample);
86
150
  if (!format.isPcm16()) return false;
151
+ #if GEA_AUDIO_ECHO_CANCELLATION
152
+ if (sampleRate != 16000 || (channels != 1 && channels != 2)) return false;
153
+ #endif
87
154
 
88
155
  if (speakerOpen_ &&
89
156
  speakerSampleRate_ == sampleRate &&
@@ -119,6 +186,9 @@ public:
119
186
 
120
187
  applySpeakerVolumeLocked();
121
188
  speakerOpen_ = true;
189
+ #if GEA_AUDIO_ECHO_CANCELLATION
190
+ aecSpeakerActive_.store(true);
191
+ #endif
122
192
  speakerSampleRate_ = sampleRate;
123
193
  speakerChannels_ = channels;
124
194
  speakerBitsPerSample_ = bitsPerSample;
@@ -171,6 +241,29 @@ public:
171
241
  int volume() const {
172
242
  return speakerVolume_;
173
243
  }
244
+ bool flush() {
245
+ std::lock_guard<std::mutex> lock(writeMutex_);
246
+ if (!speakerOpen_ || !txChannel_) return true;
247
+ i2s_chan_info_t info{};
248
+ if (i2s_channel_get_info(txChannel_, &info) != ESP_OK) return false;
249
+ // Reset TX descriptors only; retain RX, both codecs, AEC and its history.
250
+ if (i2s_channel_disable(txChannel_) != ESP_OK) return false;
251
+ static constexpr std::array<std::uint8_t, 256> silence{};
252
+ std::size_t remaining = info.total_dma_buf_size;
253
+ bool cleared = true;
254
+ while (remaining) {
255
+ std::size_t loaded = 0;
256
+ const auto count = std::min(remaining, silence.size());
257
+ if (i2s_channel_preload_data(txChannel_, silence.data(), count, &loaded) != ESP_OK || loaded != count) {
258
+ cleared = false;
259
+ break;
260
+ }
261
+ remaining -= loaded;
262
+ }
263
+ const auto restarted = i2s_channel_enable(txChannel_) == ESP_OK;
264
+ ESP_LOGI(kTag, "Speaker DMA cancelled bytes=%u cleared=%d restarted=%d", unsigned(info.total_dma_buf_size), int(cleared), int(restarted));
265
+ return cleared && restarted;
266
+ }
174
267
 
175
268
  void setVolume(int volumePercent) {
176
269
  if (volumePercent < 0) volumePercent = 0;
@@ -199,10 +292,7 @@ private:
199
292
 
200
293
  void applySpeakerVolumeLocked() {
201
294
  if (!speakerCodec_) return;
202
- #if GEA_BOARD_SPEAKER_POWER
203
- gea::platform::board::setSpeakerPower(speakerVolume_ > 0);
204
- #endif
205
- if (kPaPin != GPIO_NUM_NC) gpio_set_level(kPaPin, 1);
295
+ setAmplifierEnabled(speakerVolume_ > 0);
206
296
  const int codecVolume = codecVolumeFromUserPercent(speakerVolume_);
207
297
  const int err = esp_codec_dev_set_out_vol(speakerCodec_, codecVolume);
208
298
  if (err != ESP_CODEC_DEV_OK) {
@@ -211,14 +301,13 @@ private:
211
301
  }
212
302
 
213
303
  void closeSpeakerLocked() {
304
+ #if GEA_AUDIO_ECHO_CANCELLATION
305
+ aecSpeakerActive_.store(false);
306
+ #endif
214
307
  if (speakerCodec_ && speakerOpen_) {
215
308
  esp_codec_dev_close(speakerCodec_);
216
- i2sChannelEnabled_ = false;
217
309
  }
218
- #if GEA_BOARD_SPEAKER_POWER
219
- gea::platform::board::setSpeakerPower(false);
220
- #endif
221
- if (kPaPin != GPIO_NUM_NC) gpio_set_level(kPaPin, 0);
310
+ setAmplifierEnabled(false);
222
311
  speakerOpen_ = false;
223
312
  speakerSampleRate_ = 0;
224
313
  speakerChannels_ = 0;
@@ -228,37 +317,41 @@ private:
228
317
  void closeRecorderLocked() {
229
318
  if (recordCodec_ && recordOpen_) {
230
319
  esp_codec_dev_close(recordCodec_);
231
- rxChannelEnabled_ = false;
232
320
  }
233
321
  recordOpen_ = false;
322
+ #if GEA_AUDIO_ECHO_CANCELLATION
323
+ geaAudioAecStop();
324
+ #endif
234
325
  }
235
326
 
236
- esp_err_t disableI2sChannel(i2s_chan_handle_t channel, bool &enabled, const char *label) {
237
- if (!channel || !enabled) return ESP_OK;
327
+ esp_err_t disableI2sChannel(i2s_chan_handle_t channel, const char *label) {
328
+ if (!channel) return ESP_OK;
238
329
  esp_err_t err = i2s_channel_disable(channel);
239
330
  if (err == ESP_ERR_INVALID_STATE) {
240
- ESP_LOGW(kTag, "I2S %s channel was already disabled", label);
241
331
  err = ESP_OK;
242
332
  }
243
333
  if (err != ESP_OK) {
244
334
  ESP_LOGE(kTag, "I2S %s disable failed: %s", label, esp_err_to_name(err));
245
335
  return err;
246
336
  }
247
- enabled = false;
248
337
  return ESP_OK;
249
338
  }
250
339
 
251
340
  void releaseI2s() {
252
- if (txChannel_) {
253
- (void)disableI2sChannel(txChannel_, i2sChannelEnabled_, "TX");
254
- i2s_del_channel(txChannel_);
255
- txChannel_ = nullptr;
256
- }
257
- if (rxChannel_) {
258
- (void)disableI2sChannel(rxChannel_, rxChannelEnabled_, "RX");
259
- i2s_del_channel(rxChannel_);
260
- rxChannel_ = nullptr;
261
- }
341
+ // esp_codec_dev may enable TX implicitly while opening RX for its clock.
342
+ // A cached enabled flag cannot observe that; consult the actual driver by
343
+ // disabling each channel before deletion. Keep a handle if teardown fails.
344
+ const auto release = [&](i2s_chan_handle_t &channel, const char *label) {
345
+ if (!channel || disableI2sChannel(channel, label) != ESP_OK) return;
346
+ const esp_err_t err = i2s_del_channel(channel);
347
+ if (err != ESP_OK) {
348
+ ESP_LOGE(kTag, "I2S %s deletion failed: %s", label, esp_err_to_name(err));
349
+ return;
350
+ }
351
+ channel = nullptr;
352
+ };
353
+ release(txChannel_, "TX");
354
+ release(rxChannel_, "RX");
262
355
  i2sDataIf_ = nullptr;
263
356
  }
264
357
 
@@ -282,12 +375,37 @@ private:
282
375
  i2sSampleRate_ = 0;
283
376
  }
284
377
 
378
+ #if GEA_AUDIO_ECHO_CANCELLATION
379
+ std::atomic<bool> aecSpeakerActive_{false};
380
+
381
+ static bool txSent(i2s_chan_handle_t, i2s_event_data_t *event, void *context) {
382
+ const auto *driver = static_cast<AudioOutputDriver *>(context);
383
+ static const std::array<int16_t, 240> silence{};
384
+ if (driver->aecSpeakerActive_.load(std::memory_order_relaxed)) {
385
+ geaAudioAecReference(static_cast<const int16_t *>(event->dma_buf), event->size / sizeof(int16_t));
386
+ } else {
387
+ geaAudioAecReference(silence.data(), silence.size());
388
+ }
389
+ return false;
390
+ }
391
+
392
+ static bool rxReceived(i2s_chan_handle_t, i2s_event_data_t *event, void *) {
393
+ geaAudioAecReceive(static_cast<const int16_t *>(event->dma_buf), event->size / sizeof(int16_t));
394
+ return false;
395
+ }
396
+ #endif
397
+
285
398
  esp_err_t initI2s(int sampleRate) {
286
399
  if (txChannel_ && i2sDataIf_) {
287
400
  if (i2sSampleRate_ == sampleRate) return ESP_OK;
288
401
  return reconfigureI2sClock(sampleRate);
289
402
  }
290
403
 
404
+ if (txChannel_ || rxChannel_) {
405
+ releaseI2s();
406
+ if (txChannel_ || rxChannel_) return ESP_ERR_INVALID_STATE;
407
+ }
408
+
291
409
  i2s_chan_config_t channelConfig = I2S_CHANNEL_DEFAULT_CONFIG(kI2sPort, I2S_ROLE_MASTER);
292
410
  channelConfig.auto_clear = true;
293
411
  // Match the pala_note reference's DMA buffering (the IDF default 6 x 240
@@ -309,6 +427,11 @@ private:
309
427
  i2s_std_config_t stdConfig = {};
310
428
  stdConfig.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(sampleRate));
311
429
  stdConfig.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO);
430
+ if constexpr (kHardwareAecReference) {
431
+ // Four 16-bit ADC slots share a 64-clock frame with two 32-bit DAC slots.
432
+ stdConfig.slot_cfg.slot_bit_width = I2S_SLOT_BIT_WIDTH_32BIT;
433
+ stdConfig.slot_cfg.ws_width = 32;
434
+ }
312
435
  stdConfig.gpio_cfg.mclk = kMclkPin;
313
436
  stdConfig.gpio_cfg.bclk = kBclkPin;
314
437
  stdConfig.gpio_cfg.ws = kWsPin;
@@ -325,27 +448,55 @@ private:
325
448
  return err;
326
449
  }
327
450
 
451
+ #if GEA_AUDIO_ECHO_CANCELLATION
452
+ i2s_event_callbacks_t txCallbacks{};
453
+ txCallbacks.on_sent = txSent;
454
+ err = kPairedAecReference ? ESP_OK : i2s_channel_register_event_callback(txChannel_, &txCallbacks, this);
455
+ if (err != ESP_OK) { releaseI2s(); return err; }
456
+ #endif
328
457
  err = i2s_channel_enable(txChannel_);
329
458
  if (err != ESP_OK) {
330
459
  ESP_LOGE(kTag, "I2S enable failed: %s", esp_err_to_name(err));
331
460
  releaseI2s();
332
461
  return err;
333
462
  }
334
- i2sChannelEnabled_ = true;
335
463
 
336
- err = i2s_channel_init_std_mode(rxChannel_, &stdConfig);
464
+ if constexpr (kHardwareAecReference) {
465
+ i2s_tdm_config_t rxConfig{};
466
+ rxConfig.clk_cfg = I2S_TDM_CLK_DEFAULT_CONFIG(static_cast<uint32_t>(sampleRate));
467
+ rxConfig.slot_cfg = I2S_TDM_PHILIPS_SLOT_DEFAULT_CONFIG(
468
+ I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO,
469
+ static_cast<i2s_tdm_slot_mask_t>(I2S_TDM_SLOT0 | I2S_TDM_SLOT1));
470
+ // ES7210 TDM order is MIC1, MIC3, MIC2, MIC4. Capture only the first
471
+ // microphone and hardware reference; unselected slots consume no DMA RAM.
472
+ rxConfig.slot_cfg.total_slot = 4;
473
+ rxConfig.slot_cfg.ws_width = 32;
474
+ rxConfig.gpio_cfg.mclk = kMclkPin;
475
+ rxConfig.gpio_cfg.bclk = kBclkPin;
476
+ rxConfig.gpio_cfg.ws = kWsPin;
477
+ rxConfig.gpio_cfg.dout = I2S_GPIO_UNUSED;
478
+ rxConfig.gpio_cfg.din = kDinPin;
479
+ err = i2s_channel_init_tdm_mode(rxChannel_, &rxConfig);
480
+ } else {
481
+ err = i2s_channel_init_std_mode(rxChannel_, &stdConfig);
482
+ }
337
483
  if (err != ESP_OK) {
338
484
  ESP_LOGE(kTag, "I2S RX std init failed: %s", esp_err_to_name(err));
339
485
  releaseI2s();
340
486
  return err;
341
487
  }
488
+ #if GEA_AUDIO_ECHO_CANCELLATION
489
+ i2s_event_callbacks_t rxCallbacks{};
490
+ rxCallbacks.on_recv = rxReceived;
491
+ err = i2s_channel_register_event_callback(rxChannel_, &rxCallbacks, this);
492
+ if (err != ESP_OK) { releaseI2s(); return err; }
493
+ #endif
342
494
  err = i2s_channel_enable(rxChannel_);
343
495
  if (err != ESP_OK) {
344
496
  ESP_LOGE(kTag, "I2S RX enable failed: %s", esp_err_to_name(err));
345
497
  releaseI2s();
346
498
  return err;
347
499
  }
348
- rxChannelEnabled_ = true;
349
500
 
350
501
  audio_codec_i2s_cfg_t i2sConfig = {};
351
502
  i2sConfig.port = kCodecDataPort;
@@ -365,9 +516,9 @@ private:
365
516
  esp_err_t reconfigureI2sClock(int sampleRate) {
366
517
  if (!txChannel_ || !rxChannel_) return ESP_ERR_INVALID_STATE;
367
518
 
368
- esp_err_t err = disableI2sChannel(txChannel_, i2sChannelEnabled_, "TX");
519
+ esp_err_t err = disableI2sChannel(txChannel_, "TX");
369
520
  if (err != ESP_OK) return err;
370
- err = disableI2sChannel(rxChannel_, rxChannelEnabled_, "RX");
521
+ err = disableI2sChannel(rxChannel_, "RX");
371
522
  if (err != ESP_OK) return err;
372
523
 
373
524
  i2s_std_clk_config_t clockConfig = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(sampleRate));
@@ -376,7 +527,12 @@ private:
376
527
  ESP_LOGE(kTag, "I2S TX clock reconfig failed: %s", esp_err_to_name(err));
377
528
  return err;
378
529
  }
379
- err = i2s_channel_reconfig_std_clock(rxChannel_, &clockConfig);
530
+ if constexpr (kHardwareAecReference) {
531
+ i2s_tdm_clk_config_t rxClock = I2S_TDM_CLK_DEFAULT_CONFIG(static_cast<uint32_t>(sampleRate));
532
+ err = i2s_channel_reconfig_tdm_clock(rxChannel_, &rxClock);
533
+ } else {
534
+ err = i2s_channel_reconfig_std_clock(rxChannel_, &clockConfig);
535
+ }
380
536
  if (err != ESP_OK) {
381
537
  ESP_LOGE(kTag, "I2S RX clock reconfig failed: %s", esp_err_to_name(err));
382
538
  return err;
@@ -387,13 +543,11 @@ private:
387
543
  ESP_LOGE(kTag, "I2S TX re-enable failed: %s", esp_err_to_name(err));
388
544
  return err;
389
545
  }
390
- i2sChannelEnabled_ = true;
391
546
  err = i2s_channel_enable(rxChannel_);
392
547
  if (err != ESP_OK) {
393
548
  ESP_LOGE(kTag, "I2S RX re-enable failed: %s", esp_err_to_name(err));
394
549
  return err;
395
550
  }
396
- rxChannelEnabled_ = true;
397
551
  i2sSampleRate_ = sampleRate;
398
552
  return ESP_OK;
399
553
  }
@@ -409,7 +563,25 @@ private:
409
563
  }
410
564
  if (recordOpen_) closeRecorderLocked();
411
565
  if constexpr (!GEA_AUDIO_FULL_DUPLEX) {
412
- if (speakerOpen_) closeSpeakerLocked();
566
+ if (speakerOpen_) {
567
+ // A successful write only queues PCM in TX DMA. Reclaiming I2S for
568
+ // capture immediately can truncate the final speech samples. Writing
569
+ // one complete DMA ring of silence waits for every earlier descriptor
570
+ // to finish; this follows the actual driver capacity, not a sleep.
571
+ i2s_chan_info_t info{};
572
+ esp_err_t drain = i2s_channel_get_info(txChannel_, &info);
573
+ if (drain != ESP_OK) return drain;
574
+ static constexpr std::array<std::uint8_t, 256> silence{};
575
+ std::size_t remaining = info.total_dma_buf_size;
576
+ while (remaining) {
577
+ const auto bytes = std::min(remaining, silence.size());
578
+ drain = esp_codec_dev_write(speakerCodec_, const_cast<std::uint8_t*>(silence.data()), bytes);
579
+ if (drain != ESP_OK) return drain;
580
+ remaining -= bytes;
581
+ }
582
+ ESP_LOGI(kTag, "TX DMA drained before microphone: %u bytes", static_cast<unsigned>(info.total_dma_buf_size));
583
+ closeSpeakerLocked();
584
+ }
413
585
  }
414
586
 
415
587
  esp_err_t err = initCodecDevice(sampleRate, ESP_CODEC_DEV_TYPE_IN, recordCodec_);
@@ -417,7 +589,9 @@ private:
417
589
 
418
590
  esp_codec_dev_sample_info_t sampleInfo = {};
419
591
  sampleInfo.bits_per_sample = 16;
420
- sampleInfo.channel = 2;
592
+ sampleInfo.channel = kHardwareAecReference ? 4 : 2;
593
+ if constexpr (kHardwareAecReference) sampleInfo.channel_mask =
594
+ ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0) | ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1);
421
595
  sampleInfo.sample_rate = static_cast<std::uint32_t>(sampleRate);
422
596
  sampleInfo.mclk_multiple = I2S_MCLK_MULTIPLE_256;
423
597
  err = esp_codec_dev_open(recordCodec_, &sampleInfo);
@@ -425,8 +599,28 @@ private:
425
599
  ESP_LOGE(kTag, "Recorder open failed: %s", esp_err_to_name(err));
426
600
  return err;
427
601
  }
428
- esp_codec_dev_set_in_gain(recordCodec_, 45.0f);
429
602
  recordOpen_ = true;
603
+ // Sensitivity is board-specific: the ES7210 loopback calibration is not
604
+ // the ES8311 analog microphone's calibration. Keep gain before AEC low
605
+ // enough for ADC headroom, then restore its voice level after cancellation.
606
+ esp_codec_dev_set_in_gain(recordCodec_, GEA_AUDIO_ECHO_CANCELLATION ? kAecMicGain : 45.0f);
607
+ if constexpr (kHardwareAecReference) {
608
+ // MIC1 needs sufficient near-end sensitivity at unity AEC output. An
609
+ // 18 dB PGA setting adds 6 dB over the former headroom-only setting,
610
+ // below the previously clipping 24 dB setting. MIC3 stays independent.
611
+ err = esp_codec_dev_set_in_channel_gain(recordCodec_, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0), 18.0f);
612
+ if (err != ESP_OK) { closeRecorderLocked(); return err; }
613
+ // Codec gain masks use physical MIC numbering, not TDM slot numbering.
614
+ // The board attenuates the analog loopback before MIC3. At 0 dB its
615
+ // measured reference was only 12–114 RMS against 257–1573 RMS echo;
616
+ // use the codec's nominal 30 dB input gain for this attenuated line.
617
+ err = esp_codec_dev_set_in_channel_gain(recordCodec_, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(2), 30.0f);
618
+ if (err != ESP_OK) { closeRecorderLocked(); return err; }
619
+ }
620
+ #if GEA_AUDIO_ECHO_CANCELLATION
621
+ if (!geaAudioAecStart(kPairedAecReference, kAecOutputGain, kAecAggressiveNlp)) { closeRecorderLocked(); return ESP_ERR_NO_MEM; }
622
+ ESP_LOGI(kTag, "Microphone AEC gain: ADC=%.1f dB output=%.3f", kAecMicGain, kAecOutputGain);
623
+ #endif
430
624
  i2sSampleRate_ = sampleRate;
431
625
  return ESP_OK;
432
626
  }
@@ -441,12 +635,10 @@ private:
441
635
  paConfig.pin_bit_mask = 1ULL << kPaPin;
442
636
  paConfig.mode = GPIO_MODE_OUTPUT;
443
637
  gpio_config(&paConfig);
444
- gpio_set_level(kPaPin, 1);
445
638
  }
639
+ err = setAmplifierEnabled(speakerVolume_ > 0);
640
+ if (err != ESP_OK) return err;
446
641
 
447
- #if GEA_BOARD_SPEAKER_POWER
448
- if (!gea::platform::board::setSpeakerPower(speakerVolume_ > 0)) return ESP_FAIL;
449
- #endif
450
642
  if (!codecIf_) {
451
643
  auto i2cBus = gea::platform::i2c::Bus::primary();
452
644
  if (!i2cBus.available()) {
@@ -517,6 +709,7 @@ private:
517
709
  es7210Config.ctrl_if = micAdcCtrlIf_;
518
710
  es7210Config.master_mode = false;
519
711
  es7210Config.mic_selected = ES7210_SEL_MIC1 | ES7210_SEL_MIC2;
712
+ if constexpr (kHardwareAecReference) es7210Config.mic_selected |= ES7210_SEL_MIC3 | ES7210_SEL_MIC4;
520
713
  es7210Config.mclk_src = ES7210_MCLK_FROM_PAD;
521
714
  es7210Config.mclk_div = 256;
522
715
  micAdcIf_ = es7210_codec_new(&es7210Config);
@@ -539,8 +732,6 @@ private:
539
732
  const audio_codec_if_t *micAdcIf_ = nullptr;
540
733
  esp_codec_dev_handle_t speakerCodec_ = nullptr;
541
734
  esp_codec_dev_handle_t recordCodec_ = nullptr;
542
- bool i2sChannelEnabled_ = false;
543
- bool rxChannelEnabled_ = false;
544
735
  bool speakerOpen_ = false;
545
736
  bool recordOpen_ = false;
546
737
  int i2sSampleRate_ = 0;
@@ -557,7 +748,9 @@ private:
557
748
  std::vector<gea::host::NativeMediaTrackHandle> attachedTracks_;
558
749
  std::vector<gea::host::NativeMediaTrackHandle> captureSnapshot_;
559
750
  std::mutex attachedMutex_;
560
- static constexpr std::size_t kCaptureFrameSamples = GEA_AUDIO_FULL_DUPLEX ? 320 : 2048;
751
+ // Match the 20 ms Opus input frame in either duplex mode. A 2048-sample
752
+ // half-duplex read batches 128 ms of speech before the encoder can see it.
753
+ static constexpr std::size_t kCaptureFrameSamples = 320;
561
754
  std::array<std::int16_t, kCaptureFrameSamples * 2> stereoFrame_{};
562
755
  std::array<std::int16_t, kCaptureFrameSamples> monoFrame_{};
563
756
 
@@ -596,11 +789,36 @@ private:
596
789
  if (!speakerOpen_) releaseAudioLocked();
597
790
  }
598
791
  }
792
+ #if GEA_AUDIO_ECHO_CANCELLATION
793
+ vTaskDeleteWithCaps(nullptr);
794
+ #else
599
795
  vTaskDelete(nullptr);
796
+ #endif
600
797
  return;
601
798
  }
602
799
 
603
800
  int err = ESP_OK;
801
+ #if GEA_AUDIO_ECHO_CANCELLATION
802
+ {
803
+ // A running RX uses DMA callbacks, so TX's blocking write never owns
804
+ // the microphone read path. Take both locks only for configuration.
805
+ std::unique_lock<std::mutex> captureLock(readMutex_);
806
+ if (!recordOpen_) {
807
+ captureLock.unlock();
808
+ std::lock_guard<std::mutex> configLock(writeMutex_);
809
+ captureLock.lock();
810
+ err = initRecorder(16000);
811
+ }
812
+ if (err == ESP_OK) {
813
+ bool ready = false;
814
+ for (int retry = 0; retry < 500 && !ready; ++retry) {
815
+ ready = geaAudioAecRead(monoFrame_.data(), kCaptureFrameSamples);
816
+ if (!ready) vTaskDelay(1);
817
+ }
818
+ err = ready ? ESP_OK : ESP_ERR_TIMEOUT;
819
+ }
820
+ }
821
+ #else
604
822
  {
605
823
  std::unique_lock<std::mutex> lock(writeMutex_);
606
824
  std::lock_guard<std::mutex> captureLock(readMutex_);
@@ -610,6 +828,7 @@ private:
610
828
  err = esp_codec_dev_read(recordCodec_, stereoFrame_.data(), stereoFrame_.size() * sizeof(std::int16_t));
611
829
  }
612
830
  }
831
+ #endif
613
832
  if (err != ESP_OK) {
614
833
  if (consecutiveReadErrors == 0 || consecutiveReadErrors % 100 == 0) {
615
834
  ESP_LOGW(kTag, "Mic read failed err=%s snapshot_tracks=%u", esp_err_to_name(err), static_cast<unsigned>(captureSnapshot_.size()));
@@ -621,13 +840,20 @@ private:
621
840
  consecutiveReadErrors = 0;
622
841
  const std::size_t framesRead = kCaptureFrameSamples;
623
842
 
843
+ #if !GEA_AUDIO_ECHO_CANCELLATION
624
844
  // ES8311 input is interleaved as [mic, ref]. For local voice notes, keep
625
845
  // the real mic channel only; mixing in the ref slot makes speech metallic.
626
846
  for (std::size_t i = 0; i < framesRead; ++i) {
627
847
  monoFrame_[i] = stereoFrame_[2 * i];
628
848
  }
849
+ #endif
850
+ const int16_t *captured = monoFrame_.data();
851
+ size_t capturedCount = framesRead;
852
+ #if GEA_AUDIO_ECHO_CANCELLATION
853
+ capturedCount = geaAudioAecProcess(captured, framesRead, &captured);
854
+ #endif
629
855
  for (auto handle : captureSnapshot_) {
630
- gea::host::media::track_inject_pcm(handle, monoFrame_.data(), framesRead);
856
+ if (capturedCount) gea::host::media::track_inject_pcm(handle, captured, capturedCount);
631
857
  }
632
858
  }
633
859
  }
@@ -638,7 +864,22 @@ public:
638
864
  attachedTracks_.push_back(handle);
639
865
  // Checked and set under the lock the capture task clears it with, so only one capture task runs.
640
866
  if (captureTask_ == nullptr) {
867
+ #if GEA_AUDIO_ECHO_CANCELLATION
868
+ // Capture DSP and speaker feeding share the highest audio priority.
869
+ // Rendering may share this core at priority 23: using that priority here
870
+ // time-sliced DSP with rendering and missed the 32 ms frame deadline.
871
+ const auto created = xTaskCreatePinnedToCoreWithCaps(
872
+ captureTaskTrampoline, "gea_mic", 8192, this,
873
+ configMAX_PRIORITIES - 1, &captureTask_, 0,
874
+ MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
875
+ if (created != pdPASS) {
876
+ attachedTracks_.pop_back();
877
+ ESP_LOGE(kTag, "Microphone task allocation failed");
878
+ throw std::runtime_error("Microphone task allocation failed");
879
+ }
880
+ #else
641
881
  xTaskCreatePinnedToCore(captureTaskTrampoline, "gea_mic", 8192, this, 5, &captureTask_, 1);
882
+ #endif
642
883
  }
643
884
  }
644
885
 
@@ -650,6 +891,10 @@ public:
650
891
 
651
892
  } // namespace gea::platform::esp32::chip_bindings::es8311
652
893
 
894
+ double gea::platform::audio::OutputDriver::outputLatency(int sampleRate) {
895
+ return sampleRate > 0 ? double(GEA_AUDIO_DMA_DESCRIPTORS * GEA_AUDIO_DMA_FRAMES) / sampleRate : 0;
896
+ }
897
+
653
898
  bool gea::platform::audio::OutputDriver::open(int sampleRate, int channels, int bitsPerSample) {
654
899
  return gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().open(sampleRate, channels, bitsPerSample);
655
900
  }
@@ -661,6 +906,9 @@ bool gea::platform::audio::OutputDriver::write(const std::int16_t *pcm, std::siz
661
906
  void gea::platform::audio::OutputDriver::close() {
662
907
  gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().close();
663
908
  }
909
+ bool gea::platform::audio::OutputDriver::flush() {
910
+ return gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().flush();
911
+ }
664
912
 
665
913
  int gea::platform::audio::OutputDriver::volume() {
666
914
  return gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().volume();