Guest User

Untitled

a guest
Jan 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. // g++ -O3 -std=c++11 -o ps_boilerplate mic.cpp \
  2. -DMODELDIR=\"`pkg-config --variable=modeldir pocketsphinx`\" \
  3. `pkg-config --cflags --libs pocketsphinx sphinxbase`
  4. #include <iostream>
  5. #include <string>
  6. #include <pocketsphinx.h>
  7. #include <sphinxbase/ad.h>
  8. #include <sphinxbase/err.h>
  9.  
  10. using namespace std;
  11.  
  12. void recognize_from_microphone();
  13.  
  14. ps_decoder_t *ps; // create pocketsphinx decoder structure
  15. cmd_ln_t *config; // create configuration structure
  16. ad_rec_t *ad; // create audio recording structure - for use with ALSA functions
  17.  
  18. int16 adbuf[4096]; // buffer array to hold audio data
  19. uint8 utt_started, in_speech; // flags for tracking active speech - has speech started? - is speech currently happening?
  20. int32 k; // holds the number of frames in the audio buffer
  21. // char const *hyp; // pointer to "hypothesis" (best guess at the decoded result)
  22.  
  23.  
  24. int main(int argc, char *argv[]) {
  25.  
  26. config = cmd_ln_init(NULL, ps_args(), TRUE, // Load the configuration structure - ps_args() passes the default values
  27. "-hmm", MODELDIR "/en-us/en-us",
  28. "-kws", "./keyphrase.file",
  29. "-dict", MODELDIR "/en-us/cmudict-en-us.dict",
  30. "-logfn", "/dev/null", // suppress log info from being sent to screen
  31. NULL);
  32.  
  33. ps = ps_init(config); // initialize the pocketsphinx decoder
  34. ad = ad_open_dev("sysdefault", (int) cmd_ln_float32_r(config, "-samprate")); // open default microphone at default samplerate
  35.  
  36. while(1){
  37. // string decoded_speech = recognize_from_microphone(); // call the function to capture and decode speech
  38. // cout << "Decoded Speech: "<< decoded_speech << "\n" <<endl; // send decoded speech to screen
  39. cout << "calling recognize_from_microphone " <<endl;
  40. recognize_from_microphone(); // call the function to capture and decode speech
  41.  
  42. }
  43.  
  44. ad_close(ad); // close the microphone
  45. }
  46.  
  47. void recognize_from_microphone(){
  48. const char* hyp;
  49. string wakeup("zane");
  50. ad_start_rec(ad); // start recording
  51. ps_start_utt(ps); // mark the start of the utterance
  52. utt_started = FALSE; // clear the utt_started flag
  53.  
  54. while(1) {
  55. k = ad_read(ad, adbuf, 4096); // capture the number of frames in the audio buffer
  56. ps_process_raw(ps, adbuf, k, FALSE, FALSE); // send the audio buffer to the pocketsphinx decoder
  57.  
  58. in_speech = ps_get_in_speech(ps); // test to see if speech is being detected
  59.  
  60. if (in_speech && !utt_started) { // if speech has started and utt_started flag is false
  61. utt_started = TRUE; // then set the flag
  62. }
  63.  
  64. if (!in_speech && utt_started) { // if speech has ended and the utt_started flag is true
  65. ps_end_utt(ps); // then mark the end of the utterance
  66. ad_stop_rec(ad); // stop recording
  67. hyp = ps_get_hyp(ps, NULL ); // query pocketsphinx for "hypothesis" of decoded statement
  68. // return hyp; // the function returns the hypothesis
  69. // cout << "Decoded Speech: "<< hyp << "\n" <<endl; // send decoded speech to screen
  70. if (hyp != NULL)
  71. {
  72. string str(hyp);
  73. if (str.compare(wakeup) != 0) {
  74. // printf("Recognized: %s\n", hyp);
  75. cout << "Wake up word detected" << endl;
  76. }
  77. }
  78. break; // exit the while loop and return to main
  79. }
  80. }
  81.  
  82. }
Add Comment
Please, Sign In to add comment