PocketSphinx  5.0.0
A small speech recognizer
simple.c

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)
/* Example of simple PocketSphinx recognition.
*
* MIT license (c) 2022, see LICENSE for more information.
*
* Author: David Huggins-Daines <dhdaines@gmail.com>
*/
#include <pocketsphinx.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
ps_decoder_t *decoder;
ps_config_t *config;
FILE *fh;
short *buf;
size_t len, nsamples;
/* Look for a single audio file as input parameter. */
if (argc < 2)
E_FATAL("Usage: %s FILE\n");
if ((fh = fopen(argv[1], "rb")) == NULL)
E_FATAL_SYSTEM("Failed to open %s", argv[1]);
/* Get the size of the input. */
if (fseek(fh, 0, SEEK_END) < 0)
E_FATAL_SYSTEM("Unable to find end of input file %s", argv[1]);
len = ftell(fh);
rewind(fh);
/* Initialize configuration from input file. */
config = ps_config_init(NULL);
if (ps_config_soundfile(config, fh, argv[1]) < 0)
E_FATAL("Unsupported input file %s\n", argv[1]);
if ((decoder = ps_init(config)) == NULL)
E_FATAL("PocketSphinx decoder init failed\n");
/* Allocate data (skipping header) */
len -= ftell(fh);
if ((buf = malloc(len)) == NULL)
E_FATAL_SYSTEM("Unable to allocate %d bytes", len);
/* Read input */
nsamples = fread(buf, sizeof(buf[0]), len / sizeof(buf[0]), fh);
if (nsamples != len / sizeof(buf[0]))
E_FATAL_SYSTEM("Unable to read %d samples", len / sizeof(buf[0]));
/* Recognize it! */
if (ps_start_utt(decoder) < 0)
E_FATAL("Failed to start processing\n");
if (ps_process_raw(decoder, buf, nsamples, FALSE, TRUE) < 0)
E_FATAL("ps_process_raw() failed\n");
if (ps_end_utt(decoder) < 0)
E_FATAL("Failed to end processing\n");
/* Print the result */
if (ps_get_hyp(decoder, NULL) != NULL)
printf("%s\n", ps_get_hyp(decoder, NULL));
/* Clean up */
if (fclose(fh) < 0)
E_FATAL_SYSTEM("Failed to close %s", argv[1]);
free(buf);
ps_free(decoder);
ps_config_free(config);
return 0;
}
ps_decoder_t::ps_process_raw
POCKETSPHINX_EXPORT int ps_process_raw(ps_decoder_t *ps, int16 const *data, size_t n_samples, int no_search, int full_utt)
ps_config_t::ps_config_init
POCKETSPHINX_EXPORT ps_config_t * ps_config_init(const ps_arg_t *defn)
ps_decoder_t::ps_get_hyp
POCKETSPHINX_EXPORT const char * ps_get_hyp(ps_decoder_t *ps, int32 *out_best_score)
ps_config_t::ps_config_soundfile
POCKETSPHINX_EXPORT int ps_config_soundfile(ps_config_t *config, FILE *fh, const char *file)
ps_config_t::ps_config_free
POCKETSPHINX_EXPORT int ps_config_free(ps_config_t *config)
ps_decoder_t::ps_free
POCKETSPHINX_EXPORT int ps_free(ps_decoder_t *ps)
ps_config_t
configuration object.
E_FATAL_SYSTEM
#define E_FATAL_SYSTEM(...)
Definition: err.h:89
pocketsphinx.h
ps_decoder_t::ps_start_utt
POCKETSPHINX_EXPORT int ps_start_utt(ps_decoder_t *ps)
ps_config_t::ps_init
POCKETSPHINX_EXPORT ps_decoder_t * ps_init(ps_config_t *config)
ps_decoder_t
Speech recognizer object.
ps_decoder_t::ps_end_utt
POCKETSPHINX_EXPORT int ps_end_utt(ps_decoder_t *ps)
ps_config_t::ps_default_search_args
POCKETSPHINX_EXPORT void ps_default_search_args(ps_config_t *config)
E_FATAL
#define E_FATAL(...)
Definition: err.h:80