-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNovaAI.ino
More file actions
440 lines (358 loc) · 9.67 KB
/
Copy pathNovaAI.ino
File metadata and controls
440 lines (358 loc) · 9.67 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include <Adafruit_NeoPixel.h>
#include <WiFi.h>
#include <WebSocketsClient.h>
#include <driver/i2s.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
// NETWORK
const char* ssid = "your_wifi_name";
const char* password = "your_wifi_password";
const char* websocket_server = "192.168.29.232";
const uint16_t websocket_port = 8000;
WebSocketsClient webSocket;
// MIC (INMP441)
#define MIC_WS 5
#define MIC_SCK 18
#define MIC_SD 32
// SPEAKER (MAX98357A)
#define SPK_LRC 19
#define SPK_BCLK 21
#define SPK_DIN 22
#define BUFFER_SAMPLES 128
// RING LED (WS2812B 12 LED Ring)
#define LED_PIN 4
#define NUM_LEDS 12
//IR
#define IR_PIN 25
IRsend irsend(IR_PIN);
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
int16_t audio_buffer[BUFFER_SAMPLES];
// STATE
volatile bool nova_is_talking = false;
unsigned long last_speaker_time = 0;
// JITTER BUFFER
#define RING_BUFFER_SIZE (32 * 1024)
uint8_t ring_buffer[RING_BUFFER_SIZE];
volatile size_t ring_head = 0;
volatile size_t ring_tail = 0;
volatile size_t ring_count = 0;
SemaphoreHandle_t ring_mutex;
#define PREBUFFER_BYTES (6000)
volatile bool speaker_primed = false;
enum NovaState {
BOOTING,
LISTENING,
THINKING,
SPEAKING,
SLEEPING
};
NovaState novaState = BOOTING;
volatile bool is_sleeping = false;
void ring_buffer_write(const uint8_t* data, size_t len) {
if (xSemaphoreTake(ring_mutex, portMAX_DELAY) == pdTRUE) {
for (size_t i = 0; i < len; i++) {
if (ring_count >= RING_BUFFER_SIZE) {
ring_tail = (ring_tail + 1) % RING_BUFFER_SIZE;
ring_count--;
}
ring_buffer[ring_head] = data[i];
ring_head = (ring_head + 1) % RING_BUFFER_SIZE;
ring_count++;
}
xSemaphoreGive(ring_mutex);
}
}
size_t ring_buffer_read(uint8_t* out, size_t max_len) {
size_t read_len = 0;
if (xSemaphoreTake(ring_mutex, portMAX_DELAY) == pdTRUE) {
read_len = (max_len < ring_count) ? max_len : ring_count;
for (size_t i = 0; i < read_len; i++) {
out[i] = ring_buffer[ring_tail];
ring_tail = (ring_tail + 1) % RING_BUFFER_SIZE;
}
ring_count -= read_len;
xSemaphoreGive(ring_mutex);
}
return read_len;
}
size_t ring_buffer_available() {
size_t avail = 0;
if (xSemaphoreTake(ring_mutex, portMAX_DELAY) == pdTRUE) {
avail = ring_count;
xSemaphoreGive(ring_mutex);
}
return avail;
}
void ring_buffer_clear() {
if (xSemaphoreTake(ring_mutex, portMAX_DELAY) == pdTRUE) {
ring_head = 0;
ring_tail = 0;
ring_count = 0;
xSemaphoreGive(ring_mutex);
}
speaker_primed = false;
}
// SPEAKER TASK
void speakerTask(void* param) {
uint8_t play_chunk[1024];
for (;;) {
if (!speaker_primed) {
if (ring_buffer_available() >= PREBUFFER_BYTES) {
speaker_primed = true;
} else {
vTaskDelay(pdMS_TO_TICKS(5));
continue;
}
}
size_t got = ring_buffer_read(play_chunk, sizeof(play_chunk));
if (got > 0) {
nova_is_talking = true;
last_speaker_time = millis();
size_t bytes_written;
i2s_write(I2S_NUM_1, play_chunk, got, &bytes_written, portMAX_DELAY);
} else {
memset(play_chunk, 0, sizeof(play_chunk));
size_t bytes_written;
i2s_write(I2S_NUM_1, play_chunk, sizeof(play_chunk), &bytes_written, portMAX_DELAY);
speaker_primed = false;
}
}
}
// I2S SETUP
void setup_i2s() {
// ----- MIC -----
i2s_config_t mic_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 16000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 8,
.dma_buf_len = 128,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0
};
i2s_pin_config_t mic_pins = {
.mck_io_num = I2S_PIN_NO_CHANGE,
.bck_io_num = MIC_SCK,
.ws_io_num = MIC_WS,
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = MIC_SD
};
i2s_driver_install(I2S_NUM_0, &mic_config, 0, NULL);
i2s_set_pin(I2S_NUM_0, &mic_pins);
// ----- SPEAKER -----
i2s_config_t spk_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = 24000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 16,
.dma_buf_len = 1024,
.use_apll = false,
.tx_desc_auto_clear = true,
.fixed_mclk = 0
};
i2s_pin_config_t spk_pins = {
.mck_io_num = I2S_PIN_NO_CHANGE,
.bck_io_num = SPK_BCLK,
.ws_io_num = SPK_LRC,
.data_out_num = SPK_DIN,
.data_in_num = I2S_PIN_NO_CHANGE
};
i2s_driver_install(I2S_NUM_1, &spk_config, 0, NULL);
i2s_set_pin(I2S_NUM_1, &spk_pins);
}
// WEBSOCKET EVENTS
void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) {
switch(type) {
case WStype_CONNECTED:
Serial.println("[+] Successfully Linked to Python Brain!");
novaState = LISTENING;
nova_is_talking = false;
ring_buffer_clear();
break;
case WStype_DISCONNECTED:
Serial.println("[-] Disconnected from Brain.");
nova_is_talking = false;
novaState = BOOTING;
ring_buffer_clear();
break;
case WStype_TEXT: {
String msg = String((char*)payload).substring(0, length);
Serial.print("[TEXT CMD] ");
Serial.println(msg);
if (msg == "CLEAR_AUDIO") {
Serial.println("[i] Barge-in: clearing speaker buffer.");
ring_buffer_clear();
}
else if (msg == "SLEEP") {
Serial.println("[i] AI commanded sleep.");
novaState = SLEEPING;
is_sleeping = true;
webSocket.disconnect();
}
// RGB LIGHT CONTROLS
else if (msg == "RGB_ON") {
irsend.sendNEC(0xF740BF);
Serial.println("RGB ON");
}
else if (msg == "RGB_OFF") {
irsend.sendNEC(0xF7C03F);
Serial.println("RGB OFF");
}
else if (msg == "RGB_RED") {
irsend.sendNEC(0xF720DF);
Serial.println("RGB RED");
}
else if (msg == "RGB_GREEN") {
irsend.sendNEC(0xF7A05F);
Serial.println("RGB GREEN");
}
else if (msg == "RGB_BLUE") {
irsend.sendNEC(0xF7609F);
Serial.println("RGB BLUE");
}
break;
}
case WStype_BIN:
if (length > 0) {
novaState = SPEAKING;
ring_buffer_write(payload, length);
}
break;
}
}
// LED UPDATE LOGIC
void updateLEDs() {
static int brightness = 20;
static int direction = 5;
switch(novaState) {
case LISTENING:
// Solid Green
for(int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(0, 255, 0));
}
break;
case THINKING:
// Solid Yellow
for(int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(255, 255, 0));
}
break;
case SPEAKING:
brightness += direction;
if(brightness >= 200) {
brightness = 200;
direction = -5;
}
if(brightness <= 20) {
brightness = 20;
direction = 5;
}
for(int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(brightness / 2, 0, brightness));
}
break;
case SLEEPING:
strip.clear();
break;
default:
for(int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 255));
}
break;
}
strip.show();
}
// SETUP
void setup() {
strip.begin();
strip.setBrightness(40);
for(int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 255));
}
strip.show();
Serial.begin(115200);
Serial.println("\n>>> Booting Nova Core...");
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n[+] Wi-Fi Connected!");
novaState = LISTENING;
setup_i2s();
irsend.begin();
Serial.println("[+] Audio Hardware Initialized");
ring_mutex = xSemaphoreCreateMutex();
xTaskCreatePinnedToCore(
speakerTask,
"SpeakerTask",
4096,
NULL,
2,
NULL,
1
);
webSocket.begin(websocket_server, websocket_port, "/esp32");
webSocket.onEvent(webSocketEvent);
webSocket.setReconnectInterval(5000);
}
// LOOP WITH SMART NOISE GATE
void loop() {
updateLEDs();
if (is_sleeping) {
delay(100);
return;
}
webSocket.loop();
if (nova_is_talking && (millis() - last_speaker_time > 500)) {
nova_is_talking = false;
novaState = LISTENING;
}
size_t bytes_read = 0;
i2s_read(
I2S_NUM_0,
audio_buffer,
sizeof(audio_buffer),
&bytes_read,
portMAX_DELAY
);
if (webSocket.isConnected() && bytes_read > 0) {
int16_t peak_amplitude = 0;
int sample_count = bytes_read / 2;
for (int i = 0; i < sample_count; i++) {
int16_t abs_sample = abs(audio_buffer[i]);
if (abs_sample > peak_amplitude) {
peak_amplitude = abs_sample;
}
}
const int16_t THRESHOLD_IDLE = 600;
const int16_t THRESHOLD_TALKING = 7000;
bool should_send_audio = false;
if (nova_is_talking) {
if (peak_amplitude > THRESHOLD_TALKING) {
should_send_audio = true;
}
} else {
if (peak_amplitude > THRESHOLD_IDLE) {
should_send_audio = true;
}
}
if (should_send_audio) {
if(!nova_is_talking) {
novaState = THINKING;
}
webSocket.sendBIN(
(uint8_t*)audio_buffer,
bytes_read
);
}
}
}