@geastack/targets 0.1.87 → 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 (80) 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 +265 -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 +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 +8 -1
  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-waveshare-touch-lcd-4b.json +504 -0
  80. 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,13 @@ 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;
71
107
 
72
108
  class AudioOutputDriver {
73
109
  public:
@@ -84,6 +120,9 @@ public:
84
120
  std::lock_guard<std::mutex> captureLock(readMutex_);
85
121
  const gea::chips::es8311::OutputFormat format(sampleRate, channels, bitsPerSample);
86
122
  if (!format.isPcm16()) return false;
123
+ #if GEA_AUDIO_ECHO_CANCELLATION
124
+ if (sampleRate != 16000 || (channels != 1 && channels != 2)) return false;
125
+ #endif
87
126
 
88
127
  if (speakerOpen_ &&
89
128
  speakerSampleRate_ == sampleRate &&
@@ -119,6 +158,9 @@ public:
119
158
 
120
159
  applySpeakerVolumeLocked();
121
160
  speakerOpen_ = true;
161
+ #if GEA_AUDIO_ECHO_CANCELLATION
162
+ aecSpeakerActive_.store(true);
163
+ #endif
122
164
  speakerSampleRate_ = sampleRate;
123
165
  speakerChannels_ = channels;
124
166
  speakerBitsPerSample_ = bitsPerSample;
@@ -171,6 +213,29 @@ public:
171
213
  int volume() const {
172
214
  return speakerVolume_;
173
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
+ }
174
239
 
175
240
  void setVolume(int volumePercent) {
176
241
  if (volumePercent < 0) volumePercent = 0;
@@ -199,10 +264,7 @@ private:
199
264
 
200
265
  void applySpeakerVolumeLocked() {
201
266
  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);
267
+ setAmplifierEnabled(speakerVolume_ > 0);
206
268
  const int codecVolume = codecVolumeFromUserPercent(speakerVolume_);
207
269
  const int err = esp_codec_dev_set_out_vol(speakerCodec_, codecVolume);
208
270
  if (err != ESP_CODEC_DEV_OK) {
@@ -211,14 +273,13 @@ private:
211
273
  }
212
274
 
213
275
  void closeSpeakerLocked() {
276
+ #if GEA_AUDIO_ECHO_CANCELLATION
277
+ aecSpeakerActive_.store(false);
278
+ #endif
214
279
  if (speakerCodec_ && speakerOpen_) {
215
280
  esp_codec_dev_close(speakerCodec_);
216
- i2sChannelEnabled_ = false;
217
281
  }
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);
282
+ setAmplifierEnabled(false);
222
283
  speakerOpen_ = false;
223
284
  speakerSampleRate_ = 0;
224
285
  speakerChannels_ = 0;
@@ -228,37 +289,41 @@ private:
228
289
  void closeRecorderLocked() {
229
290
  if (recordCodec_ && recordOpen_) {
230
291
  esp_codec_dev_close(recordCodec_);
231
- rxChannelEnabled_ = false;
232
292
  }
233
293
  recordOpen_ = false;
294
+ #if GEA_AUDIO_ECHO_CANCELLATION
295
+ geaAudioAecStop();
296
+ #endif
234
297
  }
235
298
 
236
- esp_err_t disableI2sChannel(i2s_chan_handle_t channel, bool &enabled, const char *label) {
237
- 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;
238
301
  esp_err_t err = i2s_channel_disable(channel);
239
302
  if (err == ESP_ERR_INVALID_STATE) {
240
- ESP_LOGW(kTag, "I2S %s channel was already disabled", label);
241
303
  err = ESP_OK;
242
304
  }
243
305
  if (err != ESP_OK) {
244
306
  ESP_LOGE(kTag, "I2S %s disable failed: %s", label, esp_err_to_name(err));
245
307
  return err;
246
308
  }
247
- enabled = false;
248
309
  return ESP_OK;
249
310
  }
250
311
 
251
312
  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
- }
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");
262
327
  i2sDataIf_ = nullptr;
263
328
  }
264
329
 
@@ -282,12 +347,37 @@ private:
282
347
  i2sSampleRate_ = 0;
283
348
  }
284
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
+
285
370
  esp_err_t initI2s(int sampleRate) {
286
371
  if (txChannel_ && i2sDataIf_) {
287
372
  if (i2sSampleRate_ == sampleRate) return ESP_OK;
288
373
  return reconfigureI2sClock(sampleRate);
289
374
  }
290
375
 
376
+ if (txChannel_ || rxChannel_) {
377
+ releaseI2s();
378
+ if (txChannel_ || rxChannel_) return ESP_ERR_INVALID_STATE;
379
+ }
380
+
291
381
  i2s_chan_config_t channelConfig = I2S_CHANNEL_DEFAULT_CONFIG(kI2sPort, I2S_ROLE_MASTER);
292
382
  channelConfig.auto_clear = true;
293
383
  // Match the pala_note reference's DMA buffering (the IDF default 6 x 240
@@ -309,6 +399,11 @@ private:
309
399
  i2s_std_config_t stdConfig = {};
310
400
  stdConfig.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(sampleRate));
311
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
+ }
312
407
  stdConfig.gpio_cfg.mclk = kMclkPin;
313
408
  stdConfig.gpio_cfg.bclk = kBclkPin;
314
409
  stdConfig.gpio_cfg.ws = kWsPin;
@@ -325,27 +420,55 @@ private:
325
420
  return err;
326
421
  }
327
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
328
429
  err = i2s_channel_enable(txChannel_);
329
430
  if (err != ESP_OK) {
330
431
  ESP_LOGE(kTag, "I2S enable failed: %s", esp_err_to_name(err));
331
432
  releaseI2s();
332
433
  return err;
333
434
  }
334
- i2sChannelEnabled_ = true;
335
435
 
336
- 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
+ }
337
455
  if (err != ESP_OK) {
338
456
  ESP_LOGE(kTag, "I2S RX std init failed: %s", esp_err_to_name(err));
339
457
  releaseI2s();
340
458
  return err;
341
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
342
466
  err = i2s_channel_enable(rxChannel_);
343
467
  if (err != ESP_OK) {
344
468
  ESP_LOGE(kTag, "I2S RX enable failed: %s", esp_err_to_name(err));
345
469
  releaseI2s();
346
470
  return err;
347
471
  }
348
- rxChannelEnabled_ = true;
349
472
 
350
473
  audio_codec_i2s_cfg_t i2sConfig = {};
351
474
  i2sConfig.port = kCodecDataPort;
@@ -365,9 +488,9 @@ private:
365
488
  esp_err_t reconfigureI2sClock(int sampleRate) {
366
489
  if (!txChannel_ || !rxChannel_) return ESP_ERR_INVALID_STATE;
367
490
 
368
- esp_err_t err = disableI2sChannel(txChannel_, i2sChannelEnabled_, "TX");
491
+ esp_err_t err = disableI2sChannel(txChannel_, "TX");
369
492
  if (err != ESP_OK) return err;
370
- err = disableI2sChannel(rxChannel_, rxChannelEnabled_, "RX");
493
+ err = disableI2sChannel(rxChannel_, "RX");
371
494
  if (err != ESP_OK) return err;
372
495
 
373
496
  i2s_std_clk_config_t clockConfig = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(sampleRate));
@@ -376,7 +499,12 @@ private:
376
499
  ESP_LOGE(kTag, "I2S TX clock reconfig failed: %s", esp_err_to_name(err));
377
500
  return err;
378
501
  }
379
- 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
+ }
380
508
  if (err != ESP_OK) {
381
509
  ESP_LOGE(kTag, "I2S RX clock reconfig failed: %s", esp_err_to_name(err));
382
510
  return err;
@@ -387,13 +515,11 @@ private:
387
515
  ESP_LOGE(kTag, "I2S TX re-enable failed: %s", esp_err_to_name(err));
388
516
  return err;
389
517
  }
390
- i2sChannelEnabled_ = true;
391
518
  err = i2s_channel_enable(rxChannel_);
392
519
  if (err != ESP_OK) {
393
520
  ESP_LOGE(kTag, "I2S RX re-enable failed: %s", esp_err_to_name(err));
394
521
  return err;
395
522
  }
396
- rxChannelEnabled_ = true;
397
523
  i2sSampleRate_ = sampleRate;
398
524
  return ESP_OK;
399
525
  }
@@ -409,7 +535,25 @@ private:
409
535
  }
410
536
  if (recordOpen_) closeRecorderLocked();
411
537
  if constexpr (!GEA_AUDIO_FULL_DUPLEX) {
412
- 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
+ }
413
557
  }
414
558
 
415
559
  esp_err_t err = initCodecDevice(sampleRate, ESP_CODEC_DEV_TYPE_IN, recordCodec_);
@@ -417,7 +561,9 @@ private:
417
561
 
418
562
  esp_codec_dev_sample_info_t sampleInfo = {};
419
563
  sampleInfo.bits_per_sample = 16;
420
- 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);
421
567
  sampleInfo.sample_rate = static_cast<std::uint32_t>(sampleRate);
422
568
  sampleInfo.mclk_multiple = I2S_MCLK_MULTIPLE_256;
423
569
  err = esp_codec_dev_open(recordCodec_, &sampleInfo);
@@ -425,8 +571,28 @@ private:
425
571
  ESP_LOGE(kTag, "Recorder open failed: %s", esp_err_to_name(err));
426
572
  return err;
427
573
  }
428
- esp_codec_dev_set_in_gain(recordCodec_, 45.0f);
429
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
430
596
  i2sSampleRate_ = sampleRate;
431
597
  return ESP_OK;
432
598
  }
@@ -441,12 +607,10 @@ private:
441
607
  paConfig.pin_bit_mask = 1ULL << kPaPin;
442
608
  paConfig.mode = GPIO_MODE_OUTPUT;
443
609
  gpio_config(&paConfig);
444
- gpio_set_level(kPaPin, 1);
445
610
  }
611
+ err = setAmplifierEnabled(speakerVolume_ > 0);
612
+ if (err != ESP_OK) return err;
446
613
 
447
- #if GEA_BOARD_SPEAKER_POWER
448
- if (!gea::platform::board::setSpeakerPower(speakerVolume_ > 0)) return ESP_FAIL;
449
- #endif
450
614
  if (!codecIf_) {
451
615
  auto i2cBus = gea::platform::i2c::Bus::primary();
452
616
  if (!i2cBus.available()) {
@@ -517,6 +681,7 @@ private:
517
681
  es7210Config.ctrl_if = micAdcCtrlIf_;
518
682
  es7210Config.master_mode = false;
519
683
  es7210Config.mic_selected = ES7210_SEL_MIC1 | ES7210_SEL_MIC2;
684
+ if constexpr (kHardwareAecReference) es7210Config.mic_selected |= ES7210_SEL_MIC3 | ES7210_SEL_MIC4;
520
685
  es7210Config.mclk_src = ES7210_MCLK_FROM_PAD;
521
686
  es7210Config.mclk_div = 256;
522
687
  micAdcIf_ = es7210_codec_new(&es7210Config);
@@ -539,8 +704,6 @@ private:
539
704
  const audio_codec_if_t *micAdcIf_ = nullptr;
540
705
  esp_codec_dev_handle_t speakerCodec_ = nullptr;
541
706
  esp_codec_dev_handle_t recordCodec_ = nullptr;
542
- bool i2sChannelEnabled_ = false;
543
- bool rxChannelEnabled_ = false;
544
707
  bool speakerOpen_ = false;
545
708
  bool recordOpen_ = false;
546
709
  int i2sSampleRate_ = 0;
@@ -557,7 +720,9 @@ private:
557
720
  std::vector<gea::host::NativeMediaTrackHandle> attachedTracks_;
558
721
  std::vector<gea::host::NativeMediaTrackHandle> captureSnapshot_;
559
722
  std::mutex attachedMutex_;
560
- 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;
561
726
  std::array<std::int16_t, kCaptureFrameSamples * 2> stereoFrame_{};
562
727
  std::array<std::int16_t, kCaptureFrameSamples> monoFrame_{};
563
728
 
@@ -596,11 +761,36 @@ private:
596
761
  if (!speakerOpen_) releaseAudioLocked();
597
762
  }
598
763
  }
764
+ #if GEA_AUDIO_ECHO_CANCELLATION
765
+ vTaskDeleteWithCaps(nullptr);
766
+ #else
599
767
  vTaskDelete(nullptr);
768
+ #endif
600
769
  return;
601
770
  }
602
771
 
603
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
604
794
  {
605
795
  std::unique_lock<std::mutex> lock(writeMutex_);
606
796
  std::lock_guard<std::mutex> captureLock(readMutex_);
@@ -610,6 +800,7 @@ private:
610
800
  err = esp_codec_dev_read(recordCodec_, stereoFrame_.data(), stereoFrame_.size() * sizeof(std::int16_t));
611
801
  }
612
802
  }
803
+ #endif
613
804
  if (err != ESP_OK) {
614
805
  if (consecutiveReadErrors == 0 || consecutiveReadErrors % 100 == 0) {
615
806
  ESP_LOGW(kTag, "Mic read failed err=%s snapshot_tracks=%u", esp_err_to_name(err), static_cast<unsigned>(captureSnapshot_.size()));
@@ -621,13 +812,20 @@ private:
621
812
  consecutiveReadErrors = 0;
622
813
  const std::size_t framesRead = kCaptureFrameSamples;
623
814
 
815
+ #if !GEA_AUDIO_ECHO_CANCELLATION
624
816
  // ES8311 input is interleaved as [mic, ref]. For local voice notes, keep
625
817
  // the real mic channel only; mixing in the ref slot makes speech metallic.
626
818
  for (std::size_t i = 0; i < framesRead; ++i) {
627
819
  monoFrame_[i] = stereoFrame_[2 * i];
628
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
629
827
  for (auto handle : captureSnapshot_) {
630
- gea::host::media::track_inject_pcm(handle, monoFrame_.data(), framesRead);
828
+ if (capturedCount) gea::host::media::track_inject_pcm(handle, captured, capturedCount);
631
829
  }
632
830
  }
633
831
  }
@@ -638,7 +836,22 @@ public:
638
836
  attachedTracks_.push_back(handle);
639
837
  // Checked and set under the lock the capture task clears it with, so only one capture task runs.
640
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
641
853
  xTaskCreatePinnedToCore(captureTaskTrampoline, "gea_mic", 8192, this, 5, &captureTask_, 1);
854
+ #endif
642
855
  }
643
856
  }
644
857
 
@@ -650,6 +863,10 @@ public:
650
863
 
651
864
  } // namespace gea::platform::esp32::chip_bindings::es8311
652
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
+
653
870
  bool gea::platform::audio::OutputDriver::open(int sampleRate, int channels, int bitsPerSample) {
654
871
  return gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().open(sampleRate, channels, bitsPerSample);
655
872
  }
@@ -661,6 +878,9 @@ bool gea::platform::audio::OutputDriver::write(const std::int16_t *pcm, std::siz
661
878
  void gea::platform::audio::OutputDriver::close() {
662
879
  gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().close();
663
880
  }
881
+ bool gea::platform::audio::OutputDriver::flush() {
882
+ return gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().flush();
883
+ }
664
884
 
665
885
  int gea::platform::audio::OutputDriver::volume() {
666
886
  return gea::platform::esp32::chip_bindings::es8311::AudioOutputDriver::instance().volume();