@geastack/targets 0.1.108 → 0.1.110
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.
- package/build-config.json +57 -1
- package/package.json +5 -5
- package/targets/esp32/chip_bindings/audio/ns4168.cpp +326 -0
- package/targets/esp32/chip_bindings/displays/rgb_panel.cpp +2485 -105
- package/targets/esp32/chip_bindings/expanders/elecrow_board_control.cpp +169 -0
- package/targets/esp32/chip_bindings/expanders/io_expander.h +4 -0
- package/targets/esp32/chip_bindings/rtc/bm8563.cpp +129 -0
- package/targets/esp32/services/device_control.cpp +70 -6
- package/targets/esp32/services/heap.cpp +25 -1
- package/targets/esp32-s3-elecrow-crowpanel-advance-7.0.json +182 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/CMakeLists.txt +3 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/CMakeLists.txt +31 -2
- package/targets/esp32-s3-touch-amoled-2.06/main/app_main.cpp +11 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/sdcard_mount.cpp +56 -0
- package/targets.json +10 -0
package/build-config.json
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"compiler.staticCssTapeMinChunk",
|
|
10
10
|
"compiler.translationUnits",
|
|
11
11
|
"compiler.storeRelowering",
|
|
12
|
+
"compiler.heapMiB",
|
|
12
13
|
"compiler.moduleGraph",
|
|
13
14
|
"compiler.allowAny",
|
|
14
15
|
"compiler.numberPrecision",
|
|
@@ -44,6 +45,7 @@
|
|
|
44
45
|
"compiler.staticCssTapeMinChunk",
|
|
45
46
|
"compiler.translationUnits",
|
|
46
47
|
"compiler.storeRelowering",
|
|
48
|
+
"compiler.heapMiB",
|
|
47
49
|
"compiler.moduleGraph",
|
|
48
50
|
"compiler.allowAny",
|
|
49
51
|
"renderer.bandedUi"
|
|
@@ -73,6 +75,7 @@
|
|
|
73
75
|
"compiler.staticCssTapeMinChunk",
|
|
74
76
|
"compiler.translationUnits",
|
|
75
77
|
"compiler.storeRelowering",
|
|
78
|
+
"compiler.heapMiB",
|
|
76
79
|
"compiler.moduleGraph",
|
|
77
80
|
"compiler.allowAny"
|
|
78
81
|
],
|
|
@@ -95,8 +98,11 @@
|
|
|
95
98
|
"compiler.staticCssTapeMinChunk",
|
|
96
99
|
"compiler.translationUnits",
|
|
97
100
|
"compiler.storeRelowering",
|
|
101
|
+
"compiler.heapMiB",
|
|
98
102
|
"compiler.moduleGraph",
|
|
99
|
-
"compiler.allowAny"
|
|
103
|
+
"compiler.allowAny",
|
|
104
|
+
"display.width",
|
|
105
|
+
"display.height"
|
|
100
106
|
],
|
|
101
107
|
"defaults": {
|
|
102
108
|
"compiler": {
|
|
@@ -117,6 +123,7 @@
|
|
|
117
123
|
"compiler.staticCssTapeMinChunk",
|
|
118
124
|
"compiler.translationUnits",
|
|
119
125
|
"compiler.storeRelowering",
|
|
126
|
+
"compiler.heapMiB",
|
|
120
127
|
"compiler.moduleGraph",
|
|
121
128
|
"compiler.allowAny",
|
|
122
129
|
"native.jobs",
|
|
@@ -163,6 +170,7 @@
|
|
|
163
170
|
"compiler.staticCssTapeMinChunk",
|
|
164
171
|
"compiler.translationUnits",
|
|
165
172
|
"compiler.storeRelowering",
|
|
173
|
+
"compiler.heapMiB",
|
|
166
174
|
"compiler.moduleGraph",
|
|
167
175
|
"compiler.allowAny"
|
|
168
176
|
],
|
|
@@ -185,6 +193,7 @@
|
|
|
185
193
|
"compiler.staticCssTapeMinChunk",
|
|
186
194
|
"compiler.translationUnits",
|
|
187
195
|
"compiler.storeRelowering",
|
|
196
|
+
"compiler.heapMiB",
|
|
188
197
|
"compiler.moduleGraph",
|
|
189
198
|
"compiler.allowAny"
|
|
190
199
|
],
|
|
@@ -207,6 +216,7 @@
|
|
|
207
216
|
"compiler.staticCssTapeMinChunk",
|
|
208
217
|
"compiler.translationUnits",
|
|
209
218
|
"compiler.storeRelowering",
|
|
219
|
+
"compiler.heapMiB",
|
|
210
220
|
"compiler.moduleGraph",
|
|
211
221
|
"compiler.allowAny"
|
|
212
222
|
],
|
|
@@ -229,6 +239,7 @@
|
|
|
229
239
|
"compiler.staticCssTapeMinChunk",
|
|
230
240
|
"compiler.translationUnits",
|
|
231
241
|
"compiler.storeRelowering",
|
|
242
|
+
"compiler.heapMiB",
|
|
232
243
|
"compiler.moduleGraph",
|
|
233
244
|
"compiler.allowAny",
|
|
234
245
|
"ui.styleStorage"
|
|
@@ -251,6 +262,31 @@
|
|
|
251
262
|
"inference": {
|
|
252
263
|
"canvasOnly": true
|
|
253
264
|
}
|
|
265
|
+
},
|
|
266
|
+
"tvos": {
|
|
267
|
+
"supported": [
|
|
268
|
+
"compiler.staticCssRules",
|
|
269
|
+
"compiler.staticCssTape",
|
|
270
|
+
"compiler.staticCssTapeMinChunk",
|
|
271
|
+
"compiler.translationUnits",
|
|
272
|
+
"compiler.storeRelowering",
|
|
273
|
+
"compiler.heapMiB",
|
|
274
|
+
"compiler.moduleGraph",
|
|
275
|
+
"compiler.allowAny",
|
|
276
|
+
"display.width",
|
|
277
|
+
"display.height"
|
|
278
|
+
],
|
|
279
|
+
"defaults": {
|
|
280
|
+
"compiler": {
|
|
281
|
+
"staticCssRules": true,
|
|
282
|
+
"staticCssTape": true,
|
|
283
|
+
"staticCssTapeMinChunk": 5,
|
|
284
|
+
"translationUnits": "default",
|
|
285
|
+
"storeRelowering": true,
|
|
286
|
+
"moduleGraph": "auto",
|
|
287
|
+
"allowAny": false
|
|
288
|
+
}
|
|
289
|
+
}
|
|
254
290
|
}
|
|
255
291
|
},
|
|
256
292
|
"boards": {
|
|
@@ -732,6 +768,26 @@
|
|
|
732
768
|
"inference": {
|
|
733
769
|
"canvasOnly": true
|
|
734
770
|
}
|
|
771
|
+
},
|
|
772
|
+
"esp32-s3-elecrow-crowpanel-advance-7.0": {
|
|
773
|
+
"supported": [
|
|
774
|
+
"compiler.generatedCodeOptimization",
|
|
775
|
+
"services.wifi",
|
|
776
|
+
"renderer.fuseReplayFlush"
|
|
777
|
+
],
|
|
778
|
+
"defaults": {
|
|
779
|
+
"compiler": {
|
|
780
|
+
"generatedCodeOptimization": "O2",
|
|
781
|
+
"numberPrecision": "float32"
|
|
782
|
+
},
|
|
783
|
+
"services": {
|
|
784
|
+
"wifi": "auto"
|
|
785
|
+
},
|
|
786
|
+
"renderer": {
|
|
787
|
+
"fuseReplayFlush": false
|
|
788
|
+
}
|
|
789
|
+
},
|
|
790
|
+
"cssDevicePixelRatio": 1.5
|
|
735
791
|
}
|
|
736
792
|
}
|
|
737
793
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geastack/targets",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.110",
|
|
4
4
|
"description": "Native embedded build targets (ESP32-IDF, RP2350) for geastack apps: board sources, target catalog, and CMake glue driven by the gea CLI.",
|
|
5
5
|
"license": "GPL-3.0-only",
|
|
6
6
|
"repository": {
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@geastack/chips": "^0.1.7",
|
|
40
40
|
"@geastack/compiler": "^1.0.41",
|
|
41
|
-
"@geastack/core": "0.1.
|
|
41
|
+
"@geastack/core": "0.1.53",
|
|
42
42
|
"@geastack/elements": "^0.1.6",
|
|
43
|
-
"@geastack/engine": "^0.1.
|
|
43
|
+
"@geastack/engine": "^0.1.23",
|
|
44
44
|
"@geastack/geaos": "^0.1.3",
|
|
45
|
-
"@geastack/geatsc-plugin-gea": "0.1.
|
|
46
|
-
"@geastack/host": "0.1.
|
|
45
|
+
"@geastack/geatsc-plugin-gea": "0.1.28",
|
|
46
|
+
"@geastack/host": "0.1.23"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"test": "for f in $(git ls-files | grep -E '(^|/)(test[-_][^/]*|[^/]*\\.test)\\.mjs$'); do echo \"==> $f\"; node \"$f\" || exit 1; done"
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
// NS4168 I2S class-D amplifier, plus an optional separate I2S MEMS microphone
|
|
2
|
+
// (INMP441-style: 24-bit left-justified samples in a 32-bit left slot).
|
|
3
|
+
//
|
|
4
|
+
// There is no codec to configure: the S3 is the I2S master and streams PCM to
|
|
5
|
+
// the amplifier, which decodes it itself. So the speaker path is one TX
|
|
6
|
+
// channel, the volume is applied to the samples in software, and the only
|
|
7
|
+
// control lines are the amplifier's shutdown and mute, which a board may route
|
|
8
|
+
// through its I/O expander (board::audio.expanderShutdown/expanderMute: high
|
|
9
|
+
// shuts down / mutes, as on the Elecrow CrowPanel Advance). The microphone,
|
|
10
|
+
// when the board has one, runs on the other I2S controller and its own pins, so
|
|
11
|
+
// capture and playback never contend for a clock. It is a standard-I2S MEMS
|
|
12
|
+
// part or a PDM one (board::audio.micFormat); the S3 receives PDM only on I2S0,
|
|
13
|
+
// so the speaker always takes I2S1. Format 2 asks the board controller, for
|
|
14
|
+
// boards whose revisions differ only in the microphone (CrowPanel V1.0 has an
|
|
15
|
+
// INMP441, V1.3+ a PDM part).
|
|
16
|
+
|
|
17
|
+
#define GEA_AUDIO_DRIVER_INTERNAL 1
|
|
18
|
+
#include "audio.h"
|
|
19
|
+
#include "board.h"
|
|
20
|
+
#if GEA_BOARD_HAS_EXPANDER
|
|
21
|
+
#include "chip_bindings/expanders/io_expander.h"
|
|
22
|
+
#endif
|
|
23
|
+
|
|
24
|
+
#include <algorithm>
|
|
25
|
+
#include <array>
|
|
26
|
+
#include <cstddef>
|
|
27
|
+
#include <cstdint>
|
|
28
|
+
#include <mutex>
|
|
29
|
+
#include <string_view>
|
|
30
|
+
#include <vector>
|
|
31
|
+
|
|
32
|
+
#include "driver/i2s_pdm.h"
|
|
33
|
+
#include "driver/i2s_std.h"
|
|
34
|
+
#include "esp_heap_caps.h"
|
|
35
|
+
#include "esp_log.h"
|
|
36
|
+
#include "freertos/FreeRTOS.h"
|
|
37
|
+
#include "freertos/task.h"
|
|
38
|
+
|
|
39
|
+
#include "host/media.h"
|
|
40
|
+
|
|
41
|
+
#ifndef GEA_AUDIO_DMA_DESCRIPTORS
|
|
42
|
+
#define GEA_AUDIO_DMA_DESCRIPTORS 6
|
|
43
|
+
#endif
|
|
44
|
+
#ifndef GEA_AUDIO_DMA_FRAMES
|
|
45
|
+
#define GEA_AUDIO_DMA_FRAMES 240
|
|
46
|
+
#endif
|
|
47
|
+
static_assert(GEA_AUDIO_DMA_DESCRIPTORS >= 2);
|
|
48
|
+
static_assert(GEA_AUDIO_DMA_FRAMES >= 8 && GEA_AUDIO_DMA_FRAMES <= 511);
|
|
49
|
+
|
|
50
|
+
namespace gea::platform::esp32::chip_bindings::ns4168 {
|
|
51
|
+
namespace {
|
|
52
|
+
|
|
53
|
+
constexpr char kTag[] = "ns4168";
|
|
54
|
+
constexpr auto &kAudio = gea::platform::board::audio;
|
|
55
|
+
constexpr bool kHasMicrophone = kAudio.micBclk != GPIO_NUM_NC && kAudio.micDin != GPIO_NUM_NC;
|
|
56
|
+
constexpr i2s_port_t kSpeakerPort = I2S_NUM_1;
|
|
57
|
+
constexpr i2s_port_t kMicPort = I2S_NUM_0;
|
|
58
|
+
// The INMP441's 24 significant bits sit at the top of the 32-bit slot; keeping
|
|
59
|
+
// bits 29..14 gives ~12 dB of headroom over the raw top 16 bits, which is where
|
|
60
|
+
// speech from arm's length lands for this part.
|
|
61
|
+
constexpr int kMicShift = 14;
|
|
62
|
+
constexpr std::size_t kMicFrames = 256;
|
|
63
|
+
|
|
64
|
+
bool micIsPdm() {
|
|
65
|
+
if (kAudio.micFormat == 1) return true;
|
|
66
|
+
if (kAudio.micFormat == 0) return false;
|
|
67
|
+
#if GEA_BOARD_HAS_EXPANDER
|
|
68
|
+
auto &expander = gea::platform::esp32::chip_bindings::expanders::ioExpander();
|
|
69
|
+
expander.init();
|
|
70
|
+
return std::string_view(gea::platform::esp32::chip_bindings::expanders::ioExpanderDriverName()) == "elecrow-stc8h";
|
|
71
|
+
#else
|
|
72
|
+
return false;
|
|
73
|
+
#endif
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
bool setExpanderLine(int pin, bool high) {
|
|
77
|
+
#if GEA_BOARD_HAS_EXPANDER
|
|
78
|
+
if (pin < 0) return true;
|
|
79
|
+
return gea::platform::esp32::chip_bindings::expanders::ioExpander().writePin(pin, high);
|
|
80
|
+
#else
|
|
81
|
+
(void)pin;
|
|
82
|
+
(void)high;
|
|
83
|
+
return true;
|
|
84
|
+
#endif
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
class AudioDriver {
|
|
88
|
+
public:
|
|
89
|
+
static AudioDriver &instance() {
|
|
90
|
+
// Heap-allocated on first use, so an app that never plays sound pays nothing.
|
|
91
|
+
static AudioDriver *const driver = new AudioDriver();
|
|
92
|
+
return *driver;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
bool open(int sampleRate, int channels, int bitsPerSample) {
|
|
96
|
+
std::lock_guard<std::mutex> lock(writeMutex_);
|
|
97
|
+
if (bitsPerSample != 16 || (channels != 1 && channels != 2) || sampleRate < 8000 || sampleRate > 48000) return false;
|
|
98
|
+
if (tx_ && sampleRate == sampleRate_ && channels == channels_) return true;
|
|
99
|
+
closeLocked();
|
|
100
|
+
|
|
101
|
+
i2s_chan_config_t channelConfig = I2S_CHANNEL_DEFAULT_CONFIG(kSpeakerPort, I2S_ROLE_MASTER);
|
|
102
|
+
channelConfig.auto_clear = true; // an underrun plays silence, not the last buffer again
|
|
103
|
+
channelConfig.dma_desc_num = GEA_AUDIO_DMA_DESCRIPTORS;
|
|
104
|
+
channelConfig.dma_frame_num = GEA_AUDIO_DMA_FRAMES;
|
|
105
|
+
esp_err_t err = i2s_new_channel(&channelConfig, &tx_, nullptr);
|
|
106
|
+
if (err != ESP_OK) {
|
|
107
|
+
ESP_LOGE(kTag, "I2S TX channel: %s", esp_err_to_name(err));
|
|
108
|
+
tx_ = nullptr;
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
// Mono PCM goes out on both slots, so the amplifier plays it whichever
|
|
112
|
+
// channel its CTRL strap selects.
|
|
113
|
+
i2s_std_config_t config{};
|
|
114
|
+
config.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(sampleRate));
|
|
115
|
+
config.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT,
|
|
116
|
+
channels == 1 ? I2S_SLOT_MODE_MONO : I2S_SLOT_MODE_STEREO);
|
|
117
|
+
config.gpio_cfg.mclk = GPIO_NUM_NC;
|
|
118
|
+
config.gpio_cfg.bclk = kAudio.bclk;
|
|
119
|
+
config.gpio_cfg.ws = kAudio.ws;
|
|
120
|
+
config.gpio_cfg.dout = kAudio.dout;
|
|
121
|
+
config.gpio_cfg.din = GPIO_NUM_NC;
|
|
122
|
+
err = i2s_channel_init_std_mode(tx_, &config);
|
|
123
|
+
if (err == ESP_OK) err = i2s_channel_enable(tx_);
|
|
124
|
+
if (err != ESP_OK) {
|
|
125
|
+
ESP_LOGE(kTag, "I2S TX init: %s", esp_err_to_name(err));
|
|
126
|
+
i2s_del_channel(tx_);
|
|
127
|
+
tx_ = nullptr;
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
setExpanderLine(kAudio.expanderMute, false);
|
|
131
|
+
setExpanderLine(kAudio.expanderShutdown, false);
|
|
132
|
+
sampleRate_ = sampleRate;
|
|
133
|
+
channels_ = channels;
|
|
134
|
+
ESP_LOGI(kTag, "speaker ready sample_rate=%d channels=%d volume=%d", sampleRate, channels, volume_);
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
bool write(const std::int16_t *pcm, std::size_t sampleCount, int timeoutMs) {
|
|
139
|
+
if (!pcm || sampleCount == 0) return false;
|
|
140
|
+
std::lock_guard<std::mutex> lock(writeMutex_);
|
|
141
|
+
if (!tx_) return false;
|
|
142
|
+
const TickType_t timeout = timeoutMs < 0 ? portMAX_DELAY : pdMS_TO_TICKS(timeoutMs);
|
|
143
|
+
const int gain = gainQ15();
|
|
144
|
+
while (sampleCount > 0) {
|
|
145
|
+
const std::size_t count = std::min(sampleCount, scaled_.size());
|
|
146
|
+
for (std::size_t i = 0; i < count; ++i) scaled_[i] = static_cast<std::int16_t>((pcm[i] * gain) >> 15);
|
|
147
|
+
std::size_t written = 0;
|
|
148
|
+
if (i2s_channel_write(tx_, scaled_.data(), count * sizeof(std::int16_t), &written, timeout) != ESP_OK) return false;
|
|
149
|
+
const std::size_t samples = written / sizeof(std::int16_t);
|
|
150
|
+
if (samples == 0) return false;
|
|
151
|
+
pcm += samples;
|
|
152
|
+
sampleCount -= samples;
|
|
153
|
+
}
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
void close() {
|
|
158
|
+
std::lock_guard<std::mutex> lock(writeMutex_);
|
|
159
|
+
closeLocked();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
bool flush() {
|
|
163
|
+
std::lock_guard<std::mutex> lock(writeMutex_);
|
|
164
|
+
if (!tx_) return true;
|
|
165
|
+
// Disabling drops what is queued; re-enabling starts from silence
|
|
166
|
+
// (auto_clear), which is what a seek or stop wants.
|
|
167
|
+
if (i2s_channel_disable(tx_) != ESP_OK) return false;
|
|
168
|
+
return i2s_channel_enable(tx_) == ESP_OK;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
int volume() const { return volume_; }
|
|
172
|
+
void setVolume(int percent) { volume_ = std::clamp(percent, 0, 100); }
|
|
173
|
+
|
|
174
|
+
void attachTrack(gea::host::NativeMediaTrackHandle handle) {
|
|
175
|
+
if constexpr (!kHasMicrophone) {
|
|
176
|
+
(void)handle;
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
std::lock_guard<std::mutex> lock(trackMutex_);
|
|
180
|
+
tracks_.push_back(handle);
|
|
181
|
+
if (captureTask_) return;
|
|
182
|
+
if (xTaskCreatePinnedToCoreWithCaps(captureEntry, "gea_mic", 6144, this, 5, &captureTask_, 1,
|
|
183
|
+
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT) != pdPASS) {
|
|
184
|
+
captureTask_ = nullptr;
|
|
185
|
+
tracks_.pop_back();
|
|
186
|
+
ESP_LOGE(kTag, "microphone task allocation failed");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
void detachTrack(gea::host::NativeMediaTrackHandle handle) {
|
|
191
|
+
std::lock_guard<std::mutex> lock(trackMutex_);
|
|
192
|
+
tracks_.erase(std::remove(tracks_.begin(), tracks_.end(), handle), tracks_.end());
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private:
|
|
196
|
+
AudioDriver() = default;
|
|
197
|
+
|
|
198
|
+
// Perceptual volume: 50% is about -12 dB, 0% is silent.
|
|
199
|
+
int gainQ15() const {
|
|
200
|
+
const int v = volume_;
|
|
201
|
+
return (v * v * 32767) / 10000;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
void closeLocked() {
|
|
205
|
+
if (!tx_) return;
|
|
206
|
+
setExpanderLine(kAudio.expanderShutdown, true);
|
|
207
|
+
i2s_channel_disable(tx_);
|
|
208
|
+
i2s_del_channel(tx_);
|
|
209
|
+
tx_ = nullptr;
|
|
210
|
+
sampleRate_ = 0;
|
|
211
|
+
channels_ = 0;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
static void captureEntry(void *arg) { static_cast<AudioDriver *>(arg)->runCapture(); }
|
|
215
|
+
|
|
216
|
+
void runCapture() {
|
|
217
|
+
i2s_chan_handle_t rx = nullptr;
|
|
218
|
+
i2s_chan_config_t channelConfig = I2S_CHANNEL_DEFAULT_CONFIG(kMicPort, I2S_ROLE_MASTER);
|
|
219
|
+
channelConfig.dma_desc_num = GEA_AUDIO_DMA_DESCRIPTORS;
|
|
220
|
+
channelConfig.dma_frame_num = GEA_AUDIO_DMA_FRAMES;
|
|
221
|
+
const bool pdm = micIsPdm();
|
|
222
|
+
esp_err_t err = i2s_new_channel(&channelConfig, nullptr, &rx);
|
|
223
|
+
if (err == ESP_OK && pdm) {
|
|
224
|
+
// PDM: the S3 converts to 16-bit PCM in hardware.
|
|
225
|
+
i2s_pdm_rx_config_t config{};
|
|
226
|
+
config.clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(gea::platform::audio::deviceSampleRate));
|
|
227
|
+
config.slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO);
|
|
228
|
+
config.gpio_cfg.clk = kAudio.micBclk;
|
|
229
|
+
config.gpio_cfg.din = kAudio.micDin;
|
|
230
|
+
err = i2s_channel_init_pdm_rx_mode(rx, &config);
|
|
231
|
+
if (err == ESP_OK) err = i2s_channel_enable(rx);
|
|
232
|
+
} else if (err == ESP_OK) {
|
|
233
|
+
i2s_std_config_t config{};
|
|
234
|
+
config.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(gea::platform::audio::deviceSampleRate));
|
|
235
|
+
config.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_MONO);
|
|
236
|
+
config.slot_cfg.slot_mask = I2S_STD_SLOT_LEFT; // L/R strapped low
|
|
237
|
+
config.gpio_cfg.mclk = GPIO_NUM_NC;
|
|
238
|
+
config.gpio_cfg.bclk = kAudio.micBclk;
|
|
239
|
+
config.gpio_cfg.ws = kAudio.micWs;
|
|
240
|
+
config.gpio_cfg.dout = GPIO_NUM_NC;
|
|
241
|
+
config.gpio_cfg.din = kAudio.micDin;
|
|
242
|
+
err = i2s_channel_init_std_mode(rx, &config);
|
|
243
|
+
if (err == ESP_OK) err = i2s_channel_enable(rx);
|
|
244
|
+
}
|
|
245
|
+
if (err != ESP_OK) ESP_LOGE(kTag, "microphone I2S init: %s", esp_err_to_name(err));
|
|
246
|
+
else ESP_LOGI(kTag, "microphone ready (%s) sample_rate=%d", pdm ? "PDM" : "I2S", gea::platform::audio::deviceSampleRate);
|
|
247
|
+
|
|
248
|
+
std::array<std::int32_t, kMicFrames> raw{};
|
|
249
|
+
std::array<std::int16_t, kMicFrames> pcm{};
|
|
250
|
+
std::vector<gea::host::NativeMediaTrackHandle> snapshot;
|
|
251
|
+
while (err == ESP_OK) {
|
|
252
|
+
{
|
|
253
|
+
std::lock_guard<std::mutex> lock(trackMutex_);
|
|
254
|
+
if (tracks_.empty()) break;
|
|
255
|
+
snapshot = tracks_;
|
|
256
|
+
}
|
|
257
|
+
std::size_t bytes = 0;
|
|
258
|
+
std::size_t frames = 0;
|
|
259
|
+
if (pdm) {
|
|
260
|
+
if (i2s_channel_read(rx, pcm.data(), sizeof(pcm), &bytes, pdMS_TO_TICKS(200)) != ESP_OK) continue;
|
|
261
|
+
frames = bytes / sizeof(std::int16_t);
|
|
262
|
+
} else {
|
|
263
|
+
if (i2s_channel_read(rx, raw.data(), sizeof(raw), &bytes, pdMS_TO_TICKS(200)) != ESP_OK) continue;
|
|
264
|
+
frames = bytes / sizeof(std::int32_t);
|
|
265
|
+
for (std::size_t i = 0; i < frames; ++i) {
|
|
266
|
+
const std::int32_t sample = raw[i] >> kMicShift;
|
|
267
|
+
pcm[i] = static_cast<std::int16_t>(std::clamp<std::int32_t>(sample, -32768, 32767));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
for (auto handle : snapshot) gea::host::media::track_inject_pcm(handle, pcm.data(), frames);
|
|
271
|
+
}
|
|
272
|
+
if (rx) {
|
|
273
|
+
i2s_channel_disable(rx);
|
|
274
|
+
i2s_del_channel(rx);
|
|
275
|
+
}
|
|
276
|
+
{
|
|
277
|
+
std::lock_guard<std::mutex> lock(trackMutex_);
|
|
278
|
+
captureTask_ = nullptr;
|
|
279
|
+
// A track attached after the loop saw none starts its own task.
|
|
280
|
+
if (!tracks_.empty()) attachTrackRestart();
|
|
281
|
+
}
|
|
282
|
+
vTaskDeleteWithCaps(nullptr);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
void attachTrackRestart() {
|
|
286
|
+
if (xTaskCreatePinnedToCoreWithCaps(captureEntry, "gea_mic", 6144, this, 5, &captureTask_, 1,
|
|
287
|
+
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT) != pdPASS) {
|
|
288
|
+
captureTask_ = nullptr;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
std::mutex writeMutex_;
|
|
293
|
+
i2s_chan_handle_t tx_ = nullptr;
|
|
294
|
+
int sampleRate_ = 0;
|
|
295
|
+
int channels_ = 0;
|
|
296
|
+
int volume_ = 70;
|
|
297
|
+
std::array<std::int16_t, 512> scaled_{};
|
|
298
|
+
|
|
299
|
+
std::mutex trackMutex_;
|
|
300
|
+
std::vector<gea::host::NativeMediaTrackHandle> tracks_;
|
|
301
|
+
TaskHandle_t captureTask_ = nullptr;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
} // namespace
|
|
305
|
+
} // namespace gea::platform::esp32::chip_bindings::ns4168
|
|
306
|
+
|
|
307
|
+
using gea::platform::esp32::chip_bindings::ns4168::AudioDriver;
|
|
308
|
+
|
|
309
|
+
double gea::platform::audio::OutputDriver::outputLatency(int sampleRate) {
|
|
310
|
+
return sampleRate > 0 ? double(GEA_AUDIO_DMA_DESCRIPTORS * GEA_AUDIO_DMA_FRAMES) / sampleRate : 0;
|
|
311
|
+
}
|
|
312
|
+
bool gea::platform::audio::OutputDriver::open(int sampleRate, int channels, int bitsPerSample) {
|
|
313
|
+
return AudioDriver::instance().open(sampleRate, channels, bitsPerSample);
|
|
314
|
+
}
|
|
315
|
+
bool gea::platform::audio::OutputDriver::write(const std::int16_t *pcm, std::size_t sampleCount, int timeoutMs) {
|
|
316
|
+
return AudioDriver::instance().write(pcm, sampleCount, timeoutMs);
|
|
317
|
+
}
|
|
318
|
+
void gea::platform::audio::OutputDriver::close() { AudioDriver::instance().close(); }
|
|
319
|
+
bool gea::platform::audio::OutputDriver::flush() { return AudioDriver::instance().flush(); }
|
|
320
|
+
int gea::platform::audio::OutputDriver::volume() { return AudioDriver::instance().volume(); }
|
|
321
|
+
void gea::platform::audio::OutputDriver::setVolume(int volumePercent) { AudioDriver::instance().setVolume(volumePercent); }
|
|
322
|
+
|
|
323
|
+
namespace gea::host::media {
|
|
324
|
+
void platform_attach_track(NativeMediaTrackHandle handle) { AudioDriver::instance().attachTrack(handle); }
|
|
325
|
+
void platform_detach_track(NativeMediaTrackHandle handle) { AudioDriver::instance().detachTrack(handle); }
|
|
326
|
+
} // namespace gea::host::media
|