-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode for Edge Impluse Training
More file actions
186 lines (157 loc) · 5.82 KB
/
Code for Edge Impluse Training
File metadata and controls
186 lines (157 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* ================================================================
* VAANI NODE — XIAO ESP32S3 Sense
* EDGE IMPULSE DATASET COLLECTOR
* ================================================================
*/
#include <Arduino.h>
#include <driver/i2s.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
// ─── PIN DEFINITIONS ────────────────────────────────────────────
#define MIC_DATA_PIN 41
#define MIC_CLK_PIN 42
#define SD_MOSI 9
#define SD_MISO 8
#define SD_SCK 7
#define SD_CS 21
#define LED_PIN 3
// ─── AUDIO CONFIG ───────────────────────────────────────────────
#define SAMPLE_RATE 16000
#define RECORD_SECONDS 1 // FIXED: Exactly 1 second for Edge Impulse
#define I2S_PORT I2S_NUM_0
#define DMA_BUF_COUNT 8
#define DMA_BUF_LEN 512
#define CHUNK_SAMPLES 1024
// ─── WAV HEADER WRITER ──────────────────────────────────────────
struct wav_header_t {
char riff_tag[4];
uint32_t riff_length;
char wave_tag[4];
char fmt_tag[4];
uint32_t fmt_length;
uint16_t audio_format;
uint16_t num_channels;
uint32_t sample_rate;
uint32_t byte_rate;
uint16_t block_align;
uint16_t bits_per_sample;
char data_tag[4];
uint32_t data_length;
};
void buildWavHeader(wav_header_t* h, uint32_t dataBytes) {
memcpy(h->riff_tag, "RIFF", 4);
h->riff_length = dataBytes + sizeof(wav_header_t) - 8;
memcpy(h->wave_tag, "WAVE", 4);
memcpy(h->fmt_tag, "fmt ", 4);
h->fmt_length = 16;
h->audio_format = 1;
h->num_channels = 1;
h->sample_rate = SAMPLE_RATE;
h->byte_rate = SAMPLE_RATE * 2;
h->block_align = 2;
h->bits_per_sample = 16;
memcpy(h->data_tag, "data", 4);
h->data_length = dataBytes;
}
// ─── I2S (PDM MIC) SETUP ────────────────────────────────────────
void i2s_install() {
const i2s_config_t cfg = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM),
.sample_rate = SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = DMA_BUF_COUNT,
.dma_buf_len = DMA_BUF_LEN,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0
};
i2s_driver_install(I2S_PORT, &cfg, 0, NULL);
}
void i2s_setpin() {
const i2s_pin_config_t pins = {
.bck_io_num = -1,
.ws_io_num = MIC_CLK_PIN,
.data_out_num = -1,
.data_in_num = MIC_DATA_PIN
};
i2s_set_pin(I2S_PORT, &pins);
}
// ─── LED HELPERS ────────────────────────────────────────────────
void ledOn() { digitalWrite(LED_PIN, HIGH); }
void ledOff() { digitalWrite(LED_PIN, LOW); }
void blinkConfirm(int times = 2, int ms = 100) {
for (int i = 0; i < times; i++) {
ledOn(); delay(ms);
ledOff(); delay(ms);
}
}
// ─── CORE RECORDING FUNCTION ────────────────────────────────────
void recordToSD() {
ledOn();
String fileName = "/v_" + String(millis()) + ".wav";
File file = SD.open(fileName, FILE_WRITE);
if (!file) {
Serial.println("[SD] ERROR: Cannot open file!");
ledOff();
return;
}
wav_header_t header;
file.write((uint8_t*)&header, sizeof(wav_header_t));
int16_t wav_buf[CHUNK_SAMPLES];
size_t bytesIn = 0;
uint32_t totalBytes = 0;
uint32_t endTime = millis() + (RECORD_SECONDS * 1000);
// Clear stale DMA buffers before recording
i2s_read(I2S_PORT, wav_buf, sizeof(wav_buf), &bytesIn, 0);
delay(50);
Serial.println("[REC] *** RECORDING NOW ***");
while (millis() < endTime) {
esp_err_t res = i2s_read(I2S_PORT, wav_buf, sizeof(wav_buf), &bytesIn, portMAX_DELAY);
if (res == ESP_OK && bytesIn > 0) {
file.write((uint8_t*)wav_buf, bytesIn);
totalBytes += bytesIn;
}
}
buildWavHeader(&header, totalBytes);
file.seek(0);
file.write((uint8_t*)&header, sizeof(wav_header_t));
file.close();
Serial.printf("[SD] Saved: %s (%u bytes)\n", fileName.c_str(), totalBytes);
ledOff();
blinkConfirm(2);
}
// ─── SETUP ──────────────────────────────────────────────────────
void setup() {
Serial.begin(115200);
delay(500);
Serial.println("\n=== DATASET COLLECTOR STARTING ===");
pinMode(LED_PIN, OUTPUT);
ledOff();
SPI.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS)) {
Serial.println("[SD] FAILED! Check wiring and ensure card is FAT32 formatted.");
while (true) { ledOn(); delay(100); ledOff(); delay(100); }
}
Serial.printf("[SD] OK. Free: %llu MB\n", SD.totalBytes() / (1024 * 1024));
i2s_install();
i2s_setpin();
i2s_start(I2S_PORT);
Serial.println("[MIC] PDM microphone ready.");
}
// ─── MAIN LOOP ──────────────────────────────────────────────────
void loop() {
Serial.println("\n--------------------------------");
Serial.println("Get ready... Say your word in:");
Serial.println("2...");
delay(1000);
Serial.println("1...");
delay(1000);
recordToSD();
Serial.println("Good. Pausing for 2 seconds before next rep.");
delay(2000);
}