Debian Codegraph @

examples/live_portaudio.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_portaudio.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 * PortAudio (v19 and above).
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_portaudio
18 *
19 * Alternately, if PocketSphinx is installed system-wide, you can run:
20 *
21 * gcc -o live_portaudio live_portaudio.c \
22 * $(pkg-config --libs --cflags pocketsphinx portaudio-2.0)
23 *
24 *
25 */
26 #include <portaudio.h>
27 #include <pocketsphinx.h>
28 #include <signal.h>
29
30 static int global_done = 0;
31 static void
32 catch_sig(int signum)
33 {
34 (void)signum;
35 global_done = 1;
36 }
37
38 int
39 main(int argc, char *argv[])
40 {
41
42 PaStream *stream;
43 PaError err;
44 ps_decoder_t *decoder;
45 ps_config_t *config;
46 ps_endpointer_t *ep;
47 short *frame;
48 size_t frame_size;
49
50 (void)argc; (void)argv;
51
52 config = ps_config_init(NULL);
53 ps_default_search_args(config);
54
55 if ((err = Pa_Initialize()) != paNoError)
56 E_FATAL("Failed to initialize PortAudio: %s\n",
57 Pa_GetErrorText(err));
58 if ((decoder = ps_init(config)) == NULL)
59 E_FATAL("PocketSphinx decoder init failed\n");
60 if ((ep = ps_endpointer_init(0, 0.0, 0, 0, 0)) == NULL)
61 E_FATAL("PocketSphinx endpointer init failed\n");
62 frame_size = ps_endpointer_frame_size(ep);
63 if ((frame = malloc(frame_size * sizeof(frame[0]))) == NULL)
64 E_FATAL_SYSTEM("Failed to allocate frame");
65 if ((err = Pa_OpenDefaultStream(&stream, 1, 0, paInt16,
66 ps_config_int(config, "samprate"),
67 frame_size, NULL, NULL)) != paNoError)
68 E_FATAL("Failed to open PortAudio stream: %s\n",
69 Pa_GetErrorText(err));
70 if ((err = Pa_StartStream(stream)) != paNoError)
71 E_FATAL("Failed to start PortAudio stream: %s\n",
72 Pa_GetErrorText(err));
73 if (signal(SIGINT, catch_sig) == SIG_ERR)
74 E_FATAL_SYSTEM("Failed to set SIGINT handler");
75 while (!global_done) {
76 const int16 *speech;
77 int prev_in_speech = ps_endpointer_in_speech(ep);
78 if ((err = Pa_ReadStream(stream, frame, frame_size)) != paNoError) {
79 E_ERROR("Error in PortAudio read: %s\n",
80 Pa_GetErrorText(err));
81 break;
82 }
83 speech = ps_endpointer_process(ep, frame);
84 if (speech != NULL) {
85 const char *hyp;
86 if (!prev_in_speech) {
87 fprintf(stderr, "Speech start at %.2f\n",
88 ps_endpointer_speech_start(ep));
89 fflush(stderr); /* For broken MSYS2 terminal */
90 ps_start_utt(decoder);
91 }
92 if (ps_process_raw(decoder, speech, frame_size, FALSE, FALSE) < 0)
93 E_FATAL("ps_process_raw() failed\n");
94 if ((hyp = ps_get_hyp(decoder, NULL)) != NULL) {
95 fprintf(stderr, "PARTIAL RESULT: %s\n", hyp);
96 fflush(stderr);
97 }
98 if (!ps_endpointer_in_speech(ep)) {
99 fprintf(stderr, "Speech end at %.2f\n",
100 ps_endpointer_speech_end(ep));
101 fflush(stderr);
102 ps_end_utt(decoder);
103 if ((hyp = ps_get_hyp(decoder, NULL)) != NULL) {
104 printf("%s\n", hyp);
105 fflush(stdout);
106 }
107 }
108 }
109 }
110 if ((err = Pa_StopStream(stream)) != paNoError)
111 E_FATAL("Failed to stop PortAudio stream: %s\n",
112 Pa_GetErrorText(err));
113 if ((err = Pa_Terminate()) != paNoError)
114 E_FATAL("Failed to terminate PortAudio: %s\n",
115 Pa_GetErrorText(err));
116 free(frame);
117 ps_endpointer_free(ep);
118 ps_free(decoder);
119 ps_config_free(config);
120
121 return 0;
122 }