forked from kevinwlu/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mic_serial_recording.ino
115 lines (88 loc) · 2.32 KB
/
mic_serial_recording.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
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
#include <mic.h>
#if defined(WIO_TERMINAL)
#include "processing/filters.h"
#endif
// Settings
#if defined(WIO_TERMINAL)
#define DEBUG 1 // Enable pin pulse during ISR
#define SAMPLES 16000*3
#elif defined(ARDUINO_ARCH_NRF52840)
#define DEBUG 1 // Enable pin pulse during ISR
#define SAMPLES 1600
#endif
mic_config_t mic_config{
.channel_cnt = 1,
.sampling_rate = 16000,
#if defined(WIO_TERMINAL)
.buf_size = 320,
.debug_pin = 1 // Toggles each DAC ISR (if DEBUG is set to 1)
#elif defined(ARDUINO_ARCH_NRF52840)
.buf_size = 1600,
.debug_pin = LED_BUILTIN // Toggles each DAC ISR (if DEBUG is set to 1)
#endif
};
#if defined(WIO_TERMINAL)
DMA_ADC_Class Mic(&mic_config);
#elif defined(ARDUINO_ARCH_NRF52840)
NRF52840_ADC_Class Mic(&mic_config);
#endif
int16_t recording_buf[SAMPLES];
volatile uint8_t recording = 0;
volatile static bool record_ready = false;
#if defined(WIO_TERMINAL)
FilterBuHp filter;
#endif
void setup() {
Serial.begin(57600);
while (!Serial) {delay(10);}
#if defined(WIO_TERMINAL)
pinMode(WIO_KEY_A, INPUT_PULLUP);
#endif
Mic.set_callback(audio_rec_callback);
if (!Mic.begin()) {
Serial.println("init_fail");
while (1);
}
Serial.println("init_ok");
}
void loop() {
String resp = Serial.readString();
if (resp == "init\n" && !recording){
Serial.println("init_ok");
}
if (resp == "rec\n" && !recording) {
recording = 1;
record_ready = false;
}
if (!recording && record_ready)
{
Serial.println("rec_ok");
for (int i = 0; i < SAMPLES; i++) {
Serial.println(recording_buf[i]);
}
Serial.println("fi");
record_ready = false;
}
}
static void audio_rec_callback(uint16_t *buf, uint32_t buf_len) {
static uint32_t idx = 0;
if (recording) {
for (uint32_t i = 0; i < buf_len; i++) {
#if defined(WIO_TERMINAL)
// Convert 12-bit unsigned ADC value to 16-bit PCM (signed) audio value
recording_buf[idx++] = filter.step((int16_t)(buf[i] - 1024) * 16);
// with filter
//recording_buf[idx++] = (int16_t)(buf[i] - 1024) * 16;
// without filter
#elif defined(ARDUINO_ARCH_NRF52840)
recording_buf[idx++] = buf[i];
#endif
if (idx >= SAMPLES){
idx = 0;
recording = 0;
record_ready = true;
break;
}
}
}
}