Advertisement
nguyenvanquan7826

Untitled

Jun 26th, 2014
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.92 KB | None | 0 0
  1. #include <pocketsphinx.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #include <sphinxbase/err.h>
  6. #include <sphinxbase/ad.h>
  7. #include <sphinxbase/cont_ad.h>
  8.  
  9. static const arg_t cont_args_def[] = {
  10.     POCKETSPHINX_OPTIONS,
  11.     { "-argfile",
  12.         ARG_STRING,
  13.         NULL,
  14.         "Argument file giving extra arguments." },
  15.     { "-adcdev",
  16.         ARG_STRING,
  17.         NULL,
  18.         "Name of audio device to use for input." },
  19.     { "-infile",
  20.         ARG_STRING,
  21.         NULL,
  22.         "Audio file to transcribe." },
  23.     { "-time",
  24.         ARG_BOOLEAN,
  25.         "no",
  26.         "Print word times in file transcription." },
  27.     CMDLN_EMPTY_OPTION
  28. };
  29.  
  30.  
  31. static const char* number_char[] = {
  32.     "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN"
  33. };
  34.  
  35.  
  36. static ps_decoder_t *ps;
  37. static cmd_ln_t *config;
  38.  
  39. static void sleep_msec(int32 ms )
  40. {
  41.     struct timeval tmo;
  42.  
  43.     tmo.tv_sec = 0;
  44.     tmo.tv_usec = ms * 1000;
  45.  
  46.     select(0, NULL, NULL, NULL, &tmo);
  47. }
  48.  
  49. static void recognize_from_microphone()
  50. {
  51.     ad_rec_t *ad;
  52.     int16 adbuf[4096];
  53.     int16 i;
  54.     int32 k, ts, rem;
  55.     char const *hyp;
  56.     char const *uttid;
  57.     cont_ad_t *cont;
  58.     char word[256];
  59.  
  60.     if ((ad = ad_open_dev(cmd_ln_str_r(config, "-adcdev"),
  61.                     (int)cmd_ln_float32_r(config, "-samprate"))) == NULL)
  62.         E_FATAL("Failed to open audio device\n");
  63.  
  64.     /* Initialize continuous listening module */
  65.     if ((cont = cont_ad_init(ad, ad_read)) == NULL)
  66.         E_FATAL("Failed to initialize voice activity detection\n");
  67.     if (ad_start_rec(ad) < 0)
  68.         E_FATAL("Failed to start recording\n");
  69.     if (cont_ad_calib(cont) < 0)
  70.         E_FATAL("Failed to calibrate voice activity detection\n");
  71.  
  72.     for (;;) {
  73.         /* Indicate listening for next utterance */
  74.         printf("READY....\n");
  75.         fflush(stdout);
  76.         fflush(stderr);
  77.  
  78.         /* Wait data for next utterance */
  79.         while ((k = cont_ad_read(cont, adbuf, 4096)) == 0)
  80.             sleep_msec(100);
  81.  
  82.         if (k < 0)
  83.             E_FATAL("Failed to read audio\n");
  84.  
  85.         /*
  86.          * Non-zero amount of data received; start recognition of new utterance.
  87.          * NULL argument to uttproc_begin_utt => automatic generation of utterance-id.
  88.          */
  89.         if (ps_start_utt(ps, NULL) < 0)
  90.             E_FATAL("Failed to start utterance\n");
  91.         ps_process_raw(ps, adbuf, k, FALSE, FALSE);
  92.         printf("Listening...\n");
  93.         fflush(stdout);
  94.  
  95.         /* Note timestamp for this first block of data */
  96.         ts = cont->read_ts;
  97.  
  98.         /* Decode utterance until end (marked by a "long" silence, >1sec) */
  99.         for (;;) {
  100.             /* Read non-silence audio data, if any, from continuous listening module */
  101.             if ((k = cont_ad_read(cont, adbuf, 4096)) < 0)
  102.                 E_FATAL("Failed to read audio\n");
  103.             if (k == 0) {
  104.                 /*
  105.                  * No speech data available; check current timestamp with most recent
  106.                  * speech to see if more than 1 sec elapsed.  If so, end of utterance.
  107.                  */
  108.                 if ((cont->read_ts - ts) > DEFAULT_SAMPLES_PER_SEC)
  109.                     break;
  110.             }
  111.             else {
  112.                 /* New speech data received; note current timestamp */
  113.                 ts = cont->read_ts;
  114.             }
  115.  
  116.             /*
  117.              * Decode whatever data was read above.
  118.              */
  119.             rem = ps_process_raw(ps, adbuf, k, FALSE, FALSE);
  120.  
  121.             /* If no work to be done, sleep a bit */
  122.             if ((rem == 0) && (k == 0))
  123.                 sleep_msec(20);
  124.         }
  125.  
  126.         /*
  127.          * Utterance ended; flush any accumulated, unprocessed A/D data and stop
  128.          * listening until current utterance completely decoded
  129.          */
  130.         ad_stop_rec(ad);
  131.         while (ad_read(ad, adbuf, 4096) >= 0);
  132.         cont_ad_reset(cont);
  133.  
  134.         printf("Stopped listening, please wait...\n");
  135.         fflush(stdout);
  136.         /* Finish decoding, obtain and print result */
  137.         ps_end_utt(ps);
  138.         hyp = ps_get_hyp(ps, NULL, &uttid);
  139.  
  140.         for (i=0; i<10; i++ ){
  141.             if (strcmp(hyp, number_char[i]) == 0){
  142.                 printf("%d\n", i+1);
  143.                 break;
  144.             }
  145.         }
  146.  
  147.         fflush(stdout);
  148.  
  149.         /* Exit if the first word spoken was GOODBYE */
  150.         if (hyp) {
  151.             sscanf(hyp, "%s", word);
  152.             if (strcmp(word, "GOODBYE") == 0){
  153.                 printf("Goodbye, see you again!\n");
  154.                 break;
  155.             }
  156.  
  157.         }
  158.  
  159.         /* Resume A/D recording for next utterance */
  160.         if (ad_start_rec(ad) < 0)
  161.             E_FATAL("Failed to start recording\n");
  162.     }
  163.  
  164.     cont_ad_close(cont);
  165.     ad_close(ad);
  166. }
  167.  
  168. static jmp_buf jbuf;
  169.  
  170. int main(int argc, char *argv[])
  171. {
  172.     char const *cfg;
  173.  
  174.     if (argc == 2) {
  175.         config = cmd_ln_parse_file_r(NULL, cont_args_def, argv[1], TRUE);
  176.     }
  177.     else {
  178.         config = cmd_ln_parse_r(NULL, cont_args_def, argc, argv, FALSE);
  179.     }
  180.     /* Handle argument file as -argfile. */
  181.     if (config && (cfg = cmd_ln_str_r(config, "-argfile")) != NULL) {
  182.         config = cmd_ln_parse_file_r(config, cont_args_def, cfg, FALSE);
  183.     }
  184.     if (config == NULL)
  185.         return 1;
  186.  
  187.     ps = ps_init(config);
  188.     if (ps == NULL)
  189.         return 1;
  190.  
  191.     E_INFO("%s COMPILED ON: %s, AT: %s\n\n", argv[0], __DATE__, __TIME__);
  192.  
  193.     if (cmd_ln_str_r(config, "-infile") != NULL) {
  194.         //recognize_from_file();
  195.     } else {
  196.         if (setjmp(jbuf) == 0) {
  197.             recognize_from_microphone();
  198.         }
  199.     }
  200.  
  201.     ps_free(ps);
  202.     return 0;
  203. }
  204.  
  205. /*gcc -o hello ^Cllo.c     -DMODELDIR=\"`pkg-config --variable=modeldir pocketsphinx`\"     `pkg-config --cflags --libs pocketsphinx sphinxbase`*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement