Debian Codegraph @

examples/live_win32.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_win32.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 * the Win32 Waveform Audio API (the only one of many terrible audio
13 * APIs on Windows that isn't made even more terrible by requiring you
14 * to use C++ in an unmanaged environment).
15 *
16 * To build it, you should be able to find a "live_win32" target in
17 * your favorite IDE after running CMake - in Visual Studio Code, look
18 * in the "CMake" tab.
19 *
20 * Microphones on Windows tend to be miscalibrated with the recording
21 * level set much too high by default, so the endpointer may give a
22 * lot of false positives at first. Programs like Audacity seem to
23 * work around this somehow, but I don't really know how they do it.
24 */
25
26 #include <windows.h>
27 #include <mmsystem.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 #define CHECK(expr) \
40 do { \
41 int err; \
42 if ((err = expr) != 0) \
43 { \
44 char errbuf[MAXERRORLENGTH]; \
45 waveInGetErrorText(err, errbuf, sizeof(errbuf)); \
46 E_FATAL("error %08x: %s\n", err, errbuf); \
47 } \
48 } while (0)
49
50 int main(int argc, char *argv[])
51 {
52 ps_decoder_t *decoder;
53 ps_config_t *config;
54 ps_endpointer_t *ep;
55 size_t frame_size;
56 HWAVEIN wavein;
57 WAVEFORMATEX wavefmt;
58 HANDLE event;
59 /* A large but somewhat arbitrary number of buffers. */
60 #define NBUF 100 /* 100 * 0.03 = 3 seconds */
61 WAVEHDR hdrs[NBUF];
62 int i;
63
64 (void)argc; (void)argv;
65 /* Initialize decoder and endpointer */
66 config = ps_config_init(NULL);
67 ps_default_search_args(config);
68 if ((decoder = ps_init(config)) == NULL)
69 E_FATAL("PocketSphinx decoder init failed\n");
70 if ((ep = ps_endpointer_init(0, 0.0, 0,
71 ps_config_int(config, "samprate"),
72 0)) == NULL)
73 E_FATAL("PocketSphinx endpointer init failed\n");
74 /* Frame size in samples (not bytes) */
75 frame_size = ps_endpointer_frame_size(ep);
76 /* Tell Windows what format we want (NOTE: may not be available...) */
77 wavefmt.wFormatTag = WAVE_FORMAT_PCM;
78 wavefmt.nChannels = 1;
79 wavefmt.nSamplesPerSec = ps_endpointer_sample_rate(ep);
80 wavefmt.wBitsPerSample = 16;
81 wavefmt.nBlockAlign = 2;
82 wavefmt.nAvgBytesPerSec = wavefmt.nSamplesPerSec * wavefmt.nBlockAlign;
83 wavefmt.cbSize = 0;
84 /* Create an event to tell us when a new buffer is ready. */
85 event = CreateEvent(NULL, TRUE, FALSE, "buffer_ready");
86 /* Open the recording device. */
87 CHECK(waveInOpen(&wavein, WAVE_MAPPER, &wavefmt,
88 (DWORD_PTR)event, 0, CALLBACK_EVENT));
89 /* Create buffers. */
90 memset(hdrs, 0, sizeof(hdrs));
91 for (i = 0; i < NBUF; ++i) {
92 hdrs[i].lpData = malloc(frame_size * 2);
93 hdrs[i].dwBufferLength = (DWORD)frame_size * 2;
94 CHECK(waveInPrepareHeader(wavein, &hdrs[i], sizeof(hdrs[i])));
95 CHECK(waveInAddBuffer(wavein, &hdrs[i], sizeof(hdrs[i])));
96 }
97 /* Start recording. */
98 CHECK(waveInStart(wavein));
99 i = 0;
100 if (signal(SIGINT, catch_sig) == SIG_ERR)
101 E_FATAL_SYSTEM("Failed to set SIGINT handler");
102 while (!global_done) {
103 const int16 *speech;
104 WaitForSingleObject(event, INFINITE);
105 /* Get as many buffers as we can. */
106 while (hdrs[i].dwFlags & WHDR_DONE) {
107 int prev_in_speech = ps_endpointer_in_speech(ep);
108 int16 *frame = (int16 *)hdrs[i].lpData;
109 /* Process them one by one. */
110 speech = ps_endpointer_process(ep, frame);
111 CHECK(waveInUnprepareHeader(wavein, &hdrs[i], sizeof(hdrs[i])));
112 CHECK(waveInPrepareHeader(wavein, &hdrs[i], sizeof(hdrs[i])));
113 CHECK(waveInAddBuffer(wavein, &hdrs[i], sizeof(hdrs[i])));
114 if (++i == NBUF)
115 i = 0;
116 if (speech != NULL) {
117 const char *hyp;
118 if (!prev_in_speech) {
119 fprintf(stderr, "Speech start at %.2f\n",
120 ps_endpointer_speech_start(ep));
121 fflush(stderr); /* For broken MSYS2 terminal */
122 ps_start_utt(decoder);
123 }
124 if (ps_process_raw(decoder, speech, frame_size, FALSE, FALSE) < 0)
125 E_FATAL("ps_process_raw() failed\n");
126 if ((hyp = ps_get_hyp(decoder, NULL)) != NULL) {
127 fprintf(stderr, "PARTIAL RESULT: %s\n", hyp);
128 fflush(stderr);
129 }
130 if (!ps_endpointer_in_speech(ep)) {
131 fprintf(stderr, "Speech end at %.2f\n",
132 ps_endpointer_speech_end(ep));
133 fflush(stderr);
134 ps_end_utt(decoder);
135 if ((hyp = ps_get_hyp(decoder, NULL)) != NULL) {
136 printf("%s\n", hyp);
137 fflush(stdout);
138 }
139 }
140 }
141 }
142 /* Wait for another buffer. */
143 ResetEvent(event);
144 }
145 /* Stop recording, cancel all buffers, and free them. */
146 CHECK(waveInStop(wavein));
147 CHECK(waveInReset(wavein));
148 for (i = 0; i < NBUF; ++i) {
149 if (hdrs[i].dwFlags & WHDR_PREPARED)
150 CHECK(waveInUnprepareHeader(wavein, &hdrs[i],
151 sizeof(hdrs[i])));
152 free(hdrs[i].lpData);
153 }
154 CloseHandle(event);
155 ps_endpointer_free(ep);
156 ps_free(decoder);
157 ps_config_free(config);
158
159 return 0;
160 }