-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive.c
More file actions
722 lines (649 loc) · 18.4 KB
/
live.c
File metadata and controls
722 lines (649 loc) · 18.4 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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#include <pocketsphinx.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#ifdef WIN32
#define popen _popen
#define pclose _pclose
#endif
typedef enum
{
END_RUNNING,
NO_SPEECH_FRAMES,
SPEECH_IS_NULL,
STILL_SPEAKING,
ERROR_START_UTTERANCE,
ERROR_END_UTTERANCE,
} UtteranceStatus;
typedef enum
{
RUN_SPEECH_RECOGNITION,
END_PROGRAM,
WRONG_COMMAND
} ProgramStatus;
static void clean_up();
static int init_main();
static void catch_sig(int signum);
static ProgramStatus ask_user_command();
static FILE *popen_sox(int sample_rate);
static int get_speech_frame(const int16 **speech);
static int handle_result_string(const char *result_string);
static UtteranceStatus process_utterance(const char **result_string, _Bool show_partial_result);
int check_if_directory_is_empty(const char *folder);
int check_if_file_exist(char *name, char *folder);
int check_if_directory_exists_parent(char *parent_dir, char *name);
int check_models_files(int *is_run_default_setup, char *model_name);
static void load_modals();
static void init_sox();
static void clear_sox();
static FILE *sox_audio_stream;
static short *audio_frame_buffer;
static size_t audio_frame_buffer_size;
static ps_config_t *speech_config;
static ps_decoder_t *speech_decoder;
static ps_endpointer_t *speech_endpointer;
int main()
{
if (init_main() == 0)
{
return 1;
}
const char *result_string = NULL;
while (1)
{
ProgramStatus status = ask_user_command();
if (status == END_PROGRAM)
{
break;
}
if (status == RUN_SPEECH_RECOGNITION)
{
init_sox();
while (1)
{
UtteranceStatus status = process_utterance(&result_string, 1);
if (status == NO_SPEECH_FRAMES)
{
break;
}
if (status == ERROR_END_UTTERANCE)
{
clear_sox();
break;
}
if (status == ERROR_START_UTTERANCE)
{
clear_sox();
break;
}
if (status == STILL_SPEAKING)
{
continue;
}
if (status == SPEECH_IS_NULL)
{
continue;
}
if (handle_result_string(result_string) == 1)
{
break;
}
}
clear_sox();
}
}
return 0;
}
static UtteranceStatus process_utterance(const char **result_string, _Bool show_partial_result)
{
static _Bool waiting_message_shown = 0;
const int16 *speech;
int prev_in_speech = ps_endpointer_in_speech(speech_endpointer);
if (get_speech_frame(&speech) == 0)
{
printf("⏳ No more speech frames. Exiting...\n");
return NO_SPEECH_FRAMES;
}
if (speech == NULL)
{
if (!waiting_message_shown)
{
printf("⏳ Waiting on your speech...\n");
waiting_message_shown = 1;
}
return SPEECH_IS_NULL;
}
waiting_message_shown = 0;
if (prev_in_speech == 0)
{
printf("\n");
printf("⏳ Start utterance processing...\n");
fprintf(stderr, "⏳ Speech start at %.2f\n", ps_endpointer_speech_start(speech_endpointer));
if (ps_start_utt(speech_decoder) == 0)
{
printf("✅ Successfully started utterance processing.\n");
printf("\n");
}
else
{
printf("❌ Failed to start utterance processing.\n");
return ERROR_START_UTTERANCE;
}
}
int number_of_searched_frames = ps_process_raw(speech_decoder, speech, audio_frame_buffer_size, FALSE, FALSE);
if (number_of_searched_frames < 0)
{
E_FATAL("❌ ps_process_raw() failed\n");
}
const char *hypothesis_string = NULL;
if (show_partial_result)
{
hypothesis_string = ps_get_hyp(speech_decoder, NULL);
if (hypothesis_string != NULL)
{
fprintf(stderr, "✅ Partial result: %s\n", hypothesis_string);
}
}
int current_in_speech = ps_endpointer_in_speech(speech_endpointer);
if (current_in_speech != 0)
{
// printf("⏳ You are speaking now...\n");
return STILL_SPEAKING;
}
printf("\n");
fprintf(stderr, "⏳ Speech end at %.2f\n", ps_endpointer_speech_end(speech_endpointer));
printf("⏳ End utterance processing...\n");
if (ps_end_utt(speech_decoder) == 0)
{
printf("✅ Successfully finished utterance processing.\n");
printf("\n");
}
else
{
printf("❌ Failed to finish utterance processing.\n");
printf("\n");
return ERROR_END_UTTERANCE;
}
(*result_string) = ps_get_hyp(speech_decoder, NULL);
return END_RUNNING;
}
static int handle_result_string(const char *result_string)
{
if (result_string == NULL)
{
printf("❌ Result string after utterance is NULL.\n");
}
if (strlen(result_string) == 0)
{
printf("❌ Result string after utterance is empty.\n");
}
printf("✅ Result string: %s\n", result_string);
if (strcmp(result_string, "browser") == 0)
{
printf("browser command\n");
if (fork() == 0)
{
execlp("firefox", "firefox", "https://www.google.com", NULL);
}
}
if (strcmp(result_string, "browser exit") == 0)
{
printf("browser exit command\n");
system("pkill firefox");
}
return 1;
}
int check_if_directory_is_empty(const char *folder)
{
DIR *dir_stream = opendir(folder);
if (dir_stream == NULL)
{
perror("❌ Cannot open reading dir stream.\n");
return -1;
}
int is_empty_flag = 1;
struct dirent *entry = readdir(dir_stream);
while (entry != NULL)
{
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
{
is_empty_flag = 0;
break;
}
entry = readdir(dir_stream);
}
if (closedir(dir_stream) != 0)
{
perror("❌ Cannot close reading dir stream.\n");
return -1;
}
return is_empty_flag;
}
int check_if_file_exist(char *name, char *folder)
{
DIR *dir_stream = opendir(folder);
if (dir_stream == NULL)
{
perror("❌ Cannot open reading dir stream.\n");
return -1;
}
int is_found_flag = 0;
struct dirent *dp = readdir(dir_stream);
while (dp != NULL)
{
if (strncmp(dp->d_name, name, strlen(name)) == 0)
{
is_found_flag = 1; // OK
break;
}
dp = readdir(dir_stream);
}
if (closedir(dir_stream) != 0)
{
perror("❌ Cannot close reading dir stream.\n");
return -1;
}
return is_found_flag;
}
int check_if_directory_exists_parent(char *parent_dir, char *name)
{
char full_path[1024];
snprintf(full_path, sizeof(full_path), "%s/%s", parent_dir, name);
struct stat path_stat;
if (stat(full_path, &path_stat) != 0)
{
return -1;
}
if (S_ISDIR(path_stat.st_mode))
{
return 1; // OK
}
else
{
return 0;
}
}
int check_models_files(int *is_run_default_setup, char *model_name)
{
int result = 1;
if (strcmp(model_name, "MODEL_UA") != 0 &&
strcmp(model_name, "MODEL_DE") != 0 &&
strcmp(model_name, "an4") != 0 &&
strcmp(model_name, "MODEL_RU") != 0)
{
result = 0;
return result;
}
char full_path[1024];
snprintf(full_path, sizeof(full_path), "./%s", model_name);
if (check_if_directory_exists_parent("./", model_name) != 1)
{
printf("❌ %s folder was not found.\n", model_name);
*is_run_default_setup = 1;
result = 0;
}
else
{
*is_run_default_setup = 0;
if (check_if_directory_is_empty(full_path) == 1)
{
printf("❌ %s: Folder is empty.\n", model_name);
*is_run_default_setup = 1;
result = 0;
return result;
}
if (check_if_directory_exists_parent(full_path, "hmm") != 1)
{
printf("❌ %s: ./hmm folder was not found.\n", model_name);
*is_run_default_setup = 1;
result = 0;
}
else
{
char hmm_full_path[1024];
snprintf(hmm_full_path, sizeof(hmm_full_path), "./%s/hmm", model_name);
if (check_if_directory_is_empty(hmm_full_path) == 1)
{
printf("❌ %s: ./hmm folder is empty.\n", model_name);
*is_run_default_setup = 1;
result = 0;
}
}
if (check_if_file_exist("dictionary.dic", full_path) != 1 && check_if_file_exist("dictionary.dic.bin", full_path) != 1)
{
printf("❌ %s: Dictionary file was not found.\n", model_name);
*is_run_default_setup = 1;
result = 0;
}
if (check_if_file_exist("language_model.lm", full_path) != 1 && check_if_file_exist("language_model.lm.bin", full_path) != 1)
{
printf("❌ %s: Language model file was not found.\n", model_name);
*is_run_default_setup = 1;
result = 0;
}
if (*is_run_default_setup == 1)
{
printf("❌ %s: Skipping...\n", model_name);
result = 0;
}
else
{
printf("⏳ Running %s...\n", model_name);
result = 1;
}
}
return result;
}
static void load_modals()
{
if (speech_config == NULL)
{
return;
}
int is_run_default_setup = 1;
#ifdef an4
printf("\n");
printf("✅ an4 flag is defined.\n");
if (check_models_files(&is_run_default_setup, "an4") == 1)
{
printf("Loading...\n");
ps_config_set_str(speech_config, "hmm", "an4/hmm");
ps_config_set_str(speech_config, "lm", "an4/language_model.lm");
ps_config_set_str(speech_config, "dict", "an4/dictionary.dic");
}
printf("\n");
#endif
#ifdef MODEL_UA
printf("\n");
printf("✅ MODEL_UA flag is defined.\n");
if (check_models_files(&is_run_default_setup, "MODEL_UA") == 1)
{
printf("Loading...\n");
// ps_config_set_str(speech_config, "hmm", "MODEL_UA/hmm");
// ps_config_set_str(speech_config, "lm", "MODEL_UA/language_model.lm");
// ps_config_set_str(speech_config, "dict", "MODEL_UA/dictionary.dic");
}
printf("\n");
#endif
#ifdef MODEL_DE
printf("\n");
printf("✅ MODEL_DE flag is defined.\n");
if (check_models_files(&is_run_default_setup, "MODEL_DE") == 1)
{
printf("Loading...\n");
// ps_config_set_str(speech_config, "hmm", "MODEL_DE/hmm");
// ps_config_set_str(speech_config, "lm", "MODEL_DE/language_model.lm");
// ps_config_set_str(speech_config, "dict", "MODEL_DE/dictionary.dic");
}
printf("\n");
#endif
#ifdef MODEL_RU
printf("\n");
printf("✅ MODEL_RU flag is defined.\n");
if (check_models_files(&is_run_default_setup, "MODEL_RU") == 1)
{
printf("Loading...\n");
// ps_config_set_str(speech_config, "hmm", "MODEL_RU/hmm");
// ps_config_set_str(speech_config, "lm", "MODEL_RU/language_model.lm");
// ps_config_set_str(speech_config, "dict", "MODEL_RU/dictionary.dic");
}
printf("\n");
#endif
if (is_run_default_setup == 1)
{
printf(" ├─ ⏳ Running default acoustic and language models...\n");
ps_default_search_args(speech_config);
}
}
static int init_main()
{
printf("⏳ Initializing main...\n");
atexit(clean_up);
speech_config = ps_config_init(NULL);
if (speech_config == NULL)
{
E_FATAL("❌ PocketSphinx config init failed\n");
}
else
{
printf(" ├─ ✅ PocketSphinx config successfully initialized.\n");
}
load_modals();
speech_decoder = ps_init(speech_config);
if (speech_decoder == NULL)
{
E_FATAL("❌ PocketSphinx decoder init failed\n");
}
else
{
printf(" ├─ ✅ PocketSphinx decoder successfully initialized.\n");
}
if (signal(SIGINT, catch_sig) == SIG_ERR)
{
E_FATAL_SYSTEM("❌ Failed to set SIGINT handler");
}
else
{
printf(" ├─ ✅ Successfully set SIGINT handler.\n");
}
printf(" └─ ✅ Initiation done!\n");
printf("\n");
return 1;
}
static int get_speech_frame(const int16 **speech)
{
size_t end_samples;
// printf("⏳ Reading speech frame...\n");
size_t len = fread(audio_frame_buffer, sizeof(audio_frame_buffer[0]), audio_frame_buffer_size, sox_audio_stream);
// printf("📏 Read %zu samples (expected %zu)\n", len, audio_frame_buffer_size);
if (len != audio_frame_buffer_size)
{
if (len > 0)
{
printf("🔚 Stream ending, processing remaining samples...\n");
(*speech) = ps_endpointer_end_stream(speech_endpointer, audio_frame_buffer, audio_frame_buffer_size, &end_samples);
// printf("✅ Processed %zu end samples\n", end_samples);
}
else
{
printf("❌ No more data to read, exiting...\n");
return 0;
}
}
else
{
// printf("✅ Processing normal speech frame...\n");
(*speech) = ps_endpointer_process(speech_endpointer, audio_frame_buffer);
}
return 1;
}
static void clean_up()
{
printf("\n");
printf("\n⏳ Cleaning up...\n");
if (audio_frame_buffer != NULL)
{
free(audio_frame_buffer);
printf(" ├─ ✅ Frame cleaned up\n");
audio_frame_buffer = NULL;
}
if (sox_audio_stream != NULL)
{
if (pclose(sox_audio_stream) < 0)
{
E_ERROR_SYSTEM("❌ Failed to pclose(sox)");
}
printf(" ├─ ✅ Sox cleaned up\n");
sox_audio_stream = NULL;
}
if (speech_endpointer != NULL)
{
ps_endpointer_free(speech_endpointer);
printf(" ├─ ✅ Endpointer cleaned up\n");
speech_endpointer = NULL;
}
if (speech_decoder != NULL)
{
ps_free(speech_decoder);
printf(" ├─ ✅ Decoder cleaned up\n");
speech_decoder = NULL;
}
if (speech_config != NULL)
{
ps_config_free(speech_config);
printf(" ├─ ✅ Config cleaned up\n");
speech_config = NULL;
}
printf(" └─ ✅ Successfully cleaned up!\n");
}
static FILE *popen_sox(int sample_rate)
{
#define SOXCMD "sox -q -r %d -c 1 -b 16 -e signed-integer -d -t raw -"
int len = snprintf(NULL, 0, SOXCMD, sample_rate);
if (len < 0)
{
E_FATAL_SYSTEM("❌ snprintf() failed to calculate buffer size");
}
else
{
printf(" ├─ ✅ Successfully calculated buffer size: %d\n", len);
}
char *soxcmd = malloc(len + 1);
if (soxcmd == NULL)
{
E_FATAL_SYSTEM("❌ Failed to allocate string");
}
else
{
printf(" ├─ ✅ Successfully allocated sox command string.\n");
}
if (snprintf(soxcmd, len + 1, SOXCMD, sample_rate) != len)
{
E_FATAL_SYSTEM("❌ snprintf() failed: unexpected string length\n");
}
else
{
printf(" ├─ ✅ Successfully formatted soz command: %s\n", soxcmd);
}
FILE *sox = popen(soxcmd, "r");
if (sox == NULL)
{
E_FATAL_SYSTEM("❌ Failed to execute sox command: %s", soxcmd);
}
else
{
printf(" ├─ ✅ Successfully started soz process with command: %s\n", soxcmd);
}
free(soxcmd);
return sox;
}
static ProgramStatus ask_user_command()
{
ProgramStatus status = WRONG_COMMAND;
int command = 1;
int result;
while (1)
{
printf("\n");
printf("🔚 Select 1 for run recognition and 0 to stop the program: \n");
result = scanf("%d", &command);
if (result != 1)
{
while (getchar() != '\n')
{
};
printf("❌ Invalid format! Please enter a number.\n");
}
else if (command != 0 && command != 1)
{
printf("❌ Invalid command! Please enter 1 or 0\n");
}
else
{
break;
}
}
switch (command)
{
case 1:
{
printf("✅ Perfect! Running speech recognition...\n");
status = RUN_SPEECH_RECOGNITION;
break;
}
case 0:
{
printf("✅ Perfect! Stopping program...\n");
status = END_PROGRAM;
break;
}
}
return status;
}
static void catch_sig(int signum)
{
(void)signum;
printf("\n📶 Perfect! Interrupt signal detected.\n");
printf("✅ Stopping program...\n");
exit(0);
}
static void init_sox()
{
double window = 0;
double ratio = 0.0;
ps_vad_mode_t mode = 0;
int sample_rate = 0;
double frame_length = 0;
speech_endpointer = ps_endpointer_init(window, ratio, mode, sample_rate, frame_length);
if (speech_endpointer == NULL)
{
E_FATAL("❌ PocketSphinx endpointer init failed\n");
}
else
{
printf(" ├─ ✅ PocketSphinx endpointer successfully initialized.\n");
}
sox_audio_stream = popen_sox(ps_endpointer_sample_rate(speech_endpointer));
audio_frame_buffer_size = ps_endpointer_frame_size(speech_endpointer);
if ((audio_frame_buffer = malloc(audio_frame_buffer_size * sizeof(audio_frame_buffer[0]))) == NULL)
{
E_FATAL_SYSTEM("❌ Failed to allocate frame");
}
else
{
printf(" ├─ ✅ Successfully allocated frame.\n");
}
printf(" └─ ✅ Initiation done!\n");
printf("\n");
}
static void clear_sox()
{
printf("\n⏳ Cleaning up sox...\n");
if (speech_endpointer != NULL)
{
ps_endpointer_free(speech_endpointer);
printf(" ├─ ✅ Endpointer cleaned up\n");
speech_endpointer = NULL;
}
if (sox_audio_stream != NULL)
{
if (pclose(sox_audio_stream) < 0)
{
E_ERROR_SYSTEM("❌ Failed to pclose(sox)");
}
printf(" ├─ ✅ Sox cleaned up\n");
sox_audio_stream = NULL;
}
if (audio_frame_buffer != NULL)
{
free(audio_frame_buffer);
printf(" ├─ ✅ Frame cleaned up\n");
audio_frame_buffer = NULL;
}
printf(" └─ ✅ Successfully cleaned up!\n");
printf("\n");
}