-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
375 lines (326 loc) · 10.2 KB
/
main.cpp
File metadata and controls
375 lines (326 loc) · 10.2 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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include "kitra_input.h"
#include "kitra_output.h"
#include "kitra_packet_generator.h"
#include "kitra_small_utility.h"
#include "serial.h"
#include <time.h>
#include <errno.h>
#include <string.h>
using namespace std;
serial kitra_serial;
char response_obj[500];
uint32_t packet_size;
uint32_t optional_mask;
static uint32_t tick = 0;
char buffer[1024];
int buffer_len;
void kitra_platform_send(char* buffer, uint32_t length)
{
/*Print through serial*/
kitra_serial.serialPuts(buffer);
k_unlock_tx();
}
std::string wait_for_packet()
{
std::string cmd;
bool begin = false;
while(1)
{
int n = kitra_serial.serialDataAvail();
for(int i=0; i<n;i++)
{
int ris = kitra_serial.serialGetchar();
if(ris >= 0 && ris <= 127)
{
if(cmd.length() > 1000) //something went wrong
cmd.clear();
if(ris == '$')
{
begin = true;
cmd.clear();
}
if(begin == true)
{
cmd+=(char)ris;
}
if(ris == '\n')
{
begin = false;
return cmd;
}
}
}
}
}
#define PACKET_LENGHT 51 /* $KITRA,683,32,################################*cc\r\n */
#define PACKET_LENGHT_S 83 /* $KITRA,683,64,################################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*cc\r\n */
#define MIC_FIX_HEADER 14 /* $KITRA,683,32, */
#define MIC_FIX_DATA 32 /* ################################ */
#define MIC_FIX_DATA_S 64 /* ################################@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
#define MIC_FIX_END 5 /* *cc\r\n */
#define INPUT_DATA_BUFF 256
bool wait_packet_complete(char* outBuff)
{
static uint32_t incoming_data_lenght = 0;
static int start_mic_data = 0;
static bool begin = false;
static char incoming_data[INPUT_DATA_BUFF];
static char* pch;
int data_available = kitra_serial.serialDataAvail();
/* 1. Wait minimum data for decode ID message */
if (data_available > 0)
{
if (incoming_data_lenght >= (INPUT_DATA_BUFF - 1))
{
incoming_data_lenght = 0;
memset(incoming_data, 0, sizeof(incoming_data));
return false;
}
incoming_data[incoming_data_lenght] = kitra_serial.serialGetchar();
incoming_data_lenght++;
}
else
{
return false;
}
/* 2. check if ID is Microfone stream "$KITRA,683,32," */
if (begin == false)
{
#if STEREO == 1
pch = (char*)memmem(incoming_data, sizeof(incoming_data), "$KITRA,683,64,", MIC_FIX_HEADER);
#else
pch = (char*)memmem(incoming_data, sizeof(incoming_data), "$KITRA,683,32,", MIC_FIX_HEADER);
#endif
if (pch != 0)
{
begin = true;
start_mic_data = (pch + MIC_FIX_HEADER) - incoming_data;
}
else
{
return false;
}
}
#if STEREO == 1
if (incoming_data_lenght < (start_mic_data + MIC_FIX_DATA_S + MIC_FIX_END))
{
return false;
}
#else
if (incoming_data_lenght < (start_mic_data + MIC_FIX_DATA + MIC_FIX_END))
{
return false;
}
#endif
/* 3. Extract RAW PCM audio */
memcpy(outBuff, (pch + MIC_FIX_HEADER), MIC_FIX_DATA_S);
/* 4. restart process */
pch = NULL;
memset(incoming_data, 0, sizeof(incoming_data));
incoming_data_lenght = 0;
begin = false;
return true;
}
void wait_ack(const char* str, uint32_t ref_id, bool blocking)
{
while(1)
{
if(k_parse_packet_safe(wait_for_packet().c_str(),(void*)response_obj,&packet_size, &optional_mask) == PARSE_OK)
{
k_output_ack* ack = (k_output_ack*) response_obj;
k_output_nack* nack = (k_output_nack*) response_obj;
if(ack->id == K_OUTPUT_ACK && ack->ref_id == ref_id)
{
printf("%s",str);
break;
}
else if(!blocking || (nack->id == K_OUTPUT_NACK && nack->ref_id == ref_id))
{
printf("ERROR %s",str);
break;
}
}
}
}
void enable_leds()
{
k_input_ldrgb_enable_disable msg1 = {K_INPUT_LDRGB_ENABLE_DISABLE,0,1};
k_send_packet(&msg1,0);
wait_ack("LED ENABLE ACKED\n",K_INPUT_LDRGB_ENABLE_DISABLE,true);
}
void change_baud_rate(uint32_t baud)
{
k_input_change_baud_rate msg1 = {K_INPUT_CHANGE_BAUD_RATE,baud};
k_send_packet(&msg1,0);
}
void leds(uint32_t color)
{
k_input_ldrgb_set msg2 = {K_INPUT_LDRGB_SET,0,color,50,1};
k_send_packet(&msg2,0);
wait_ack("LED SET ACKED\n",K_INPUT_LDRGB_SET,true);
}
void enable_mic_stream()
{
#if STEREO == 1
k_input_mic_enable_disable msg1 = {K_INPUT_MIC_ENABLE_DISABLE,2,0x12,1,2000,0,0}; /* Enable Stream with output Stereo from Mic 3 and 4 */
#else
k_input_mic_enable_disable msg1 = {K_INPUT_MIC_ENABLE_DISABLE,2,0x10,1,2000,0,0};
#endif
k_send_packet(&msg1,0);
}
void enable_source_localization()
{
k_input_mic_enable_disable msg1 = {K_INPUT_MIC_ENABLE_DISABLE,1,0x12,1,2000,0,0}; /* Enable Localization with output stereo from Mic 1 and 2 */
k_send_packet(&msg1,0);
wait_ack("MIC ENABLE ACKED\n",K_INPUT_MIC_ENABLE_DISABLE,false);
}
void disable_mic()
{
k_input_mic_enable_disable msg1 = {K_INPUT_MIC_ENABLE_DISABLE,0,0x12,0,0,0,0};
k_send_packet(&msg1,0);
usleep(20000);
if (kitra_serial.serialDataAvail() > 0)
kitra_serial.serialFlush();
}
void enable_beam_forming(uint8_t mic_1, uint8_t mic_2)
{
uint8_t mic_12 = ((mic_1 & 0x0F) << 4) | (mic_2 & 0x0F);
k_input_mic_enable_disable msg1 = {K_INPUT_MIC_ENABLE_DISABLE,3,mic_12,1,2000,3,0}; /* Enable BeamForming on Mic 1-2 */
k_send_packet(&msg1,0);
wait_ack("MIC ENABLE ACKED\n",K_INPUT_MIC_ENABLE_DISABLE,false);
}
void soft_reset()
{
k_input_kitra_reset msg1 = {K_INPUT_KITRA_RESET,1};
k_send_packet(&msg1,0);
wait_ack("RESET ACKED\n",K_INPUT_KITRA_RESET,true);
}
/* Audio RAM Buffer */
#define FREQ_SAMPLE 16000 /* Hz from MIC */
#define SAMPLE_DURATION 4 /* Seconds */
#define SAMPLE_BIT 16 /* Single sample Bit (8 - 16 - 24 - 32) */
#define FRAME_LENGHT MIC_FIX_DATA /* Number of byte in the received stream */
char audio_RAM_Buff[(SAMPLE_BIT / 8) * FREQ_SAMPLE * SAMPLE_DURATION];
uint32_t audio_frame_count = 0;
int main (int argc, char **argv)
{
char audio_tmp[FRAME_LENGHT + FRAME_LENGHT]; /* Max Stereo */
ofstream audioFile_1;
#if STEREO == 1
ofstream audioFile_2;
#endif
memset(audio_RAM_Buff, 0 , sizeof(audio_RAM_Buff));
memset(audio_tmp, 0 , sizeof(audio_tmp));
if(kitra_serial.serialOpen("/dev/ttySAC3", 115200) != -1)
{
change_baud_rate(1000000);
usleep(50000);
kitra_serial.serialClose();
usleep(80000);
}
if(kitra_serial.serialOpen("/dev/ttySAC3", 1000000) != -1)
{
srand(time(NULL));
printf("SERIAL FOUND\n");
disable_mic();
soft_reset();
enable_leds();
leds(0x000033);
/* Wait 2 sec */
sleep(2);
leds(0x330000);
if (kitra_serial.serialDataAvail() > 0)
{
kitra_serial.serialFlush();
}
enable_mic_stream();
#if ENABLE_BEAMFORMING
enable_beam_forming(0x01, 0x02);
#endif
}
((k_output_mic_sample_notification*)response_obj)->data = (char*) malloc(64);
#if STORE_IN_RAM == 1 /* Store in RAM all sample to reach SAMPLE_DURATION, and write in to the file at the end */
while(1)
{
if (wait_packet_complete(audio_tmp) == true)
{
if (audio_frame_count < ((FREQ_SAMPLE * SAMPLE_DURATION) / (FRAME_LENGHT / 2)))
{
/* Copy new data */
memcpy((char*)&audio_RAM_Buff[audio_frame_count * FRAME_LENGHT], audio_tmp, FRAME_LENGHT);
memset(audio_tmp, 0, sizeof(audio_tmp));
audio_frame_count++;
}
else
{
/* Create and open file */
audioFile.open("pcm_audio_captured.raw", ios::out | ios::binary);
if (audioFile.is_open())
{
/* Save to file */
audioFile.write(audio_RAM_Buff, sizeof(audio_RAM_Buff));
/* Close */
audioFile.close();
/* Stop Mic */
disable_mic();
/* Change LED */
leds(0x220000);
/* End */
return 1;
}
}
}
}
#else /* Little buffer in RAM and write to file each frame received until reach the second SAMPLE_DURATION */
/* Create and open file */
audioFile_1.open("pcm_audio_1.raw", ios::out | ios::binary);
#if STEREO == 1
audioFile_2.open("pcm_audio_2.raw", ios::out | ios::binary);
#endif
printf("START RECORDING\r\n");
while(1)
{
if (wait_packet_complete(audio_tmp) == true)
{
if (audio_frame_count < ((FREQ_SAMPLE * SAMPLE_DURATION) / (FRAME_LENGHT / 2)))
{
/* Write data to file */
audioFile_1.write(audio_tmp, FRAME_LENGHT);
#if STEREO == 1
audioFile_2.write(&audio_tmp[FRAME_LENGHT], FRAME_LENGHT);
#endif
memset(audio_tmp, 0, sizeof(audio_tmp));
audio_frame_count++;
}
else
{
/* Close */
audioFile_1.close();
#if STEREO == 1
audioFile_2.close();
#endif
printf("STOP RECORDING\r\n");
/* Stop Mic */
disable_mic();
/* Wait mic to disable */
usleep(1000);
/* Clear serial trash */
kitra_serial.serialFlush();
/* Change LED */
leds(0x003300);
/* Wait 2 sec */
sleep(2);
leds(0x000000);
/* End */
return 1;
}
}
}
#endif
return 0;
}