Esempio n. 1
0
bool SpRecog::start(Application *app)
{
  if (j_adin_init(this->recog) == FALSE) {    
    app->tell("error in j_adin_init");
    return false;
  }
  j_recog_info(this->recog); /* output system information to log */
  switch(j_open_stream(this->recog, NULL)) {
  case 0:			
    app->tell("ok j_open_stream");
    break;
  case -1:      		
    app->tell("error in input stream");
    return false;
  case -2:
    app->tell("error in beginning input");
    return false;
  }
  fflush(this->srm_log_fp);
  int ret = j_recognize_stream(this->recog);
  if (ret == -1) {
    app->tell("error j_recognize_stream");
    return false;
  }
  return true;
}
Esempio n. 2
0
void SpeechSystem::setup()
{
	// ewwww
	jconf = j_config_load_file_new(const_cast<char*>(jconf_filename.c_str()));

	/* 2. create recognition instance according to the jconf */
	/* it loads models, setup final parameters, build lexicon
     and set up work area for recognition */
	recog = j_create_instance_from_jconf(jconf);
	if (recog == NULL)
	{
		fprintf(stderr, "Error in startup\n");
		return;
	}

	/*********************/
	/* Register callback */
	/*********************/
	/* register result callback functions */
	callback_add(recog, CALLBACK_EVENT_SPEECH_READY, recready, this);
	callback_add(recog, CALLBACK_EVENT_SPEECH_START, recstart, this);
	callback_add(recog, CALLBACK_RESULT, recdone, this);

	/**************************/
	/* Initialize audio input */
	/**************************/
	/* initialize audio input device */
	/* ad-in thread starts at this time for microphone */
	if (j_adin_init(recog) == FALSE) {    /* error */
		return;
	}

//#ifdef JULIUS_DEBUG
	/* output system information to log */
	j_recog_info(recog);
//#endif

	/***********************************/
	/* Open input stream and recognize */
	/***********************************/
	/* raw speech input (microphone etc.) */

	switch(j_open_stream(recog, NULL)) {
		case 0:			/* succeeded */
			break;
		case -1:      		/* error */
			fprintf(stderr, "error in input stream\n");
			return;
		case -2:			/* end of recognition process */
			fprintf(stderr, "failed to begin input stream\n");
			return;
	}

	startThread(true, false); // blocking, verbose
}
Esempio n. 3
0
int
main(int argc, char *argv[])
{
  FILE *fp;
  Recog *recog;
  Jconf *jconf;

  /* inihibit system log output (default: stdout) */
  //jlog_set_output(NULL);
  /* output system log to a file */
  // FILE *fp = fopen(logfile, "w"); jlog_set_output(fp);

  /* if no option argument, output julius usage and exit */
  if (argc == 1) {
    fprintf(stderr, "Julius rev.%s - based on ", JULIUS_VERSION);
    j_put_version(stderr);
    fprintf(stderr, "Try '-setting' for built-in engine configuration.\n");
    fprintf(stderr, "Try '-help' for run time options.\n");
    return -1;
  }

  /* add application options */
  record_add_option();
  module_add_option();
  charconv_add_option();
  j_add_option("-separatescore", 0, 0, "output AM and LM scores separately", opt_separatescore);
  j_add_option("-logfile", 1, 1, "output log to file", opt_logfile);
  j_add_option("-nolog", 0, 0, "not output any log", opt_nolog);
  j_add_option("-outfile", 0, 0, "save result in separate .out file", opt_outfile);
  j_add_option("-help", 0, 0, "display this help", opt_help);
  j_add_option("--help", 0, 0, "display this help", opt_help);

  /* create a configuration variables container */
  jconf = j_jconf_new();
  // j_config_load_file(jconf, jconffile);
  if (j_config_load_args(jconf, argc, argv) == -1) {
    fprintf(stderr, "Try `-help' for more information.\n");
    return -1;
  }

  /* output system log to a file */
  if (nolog) {
    jlog_set_output(NULL);
  } else if (logfile) {
    fp = fopen(logfile, "w");
    jlog_set_output(fp);
  }

  /* here you can set/modify any parameter in the jconf before setup */
  // jconf->input.input_speech = SP_MIC;

  /* Fixate jconf parameters: it checks whether the jconf parameters
     are suitable for recognition or not, and set some internal
     parameters according to the values for recognition.  Modifying
     a value in jconf after this function may be errorous.
  */
  if (j_jconf_finalize(jconf) == FALSE) {
    if (logfile) fclose(fp);
    return -1;
  }

  /* create a recognition instance */
  recog = j_recog_new();
  /* assign configuration to the instance */
  recog->jconf = jconf;
  /* load all files according to the configurations */
  if (j_load_all(recog, jconf) == FALSE) {
    fprintf(stderr, "ERROR: Error in loading model\n");
    if (logfile) fclose(fp);
    return -1;
  }
  
#ifdef USER_LM_TEST
  {
    PROCESS_LM *lm;
    for(lm=recog->lmlist;lm;lm=lm->next) {
      if (lm->lmtype == LM_PROB) {
	j_regist_user_lm_func(lm, my_uni, my_bi, my_lm);
      }
    }
#endif

  /* checkout for recognition: build lexicon tree, allocate cache */
  if (j_final_fusion(recog) == FALSE) {
    fprintf(stderr, "ERROR: Error while setup work area for recognition\n");
    j_recog_free(recog);
    if (logfile) fclose(fp);
    return -1;
  }
  
  /* Set up some application functions */
  /* set character conversion mode */
  if (charconv_setup() == FALSE) {
    if (logfile) fclose(fp);
    return -1;
  }
  if (is_module_mode()) {
    /* set up for module mode */
    /* register result output callback functions to network module */
    module_setup(recog, NULL);
  } else {
    /* register result output callback functions to stdout */
    setup_output_tty(recog, NULL);
  }
  /* if -outfile option specified, callbacks for file output will be
     regitered */
  if (outfile_enabled) {
    if (jconf->input.speech_input == SP_MFCFILE || jconf->input.speech_input == SP_RAWFILE) {
      setup_output_file(recog, NULL);
    } else {
      fprintf(stderr, "Warning: -outfile works only for file input, disabled now\n");
      outfile_enabled = FALSE;
    }
  }

  /* setup recording if option was specified */
  record_setup(recog, NULL);

  /* on module connect with client */
  if (is_module_mode()) module_server();

  /* initialize and standby the specified audio input source */
  /* for microphone or other threaded input, ad-in thread starts here */
  if (j_adin_init(recog) == FALSE) return;

  /* output system information to log */
  j_recog_info(recog);

#ifdef VISUALIZE
  /* Visualize: initialize GTK */
  visual_init(recog);
  callback_add(recog, CALLBACK_EVENT_RECOGNITION_END, visual_show, NULL);
  callback_add(recog, CALLBACK_EVENT_PASS2_BEGIN, visual2_init, NULL);
  callback_add(recog, CALLBACK_DEBUG_PASS2_POP, visual2_popped, NULL);
  callback_add(recog, CALLBACK_DEBUG_PASS2_PUSH, visual2_next_word, NULL);
  /* below should be called at result */
  visual2_best(now, winfo);
  /* 音声取り込みはコールバックで新規作成 */
  /* 第2パスで認識結果出力時に以下を実行 */
  visual2_best(now, recog->model->winfo);
#endif
  
  /* if no grammar specified on startup, start with pause status */
  {
    RecogProcess *r;
    boolean ok_p;
    ok_p = TRUE;
    for(r=recog->process_list;r;r=r->next) {
      if (r->lmtype == LM_DFA) {
	if (r->lm->winfo == NULL) { /* stop when no grammar found */
	  j_request_pause(recog);
	}
      }
    }
  }

  /* enter recongnition loop */
  main_recognition_stream_loop(recog);

  /* end proc */
  if (is_module_mode()) module_disconnect();

  /* release all */
  j_recog_free(recog);

  if (logfile) fclose(fp);
  return(0);
}
int cTumkwsjSink::setupJulius()
{
  try {

    int argc=3;
    char* argv[3] = {"julius","-C",NULL};
    if (configfile != NULL)
      argv[2]=strdup(configfile);
    else
      argv[2]=strdup("kws.cfg");

    /* add application option dummies */
    /*
    j_add_option("-gprob", 1, 1, "garbage probability", opt_gprob);
    j_add_option("-kprob", 1, 1, "keyword probability", opt_kprob);
    j_add_option("-knum", 1, 1, "number of keywords", opt_knum);
    */  

    /* create a configuration variables container */
    jconf = j_jconf_new();
    //    j_config_load_file(jconf, strdup(configfile));
    if (j_config_load_args(jconf, argc, argv) == -1) {
      COMP_ERR("error parsing julius decoder options, this might be a bug. see tumkwsjSink.cpp!");
      j_jconf_free(jconf);
      free(argv[2]);
      return 0;
    }
    free(argv[2]);

    /* output system log to a file */
    if (getInt("debug") != 1) {
      jlog_set_output(NULL);
    }

    /* here you can set/modify any parameter in the jconf before setup */
    jconf->input.type = INPUT_VECTOR;
    jconf->input.speech_input = SP_EXTERN;
    jconf->decodeopt.realtime_flag = TRUE; // ??? 
    jconf->ext.period = (float)(reader->getLevelT());
    jconf->ext.veclen = reader->getLevelN();
    jconf->ext.userptr = (void *)this;
    jconf->ext.fv_read = external_fv_read_loader;

    /* Fixate jconf parameters: it checks whether the jconf parameters
    are suitable for recognition or not, and set some internal
    parameters according to the values for recognition.  Modifying
    a value in jconf after this function may be errorous.
    */
    if (j_jconf_finalize(jconf) == FALSE) {
      SMILE_IERR(1,"error finalising julius jconf in setupJulius()!");
      j_jconf_free(jconf);
      return 0;
    }

    /* create a recognition instance */
    recog = j_recog_new();
    /* use user-definable data hook to set a pointer to our class instance */
    recog->hook = (void *)this;
    /* assign configuration to the instance */
    recog->jconf = jconf;
    /* load all files according to the configurations */
    if (j_load_all(recog, jconf) == FALSE) {
      SMILE_IERR(1, "Error in loading model for Julius decoder");
      j_recog_free(recog);
      return 0;
    }

    SMILE_IMSG(2,"garbage prob.: %f",glogprob);
    SMILE_IMSG(2,"keyword prob.: %f",klogprob);
    SMILE_IMSG(2,"number of phonemes: %i",numphon);

    // register user LM, get vocab size, and init lmWeights with zero or load from file
    PROCESS_LM *lm; 
    for(lm=recog->lmlist;lm;lm=lm->next) {
      if (lm->lmtype == LM_PROB) {
        lm->winfo->userptr = (void*)this;  // PATCH: sent/vocabulary.h (WORD_INFO struct): ++   void * userptr;   // Pointer to userdata....
        j_regist_user_lm_func(lm, userlm_uni_loader, userlm_bi_loader, userlm_lm_loader);
        if ((numWords==0)&&((long)(lm->winfo->num)>0)) {
          lmWinfo = lm->winfo;
          numWords = (long)(lm->winfo->num);
        }
      }
    }

    // load lmWeights data:
    //printf("XXX HEREA");
    if (dynamicLm) {
      //printf("XXX HERE0");
      if (lmWinfo != NULL) {
        //printf("XXX HERE");
        lmWeights = (LOGPROB*)malloc(sizeof(LOGPROB)*numWords);
        if (lmWeights == NULL) { OUT_OF_MEMORY; }
        int i;
        for (i=0; i<numWords; i++) { lmWeights[i] = (LOGPROB)lmpenalty; }
        const char *lmfile = getStr("lmfile");
        if (lmfile != NULL) {
          FILE *lf=fopen(lmfile,"r");
          if (lf == NULL) { SMILE_IERR(1,"Error opening word weights file (lmfile) '%s'",lmfile); }
          else  {
            SMILE_IMSG(1,"lmfile: '%s'",lmfile);
            long lineNr = 0;
            char *line=NULL; size_t len=0;
            size_t r=-1;
            do {      
              r = getline(&line, &len, lf);
              //printf("XXX LINE '%s'",line);
              if ((r != (size_t)-1)&&(line!=NULL)) {
                lineNr++;
                parseLmWeightsLine(line,lineNr,lmfile);
              } 
            } while (r != (size_t)-1);
          }
        }

      } else { 
        SMILE_IERR(1,"no language model word info (vocabulary) found, check julius config!"); 
      }
    }
    //----

    /* checkout for recognition: build lexicon tree, allocate cache */
    if (j_final_fusion(recog) == FALSE) {
      fprintf(stderr, "ERROR: Error while setup work area for recognition\n");
      j_recog_free(recog);
      if (logfile) fclose(fp);
      return 0;
    }

    setupCallbacks(recog, NULL);

    /* output system information to log */
    j_recog_info(recog);

    terminated = FALSE;

  }
  catch (int) { }

  juliusIsSetup=1;

  return 1;
}
Esempio n. 5
0
int main(int argc, char* argv[])
{
	// Jconf: configuration parameters
	// load configurations from command arguments
	Jconf *jconf = j_config_load_args_new(argc, argv);
	if (jconf == NULL) {
		std::cout << "Error @ j_config_load_args_new" << std::endl;
		return -1;
	}

	// Recog: Top level instance for the whole recognition process
	// create recognition instance according to the jconf
	Recog *recog = j_create_instance_from_jconf(jconf);
	if (recog == NULL) {
		std::cout << "Error @ j_create_instance_from_jconf" << std::endl;
		return -1;
	}

	// Regster callback
	callback_add(recog, CALLBACK_EVENT_SPEECH_READY, [](Recog *recog, void*) {
		std::cout << "<<< PLEASE SPEAK! >>>" << std::endl;
	}, NULL);

	callback_add(recog, CALLBACK_EVENT_SPEECH_START, [](Recog *recog, void*) {
		std::cout << "...SPEECH START..." << std::endl;
	}, NULL);

	callback_add(recog, CALLBACK_RESULT, [](Recog *recog, void*) {
		for (const RecogProcess *r = recog->process_list; r; r = r->next) {
			WORD_INFO *winfo = r->lm->winfo;
			for (int n = 0; n < r->result.sentnum; ++n) {
				Sentence *s   = &(r->result.sent[n]);
				WORD_ID *seq = s->word;
				int seqnum   = s->word_num;
				for (int i = 0; i < seqnum; ++i) {
					std::cout << winfo->woutput[seq[i]];
				}
			}
		}
	}, NULL);

	// Initialize audio input
	if (j_adin_init(recog) == FALSE) {
		return -1;
	}

	// output system information to log
	j_recog_info(recog);

	// Open input stream and recognize
	switch (j_open_stream(recog, NULL)) {
		case  0: break; // success
		case -1: std::cout << "Error in input stream" << std::endl; return -1;
		case -2: std::cout << "Failed to begin input stream" << std::endl; return -1;
	}

	// Recognition loop
	int ret = j_recognize_stream(recog);
	if (ret == -1) return -1;

	// exit
	j_close_stream(recog);
	j_recog_free(recog);

	return 0;
}