예제 #1
0
파일: jfunc.c 프로젝트: xawirq/julius
/**
 * <EN>
 * @brief  Instanciate / generate a new engine instance according
 * to the given global configuration instance.
 *
 * It inspects all parameters in the global configuration instance, load
 * all models into memory, build tree lexicons, allocate work area and
 * caches.  It does all setup to start recognition except A/D-in
 * initialization.
 * </EN>
 *
 * <JA>
 * @brief  与えられた設定インスタンス内の情報に従って,新たな
 * エンジンインスタンスを 起動・生成する.
 *
 * 設定インスタンス内のパラメータのチェック後,モデルを読み込み,木構
 * 造化辞書の生成,ワークエリアおよびキャッシュの確保などを行う.
 * A/D-in の初期化以外で認識を開始するのに必要な処理をすべて行う.
 * </JA>
 *
 * @param jconf [in] gloabl configuration instance
 *
 * @return the newly created engine instance.
 *
 * @callgraph
 * @callergraph
 * @ingroup instance
 */
Recog *
j_create_instance_from_jconf(Jconf *jconf)
{
    Recog *recog;

    /* check option values and set parameters needed for model loading */
    if (j_jconf_finalize(jconf) == FALSE) {
        return NULL;
    }

    /* 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) {
        jlog("ERROR: j_create_instance_from_jconf: error in loading model\n");
        /* j_model_free(model); */
        return NULL;
    }

    /* checkout for recognition: build lexicon tree, allocate cache */
    if (j_final_fusion(recog) == FALSE) {
        jlog("ERROR: j_create_instance_from_jconf: error while setup for recognition\n");
        j_recog_free(recog);
        return NULL;
    }

    return recog;
}
예제 #2
0
파일: jfunc.c 프로젝트: xawirq/julius
/**
 * <EN>
 * @brief  Create a new recognizer with a new LM and SR configurations.
 *
 * This function creates new LM process instance and recognition process
 * instance corresponding to the given LM and SR configurations.
 * AM process to be assigned to them is the current default AM.
 * Both the new LM and SR will be assigned the same instance name.
 * </EN>
 * <JA>
 * @brief  LM および SR 設定に基づき認識処理プロセスを追加する.
 *
 * この関数は与えられたLM設定およびSR設定データに基づき,新たな
 * LMインスタンスおよび認識プロセスインスタンスをエンジン内部に
 * 生成する. AMについては現在のデフォルトAMが自動的に用いられる.
 * 名前はLMインスタンス,認識プロセスインスタンスとも同じ名前が
 * あたえられる.
 * </JA>
 *
 * @param recog [i/o] engine instance
 * @param lmconf [in] a new LM configuration
 * @param sconf [in] a new SR configuration
 * @param name [in] name of the new instances
 *
 * @return TRUE on success, FALSE on error.
 *
 * @callgraph
 * @callergraph
 * @ingroup jfunc_process
 */
boolean
j_process_add_lm(Recog *recog, JCONF_LM *lmconf, JCONF_SEARCH *sconf, char *name)
{
    /* add lmconf to global config */
    if (j_jconf_lm_regist(recog->jconf, lmconf, name) == FALSE) {
        jlog("ERROR: j_process_add_lm: failed to regist new LM conf as \"%s\"\n", name);
        return FALSE;
    }
    /* assign lmconf and default amconf to the sconf */
    sconf->amconf = j_get_amconf_default(recog->jconf);
    sconf->lmconf = lmconf;
    /* add the sconf to global config */
    if (j_jconf_search_regist(recog->jconf, sconf, name) == FALSE) {
        jlog("ERROR: j_process_add_lm: failed to regist new SR conf as \"%s\"\n", name);
        j_jconf_search_free(sconf);
        return FALSE;
    }
    /* finalize the whole parameters */
    if (j_jconf_finalize(recog->jconf) == FALSE) {
        jlog("ERROR: j_process_add_lm: failed to finalize the updated whole jconf\n");
        return FALSE;
    }
    /* create LM process intance for the lmconf, and load LM */
    if (j_load_lm(recog, lmconf) == FALSE) {
        jlog("ERROR: j_process_add_lm: failed to load LM \"%s\"\n", lmconf->name);
        return FALSE;
    }
    /* create recognition process instance for the sconf, and setup for recognition */
    if (j_launch_recognition_instance(recog, sconf) == FALSE) {
        jlog("ERROR: j_process_add_lm: failed to start a new recognizer instance \"%s\"\n", sconf->name);
        return FALSE;
    }
    /* the created process will be live=FALSE, active = 1, so
       the new recognition instance is dead now but
       will be made live at next session */

    /* tell engine to update */
    recog->process_want_reload = TRUE;

    return TRUE;
}
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;
}
예제 #4
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);
}