| 1 |
/* Example of simple PocketSphinx recognition. |
| 2 |
* |
| 3 |
* MIT license (c) 2022, see LICENSE for more information. |
| 4 |
* |
| 5 |
* Author: David Huggins-Daines <[email protected]> |
| 6 |
*/ |
| 7 |
/** |
| 8 |
* @example simple.c |
| 9 |
* @brief Simplest possible example of speech recognition in C. |
| 10 |
* |
| 11 |
* This file shows how to use PocketSphinx to recognize a single input |
| 12 |
* file. To compile it, assuming you have built the library as in |
| 13 |
* \ref unix_install "these directions", you can run: |
| 14 |
* |
| 15 |
* cmake --build build --target simple |
| 16 |
* |
| 17 |
* Alternately, if PocketSphinx is installed system-wide, you can run: |
| 18 |
* |
| 19 |
* gcc -o simple simple.c $(pkg-config --libs --cflags pocketsphinx) |
| 20 |
* |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <pocketsphinx.h> |
| 25 |
#include <stdlib.h> |
| 26 |
|
| 27 |
int |
| 28 |
main(int argc, char *argv[]) |
| 29 |
{ |
| 30 |
ps_decoder_t *decoder; |
| 31 |
ps_config_t *config; |
| 32 |
FILE *fh; |
| 33 |
short *buf; |
| 34 |
size_t len, nsamples; |
| 35 |
|
| 36 |
/* Look for a single audio file as input parameter. */ |
| 37 |
if (argc < 2) |
| 38 |
E_FATAL("Usage: %s FILE\n"); |
| 39 |
if ((fh = fopen(argv[1], "rb")) == NULL) |
| 40 |
E_FATAL_SYSTEM("Failed to open %s", argv[1]); |
| 41 |
|
| 42 |
/* Get the size of the input. */ |
| 43 |
if (fseek(fh, 0, SEEK_END) < 0) |
| 44 |
E_FATAL_SYSTEM("Unable to find end of input file %s", argv[1]); |
| 45 |
len = ftell(fh); |
| 46 |
rewind(fh); |
| 47 |
|
| 48 |
/* Initialize configuration from input file. */ |
| 49 |
config = ps_config_init(NULL); |
| 50 |
ps_default_search_args(config); |
| 51 |
if (ps_config_soundfile(config, fh, argv[1]) < 0) |
| 52 |
E_FATAL("Unsupported input file %s\n", argv[1]); |
| 53 |
if ((decoder = ps_init(config)) == NULL) |
| 54 |
E_FATAL("PocketSphinx decoder init failed\n"); |
| 55 |
|
| 56 |
/* Allocate data (skipping header) */ |
| 57 |
len -= ftell(fh); |
| 58 |
if ((buf = malloc(len)) == NULL) |
| 59 |
E_FATAL_SYSTEM("Unable to allocate %d bytes", len); |
| 60 |
/* Read input */ |
| 61 |
nsamples = fread(buf, sizeof(buf[0]), len / sizeof(buf[0]), fh); |
| 62 |
if (nsamples != len / sizeof(buf[0])) |
| 63 |
E_FATAL_SYSTEM("Unable to read %d samples", len / sizeof(buf[0])); |
| 64 |
|
| 65 |
/* Recognize it! */ |
| 66 |
if (ps_start_utt(decoder) < 0) |
| 67 |
E_FATAL("Failed to start processing\n"); |
| 68 |
if (ps_process_raw(decoder, buf, nsamples, FALSE, TRUE) < 0) |
| 69 |
E_FATAL("ps_process_raw() failed\n"); |
| 70 |
if (ps_end_utt(decoder) < 0) |
| 71 |
E_FATAL("Failed to end processing\n"); |
| 72 |
|
| 73 |
/* Print the result */ |
| 74 |
if (ps_get_hyp(decoder, NULL) != NULL) |
| 75 |
printf("%s\n", ps_get_hyp(decoder, NULL)); |
| 76 |
|
| 77 |
/* Clean up */ |
| 78 |
if (fclose(fh) < 0) |
| 79 |
E_FATAL_SYSTEM("Failed to close %s", argv[1]); |
| 80 |
free(buf); |
| 81 |
ps_free(decoder); |
| 82 |
ps_config_free(config); |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |