forked from kevinwlu/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXIAO-mic.ino
71 lines (52 loc) · 1.36 KB
/
XIAO-mic.ino
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
#include <mic.h>
// Settings
#define DEBUG 1 // Enable pin pulse during ISR
#define SAMPLES 800
mic_config_t mic_config{
.channel_cnt = 1,
.sampling_rate = 16000,
.buf_size = 1600,
//.debug_pin = LED_BUILTIN // Toggles each DAC ISR (if DEBUG is set to 1)
};
NRF52840_ADC_Class Mic(&mic_config);
int16_t recording_buf[SAMPLES];
volatile uint8_t recording = 0;
volatile static bool record_ready = false;
void setup() {
Serial.begin(9600);
while (!Serial) {delay(10);}
Mic.set_callback(audio_rec_callback);
if (!Mic.begin()) {
Serial.println("Mic initialization failed");
while (1);
}
Serial.println("Mic initialization done.");
}
void loop() {
if (record_ready)
{
Serial.println("Finished sampling");
for (int i = 0; i < SAMPLES; i++) {
//int16_t sample = filter.step(recording_buf[i]);
int16_t sample = recording_buf[i];
Serial.println(sample);
}
record_ready = false;
}
}
static void audio_rec_callback(uint16_t *buf, uint32_t buf_len) {
static uint32_t idx = 0;
// Copy samples from DMA buffer to inference buffer
{
for (uint32_t i = 0; i < buf_len; i++) {
// Convert 12-bit unsigned ADC value to 16-bit PCM (signed) audio value
recording_buf[idx++] = buf[i];
if (idx >= SAMPLES){
idx = 0;
recording = 0;
record_ready = true;
break;
}
}
}
}