1 /*
2  * Copyright 2008-2014 Arsen Chaloyan
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Id: main.c 2213 2014-11-06 03:02:45Z achaloyan@gmail.com $
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <apr_getopt.h>
22 #include <apr_file_info.h>
23 #include <apr_thread_proc.h>
24 #include "asr_engine.h"
25 
26 typedef struct {
27 	const char        *root_dir_path;
28 	apt_log_priority_e log_priority;
29 	apt_log_output_e   log_output;
30 	apr_pool_t        *pool;
31 } client_options_t;
32 
33 typedef struct {
34 	asr_engine_t      *engine;
35 	const char        *grammar_file;
36 	const char        *input_file;
37 	const char        *profile;
38 
39 	apr_thread_t      *thread;
40 	apr_pool_t        *pool;
41 } asr_params_t;
42 
43 /** Thread function to run ASR scenario in */
asr_session_run(apr_thread_t * thread,void * data)44 static void* APR_THREAD_FUNC asr_session_run(apr_thread_t *thread, void *data)
45 {
46 	asr_params_t *params = data;
47 	asr_session_t *session = asr_session_create(params->engine,params->profile);
48 	if(session) {
49 		const char *result = asr_session_file_recognize(session,params->grammar_file,params->input_file);
50 		if(result) {
51 			printf("Recog Result [%s]",result);
52 		}
53 
54 		asr_session_destroy(session);
55 	}
56 
57 	/* destroy pool params allocated from */
58 	apr_pool_destroy(params->pool);
59 	return NULL;
60 }
61 
62 /** Launch demo ASR session */
asr_session_launch(asr_engine_t * engine,const char * grammar_file,const char * input_file,const char * profile)63 static apt_bool_t asr_session_launch(asr_engine_t *engine, const char *grammar_file, const char *input_file, const char *profile)
64 {
65 	apr_pool_t *pool;
66 	asr_params_t *params;
67 
68 	/* create pool to allocate params from */
69 	apr_pool_create(&pool,NULL);
70 	params = apr_palloc(pool,sizeof(asr_params_t));
71 	params->pool = pool;
72 	params->engine = engine;
73 
74 	if(grammar_file) {
75 		params->grammar_file = apr_pstrdup(pool,grammar_file);
76 	}
77 	else {
78 		params->grammar_file = "grammar.xml";
79 	}
80 
81 	if(input_file) {
82 		params->input_file = apr_pstrdup(pool,input_file);
83 	}
84 	else {
85 		params->input_file = "one-8kHz.pcm";
86 	}
87 
88 	if(profile) {
89 		params->profile = apr_pstrdup(pool,profile);
90 	}
91 	else {
92 		params->profile = "uni2";
93 	}
94 
95 	/* Launch a thread to run demo ASR session in */
96 	if(apr_thread_create(&params->thread,NULL,asr_session_run,params,pool) != APR_SUCCESS) {
97 		apr_pool_destroy(pool);
98 		return FALSE;
99 	}
100 
101 	return TRUE;
102 }
103 
cmdline_process(asr_engine_t * engine,char * cmdline)104 static apt_bool_t cmdline_process(asr_engine_t *engine, char *cmdline)
105 {
106 	apt_bool_t running = TRUE;
107 	char *name;
108 	char *last;
109 	name = apr_strtok(cmdline, " ", &last);
110 
111 	if(strcasecmp(name,"run") == 0) {
112 		char *grammar = apr_strtok(NULL, " ", &last);
113 		char *input = apr_strtok(NULL, " ", &last);
114 		char *profile = apr_strtok(NULL, " ", &last);
115 		asr_session_launch(engine,grammar,input,profile);
116 	}
117 	else if(strcasecmp(name,"loglevel") == 0) {
118 		char *priority = apr_strtok(NULL, " ", &last);
119 		if(priority) {
120 			asr_engine_log_priority_set(atol(priority));
121 		}
122 	}
123 	else if(strcasecmp(name,"exit") == 0 || strcmp(name,"quit") == 0) {
124 		running = FALSE;
125 	}
126 	else if(strcasecmp(name,"help") == 0) {
127 		printf("usage:\n"
128 			"\n- run [grammar_file] [audio_input_file] [profile_name] (run demo asr client)\n"
129 			"       grammar_file is the name of grammar file, (path is relative to data dir)\n"
130 			"       audio_input_file is the name of audio file, (path is relative to data dir)\n"
131 			"       profile_name is one of 'uni2', 'uni1', ...\n"
132 			"\n       examples: \n"
133 			"           run\n"
134 			"           run grammar.xml one.pcm\n"
135 			"           run grammar.xml one.pcm uni1\n"
136 		    "\n- loglevel [level] (set loglevel, one of 0,1...7)\n"
137 		    "\n- quit, exit\n");
138 	}
139 	else {
140 		printf("unknown command: %s (input help for usage)\n",name);
141 	}
142 	return running;
143 }
144 
cmdline_run(asr_engine_t * engine)145 static apt_bool_t cmdline_run(asr_engine_t *engine)
146 {
147 	apt_bool_t running = TRUE;
148 	char cmdline[1024];
149 	apr_size_t i;
150 	do {
151 		printf(">");
152 		memset(&cmdline, 0, sizeof(cmdline));
153 		for(i = 0; i < sizeof(cmdline); i++) {
154 			cmdline[i] = (char) getchar();
155 			if(cmdline[i] == '\n') {
156 				cmdline[i] = '\0';
157 				break;
158 			}
159 		}
160 		if(*cmdline) {
161 			running = cmdline_process(engine,cmdline);
162 		}
163 	}
164 	while(running != 0);
165 	return TRUE;
166 }
167 
usage(void)168 static void usage(void)
169 {
170 	printf(
171 		"\n"
172 		"Usage:\n"
173 		"\n"
174 		"  asrclient [options]\n"
175 		"\n"
176 		"  Available options:\n"
177 		"\n"
178 		"   -r [--root-dir] path     : Set the project root directory path.\n"
179 		"\n"
180 		"   -l [--log-prio] priority : Set the log priority.\n"
181 		"                              (0-emergency, ..., 7-debug)\n"
182 		"\n"
183 		"   -o [--log-output] mode   : Set the log output mode.\n"
184 		"                              (0-none, 1-console only, 2-file only, 3-both)\n"
185 		"\n"
186 		"   -h [--help]              : Show the help.\n"
187 		"\n");
188 }
189 
options_destroy(client_options_t * options)190 static void options_destroy(client_options_t *options)
191 {
192 	if(options->pool) {
193 		apr_pool_destroy(options->pool);
194 	}
195 }
196 
options_load(int argc,const char * const * argv)197 static client_options_t* options_load(int argc, const char * const *argv)
198 {
199 	apr_status_t rv;
200 	apr_getopt_t *opt = NULL;
201 	int optch;
202 	const char *optarg;
203 	apr_pool_t *pool;
204 	client_options_t *options;
205 
206 	const apr_getopt_option_t opt_option[] = {
207 		/* long-option, short-option, has-arg flag, description */
208 		{ "root-dir",    'r', TRUE,  "path to root dir" },  /* -r arg or --root-dir arg */
209 		{ "log-prio",    'l', TRUE,  "log priority" },      /* -l arg or --log-prio arg */
210 		{ "log-output",  'o', TRUE,  "log output mode" },   /* -o arg or --log-output arg */
211 		{ "help",        'h', FALSE, "show help" },         /* -h or --help */
212 		{ NULL, 0, 0, NULL },                               /* end */
213 	};
214 
215 	/* create APR pool to allocate options from */
216 	apr_pool_create(&pool,NULL);
217 	if(!pool) {
218 		return NULL;
219 	}
220 	options = apr_palloc(pool,sizeof(client_options_t));
221 	options->pool = pool;
222 	/* set the default options */
223 	options->root_dir_path = NULL;
224 	options->log_priority = APT_PRIO_INFO;
225 	options->log_output = APT_LOG_OUTPUT_CONSOLE;
226 
227 
228 	rv = apr_getopt_init(&opt, pool , argc, argv);
229 	if(rv != APR_SUCCESS) {
230 		options_destroy(options);
231 		return NULL;
232 	}
233 
234 	while((rv = apr_getopt_long(opt, opt_option, &optch, &optarg)) == APR_SUCCESS) {
235 		switch(optch) {
236 			case 'r':
237 				options->root_dir_path = optarg;
238 				break;
239 			case 'l':
240 				if(optarg) {
241 					options->log_priority = atoi(optarg);
242 				}
243 				break;
244 			case 'o':
245 				if(optarg) {
246 					options->log_output = atoi(optarg);
247 				}
248 				break;
249 			case 'h':
250 				usage();
251 				return FALSE;
252 		}
253 	}
254 
255 	if(rv != APR_EOF) {
256 		usage();
257 		options_destroy(options);
258 		return NULL;
259 	}
260 
261 	return options;
262 }
263 
main(int argc,const char * const * argv)264 int main(int argc, const char * const *argv)
265 {
266 	client_options_t *options;
267 	asr_engine_t *engine;
268 
269 	/* APR global initialization */
270 	if(apr_initialize() != APR_SUCCESS) {
271 		apr_terminate();
272 		return 0;
273 	}
274 
275 	/* load options */
276 	options = options_load(argc,argv);
277 	if(!options) {
278 		apr_terminate();
279 		return 0;
280 	}
281 
282 	/* create asr engine */
283 	engine = asr_engine_create(
284 				options->root_dir_path,
285 				options->log_priority,
286 				options->log_output);
287 	if(engine) {
288 		/* run command line  */
289 		cmdline_run(engine);
290 		/* destroy demo framework */
291 		asr_engine_destroy(engine);
292 	}
293 
294 	/* destroy options */
295 	options_destroy(options);
296 
297 	/* APR global termination */
298 	apr_terminate();
299 	return 0;
300 }
301