Speech recognition with live audio input and endpointing.This file shows how to use PocketSphinx in conjunction with sox
to detect and recognize speech from the default audio input device.
This file shows how to use PocketSphinx to recognize a single input file. To compile it, assuming you have built the library as in these directions, you can run:
cmake --build build --target live
Alternately, if PocketSphinx is installed system-wide, you can run:
gcc -o live live.c $(pkg-config --libs --cflags pocketsphinx)
#include <signal.h>
static int global_done = 0;
static void
catch_sig(int signum)
{
(void)signum;
global_done = 1;
}
static FILE *
popen_sox(int sample_rate)
{
char *soxcmd;
int len;
FILE *sox;
#define SOXCMD "sox -q -r %d -c 1 -b 16 -e signed-integer -d -t raw -"
len = snprintf(NULL, 0, SOXCMD, sample_rate);
if ((soxcmd = malloc(len + 1)) == NULL)
if (snprintf(soxcmd, len + 1, SOXCMD, sample_rate) != len)
if ((sox = popen(soxcmd, "r")) == NULL)
free(soxcmd);
return sox;
}
int
main(int argc, char *argv[])
{
FILE *sox;
short *frame;
size_t frame_size;
(void)argc; (void)argv;
if ((decoder =
ps_init(config)) == NULL)
E_FATAL(
"PocketSphinx decoder init failed\n");
E_FATAL(
"PocketSphinx endpointer init failed\n");
if ((frame = malloc(frame_size * sizeof(frame[0]))) == NULL)
if (signal(SIGINT, catch_sig) == SIG_ERR)
while (!global_done) {
const int16 *speech;
size_t len, end_samples;
if ((len = fread(frame, sizeof(frame[0]),
frame_size, sox)) != frame_size) {
if (len > 0) {
frame_size,
&end_samples);
}
else
break;
} else {
}
if (speech != NULL) {
const char *hyp;
if (!prev_in_speech) {
fprintf(stderr, "Speech start at %.2f\n",
}
E_FATAL(
"ps_process_raw() failed\n");
fprintf(stderr, "PARTIAL RESULT: %s\n", hyp);
fprintf(stderr, "Speech end at %.2f\n",
printf("%s\n", hyp);
}
}
}
free(frame);
if (pclose(sox) < 0)
return 0;
}