@geastack/targets 0.1.86 → 0.1.88

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 (86) 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 +213 -0
  4. package/targets/esp32/chip_bindings/audio/echo_cancellation.h +12 -0
  5. package/targets/esp32/chip_bindings/audio/es8311.cpp +280 -41
  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 +26 -13
  41. package/targets/esp32/crypto/srtp_hmac_psa.c +118 -0
  42. package/targets/esp32/display.cpp +13 -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 +141 -5
  48. package/targets/esp32/services/frame_scheduler.cpp +29 -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 +110 -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 +11 -0
  69. package/targets/esp32-s3-touch-amoled-1.8/sdkconfig.defaults +9 -1
  70. package/targets/esp32-s3-touch-amoled-2.06/CMakeLists.txt +2 -0
  71. package/targets/esp32-s3-touch-amoled-2.06/dependencies.lock +171 -8
  72. package/targets/esp32-s3-touch-amoled-2.06/main/CMakeLists.txt +9 -2
  73. package/targets/esp32-s3-touch-amoled-2.06/main/app_main.cpp +10 -3
  74. package/targets/esp32-s3-touch-amoled-2.06/main/idf_component.yml +16 -1
  75. package/targets/esp32-s3-touch-amoled-2.06/main/include/board.h +3 -0
  76. package/targets/esp32-s3-touch-amoled-2.06/main/touch.cpp +7 -0
  77. package/targets/esp32-s3-touch-amoled-2.41/dependencies.lock +57 -1
  78. package/targets/esp32-s3-touch-amoled-2.41/main/idf_component.yml +10 -0
  79. package/targets/esp32-s3-touch-lcd-3.5b/dependencies.lock +12 -1
  80. package/targets/esp32-s3-touch-lcd-3.5b/main/CMakeLists.txt +5 -1
  81. package/targets/esp32-s3-touch-lcd-3.5b/main/idf_component.yml +2 -1
  82. package/targets/esp32-s3-touch-lcd-3.5b/main/include/board.h +3 -0
  83. package/targets/esp32-s3-touch-lcd-3.5b/main/platform_stubs.cpp +1 -18
  84. package/targets/esp32-s3-touch-lcd-3.5b/main/speaker_power.cpp +27 -0
  85. package/targets/esp32-s3-waveshare-touch-lcd-4b.json +504 -0
  86. 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,24 @@
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
48
+
49
+ // Applications with a continuous low-latency source can use smaller DMA queues.
50
+ #ifndef GEA_AUDIO_DMA_DESCRIPTORS
51
+ #define GEA_AUDIO_DMA_DESCRIPTORS 6
52
+ #endif
53
+ #ifndef GEA_AUDIO_DMA_FRAMES
54
+ #define GEA_AUDIO_DMA_FRAMES 240
55
+ #endif
56
+ static_assert(GEA_AUDIO_DMA_DESCRIPTORS >= 2);
57
+ static_assert(GEA_AUDIO_DMA_FRAMES >= 8 && GEA_AUDIO_DMA_FRAMES <= 511);
35
58
 
36
59
  namespace gea::platform::esp32::chip_bindings::es8311 {
37
60
 
@@ -45,6 +68,22 @@ constexpr gpio_num_t kWsPin = gea::platform::board::audio.ws;
45
68
  constexpr gpio_num_t kDoutPin = gea::platform::board::audio.dout;
46
69
  constexpr gpio_num_t kDinPin = gea::platform::board::audio.din;
47
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
+
48
87
  constexpr int kCodecDataPort = 0;
49
88
 
50
89
  // Boards whose microphones sit on an ES7210 ADC sharing this I2S bus (Waveshare AMOLED 2.06) name its
@@ -58,6 +97,13 @@ constexpr int micAdcAddressOf(const AudioConfig &audio) {
58
97
  }
59
98
  }
60
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;
61
107
 
62
108
  class AudioOutputDriver {
63
109
  public:
@@ -74,6 +120,9 @@ public:
74
120
  std::lock_guard<std::mutex> captureLock(readMutex_);
75
121
  const gea::chips::es8311::OutputFormat format(sampleRate, channels, bitsPerSample);
76
122
  if (!format.isPcm16()) return false;
123
+ #if GEA_AUDIO_ECHO_CANCELLATION
124
+ if (sampleRate != 16000 || (channels != 1 && channels != 2)) return false;
125
+ #endif
77
126
 
78
127
  if (speakerOpen_ &&
79
128
  speakerSampleRate_ == sampleRate &&
@@ -109,6 +158,9 @@ public:
109
158
 
110
159
  applySpeakerVolumeLocked();
111
160
  speakerOpen_ = true;
161
+ #if GEA_AUDIO_ECHO_CANCELLATION
162
+ aecSpeakerActive_.store(true);
163
+ #endif
112
164
  speakerSampleRate_ = sampleRate;
113
165
  speakerChannels_ = channels;
114
166
  speakerBitsPerSample_ = bitsPerSample;
@@ -161,6 +213,29 @@ public:
161
213
  int volume() const {
162
214
  return speakerVolume_;
163
215
  }
216
+ bool flush() {
217
+ std::lock_guard<std::mutex> lock(writeMutex_);
218
+ if (!speakerOpen_ || !txChannel_) return true;
219
+ i2s_chan_info_t info{};
220
+ if (i2s_channel_get_info(txChannel_, &info) != ESP_OK) return false;
221
+ // Reset TX descriptors only; retain RX, both codecs, AEC and its history.
222
+ if (i2s_channel_disable(txChannel_) != ESP_OK) return false;
223
+ static constexpr std::array<std::uint8_t, 256> silence{};
224
+ std::size_t remaining = info.total_dma_buf_size;
225
+ bool cleared = true;
226
+ while (remaining) {
227
+ std::size_t loaded = 0;
228
+ const auto count = std::min(remaining, silence.size());
229
+ if (i2s_channel_preload_data(txChannel_, silence.data(), count, &loaded) != ESP_OK || loaded != count) {
230
+ cleared = false;
231
+ break;
232
+ }
233
+ remaining -= loaded;
234
+ }
235
+ const auto restarted = i2s_channel_enable(txChannel_) == ESP_OK;
236
+ ESP_LOGI(kTag, "Speaker DMA cancelled bytes=%u cleared=%d restarted=%d", unsigned(info.total_dma_buf_size), int(cleared), int(restarted));
237
+ return cleared && restarted;
238
+ }
164
239
 
165
240
  void setVolume(int volumePercent) {
166
241
  if (volumePercent < 0) volumePercent = 0;
@@ -189,7 +264,7 @@ private:
189
264
 
190
265
  void applySpeakerVolumeLocked() {
191
266
  if (!speakerCodec_) return;
192
- if (kPaPin != GPIO_NUM_NC) gpio_set_level(kPaPin, 1);
267
+ setAmplifierEnabled(speakerVolume_ > 0);
193
268
  const int codecVolume = codecVolumeFromUserPercent(speakerVolume_);
194
269
  const int err = esp_codec_dev_set_out_vol(speakerCodec_, codecVolume);
195
270
  if (err != ESP_CODEC_DEV_OK) {
@@ -198,11 +273,13 @@ private:
198
273
  }
199
274
 
200
275
  void closeSpeakerLocked() {
276
+ #if GEA_AUDIO_ECHO_CANCELLATION
277
+ aecSpeakerActive_.store(false);
278
+ #endif
201
279
  if (speakerCodec_ && speakerOpen_) {
202
280
  esp_codec_dev_close(speakerCodec_);
203
- i2sChannelEnabled_ = false;
204
281
  }
205
- if (kPaPin != GPIO_NUM_NC) gpio_set_level(kPaPin, 0);
282
+ setAmplifierEnabled(false);
206
283
  speakerOpen_ = false;
207
284
  speakerSampleRate_ = 0;
208
285
  speakerChannels_ = 0;
@@ -212,37 +289,41 @@ private:
212
289
  void closeRecorderLocked() {
213
290
  if (recordCodec_ && recordOpen_) {
214
291
  esp_codec_dev_close(recordCodec_);
215
- rxChannelEnabled_ = false;
216
292
  }
217
293
  recordOpen_ = false;
294
+ #if GEA_AUDIO_ECHO_CANCELLATION
295
+ geaAudioAecStop();
296
+ #endif
218
297
  }
219
298
 
220
- esp_err_t disableI2sChannel(i2s_chan_handle_t channel, bool &enabled, const char *label) {
221
- if (!channel || !enabled) return ESP_OK;
299
+ esp_err_t disableI2sChannel(i2s_chan_handle_t channel, const char *label) {
300
+ if (!channel) return ESP_OK;
222
301
  esp_err_t err = i2s_channel_disable(channel);
223
302
  if (err == ESP_ERR_INVALID_STATE) {
224
- ESP_LOGW(kTag, "I2S %s channel was already disabled", label);
225
303
  err = ESP_OK;
226
304
  }
227
305
  if (err != ESP_OK) {
228
306
  ESP_LOGE(kTag, "I2S %s disable failed: %s", label, esp_err_to_name(err));
229
307
  return err;
230
308
  }
231
- enabled = false;
232
309
  return ESP_OK;
233
310
  }
234
311
 
235
312
  void releaseI2s() {
236
- if (txChannel_) {
237
- (void)disableI2sChannel(txChannel_, i2sChannelEnabled_, "TX");
238
- i2s_del_channel(txChannel_);
239
- txChannel_ = nullptr;
240
- }
241
- if (rxChannel_) {
242
- (void)disableI2sChannel(rxChannel_, rxChannelEnabled_, "RX");
243
- i2s_del_channel(rxChannel_);
244
- rxChannel_ = nullptr;
245
- }
313
+ // esp_codec_dev may enable TX implicitly while opening RX for its clock.
314
+ // A cached enabled flag cannot observe that; consult the actual driver by
315
+ // disabling each channel before deletion. Keep a handle if teardown fails.
316
+ const auto release = [&](i2s_chan_handle_t &channel, const char *label) {
317
+ if (!channel || disableI2sChannel(channel, label) != ESP_OK) return;
318
+ const esp_err_t err = i2s_del_channel(channel);
319
+ if (err != ESP_OK) {
320
+ ESP_LOGE(kTag, "I2S %s deletion failed: %s", label, esp_err_to_name(err));
321
+ return;
322
+ }
323
+ channel = nullptr;
324
+ };
325
+ release(txChannel_, "TX");
326
+ release(rxChannel_, "RX");
246
327
  i2sDataIf_ = nullptr;
247
328
  }
248
329
 
@@ -266,12 +347,37 @@ private:
266
347
  i2sSampleRate_ = 0;
267
348
  }
268
349
 
350
+ #if GEA_AUDIO_ECHO_CANCELLATION
351
+ std::atomic<bool> aecSpeakerActive_{false};
352
+
353
+ static bool txSent(i2s_chan_handle_t, i2s_event_data_t *event, void *context) {
354
+ const auto *driver = static_cast<AudioOutputDriver *>(context);
355
+ static const std::array<int16_t, 240> silence{};
356
+ if (driver->aecSpeakerActive_.load(std::memory_order_relaxed)) {
357
+ geaAudioAecReference(static_cast<const int16_t *>(event->dma_buf), event->size / sizeof(int16_t));
358
+ } else {
359
+ geaAudioAecReference(silence.data(), silence.size());
360
+ }
361
+ return false;
362
+ }
363
+
364
+ static bool rxReceived(i2s_chan_handle_t, i2s_event_data_t *event, void *) {
365
+ geaAudioAecReceive(static_cast<const int16_t *>(event->dma_buf), event->size / sizeof(int16_t));
366
+ return false;
367
+ }
368
+ #endif
369
+
269
370
  esp_err_t initI2s(int sampleRate) {
270
371
  if (txChannel_ && i2sDataIf_) {
271
372
  if (i2sSampleRate_ == sampleRate) return ESP_OK;
272
373
  return reconfigureI2sClock(sampleRate);
273
374
  }
274
375
 
376
+ if (txChannel_ || rxChannel_) {
377
+ releaseI2s();
378
+ if (txChannel_ || rxChannel_) return ESP_ERR_INVALID_STATE;
379
+ }
380
+
275
381
  i2s_chan_config_t channelConfig = I2S_CHANNEL_DEFAULT_CONFIG(kI2sPort, I2S_ROLE_MASTER);
276
382
  channelConfig.auto_clear = true;
277
383
  // Match the pala_note reference's DMA buffering (the IDF default 6 x 240
@@ -280,19 +386,24 @@ private:
280
386
  // mic samples (recording plays back "sped up"); on TX any scheduling jitter
281
387
  // starves the codec and the playback clicks/underruns. Six descriptors give
282
388
  // the headroom that makes pala_note's record/playback smooth on this codec.
283
- channelConfig.dma_desc_num = 6;
284
- channelConfig.dma_frame_num = 240;
389
+ channelConfig.dma_desc_num = GEA_AUDIO_DMA_DESCRIPTORS;
390
+ channelConfig.dma_frame_num = GEA_AUDIO_DMA_FRAMES;
285
391
  esp_err_t err = i2s_new_channel(&channelConfig, &txChannel_, &rxChannel_);
286
392
  if (err != ESP_OK) {
287
393
  ESP_LOGE(kTag, "I2S channel creation failed: %s", esp_err_to_name(err));
288
394
  return err;
289
395
  }
290
396
 
291
- // esp_codec_dev_open() reconfigures each channel's slots to Philips I2S (the format the ES8311 and
292
- // ES7210 use) before any data moves, so this initial slot format is only a placeholder.
397
+ // Start with the codec's actual 16-bit stereo slots. A 32-bit placeholder
398
+ // doubles DMA storage and forces allocation again during codec open.
293
399
  i2s_std_config_t stdConfig = {};
294
400
  stdConfig.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(sampleRate));
295
- stdConfig.slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_STEREO);
401
+ stdConfig.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO);
402
+ if constexpr (kHardwareAecReference) {
403
+ // Four 16-bit ADC slots share a 64-clock frame with two 32-bit DAC slots.
404
+ stdConfig.slot_cfg.slot_bit_width = I2S_SLOT_BIT_WIDTH_32BIT;
405
+ stdConfig.slot_cfg.ws_width = 32;
406
+ }
296
407
  stdConfig.gpio_cfg.mclk = kMclkPin;
297
408
  stdConfig.gpio_cfg.bclk = kBclkPin;
298
409
  stdConfig.gpio_cfg.ws = kWsPin;
@@ -309,27 +420,55 @@ private:
309
420
  return err;
310
421
  }
311
422
 
423
+ #if GEA_AUDIO_ECHO_CANCELLATION
424
+ i2s_event_callbacks_t txCallbacks{};
425
+ txCallbacks.on_sent = txSent;
426
+ err = kHardwareAecReference ? ESP_OK : i2s_channel_register_event_callback(txChannel_, &txCallbacks, this);
427
+ if (err != ESP_OK) { releaseI2s(); return err; }
428
+ #endif
312
429
  err = i2s_channel_enable(txChannel_);
313
430
  if (err != ESP_OK) {
314
431
  ESP_LOGE(kTag, "I2S enable failed: %s", esp_err_to_name(err));
315
432
  releaseI2s();
316
433
  return err;
317
434
  }
318
- i2sChannelEnabled_ = true;
319
435
 
320
- err = i2s_channel_init_std_mode(rxChannel_, &stdConfig);
436
+ if constexpr (kHardwareAecReference) {
437
+ i2s_tdm_config_t rxConfig{};
438
+ rxConfig.clk_cfg = I2S_TDM_CLK_DEFAULT_CONFIG(static_cast<uint32_t>(sampleRate));
439
+ rxConfig.slot_cfg = I2S_TDM_PHILIPS_SLOT_DEFAULT_CONFIG(
440
+ I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO,
441
+ static_cast<i2s_tdm_slot_mask_t>(I2S_TDM_SLOT0 | I2S_TDM_SLOT1));
442
+ // ES7210 TDM order is MIC1, MIC3, MIC2, MIC4. Capture only the first
443
+ // microphone and hardware reference; unselected slots consume no DMA RAM.
444
+ rxConfig.slot_cfg.total_slot = 4;
445
+ rxConfig.slot_cfg.ws_width = 32;
446
+ rxConfig.gpio_cfg.mclk = kMclkPin;
447
+ rxConfig.gpio_cfg.bclk = kBclkPin;
448
+ rxConfig.gpio_cfg.ws = kWsPin;
449
+ rxConfig.gpio_cfg.dout = I2S_GPIO_UNUSED;
450
+ rxConfig.gpio_cfg.din = kDinPin;
451
+ err = i2s_channel_init_tdm_mode(rxChannel_, &rxConfig);
452
+ } else {
453
+ err = i2s_channel_init_std_mode(rxChannel_, &stdConfig);
454
+ }
321
455
  if (err != ESP_OK) {
322
456
  ESP_LOGE(kTag, "I2S RX std init failed: %s", esp_err_to_name(err));
323
457
  releaseI2s();
324
458
  return err;
325
459
  }
460
+ #if GEA_AUDIO_ECHO_CANCELLATION
461
+ i2s_event_callbacks_t rxCallbacks{};
462
+ rxCallbacks.on_recv = rxReceived;
463
+ err = i2s_channel_register_event_callback(rxChannel_, &rxCallbacks, this);
464
+ if (err != ESP_OK) { releaseI2s(); return err; }
465
+ #endif
326
466
  err = i2s_channel_enable(rxChannel_);
327
467
  if (err != ESP_OK) {
328
468
  ESP_LOGE(kTag, "I2S RX enable failed: %s", esp_err_to_name(err));
329
469
  releaseI2s();
330
470
  return err;
331
471
  }
332
- rxChannelEnabled_ = true;
333
472
 
334
473
  audio_codec_i2s_cfg_t i2sConfig = {};
335
474
  i2sConfig.port = kCodecDataPort;
@@ -349,9 +488,9 @@ private:
349
488
  esp_err_t reconfigureI2sClock(int sampleRate) {
350
489
  if (!txChannel_ || !rxChannel_) return ESP_ERR_INVALID_STATE;
351
490
 
352
- esp_err_t err = disableI2sChannel(txChannel_, i2sChannelEnabled_, "TX");
491
+ esp_err_t err = disableI2sChannel(txChannel_, "TX");
353
492
  if (err != ESP_OK) return err;
354
- err = disableI2sChannel(rxChannel_, rxChannelEnabled_, "RX");
493
+ err = disableI2sChannel(rxChannel_, "RX");
355
494
  if (err != ESP_OK) return err;
356
495
 
357
496
  i2s_std_clk_config_t clockConfig = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(sampleRate));
@@ -360,7 +499,12 @@ private:
360
499
  ESP_LOGE(kTag, "I2S TX clock reconfig failed: %s", esp_err_to_name(err));
361
500
  return err;
362
501
  }
363
- err = i2s_channel_reconfig_std_clock(rxChannel_, &clockConfig);
502
+ if constexpr (kHardwareAecReference) {
503
+ i2s_tdm_clk_config_t rxClock = I2S_TDM_CLK_DEFAULT_CONFIG(static_cast<uint32_t>(sampleRate));
504
+ err = i2s_channel_reconfig_tdm_clock(rxChannel_, &rxClock);
505
+ } else {
506
+ err = i2s_channel_reconfig_std_clock(rxChannel_, &clockConfig);
507
+ }
364
508
  if (err != ESP_OK) {
365
509
  ESP_LOGE(kTag, "I2S RX clock reconfig failed: %s", esp_err_to_name(err));
366
510
  return err;
@@ -371,13 +515,11 @@ private:
371
515
  ESP_LOGE(kTag, "I2S TX re-enable failed: %s", esp_err_to_name(err));
372
516
  return err;
373
517
  }
374
- i2sChannelEnabled_ = true;
375
518
  err = i2s_channel_enable(rxChannel_);
376
519
  if (err != ESP_OK) {
377
520
  ESP_LOGE(kTag, "I2S RX re-enable failed: %s", esp_err_to_name(err));
378
521
  return err;
379
522
  }
380
- rxChannelEnabled_ = true;
381
523
  i2sSampleRate_ = sampleRate;
382
524
  return ESP_OK;
383
525
  }
@@ -393,7 +535,25 @@ private:
393
535
  }
394
536
  if (recordOpen_) closeRecorderLocked();
395
537
  if constexpr (!GEA_AUDIO_FULL_DUPLEX) {
396
- if (speakerOpen_) closeSpeakerLocked();
538
+ if (speakerOpen_) {
539
+ // A successful write only queues PCM in TX DMA. Reclaiming I2S for
540
+ // capture immediately can truncate the final speech samples. Writing
541
+ // one complete DMA ring of silence waits for every earlier descriptor
542
+ // to finish; this follows the actual driver capacity, not a sleep.
543
+ i2s_chan_info_t info{};
544
+ esp_err_t drain = i2s_channel_get_info(txChannel_, &info);
545
+ if (drain != ESP_OK) return drain;
546
+ static constexpr std::array<std::uint8_t, 256> silence{};
547
+ std::size_t remaining = info.total_dma_buf_size;
548
+ while (remaining) {
549
+ const auto bytes = std::min(remaining, silence.size());
550
+ drain = esp_codec_dev_write(speakerCodec_, const_cast<std::uint8_t*>(silence.data()), bytes);
551
+ if (drain != ESP_OK) return drain;
552
+ remaining -= bytes;
553
+ }
554
+ ESP_LOGI(kTag, "TX DMA drained before microphone: %u bytes", static_cast<unsigned>(info.total_dma_buf_size));
555
+ closeSpeakerLocked();
556
+ }
397
557
  }
398
558
 
399
559
  esp_err_t err = initCodecDevice(sampleRate, ESP_CODEC_DEV_TYPE_IN, recordCodec_);
@@ -401,7 +561,9 @@ private:
401
561
 
402
562
  esp_codec_dev_sample_info_t sampleInfo = {};
403
563
  sampleInfo.bits_per_sample = 16;
404
- sampleInfo.channel = 2;
564
+ sampleInfo.channel = kHardwareAecReference ? 4 : 2;
565
+ if constexpr (kHardwareAecReference) sampleInfo.channel_mask =
566
+ ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0) | ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1);
405
567
  sampleInfo.sample_rate = static_cast<std::uint32_t>(sampleRate);
406
568
  sampleInfo.mclk_multiple = I2S_MCLK_MULTIPLE_256;
407
569
  err = esp_codec_dev_open(recordCodec_, &sampleInfo);
@@ -409,8 +571,28 @@ private:
409
571
  ESP_LOGE(kTag, "Recorder open failed: %s", esp_err_to_name(err));
410
572
  return err;
411
573
  }
412
- esp_codec_dev_set_in_gain(recordCodec_, 45.0f);
413
574
  recordOpen_ = true;
575
+ // Keep ADC headroom for loud speaker echo. No digital makeup gain follows
576
+ // AEC; calibrate the physical microphone independently of the loopback.
577
+ // Playback peaks clipped the ADC at 24 dB, producing nonlinear echo that
578
+ // AEC cannot subtract; keep the microphone below that clipping setting.
579
+ esp_codec_dev_set_in_gain(recordCodec_, GEA_AUDIO_ECHO_CANCELLATION ? 12.0f : 45.0f);
580
+ if constexpr (kHardwareAecReference) {
581
+ // MIC1 needs sufficient near-end sensitivity at unity AEC output. An
582
+ // 18 dB PGA setting adds 6 dB over the former headroom-only setting,
583
+ // below the previously clipping 24 dB setting. MIC3 stays independent.
584
+ err = esp_codec_dev_set_in_channel_gain(recordCodec_, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0), 18.0f);
585
+ if (err != ESP_OK) { closeRecorderLocked(); return err; }
586
+ // Codec gain masks use physical MIC numbering, not TDM slot numbering.
587
+ // The board attenuates the analog loopback before MIC3. At 0 dB its
588
+ // measured reference was only 12–114 RMS against 257–1573 RMS echo;
589
+ // use the codec's nominal 30 dB input gain for this attenuated line.
590
+ err = esp_codec_dev_set_in_channel_gain(recordCodec_, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(2), 30.0f);
591
+ if (err != ESP_OK) { closeRecorderLocked(); return err; }
592
+ }
593
+ #if GEA_AUDIO_ECHO_CANCELLATION
594
+ if (!geaAudioAecStart(kHardwareAecReference)) { closeRecorderLocked(); return ESP_ERR_NO_MEM; }
595
+ #endif
414
596
  i2sSampleRate_ = sampleRate;
415
597
  return ESP_OK;
416
598
  }
@@ -425,8 +607,9 @@ private:
425
607
  paConfig.pin_bit_mask = 1ULL << kPaPin;
426
608
  paConfig.mode = GPIO_MODE_OUTPUT;
427
609
  gpio_config(&paConfig);
428
- gpio_set_level(kPaPin, 1);
429
610
  }
611
+ err = setAmplifierEnabled(speakerVolume_ > 0);
612
+ if (err != ESP_OK) return err;
430
613
 
431
614
  if (!codecIf_) {
432
615
  auto i2cBus = gea::platform::i2c::Bus::primary();
@@ -498,6 +681,7 @@ private:
498
681
  es7210Config.ctrl_if = micAdcCtrlIf_;
499
682
  es7210Config.master_mode = false;
500
683
  es7210Config.mic_selected = ES7210_SEL_MIC1 | ES7210_SEL_MIC2;
684
+ if constexpr (kHardwareAecReference) es7210Config.mic_selected |= ES7210_SEL_MIC3 | ES7210_SEL_MIC4;
501
685
  es7210Config.mclk_src = ES7210_MCLK_FROM_PAD;
502
686
  es7210Config.mclk_div = 256;
503
687
  micAdcIf_ = es7210_codec_new(&es7210Config);
@@ -520,8 +704,6 @@ private:
520
704
  const audio_codec_if_t *micAdcIf_ = nullptr;
521
705
  esp_codec_dev_handle_t speakerCodec_ = nullptr;
522
706
  esp_codec_dev_handle_t recordCodec_ = nullptr;
523
- bool i2sChannelEnabled_ = false;
524
- bool rxChannelEnabled_ = false;
525
707
  bool speakerOpen_ = false;
526
708
  bool recordOpen_ = false;
527
709
  int i2sSampleRate_ = 0;
@@ -538,7 +720,9 @@ private:
538
720
  std::vector<gea::host::NativeMediaTrackHandle> attachedTracks_;
539
721
  std::vector<gea::host::NativeMediaTrackHandle> captureSnapshot_;
540
722
  std::mutex attachedMutex_;
541
- static constexpr std::size_t kCaptureFrameSamples = GEA_AUDIO_FULL_DUPLEX ? 320 : 2048;
723
+ // Match the 20 ms Opus input frame in either duplex mode. A 2048-sample
724
+ // half-duplex read batches 128 ms of speech before the encoder can see it.
725
+ static constexpr std::size_t kCaptureFrameSamples = 320;
542
726
  std::array<std::int16_t, kCaptureFrameSamples * 2> stereoFrame_{};
543
727
  std::array<std::int16_t, kCaptureFrameSamples> monoFrame_{};
544
728
 
@@ -577,11 +761,36 @@ private:
577
761
  if (!speakerOpen_) releaseAudioLocked();
578
762
  }
579
763
  }
764
+ #if GEA_AUDIO_ECHO_CANCELLATION
765
+ vTaskDeleteWithCaps(nullptr);
766
+ #else
580
767
  vTaskDelete(nullptr);
768
+ #endif
581
769
  return;
582
770
  }
583
771
 
584
772
  int err = ESP_OK;
773
+ #if GEA_AUDIO_ECHO_CANCELLATION
774
+ {
775
+ // A running RX uses DMA callbacks, so TX's blocking write never owns
776
+ // the microphone read path. Take both locks only for configuration.
777
+ std::unique_lock<std::mutex> captureLock(readMutex_);
778
+ if (!recordOpen_) {
779
+ captureLock.unlock();
780
+ std::lock_guard<std::mutex> configLock(writeMutex_);
781
+ captureLock.lock();
782
+ err = initRecorder(16000);
783
+ }
784
+ if (err == ESP_OK) {
785
+ bool ready = false;
786
+ for (int retry = 0; retry < 500 && !ready; ++retry) {
787
+ ready = geaAudioAecRead(monoFrame_.data(), kCaptureFrameSamples);
788
+ if (!ready) vTaskDelay(1);
789
+ }
790
+ err = ready ? ESP_OK : ESP_ERR_TIMEOUT;
791
+ }
792
+ }
793
+ #else
585
794
  {
586
795
  std::unique_lock<std::mutex> lock(writeMutex_);
587
796
  std::lock_guard<std::mutex> captureLock(readMutex_);
@@ -591,6 +800,7 @@ private:
591
800
  err = esp_codec_dev_read(recordCodec_, stereoFrame_.data(), stereoFrame_.size() * sizeof(std::int16_t));
592
801
  }
593
802
  }
803
+ #endif
594
804
  if (err != ESP_OK) {
595
805
  if (consecutiveReadErrors == 0 || consecutiveReadErrors % 100 == 0) {
596
806
  ESP_LOGW(kTag, "Mic read failed err=%s snapshot_tracks=%u", esp_err_to_name(err), static_cast<unsigned>(captureSnapshot_.size()));
@@ -602,13 +812,20 @@ private:
602
812
  consecutiveReadErrors = 0;
603
813
  const std::size_t framesRead = kCaptureFrameSamples;
604
814
 
815
+ #if !GEA_AUDIO_ECHO_CANCELLATION
605
816
  // ES8311 input is interleaved as [mic, ref]. For local voice notes, keep
606
817
  // the real mic channel only; mixing in the ref slot makes speech metallic.
607
818
  for (std::size_t i = 0; i < framesRead; ++i) {
608
819
  monoFrame_[i] = stereoFrame_[2 * i];
609
820
  }
821
+ #endif
822
+ const int16_t *captured = monoFrame_.data();
823
+ size_t capturedCount = framesRead;
824
+ #if GEA_AUDIO_ECHO_CANCELLATION
825
+ capturedCount = geaAudioAecProcess(captured, framesRead, &captured);
826
+ #endif
610
827
  for (auto handle : captureSnapshot_) {
611
- gea::host::media::track_inject_pcm(handle, monoFrame_.data(), framesRead);
828
+ if (capturedCount) gea::host::media::track_inject_pcm(handle, captured, capturedCount);
612
829
  }
613
830
  }
614
831
  }
@@ -619,7 +836,22 @@ public:
619
836
  attachedTracks_.push_back(handle);
620
837
  // Checked and set under the lock the capture task clears it with, so only one capture task runs.
621
838
  if (captureTask_ == nullptr) {
839
+ #if GEA_AUDIO_ECHO_CANCELLATION
840
+ // Capture DSP and speaker feeding share the highest audio priority.
841
+ // Rendering may share this core at priority 23: using that priority here
842
+ // time-sliced DSP with rendering and missed the 32 ms frame deadline.
843
+ const auto created = xTaskCreatePinnedToCoreWithCaps(
844
+ captureTaskTrampoline, "gea_mic", 8192, this,
845
+ configMAX_PRIORITIES - 1, &captureTask_, 0,
846
+ MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
847
+ if (created != pdPASS) {
848
+ attachedTracks_.pop_back();
849
+ ESP_LOGE(kTag, "Microphone task allocation failed");
850
+ throw std::runtime_error("Microphone task allocation failed");
851
+ }
852
+ #else
622
853
  xTaskCreatePinnedToCore(captureTaskTrampoline, "gea_mic", 8192, this, 5, &captureTask_, 1);
854
+ #endif
623
855
  }
624
856
  }
625
857
 
@@ -631,6 +863,10 @@ public:
631
863
 
632
864
  } // namespace gea::platform::esp32::chip_bindings::es8311
633
865
 
866
+ double gea::platform::audio::OutputDriver::outputLatency(int sampleRate) {
867
+ return sampleRate > 0 ? double(GEA_AUDIO_DMA_DESCRIPTORS * GEA_AUDIO_DMA_FRAMES) / sampleRate : 0;
868
+ }
869
+
634
870
  bool gea::platform::audio::OutputDriver::open(int sampleRate, int channels, int bitsPerSample) {
635
871
  return gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().open(sampleRate, channels, bitsPerSample);
636
872
  }
@@ -642,6 +878,9 @@ bool gea::platform::audio::OutputDriver::write(const std::int16_t *pcm, std::siz
642
878
  void gea::platform::audio::OutputDriver::close() {
643
879
  gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().close();
644
880
  }
881
+ bool gea::platform::audio::OutputDriver::flush() {
882
+ return gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().flush();
883
+ }
645
884
 
646
885
  int gea::platform::audio::OutputDriver::volume() {
647
886
  return gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().volume();