Simplest possible example of speech recognition in C.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 simple
Alternately, if PocketSphinx is installed system-wide, you can run:
gcc -o simple simple.c $(pkg-config --libs --cflags pocketsphinx)
#include <stdlib.h>
int
main(int argc, char *argv[])
{
FILE *fh;
short *buf;
size_t len, nsamples;
if (argc < 2)
if ((fh = fopen(argv[1], "rb")) == NULL)
if (fseek(fh, 0, SEEK_END) < 0)
len = ftell(fh);
rewind(fh);
E_FATAL(
"Unsupported input file %s\n", argv[1]);
if ((decoder =
ps_init(config)) == NULL)
E_FATAL(
"PocketSphinx decoder init failed\n");
len -= ftell(fh);
if ((buf = malloc(len)) == NULL)
nsamples = fread(buf, sizeof(buf[0]), len / sizeof(buf[0]), fh);
if (nsamples != len / sizeof(buf[0]))
E_FATAL(
"Failed to start processing\n");
E_FATAL(
"ps_process_raw() failed\n");
E_FATAL(
"Failed to end processing\n");
if (fclose(fh) < 0)
free(buf);
return 0;
}