julius/main.c

Go to the documentation of this file.
00001 
00018 /*
00019  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
00020  * Copyright (c) 1997-2000 Information-technology Promotion Agency, Japan
00021  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00022  * Copyright (c) 2005-2007 Julius project team, Nagoya Institute of Technology
00023  * All rights reserved
00024  */
00025 
00026 #include "app.h"
00027 
00028 boolean separate_score_flag = FALSE;
00029 boolean callback_debug_flag = FALSE;
00030 boolean outfile_enabled = FALSE;
00031 
00032 static char *logfile = NULL;
00033 static boolean nolog = FALSE;
00034 
00035 /************************************************************************/
00040 static boolean
00041 opt_help(Jconf *jconf, char *arg[], int argnum)
00042 {
00043   fprintf(stderr, "Julius rev.%s - based on ", JULIUS_VERSION);
00044   j_output_argument_help(stderr);
00045   exit(1);                      /* terminates here! */
00046   return TRUE;
00047 }
00048 static boolean
00049 opt_separatescore(Jconf *jconf, char *arg[], int argnum)
00050 {
00051   separate_score_flag = TRUE;
00052   return TRUE;
00053 }
00054 static boolean
00055 opt_callbackdebug(Jconf *jconf, char *arg[], int argnum)
00056 {
00057   callback_debug_flag = TRUE;
00058   return TRUE;
00059 }
00060 static boolean
00061 opt_logfile(Jconf *jconf, char *arg[], int argnum)
00062 {
00063   logfile = (char *)malloc(strlen(arg[0]) + 1);
00064   strcpy(logfile, arg[0]);
00065   return TRUE;
00066 }
00067 static boolean
00068 opt_nolog(Jconf *jconf, char *arg[], int argnum)
00069 {
00070   nolog = TRUE;
00071   return TRUE;
00072 }
00073 static boolean
00074 opt_outfile(Jconf *jconf, char *arg[], int argnum)
00075 {
00076   outfile_enabled = TRUE;
00077   return TRUE;
00078 }
00079    
00080 /**********************************************************************/
00081 int
00082 main(int argc, char *argv[])
00083 {
00084   FILE *fp;
00085   Recog *recog;
00086   Jconf *jconf;
00087 
00088   /* inihibit system log output (default: stdout) */
00089   //jlog_set_output(NULL);
00090   /* output system log to a file */
00091   // FILE *fp = fopen(logfile, "w"); jlog_set_output(fp);
00092 
00093   /* if no option argument, output julius usage and exit */
00094   if (argc == 1) {
00095     fprintf(stderr, "Julius rev.%s - based on ", JULIUS_VERSION);
00096     j_put_version(stderr);
00097     fprintf(stderr, "Try '-setting' for built-in engine configuration.\n");
00098     fprintf(stderr, "Try '-help' for run time options.\n");
00099     return -1;
00100   }
00101 
00102   /* add application options */
00103   record_add_option();
00104   module_add_option();
00105   charconv_add_option();
00106   j_add_option("-separatescore", 0, 0, "output AM and LM scores separately", opt_separatescore);
00107   j_add_option("-callbackdebug", 0, 0, "output callback debug message", opt_callbackdebug);
00108   j_add_option("-logfile", 1, 1, "output log to file", opt_logfile);
00109   j_add_option("-nolog", 0, 0, "not output any log", opt_nolog);
00110   j_add_option("-outfile", 0, 0, "save result in separate .out file", opt_outfile);
00111   j_add_option("-help", 0, 0, "display this help", opt_help);
00112   j_add_option("--help", 0, 0, "display this help", opt_help);
00113 
00114   /* create a configuration variables container */
00115   jconf = j_jconf_new();
00116   // j_config_load_file(jconf, jconffile);
00117   if (j_config_load_args(jconf, argc, argv) == -1) {
00118     fprintf(stderr, "Try `-help' for more information.\n");
00119     return -1;
00120   }
00121 
00122   /* output system log to a file */
00123   if (nolog) {
00124     jlog_set_output(NULL);
00125   } else if (logfile) {
00126     fp = fopen(logfile, "w");
00127     jlog_set_output(fp);
00128   }
00129 
00130   /* here you can set/modify any parameter in the jconf before setup */
00131   // jconf->input.input_speech = SP_MIC;
00132 
00133   /* Fixate jconf parameters: it checks whether the jconf parameters
00134      are suitable for recognition or not, and set some internal
00135      parameters according to the values for recognition.  Modifying
00136      a value in jconf after this function may be errorous.
00137   */
00138   if (j_jconf_finalize(jconf) == FALSE) {
00139     if (logfile) fclose(fp);
00140     return -1;
00141   }
00142 
00143   /* create a recognition instance */
00144   recog = j_recog_new();
00145   /* assign configuration to the instance */
00146   recog->jconf = jconf;
00147   /* load all files according to the configurations */
00148   if (j_load_all(recog, jconf) == FALSE) {
00149     fprintf(stderr, "ERROR: Error in loading model\n");
00150     if (logfile) fclose(fp);
00151     return -1;
00152   }
00153   
00154 #ifdef USER_LM_TEST
00155   {
00156     PROCESS_LM *lm;
00157     for(lm=recog->lmlist;lm;lm=lm->next) {
00158       if (lm->lmtype == LM_PROB) {
00159         j_regist_user_lm_func(lm, my_uni, my_bi, my_lm);
00160       }
00161     }
00162 #endif
00163 
00164   /* checkout for recognition: build lexicon tree, allocate cache */
00165   if (j_final_fusion(recog) == FALSE) {
00166     fprintf(stderr, "ERROR: Error while setup work area for recognition\n");
00167     j_recog_free(recog);
00168     if (logfile) fclose(fp);
00169     return -1;
00170   }
00171   
00172   /* Set up some application functions */
00173   /* set character conversion mode */
00174   if (charconv_setup() == FALSE) {
00175     if (logfile) fclose(fp);
00176     return -1;
00177   }
00178   if (is_module_mode()) {
00179     /* set up for module mode */
00180     /* register result output callback functions to network module */
00181     module_setup(recog, NULL);
00182   } else {
00183     /* register result output callback functions to stdout */
00184     setup_output_tty(recog, NULL);
00185   }
00186   /* if -outfile option specified, callbacks for file output will be
00187      regitered */
00188   if (outfile_enabled) {
00189     if (jconf->input.speech_input == SP_MFCFILE || jconf->input.speech_input == SP_RAWFILE) {
00190       setup_output_file(recog, NULL);
00191     } else {
00192       fprintf(stderr, "Warning: -outfile works only for file input, disabled now\n");
00193       outfile_enabled = FALSE;
00194     }
00195   }
00196 
00197   /* setup recording if option was specified */
00198   record_setup(recog, NULL);
00199 
00200   /* on module connect with client */
00201   if (is_module_mode()) module_server();
00202 
00203   /* initialize and standby the specified audio input source */
00204   /* for microphone or other threaded input, ad-in thread starts here */
00205   if (j_adin_init(recog) == FALSE) return;
00206 
00207   /* output system information to log */
00208   j_recog_info(recog);
00209 
00210 #ifdef VISUALIZE
00211   /* Visualize: initialize GTK */
00212   visual_init(recog);
00213   callback_add(recog, CALLBACK_EVENT_RECOGNITION_END, visual_show, NULL);
00214   callback_add(recog, CALLBACK_EVENT_PASS2_BEGIN, visual2_init, NULL);
00215   callback_add(recog, CALLBACK_DEBUG_PASS2_POP, visual2_popped, NULL);
00216   callback_add(recog, CALLBACK_DEBUG_PASS2_PUSH, visual2_next_word, NULL);
00217   /* below should be called at result */
00218   visual2_best(now, winfo);
00219   /* 音声取り込みはコールバックで新規作成 */
00220   /* 第2パスで認識結果出力時に以下を実行 */
00221   visual2_best(now, recog->model->winfo);
00222 #endif
00223   
00224   /* if no grammar specified on startup, start with pause status */
00225   {
00226     RecogProcess *r;
00227     boolean ok_p;
00228     ok_p = TRUE;
00229     for(r=recog->process_list;r;r=r->next) {
00230       if (r->lmtype == LM_DFA) {
00231         if (r->lm->winfo == NULL) { /* stop when no grammar found */
00232           j_request_pause(recog);
00233         }
00234       }
00235     }
00236   }
00237 
00238   /* enter recongnition loop */
00239   main_recognition_stream_loop(recog);
00240 
00241   /* end proc */
00242   if (is_module_mode()) module_disconnect();
00243 
00244   /* release all */
00245   j_recog_free(recog);
00246 
00247   if (logfile) fclose(fp);
00248   return(0);
00249 }

Generated on Tue Dec 18 15:59:49 2007 for Julius by  doxygen 1.5.4