Debian Codegraph @

examples/live.c

raw
1 /* Example of simple PocketSphinx speech segmentation.
2 *
3 * MIT license (c) 2022, see LICENSE for more information.
4 *
5 * Author: David Huggins-Daines <[email protected]>
6 */
7 /**
8 * @example live.c
9 * @brief Speech recognition with live audio input and endpointing.
10 *
11 * This file shows how to use PocketSphinx in conjunction with `sox`
12 * to detect and recognize speech from the default audio input device.
13 *
14 * This file shows how to use PocketSphinx to recognize a single input
15 * file. To compile it, assuming you have built the library as in
16 * \ref unix_install "these directions", you can run:
17 *
18 * cmake --build build --target live
19 *
20 * Alternately, if PocketSphinx is installed system-wide, you can run:
21 *
22 * gcc -o live live.c $(pkg-config --libs --cflags pocketsphinx)
23 *
24 * Sadly, this example does *not* seem to work on Windows, even if you
25 * manage to get `sox` in your `PATH` (which is not easy), because it
26 * seems that it can't actually read from the microphone. Try
27 * live_win32.c or live_portaudio.c instead.
28 */
29 #include <pocketsphinx.h>
30 #include <signal.h>
31
32 static int global_done = 0;
33 static void
34 catch_sig(int signum)
35 {
36 (void)signum;
37 global_done = 1;
38 }
39
40 #ifdef WIN32
41 #define popen _popen
42 #define pclose _pclose
43 #endif
44
45 static FILE *
46 popen_sox(int sample_rate)
47 {
48 char *soxcmd;
49 int len;
50 FILE *sox;
51 #define SOXCMD "sox -q -r %d -c 1 -b 16 -e signed-integer -d -t raw -"
52 len = snprintf(NULL, 0, SOXCMD, sample_rate);
53 if ((soxcmd = malloc(len + 1)) == NULL)
54 E_FATAL_SYSTEM("Failed to allocate string");
55 if (snprintf(soxcmd, len + 1, SOXCMD, sample_rate) != len)
56 E_FATAL_SYSTEM("snprintf() failed");
57 if ((sox = popen(soxcmd, "r")) == NULL)
58 E_FATAL_SYSTEM("Failed to popen(%s)", soxcmd);
59 free(soxcmd);
60
61 return sox;
62 }
63
64 int
65 main(int argc, char *argv[])
66 {
67 ps_decoder_t *decoder;
68 ps_config_t *config;
69 ps_endpointer_t *ep;
70 FILE *sox;
71 short *frame;
72 size_t frame_size;
73
74 (void)argc; (void)argv;
75 config = ps_config_init(NULL);
76 ps_default_search_args(config);
77 if ((decoder = ps_init(config)) == NULL)
78 E_FATAL("PocketSphinx decoder init failed\n");
79
80 if ((ep = ps_endpointer_init(0, 0.0, 0, 0, 0)) == NULL)
81 E_FATAL("PocketSphinx endpointer init failed\n");
82 sox = popen_sox(ps_endpointer_sample_rate(ep));
83 frame_size = ps_endpointer_frame_size(ep);
84 if ((frame = malloc(frame_size * sizeof(frame[0]))) == NULL)
85 E_FATAL_SYSTEM("Failed to allocate frame");
86 if (signal(SIGINT, catch_sig) == SIG_ERR)
87 E_FATAL_SYSTEM("Failed to set SIGINT handler");
88 while (!global_done) {
89 const int16 *speech;
90 int prev_in_speech = ps_endpointer_in_speech(ep);
91 size_t len, end_samples;
92 if ((len = fread(frame, sizeof(frame[0]),
93 frame_size, sox)) != frame_size) {
94 if (len > 0) {
95 speech = ps_endpointer_end_stream(ep, frame,
96 frame_size,
97 &end_samples);
98 }
99 else
100 break;
101 } else {
102 speech = ps_endpointer_process(ep, frame);
103 }
104 if (speech != NULL) {
105 const char *hyp;
106 if (!prev_in_speech) {
107 fprintf(stderr, "Speech start at %.2f\n",
108 ps_endpointer_speech_start(ep));
109 ps_start_utt(decoder);
110 }
111 if (ps_process_raw(decoder, speech, frame_size, FALSE, FALSE) < 0)
112 E_FATAL("ps_process_raw() failed\n");
113 if ((hyp = ps_get_hyp(decoder, NULL)) != NULL)
114 fprintf(stderr, "PARTIAL RESULT: %s\n", hyp);
115 if (!ps_endpointer_in_speech(ep)) {
116 fprintf(stderr, "Speech end at %.2f\n",
117 ps_endpointer_speech_end(ep));
118 ps_end_utt(decoder);
119 if ((hyp = ps_get_hyp(decoder, NULL)) != NULL)
120 printf("%s\n", hyp);
121 }
122 }
123 }
124 free(frame);
125 if (pclose(sox) < 0)
126 E_ERROR_SYSTEM("Failed to pclose(sox)");
127 ps_endpointer_free(ep);
128 ps_free(decoder);
129 ps_config_free(config);
130
131 return 0;
132 }