Advertisement
Guest User

Untitled

a guest
Apr 4th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. void QtCVMainWindow::speech()
  2. {
  3.     ps_decoder_t *ps;
  4.     cmd_ln_t *config;
  5.     char const *hyp, *uttid;
  6.     int rv;
  7.     int32 score;
  8.  
  9.     config = cmd_ln_init(NULL, ps_args(), TRUE,
  10.                          "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k",
  11.                          "-lm", MODELDIR "/lm/en/turtle.DMP",
  12.                          "-dict", MODELDIR "/lm/en/turtle.dic",
  13.                          NULL);
  14.     if (config == NULL)
  15.         return;
  16.     ps = ps_init(config);
  17.     if (ps == NULL)
  18.         return;
  19.  
  20.     AUD_DeviceSpecs specs;
  21.     specs.channels = AUD_CHANNELS_MONO;
  22.     specs.rate = AUD_RATE_16000;
  23.     specs.format = AUD_FORMAT_S16;
  24.     AUD_Reference<AUD_IReader> capture = new AUD_ConverterReader(new AUD_OpenALReader(specs.specs), specs);
  25.  
  26.     AUD_Buffer buffer(specs.rate * 5 * AUD_DEVICE_SAMPLE_SIZE(specs));
  27.     int samples = specs.rate * 5;
  28.     int len;
  29.     int16_t* sbuf = reinterpret_cast<int16_t*>(buffer.getBuffer());
  30.     bool eos;
  31.  
  32.     while(samples > 0)
  33.     {
  34.         len = capture->getLength();
  35.  
  36.         if(len > samples)
  37.             len = samples;
  38.  
  39.         if(len > 0)
  40.         {
  41.             capture->read(len, eos, reinterpret_cast<sample_t*>(sbuf));
  42.             sbuf += len;
  43.             samples -= len;
  44.         }
  45.     }
  46.  
  47.     rv = ps_start_utt(ps, "goforward");
  48.     if (rv < 0)
  49.         return;
  50.  
  51.     rv = ps_process_raw(ps, reinterpret_cast<int16*>(buffer.getBuffer()), 5 * specs.rate, false, true);
  52.     if(rv < 0)
  53.         return;
  54.  
  55.     rv = ps_end_utt(ps);
  56.     if (rv < 0)
  57.         return;
  58.  
  59.     hyp = ps_get_hyp(ps, &score, &uttid);
  60.  
  61.     QMessageBox msgBox;
  62.  
  63.     if(!hyp)
  64.         msgBox.setText("No clue what you are talking about.");
  65.     else
  66.         msgBox.setText(hyp);
  67.  
  68.     msgBox.exec();
  69.  
  70.     ps_free(ps);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement