Debian Codegraph @

examples/live_pulseaudio.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_pulseaudio.c
9 * @brief Speech recognition with live audio input and endpointing.
10 *
11 * This file shows how to use PocketSphinx with microphone input using
12 * PulseAudio.
13 *
14 * To compile it, assuming you have built the library as in
15 * \ref unix_install "these directions", you can run:
16 *
17 * cmake --build build --target live_pulseaudio
18 *
19 * Alternately, if PocketSphinx is installed system-wide, you can run:
20 *
21 * gcc -o live_pulseaudio live_pulseaudio.c \
22 * $(pkg-config --libs --cflags pocketsphinx libpulse-simple)
23 *
24 *
25 */
26 #include <pulse/simple.h>
27 #include <pulse/error.h>
28 #include <pocketsphinx.h>
29 #include <signal.h>
30
31 static int global_done = 0;
32 static void
33 catch_sig(int signum)
34 {
35 (void)signum;
36 global_done = 1;
37 }
38
39 int
40 main(int argc, char *argv[])
41 {
42
43 pa_simple *s;
44 pa_sample_spec ss;
45 int err;
46 ps_decoder_t *decoder;
47 ps_config_t *config;
48 ps_endpointer_t *ep;
49 short *frame;
50 size_t frame_size;
51
52 (void)argc; (void)argv;
53
54 config = ps_config_init(NULL);
55 ps_default_search_args(config);
56 if ((decoder = ps_init(config)) == NULL)
57 E_FATAL("PocketSphinx decoder init failed\n");
58 if ((ep = ps_endpointer_init(0, 0.0, 0, 0, 0)) == NULL)
59 E_FATAL("PocketSphinx endpointer init failed\n");
60 frame_size = ps_endpointer_frame_size(ep);
61 if ((frame = malloc(frame_size * sizeof(frame[0]))) == NULL)
62 E_FATAL_SYSTEM("Failed to allocate frame");
63
64 ss.format = PA_SAMPLE_S16NE;
65 ss.channels = 1;
66 ss.rate = ps_config_int(config, "samprate");
67 if ((s = pa_simple_new(NULL, "live_pulseaudio", PA_STREAM_RECORD, NULL,
68 "live", &ss, NULL, NULL, &err)) == NULL)
69 E_FATAL("Failed to connect to PulseAudio: %s\n",
70 pa_strerror(err));
71 if (signal(SIGINT, catch_sig) == SIG_ERR)
72 E_FATAL_SYSTEM("Failed to set SIGINT handler");
73 while (!global_done) {
74 const int16 *speech;
75 int prev_in_speech = ps_endpointer_in_speech(ep);
76 if (pa_simple_read(s, frame,
77 frame_size * sizeof(frame[0]), &err) < 0) {
78 E_ERROR("Error in pa_simple_read: %s\n",
79 pa_strerror(err));
80 break;
81 }
82 speech = ps_endpointer_process(ep, frame);
83 if (speech != NULL) {
84 const char *hyp;
85 if (!prev_in_speech) {
86 fprintf(stderr, "Speech start at %.2f\n",
87 ps_endpointer_speech_start(ep));
88 ps_start_utt(decoder);
89 }
90 if (ps_process_raw(decoder, speech, frame_size, FALSE, FALSE) < 0)
91 E_FATAL("ps_process_raw() failed\n");
92 if ((hyp = ps_get_hyp(decoder, NULL)) != NULL)
93 fprintf(stderr, "PARTIAL RESULT: %s\n", hyp);
94 if (!ps_endpointer_in_speech(ep)) {
95 fprintf(stderr, "Speech end at %.2f\n",
96 ps_endpointer_speech_end(ep));
97 ps_end_utt(decoder);
98 if ((hyp = ps_get_hyp(decoder, NULL)) != NULL)
99 printf("%s\n", hyp);
100 }
101 }
102 }
103 pa_simple_free(s);
104 free(frame);
105 ps_endpointer_free(ep);
106 ps_free(decoder);
107 ps_config_free(config);
108
109 return 0;
110 }