SRecTest.c revision 8fc5a7f51e62cb4ae44a27bdf4176d04adc80ede
1 2/*---------------------------------------------------------------------------* 3 * SRecTest.c * 4 * * 5 * Copyright 2007, 2008 Nuance Communciations, Inc. * 6 * * 7 * Licensed under the Apache License, Version 2.0 (the 'License'); * 8 * you may not use this file except in compliance with the License. * 9 * * 10 * You may obtain a copy of the License at * 11 * http://www.apache.org/licenses/LICENSE-2.0 * 12 * * 13 * Unless required by applicable law or agreed to in writing, software * 14 * distributed under the License is distributed on an 'AS IS' BASIS, * 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 16 * See the License for the specific language governing permissions and * 17 * limitations under the License. * 18 * * 19 *---------------------------------------------------------------------------*/ 20 21#ifdef _WIN32 22#else 23 #include <sys/select.h> 24#endif 25 26#ifdef _WIN32 27 #include "signal.h" 28 #include "direct.h" 29#endif 30 31#include "passert.h" 32#include "ESR_CommandLine.h" 33#include "ESR_Session.h" 34#include "LCHAR.h" 35#include "PFile.h" 36#include "PFileSystem.h" 37#include "PANSIFileSystem.h" 38//#include "PMemoryFileSystem.h" 39#include "plog.h" 40#include "pmemory.h" 41#include "ptypes.h" 42#include "string.h" 43#include "stdio.h" 44#include "stdlib.h" 45#if defined(APP_ENABLE_TIMER) 46#include "ptimer.h" 47#endif 48#include "SR_Grammar.h" 49#include "SR_Recognizer.h" 50#include "SR_RecognizerResult.h" 51#include "SR_Session.h" 52#include "SR_Vocabulary.h" 53#include "SR_AcousticState.h" 54#include "SR_Nametag.h" 55#include "PStackSize.h" 56 57// #define ACCURACY_TESTING 58#ifdef ACCURACY_TESTING 59#include "SR_GrammarImpl.h" 60#include "SR_SemanticProcessorImpl.h" 61#endif 62 63#include "srec_test_config.h" 64 65//Define this in makefile 66//#define _DEBUGHEAP 67 68 69#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32) 70#include <crtdbg.h> 71#endif 72 73 74#define LOG_BUFFER_SIZE 512 75#define MAX_AUDIO_BUFFER_SIZE 10240 76#define DEFAULT_AUDIO_BUFFER_SIZE 256 77#define MAX_LINE_LENGTH 1024 78#define TRANSCRIPTION_SIZE 512 79#define MAX_SCORE_LENGTH 5 80#define MAX_NUM_REC_CONTEXTS 4 81 82#ifdef PFILE_VIRTUAL_SUPPORT 83 extern const FileRecord pFileRecTable[]; 84 extern const unsigned char pFileStart0[]; 85#endif /* #ifdef PFILE_VIRTUAL_SUPPORT */ 86 87typedef enum 88 { 89 SENTENCE_BEGIN, 90 SENTENCE_BEGIN_BRACKET_BEGIN, 91 SENTENCE_BEGIN_BRACKET_END, 92 SENTENCE_MIDDLE, 93 SENTENCE_MIDDLE_BRACKET_BEGIN, 94 SENTENCE_MIDDLE_BRACKET_END, 95 SENTENCE_MIDDLE_WITH_SPACE 96 } SENTENCE_CLEANING_STATES; 97 98 99 100typedef enum 101 { 102 ForcedRecModeNotSet = -1, 103 ForcedRecModeOff=0, 104 ForcedRecModeOneTime, 105 ForcedRecModeOn 106 } ForcedRecMode; 107 108 109typedef enum 110 { 111 ERROR_LEVEL_CRITICAL = 0, 112 ERROR_LEVEL_HIGH, 113 ERROR_LEVEL_MODERATE, 114 ERROR_LEVEL_LOW 115 } SREC_TEST_ERROR_LEVELS; 116 117 118typedef struct 119 { 120 LCHAR grammar_path [P_PATH_MAX]; /* File path of the grammar. */ 121 LCHAR grammarID [P_PATH_MAX]; /* ID of the grammar. */ 122 LCHAR ruleName [P_PATH_MAX]; /* rule name of the grammar. */ 123 SR_Grammar* grammar; /* grammar. */ 124 ESR_BOOL is_ve_grammar; /* Is voice-enrollment grammar or not */ 125 } APPL_GRAMMAR_DATA; 126 127 128 129typedef struct ApplicationData_t 130 { 131 int argc; /* The number of command-line arguments. */ 132 LCHAR **argv; /* The command-line argument values. */ 133 ESR_Locale locale; /* Current application locale. */ 134 SR_Recognizer *recognizer; /* The current recognizer. */ 135 SR_RecognizerResult *result; /* The last recognition result. */ 136 SR_Vocabulary *vocabulary; /* The current vocabulary. */ 137 SR_Nametag *nametag; /* The nametag generated by the last recognition. */ 138 SR_Nametags *nametags; /* The active nametag collection. */ 139 APPL_GRAMMAR_DATA grammars [MAX_NUM_REC_CONTEXTS]; /* Grammar data */ 140 int grammarCount; /* The number of grammars. */ 141 int active_grammar_num; /* The index number of the active grammar. */ 142 const asr_int16_t *raw_waveform; /*Points to raw waveform returned from voice enrollment */ 143 size_t raw_waveform_size; /* Size of above wave_form */ 144 asr_int16_t audio_buffer [MAX_AUDIO_BUFFER_SIZE]; 145 unsigned int audio_buffer_requested_size; 146 unsigned int num_samples_read; 147 LCHAR raw_waveform_filename [P_PATH_MAX]; /* Name of file of saved waveform data. */ 148 PFile *raw_waveform_file; /* Pointer to file of saved waveform data. */ 149 LCHAR transcription [MAX_LINE_LENGTH]; 150 ForcedRecMode forced_rec_mode; 151 } ApplicationData; 152 153 154 155static int srec_test_get_run_params ( unsigned int *num_shutdown_loops, unsigned int *num_continuous_run_loops ); 156static int srec_test_init_application_data ( ApplicationData *applicationData, int arg_count, LCHAR *arg_vals [] ); 157static int srec_test_init_file_system ( int arg_count, LCHAR *arg_vals [] ); 158static int srec_test_init_logging_system ( int arg_count, LCHAR *arg_vals [], PLogger *logger ); 159static int srec_test_init_memory_system ( unsigned int srec_test_heap_size ); 160static int srec_test_init_system ( unsigned int srec_test_heap_size, PLogger* logger, int arg_count, LCHAR *arg_vals [] ); 161static int srec_test_run_test_execute ( ApplicationData *applicationData ); 162static int srec_test_run_test_init ( ApplicationData *applicationData ); 163static int srec_test_run_test ( ApplicationData *applicationData ); 164static int srec_test_shutdown_file_system ( void ); 165static int srec_test_shutdown_logging_system ( PLogger *logger ); 166static int srec_test_shutdown_memory_system ( void ); 167static int srec_test_recognize_nist_file ( SR_Grammar *active_grammar, ApplicationData *data, FILE *results_file, 168 LCHAR *command_text, size_t *recognition_count ); 169static int srec_test_recognize_pcm_file ( SR_Grammar *active_grammar, ApplicationData *data, FILE *results_file, 170 LCHAR *command_text, size_t *recognition_count ); 171static void srec_test_log_recognition_failure ( ApplicationData *data ); 172 173 174 175static ESR_BOOL interrupted = ESR_FALSE; 176 177/* static ESR_BOOL no_enroll = ESR_FALSE; //BJP */ 178 179int signal_handler_SIGINT(int v) 180{ 181 interrupted = ESR_TRUE; 182 return 0; 183} 184 185 186 187ESR_ReturnCode myDSMCallback(LCHAR* functionName, LCHAR** argv, size_t argc, void* value, LCHAR* result, size_t* resultSize) 188{ 189 LCHAR* meaning; 190 191 PLOG_DBG_TRACE((L("myDSMCallback(%s) invoked\n"), functionName)); 192 if ((LSTRCMP(functionName, "myDSMCallback")!=0) || (argc > 1)) 193 { 194 /* Unsupported semantic function */ 195 return ESR_INVALID_STATE; 196 } 197 if (argc > 0) 198 meaning = argv[0]; 199 else 200 meaning = L(""); 201 lstrtrim(meaning); 202 LSTRLWR(meaning); 203 204 if (LISDIGIT(*meaning)) 205 { 206 /* Penalize meaning starting with "<digit>" */ 207 if (*resultSize < LSTRLEN(L("1000"))+1) 208 { 209 *resultSize = LSTRLEN(L("1000"))+1; 210 return ESR_BUFFER_OVERFLOW; 211 } 212 LSTRCPY(result, "1000"); 213 *resultSize = LSTRLEN(L("1000"))+1; 214 return ESR_SUCCESS; 215 } 216 if (*resultSize < LSTRLEN(L("0"))+1) 217 { 218 *resultSize = LSTRLEN(L("0"))+1; 219 return ESR_BUFFER_OVERFLOW; 220 } 221 LSTRCPY(result, "0"); 222 *resultSize = LSTRLEN(L("0"))+1; 223 return ESR_SUCCESS; 224} 225 226 227 228void srec_test_log_error ( unsigned int error_id, SREC_TEST_ERROR_LEVELS error_level, LCHAR *error_text ) 229 { 230 231 switch ( error_level ) 232 { 233 case ERROR_LEVEL_CRITICAL: 234 LPRINTF ( "Critical Level Error %s\n", error_text ); 235 break; 236 237 case ERROR_LEVEL_HIGH: 238 LPRINTF ( "High Level Error %s\n", error_text ); 239 break; 240 241 case ERROR_LEVEL_MODERATE: 242 LPRINTF ( "Moderate Level Error %s\n", error_text ); 243 break; 244 245 case ERROR_LEVEL_LOW: 246 LPRINTF ( "Low Level Error %s\n", error_text ); 247 break; 248 249 default: 250 LPRINTF ( "Unknown Level Error %d : %s\n", error_level, error_text ); 251 break; 252 } 253 } 254 255 256 257void srec_test_delete_grammar ( ApplicationData *data, int grammar_num ) 258 { 259 LCHAR log_buffer[LOG_BUFFER_SIZE]; 260 261 if ( grammar_num < data->grammarCount ) 262 { 263 if ( ( data->grammarCount - grammar_num ) > 1 ) 264 { 265 memmove ( &data->grammars [grammar_num], &data->grammars [data->grammarCount - 1], 266 ( data->grammarCount - grammar_num - 1 ) * sizeof ( APPL_GRAMMAR_DATA ) ); 267 if ( grammar_num > data->active_grammar_num ) 268 data->active_grammar_num--; 269 } 270 data->grammarCount--; 271 } 272 else 273 { 274 LSPRINTF ( log_buffer, L("Internal Error Grammar TableCorrupt : Grammar %d Does Not Exist"), grammar_num ); 275 srec_test_log_error ( 0, ERROR_LEVEL_CRITICAL, log_buffer ); 276 } 277 } 278 279 280 281static void srec_test_get_active_grammar ( ApplicationData *data, SR_Grammar **active_grammar ) 282 { 283 284 if ( data->active_grammar_num >= 0 ) 285 *active_grammar = data->grammars [data->active_grammar_num].grammar; 286 else 287 *active_grammar = NULL; 288 } 289 290 291 292static void srec_test_get_active_grammar_data ( ApplicationData *data, APPL_GRAMMAR_DATA **active_grammar_data ) 293 { 294 295 if ( data->active_grammar_num >= 0 ) 296 *active_grammar_data = &data->grammars [data->active_grammar_num]; 297 else 298 *active_grammar_data = NULL; 299 } 300 301 302 303static ESR_BOOL srec_test_get_grammar_from_id ( ApplicationData *data, char *grammar_id, 304 int *grammar_index, ESR_BOOL *is_active, ESR_BOOL *is_ve_grammar ) 305 { 306 ESR_BOOL found_grammar; 307 int grammar_counter; 308 309 found_grammar = ESR_FALSE; 310 grammar_counter = 0; 311 312 while ( ( found_grammar == ESR_FALSE ) && ( grammar_counter < data->grammarCount ) ) 313 { 314 if ( strcmp ( grammar_id, data->grammars [grammar_counter].grammarID ) == 0 ) 315 { 316 *grammar_index = grammar_counter; 317 *is_ve_grammar = data->grammars [grammar_counter].is_ve_grammar; 318 319 if ( grammar_counter == data->active_grammar_num ) 320 *is_active = ESR_TRUE; 321 else 322 *is_active = ESR_FALSE; 323 found_grammar = ESR_TRUE; 324 } 325 else 326 { 327 grammar_counter++; 328 } 329 } 330 return ( found_grammar ); 331 } 332 333 334 335static int srec_test_get_empty_grammar_index ( ApplicationData *data, unsigned int *grammar_index ) 336 { 337 int get_status; 338 339 if ( data->grammarCount < MAX_NUM_REC_CONTEXTS ) 340 { 341 get_status = 0; 342 *grammar_index = data->grammarCount; 343 } 344 else 345 { 346 get_status = -1; 347 } 348 return ( get_status ); 349 } 350 351 352 353ESR_ReturnCode ShutdownSession ( void ) 354 { 355 ESR_ReturnCode shutdown_status; 356 357 shutdown_status = SR_SessionDestroy ( ); 358 359 return ( shutdown_status ); 360 } 361 362 363 364ESR_ReturnCode InitSession ( int argc, LCHAR *argv [] ) 365 { 366 ESR_ReturnCode init_status; 367 LCHAR path[P_PATH_MAX]; 368 size_t len; 369 370 len = P_PATH_MAX; 371 init_status = ESR_CommandLineGetValue ( argc, (const char **)argv, L("parfile"), path, &len ); 372 373 if ( init_status == ESR_SUCCESS ) 374 { 375 init_status = SR_SessionCreate ( path ); 376 377 if ( init_status == ESR_SUCCESS ) 378 { 379 /* Command-line options always override PAR file options */ 380 init_status = ESR_SessionImportCommandLine ( argc, argv ); 381 382 if ( init_status != ESR_SUCCESS ) 383 { 384 SR_SessionDestroy ( ); 385 /* Log Here */ 386 } 387 } 388 else 389 { 390 /* Log Here */ 391 } 392 } 393 else 394 { 395 srec_test_log_error ( 0, ERROR_LEVEL_CRITICAL, L("Parfile Nmae Not Specified on Command Line") ); 396 } 397 return ( init_status ); 398 } 399 400 401 402ESR_ReturnCode ShutdownGrammarUnload ( ApplicationData *data, unsigned int grammar_num ) 403 { 404 ESR_ReturnCode shutdown_status; 405 406 shutdown_status = SR_GrammarDestroy ( data->grammars [grammar_num].grammar ); 407 408 return ( shutdown_status ); 409 } 410 411 412 413ESR_ReturnCode SetupGrammarLoad ( ApplicationData *data, unsigned int grammar_num ) 414 { 415 ESR_ReturnCode setup_status; 416 417 setup_status = SR_GrammarLoad (data->grammars [grammar_num].grammar_path, &data->grammars [grammar_num].grammar ); 418 419 if ( setup_status == ESR_SUCCESS ) 420 { 421 setup_status = SR_GrammarSetupVocabulary ( data->grammars [grammar_num].grammar, data->vocabulary ); 422 423 if ( setup_status == ESR_SUCCESS ) 424 { 425 setup_status = SR_GrammarSetupRecognizer( data->grammars [grammar_num].grammar, data->recognizer ); 426 427 if ( setup_status == ESR_SUCCESS ) 428 { 429 setup_status = SR_GrammarSetDispatchFunction ( data->grammars [grammar_num].grammar, L("myDSMCallback"), NULL, myDSMCallback ); 430 431 if ( setup_status != ESR_SUCCESS ) 432 SR_GrammarDestroy ( data->grammars [grammar_num].grammar ); 433 } 434 else 435 { 436 SR_GrammarDestroy ( data->grammars [grammar_num].grammar ); 437 } 438 } 439 else 440 { 441 SR_GrammarDestroy ( data->grammars [grammar_num].grammar ); 442 } 443 } 444 return ( setup_status ); 445 } 446 447 448 449ESR_ReturnCode SetupGrammarActivate ( ApplicationData *data, unsigned int grammar_num ) 450 { 451 ESR_ReturnCode setup_status; 452 setup_status = SR_RecognizerActivateRule ( data->recognizer, data->grammars [grammar_num].grammar, data->grammars [grammar_num].ruleName, 1 ); 453 return ( setup_status ); 454 } 455 456 457 458#ifdef ACCURACY_TESTING 459ESR_ReturnCode srec_test_parse (SR_Grammar *grammar, const LCHAR* trans, LCHAR* meaning, size_t *len); 460#endif 461 462/** 463 * Parses source string and copies the first whitespace-delimited token into destination. 464 * 465 * @param source Source string to parse 466 * @param target Target string to copy into 467 * @param charsRead [in] Size of target buffer. 468 * [out] Number of characters read up to end of token. 469 * If the return code is ESR_BUFFER_OVERFLOW, the required length is 470 * returned in this variable. 471 */ 472int getFirstToken(LCHAR* source, LCHAR* target, size_t* charsRead) 473{ 474 LCHAR* beginning = source; 475 LCHAR* ending; 476 477 /* Skip whitespace */ 478 for (; *beginning!=L('\0') && LISSPACE(*beginning); ++beginning); 479 if (*beginning==L('\0')) 480 return ( -1 ); /* invalid command syntax */ 481 482 if(*beginning == '\"') { 483 beginning++; 484 for (ending=beginning; *ending!=L('\0') && *ending!='\"'; ++ending); 485 *ending = L(' '); 486 } else { 487 /* Find next whitespace */ 488 for (ending=beginning; *ending!=L('\0') && !LISSPACE(*ending); ++ending); 489 } 490 if ((size_t) (ending-beginning) > *charsRead) 491 { 492 *charsRead = ending-beginning; 493 return ( -1 ); 494 } 495 *charsRead = ending-source; 496 LSTRNCPY(target, beginning, ending-beginning); 497 target[ending-beginning] = L('\0'); 498 return ( 0 ); 499} 500 501 502 503int srec_test_get_five_command_items ( LCHAR *command_start, size_t first_max_command_size, LCHAR *first_command, 504 size_t second_max_command_size, LCHAR *second_command, 505 size_t third_max_command_size, LCHAR *third_command, 506 size_t fourth_max_command_size, LCHAR *fourth_command, 507 size_t fifth_max_command_size, LCHAR *fifth_command, 508 size_t *actual_commands, LCHAR **command_end ) 509 { 510 int get_status; 511 512 get_status = getFirstToken ( command_start, first_command, &first_max_command_size ); 513 514 if ( get_status == ESR_SUCCESS ) 515 { 516 get_status = getFirstToken ( command_start + first_max_command_size, second_command, &second_max_command_size ); 517 518 if ( get_status == ESR_SUCCESS ) 519 { 520 get_status = getFirstToken ( command_start + first_max_command_size + second_max_command_size, 521 third_command, &third_max_command_size ); 522 if ( get_status == ESR_SUCCESS ) 523 { 524 get_status = getFirstToken ( command_start + first_max_command_size + second_max_command_size + third_max_command_size, 525 fourth_command, &fourth_max_command_size ); 526 if ( get_status == ESR_SUCCESS ) 527 { 528 get_status = getFirstToken ( command_start + first_max_command_size + second_max_command_size + third_max_command_size + fourth_max_command_size, 529 fifth_command, &fifth_max_command_size ); 530 if ( get_status == ESR_SUCCESS ) 531 { 532 if ( command_end != NULL ) 533 *command_end = command_start + first_max_command_size + second_max_command_size + third_max_command_size + fourth_max_command_size + fifth_max_command_size; 534 if ( actual_commands != NULL ) 535 *actual_commands = 5; 536 } 537 else 538 { 539 if ( command_end != NULL ) 540 *command_end = command_start + first_max_command_size + second_max_command_size + third_max_command_size + fourth_max_command_size; 541 if ( actual_commands != NULL ) 542 *actual_commands = 4; 543 } 544 } 545 else 546 { 547 if ( command_end != NULL ) 548 *command_end = command_start + first_max_command_size + second_max_command_size + third_max_command_size; 549 if ( actual_commands != NULL ) 550 *actual_commands = 3; 551 } 552 } 553 else 554 { 555 if ( command_end != NULL ) 556 *command_end = command_start + first_max_command_size + second_max_command_size; 557 if ( actual_commands != NULL ) 558 *actual_commands = 2; 559 } 560 } 561 else 562 { 563 if ( command_end != NULL ) 564 *command_end = command_start + first_max_command_size; 565 if ( actual_commands != NULL ) 566 *actual_commands = 1; 567 } 568 } 569 else 570 { 571 if ( command_end != NULL ) 572 *command_end = command_start; 573 if ( actual_commands != NULL ) 574 *actual_commands = 0; 575 } 576 return ( get_status ); 577 } 578 579 580 581int srec_test_get_four_command_items ( LCHAR *command_start, size_t first_max_command_size, LCHAR *first_command, 582 size_t second_max_command_size, LCHAR *second_command, 583 size_t third_max_command_size, LCHAR *third_command, 584 size_t fourth_max_command_size, LCHAR *fourth_command, 585 size_t *actual_commands, LCHAR **command_end ) 586 { 587 int get_status; 588 589 get_status = getFirstToken ( command_start, first_command, &first_max_command_size ); 590 591 if ( get_status == ESR_SUCCESS ) 592 { 593 get_status = getFirstToken ( command_start + first_max_command_size, second_command, &second_max_command_size ); 594 595 if ( get_status == ESR_SUCCESS ) 596 { 597 get_status = getFirstToken ( command_start + first_max_command_size + second_max_command_size, 598 third_command, &third_max_command_size ); 599 if ( get_status == ESR_SUCCESS ) 600 { 601 get_status = getFirstToken ( command_start + first_max_command_size + second_max_command_size + third_max_command_size, 602 fourth_command, &fourth_max_command_size ); 603 if ( get_status == ESR_SUCCESS ) 604 { 605 if ( command_end != NULL ) 606 *command_end = command_start + first_max_command_size + second_max_command_size + third_max_command_size + fourth_max_command_size; 607 if ( actual_commands != NULL ) 608 *actual_commands = 4; 609 } 610 else 611 { 612 if ( command_end != NULL ) 613 *command_end = command_start + first_max_command_size + second_max_command_size + third_max_command_size; 614 if ( actual_commands != NULL ) 615 *actual_commands = 3; 616 } 617 } 618 else 619 { 620 if ( command_end != NULL ) 621 *command_end = command_start + first_max_command_size + second_max_command_size; 622 if ( actual_commands != NULL ) 623 *actual_commands = 2; 624 } 625 } 626 else 627 { 628 if ( command_end != NULL ) 629 *command_end = command_start + first_max_command_size; 630 if ( actual_commands != NULL ) 631 *actual_commands = 1; 632 } 633 } 634 else 635 { 636 if ( command_end != NULL ) 637 *command_end = command_start; 638 if ( actual_commands != NULL ) 639 *actual_commands = 0; 640 } 641 return ( get_status ); 642 } 643 644 645 646int srec_test_get_three_command_items ( LCHAR *command_start, size_t first_max_command_size, LCHAR *first_command, 647 size_t second_max_command_size, LCHAR *second_command, 648 size_t third_max_command_size, LCHAR *third_command, 649 size_t *actual_commands, LCHAR **command_end ) 650 { 651 int get_status; 652 653 get_status = getFirstToken ( command_start, first_command, &first_max_command_size ); 654 655 if ( get_status == ESR_SUCCESS ) 656 { 657 get_status = getFirstToken ( command_start + first_max_command_size, second_command, &second_max_command_size ); 658 659 if ( get_status == ESR_SUCCESS ) 660 { 661 get_status = getFirstToken ( command_start + first_max_command_size + second_max_command_size, 662 third_command, &third_max_command_size ); 663 if ( get_status == ESR_SUCCESS ) 664 { 665 if ( command_end != NULL ) 666 *command_end = command_start + first_max_command_size + second_max_command_size + third_max_command_size; 667 if ( actual_commands != NULL ) 668 *actual_commands = 3; 669 } 670 else 671 { 672 if ( command_end != NULL ) 673 *command_end = command_start + first_max_command_size + second_max_command_size; 674 if ( actual_commands != NULL ) 675 *actual_commands = 2; 676 } 677 } 678 else 679 { 680 if ( command_end != NULL ) 681 *command_end = command_start + first_max_command_size; 682 if ( actual_commands != NULL ) 683 *actual_commands = 1; 684 } 685 } 686 else 687 { 688 if ( command_end != NULL ) 689 *command_end = command_start; 690 if ( actual_commands != NULL ) 691 *actual_commands = 0; 692 } 693 return ( get_status ); 694 } 695 696 697 698int srec_test_get_two_command_items ( LCHAR *command_start, size_t first_max_command_size, LCHAR *first_command, 699 size_t second_max_command_size, LCHAR *second_command, 700 size_t *actual_commands, LCHAR **command_end ) 701 { 702 int get_status; 703 704 get_status = getFirstToken ( command_start, first_command, &first_max_command_size ); 705 706 if ( get_status == ESR_SUCCESS ) 707 { 708 get_status = getFirstToken ( command_start + first_max_command_size, second_command, &second_max_command_size ); 709 710 if ( get_status == ESR_SUCCESS ) 711 { 712 if ( command_end != NULL ) 713 *command_end = command_start + first_max_command_size + second_max_command_size; 714 if ( actual_commands != NULL ) 715 *actual_commands = 2; 716 } 717 else 718 { 719 if ( command_end != NULL ) 720 *command_end = command_start + first_max_command_size; 721 if ( actual_commands != NULL ) 722 *actual_commands = 1; 723 } 724 } 725 else 726 { 727 if ( command_end != NULL ) 728 *command_end = command_start; 729 if ( actual_commands != NULL ) 730 *actual_commands = 0; 731 } 732 return ( get_status ); 733 } 734 735 736 737int srec_test_get_one_command_item ( LCHAR *command_start, size_t max_command_size, LCHAR *command, LCHAR **command_end ) 738 { 739 int get_status; 740 741 get_status = getFirstToken ( command_start, command, &max_command_size ); 742 743 if ( get_status == ESR_SUCCESS ) 744 { 745 if ( command_end != NULL ) 746 *command_end = command_start + max_command_size; 747 } 748 return ( get_status ); 749 } 750 751 752 753/** 754 * Execute TCP-file commands. 755 * 756 * @param text String containing command 757 * @param recognizer The recognizer 758 */ 759//static int words_added = 0; 760 761#if defined(APP_ENABLE_TIMER) 762static PTimer *addWordTimer = NULL; 763static PTimer *compileTimer = NULL; 764#endif 765 766 767 768int srec_test_reset_slots ( SR_Grammar *active_grammar ) 769 { 770 int reset_status; 771 ESR_ReturnCode esr_status; 772 773 if ( active_grammar != NULL ) 774 { 775 reset_status = 0; 776 esr_status = SR_GrammarResetAllSlots ( active_grammar ); 777 778 if ( esr_status != ESR_SUCCESS ) 779 { 780 reset_status = -1; 781 /* Log Here */ 782 } 783 } 784 else 785 { 786 reset_status = -1; 787 /* Log Here */ 788 } 789 return ( reset_status ); 790 } 791 792 793 794int srec_test_add_word ( SR_Grammar *active_grammar, LCHAR *command_text ) 795 { 796 int add_status; 797 ESR_ReturnCode esr_status; 798 LCHAR slot [MAX_LINE_LENGTH]; 799 LCHAR word [MAX_LINE_LENGTH]; 800 LCHAR pron [MAX_LINE_LENGTH]; 801 LCHAR weight [MAX_UINT_DIGITS+1]; 802 LCHAR semanticTag [MAX_LINE_LENGTH]; 803 int weightNumber; 804 int convert_string_to_num; 805 806 if ( active_grammar != NULL ) 807 { 808 add_status = srec_test_get_five_command_items ( command_text, MAX_LINE_LENGTH, slot, 809 MAX_LINE_LENGTH, word, MAX_LINE_LENGTH, pron, 810 MAX_UINT_DIGITS + 1, weight, MAX_LINE_LENGTH, 811 semanticTag, NULL, NULL ); 812 if ( add_status == 0 ) 813 { 814 convert_string_to_num = lstrtoi (weight, &weightNumber, 10 ); 815 816 if ( convert_string_to_num == 0 ) 817 { 818#if defined(APP_ENABLE_TIMER) 819 PTimerStart ( addWordTimer ); 820#endif 821 pron[ strlen(pron)+1] = 0; // make sure we have double-null! 822 esr_status = SR_GrammarAddWordToSlot ( active_grammar, slot, word, pron, weightNumber, semanticTag ); 823#if defined(APP_ENABLE_TIMER) 824 PTimerStop(addWordTimer); 825#endif 826 if ( esr_status != ESR_SUCCESS ) 827 { 828 add_status = -1; 829 /* Log Here */ 830 } 831 } 832 else 833 { 834 add_status = -1; 835 /* Log Here */ 836 } 837 } 838 else 839 { 840 add_status = -1; 841 /* Log Here */ 842 } 843 } 844 else 845 { 846 add_status = -1; 847 srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, "No Grammar Activated" ); 848 } 849 return ( add_status ); 850 } 851 852 853 854int srec_test_compile_active_context ( SR_Grammar *active_grammar ) 855 { 856 int compile_status; 857 ESR_ReturnCode esr_status; 858 859 if ( active_grammar != NULL ) 860 { 861 compile_status = 0; 862#if defined(APP_ENABLE_TIMER) 863 PTimerStart ( compileTimer ); 864#endif 865 esr_status = SR_GrammarCompile( active_grammar ); 866#if defined(APP_ENABLE_TIMER) 867 PTimerStop ( compileTimer ); 868#endif 869 if ( esr_status != ESR_SUCCESS ) 870 { 871 compile_status = -1; 872 /* Log Here */ 873 } 874 } 875 else 876 { 877 compile_status = -1; 878 srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, L("No Active Grammar To Compile") ); 879 } 880 return ( compile_status ); 881 } 882 883 884 885int srec_test_load_grammar_data_from_command ( ApplicationData *data, unsigned int grammar_num, LCHAR *command_text ) 886 { 887 int get_status; 888 ESR_ReturnCode esr_status; 889 LCHAR path [P_PATH_MAX]; 890 LCHAR id [P_PATH_MAX]; 891 LCHAR rule [P_PATH_MAX]; 892 LCHAR ve_marker [P_PATH_MAX]; 893 size_t path_length; 894 895 get_status = srec_test_get_four_command_items ( command_text, P_PATH_MAX, path, P_PATH_MAX, id, 896 P_PATH_MAX, rule, P_PATH_MAX, ve_marker, NULL, NULL ); 897 if ( get_status == 0 ) 898 { 899 path_length = P_PATH_MAX; 900 esr_status = ESR_SessionPrefixWithBaseDirectory ( path, &path_length ); 901 902 if ( esr_status == ESR_SUCCESS ) 903 { 904 LSTRCPY ( data->grammars [grammar_num].grammar_path, path ); 905 LSTRCPY ( data->grammars [grammar_num].grammarID, id ); 906 LSTRCPY ( data->grammars [grammar_num].ruleName, rule ); 907 908 if ( LSTRCMP ( ve_marker, L("ve") ) ==0 ) 909 data->grammars [grammar_num].is_ve_grammar = ESR_TRUE; 910 else if ( LSTRCMP ( ve_marker, L("not_ve") ) ==0 ) 911 data->grammars [grammar_num].is_ve_grammar = ESR_FALSE; 912 else 913 { 914 get_status = -1; 915 /* Log Here */ 916 } 917 } 918 else 919 { 920 get_status = -1; 921 /* Log Here */ 922 } 923 } 924 else 925 { 926 /* Log Here */ 927 } 928 return ( get_status ); 929 } 930 931 932 933int srec_test_load_context ( ApplicationData *data, LCHAR *command_text ) 934 { 935 int load_status; 936 ESR_ReturnCode esr_status; 937 unsigned int open_grammar; 938 939 load_status = srec_test_get_empty_grammar_index ( data, &open_grammar ); 940 941 if ( load_status == 0 ) 942 { 943 load_status = srec_test_load_grammar_data_from_command ( data, open_grammar, command_text ); 944 945 if ( load_status == 0 ) 946 { 947 esr_status = SetupGrammarLoad ( data, open_grammar ); 948 949 if ( esr_status == ESR_SUCCESS ) 950 { 951 LPRINTF(L("Grammar Loaded\n")); 952 data->grammarCount++; 953 } 954 else 955 { 956 load_status = -1; 957 /* Log Here */ 958 } 959 } 960 else 961 { 962 /* Log Here */ 963 } 964 } 965 else 966 { 967 srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Maximum Number Of Grammars Already Loaded") ); 968 } 969 return ( load_status ); 970 } 971 972 973 974int srec_test_free_context ( SR_Grammar *active_grammar, ApplicationData *data, LCHAR *command_text ) 975 { 976 int free_status; 977 ESR_ReturnCode esr_status; 978 int grammar_num; 979 ESR_BOOL found_grammar; 980 ESR_BOOL grammar_is_active; 981 ESR_BOOL grammar_is_ve; 982 LCHAR grammar_id [P_PATH_MAX]; 983 984 free_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, grammar_id, NULL ); 985 986 if ( free_status == 0 ) 987 { 988 found_grammar = srec_test_get_grammar_from_id ( data, grammar_id, &grammar_num, &grammar_is_active, &grammar_is_ve ); 989 990 if ( found_grammar == ESR_TRUE ) 991 { 992 if ( grammar_is_active == ESR_TRUE ) 993 { 994 if ( grammar_is_ve == ESR_TRUE ) 995 { 996 esr_status = SR_RecognizerSetBoolParameter ( data->recognizer, L("enableGetWaveform"), ESR_FALSE ); 997 998 if ( esr_status != ESR_SUCCESS ) 999 { 1000 free_status = -1; 1001 /* Log Here */ 1002 } 1003 } 1004 else 1005 { 1006 esr_status = ESR_SUCCESS; 1007 } 1008 if ( esr_status == ESR_SUCCESS ) 1009 { 1010 esr_status = SR_RecognizerDeactivateRule ( data->recognizer, data->grammars [data->active_grammar_num].grammar, 1011 data->grammars [data->active_grammar_num].ruleName ); 1012 if ( esr_status == ESR_SUCCESS ) 1013 { 1014 data->active_grammar_num = -1; 1015 } 1016 else 1017 { 1018 free_status = -1; 1019 /* Log Here */ 1020 } 1021 } 1022 } 1023 else 1024 { 1025 free_status = -1; 1026 /* Log Here */ 1027 } 1028 } 1029 else 1030 { 1031 free_status = -1; 1032 /* Log Here */ 1033 } 1034 } 1035 else 1036 { 1037 /* Log Here */ 1038 } 1039 return ( free_status ); 1040 } 1041 1042 1043 1044int srec_test_unload_context ( ApplicationData *data, LCHAR *command_text ) 1045 { 1046 int unload_status; 1047 ESR_ReturnCode esr_status; 1048 int grammar_num; 1049 ESR_BOOL found_grammar; 1050 ESR_BOOL grammar_is_active; 1051 ESR_BOOL grammar_is_ve; 1052 LCHAR grammar_id [P_PATH_MAX]; 1053 1054 unload_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, grammar_id, NULL ); 1055 1056 if ( unload_status == 0 ) 1057 { 1058 found_grammar = srec_test_get_grammar_from_id ( data, grammar_id, &grammar_num, &grammar_is_active, &grammar_is_ve ); 1059 1060 if ( found_grammar == ESR_TRUE ) 1061 { 1062 if ( grammar_is_active == ESR_FALSE ) 1063 { 1064 esr_status = SR_GrammarDestroy ( data->grammars [grammar_num].grammar ); 1065 1066 if ( esr_status != ESR_SUCCESS ) 1067 { 1068 unload_status = -1; 1069 /* Log Here */ 1070 } 1071 srec_test_delete_grammar ( data, grammar_num ); 1072 } 1073 else 1074 { 1075 unload_status = -1; 1076 /* Log Here */ 1077 } 1078 } 1079 else 1080 { 1081 unload_status = -1; 1082 /* Log Here */ 1083 } 1084 } 1085 else 1086 { 1087 /* Log Here */ 1088 } 1089 return ( unload_status ); 1090 } 1091 1092 1093 1094int srec_test_use_context ( SR_Grammar *active_grammar, ApplicationData *data, LCHAR *command_text ) 1095 { 1096 int use_status; 1097 ESR_ReturnCode esr_status; 1098 int grammar_num; 1099 ESR_BOOL found_grammar; 1100 ESR_BOOL grammar_is_active; 1101 ESR_BOOL grammar_is_ve; 1102 LCHAR grammar_id [P_PATH_MAX]; 1103 1104 if ( active_grammar == NULL ) 1105 { 1106 use_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, grammar_id, NULL ); 1107 1108 if ( use_status == 0 ) 1109 { 1110 found_grammar = srec_test_get_grammar_from_id ( data, grammar_id, &grammar_num, &grammar_is_active, &grammar_is_ve ); 1111 1112 if ( found_grammar == ESR_TRUE ) 1113 { 1114 esr_status = SR_RecognizerActivateRule ( data->recognizer, data->grammars [grammar_num].grammar, 1115 data->grammars [grammar_num].ruleName, 1 ); 1116 if ( esr_status == ESR_SUCCESS ) 1117 { 1118 if ( data->grammars [grammar_num].is_ve_grammar == ESR_TRUE ) 1119 { 1120 esr_status = SR_RecognizerSetBoolParameter ( data->recognizer, L("enableGetWaveform"), ESR_TRUE ); 1121 1122 if ( esr_status == ESR_SUCCESS ) 1123 { 1124 data->active_grammar_num = (int)grammar_num; 1125 } 1126 else 1127 { 1128 use_status = -1; 1129 /* Log Here */ 1130 SR_RecognizerDeactivateRule ( data->recognizer, data->grammars [grammar_num].grammar, 1131 data->grammars [grammar_num].ruleName ); 1132 } 1133 } 1134 else 1135 { 1136 data->active_grammar_num = (int)grammar_num; 1137 } 1138 } 1139 else 1140 { 1141 use_status = -1; 1142 /* Log Here */ 1143 } 1144 } 1145 else 1146 { 1147 use_status = -1; 1148 /* Log Here */ 1149 } 1150 } 1151 else 1152 { 1153 use_status = -1; 1154 /* Log Here */ 1155 } 1156 } 1157 else 1158 { 1159 use_status = -1; 1160 PLogError("ERROR: context_use ignored, use context_free first!"); 1161 /* Log Here */ 1162 } 1163 return ( use_status ); 1164 } 1165 1166 1167 1168int srec_test_save_context ( SR_Grammar *active_grammar, LCHAR *command_text ) 1169 { 1170 int save_status; 1171 ESR_ReturnCode esr_status; 1172 LCHAR file_name [P_PATH_MAX]; 1173 1174 if ( active_grammar != NULL ) 1175 { 1176 save_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, file_name, NULL ); 1177 1178 if ( save_status == 0 ) 1179 { 1180 esr_status = SR_GrammarSave ( active_grammar, file_name ); 1181 1182 if ( esr_status != ESR_SUCCESS ) 1183 { 1184 save_status = -1; 1185 /* Log Here */ 1186 } 1187 } 1188 else 1189 { 1190 /* Log Here */ 1191 } 1192 } 1193 else 1194 { 1195 save_status = -1; 1196 srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, "No Grammar Activated" ); 1197 } 1198 return ( save_status ); 1199 } 1200 1201 1202 1203int srec_test_get_size_t_parameter ( ApplicationData *data, LCHAR *command_text ) 1204 { 1205 int get_status; 1206 ESR_ReturnCode esr_status; 1207 LCHAR key [P_PATH_MAX]; 1208 size_t value; 1209 1210 get_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, key, NULL ); 1211 1212 if ( get_status == 0 ) 1213 { 1214 esr_status = SR_RecognizerGetSize_tParameter ( data->recognizer, key, &value ); 1215 1216 if ( esr_status == ESR_SUCCESS ) 1217 { 1218 printf ( "Get Parameter : %s : %u\n", key, value ); 1219 } 1220 else 1221 { 1222 get_status = -1; 1223 /* Log Here */ 1224 } 1225 } 1226 return ( get_status ); 1227 } 1228 1229 1230 1231int srec_test_set_size_t_parameter ( ApplicationData *data, LCHAR *command_text ) 1232 { 1233 int set_status; 1234 ESR_ReturnCode esr_status; 1235 LCHAR key [P_PATH_MAX]; 1236 LCHAR value [MAX_UINT_DIGITS+1]; 1237 int value_as_number; 1238 int convert_string_to_num; 1239 1240 set_status = srec_test_get_two_command_items ( command_text, P_PATH_MAX, key, 1241 MAX_UINT_DIGITS + 1, value, NULL, NULL ); 1242 if ( set_status == 0 ) 1243 { 1244 convert_string_to_num = lstrtoi ( value, &value_as_number, 10 ); 1245 1246 if ( convert_string_to_num == 0 ) 1247 { 1248 if ( value_as_number > 0 ) 1249 { 1250 esr_status = SR_RecognizerSetSize_tParameter ( data->recognizer, key, (size_t)value_as_number ); 1251 1252 if ( esr_status != ESR_SUCCESS ) 1253 { 1254 set_status = -1; 1255 /* Log Here */ 1256 } 1257 } 1258 else 1259 { 1260 set_status = -1; 1261 /* Log Here */ 1262 } 1263 } 1264 else 1265 { 1266 set_status = -1; 1267 /* Log Here */ 1268 } 1269 } 1270 return ( set_status ); 1271 } 1272 1273 1274 1275int srec_test_voice_enroll ( SR_Grammar *active_grammar, ApplicationData *data, LCHAR *command_text ) 1276 { 1277 int enroll_status; 1278 ESR_ReturnCode esr_status; 1279 LCHAR slot [P_PATH_MAX]; 1280 LCHAR nametagID [P_PATH_MAX]; 1281 LCHAR weight [MAX_UINT_DIGITS+1]; 1282 int weightNumber; 1283 int convert_string_to_num; 1284 1285 if ( active_grammar != NULL ) 1286 { 1287 if ( data->nametag != NULL ) 1288 { 1289 enroll_status = srec_test_get_three_command_items ( command_text, P_PATH_MAX, slot, P_PATH_MAX, nametagID, 1290 MAX_UINT_DIGITS + 1, weight, NULL, NULL ); 1291 if ( enroll_status == 0 ) 1292 { 1293 convert_string_to_num = lstrtoi ( weight, &weightNumber, 10 ); 1294 1295 if ( convert_string_to_num == 0 ) 1296 { 1297 esr_status = SR_NametagSetID ( data->nametag, nametagID ); 1298 1299 if ( esr_status == ESR_SUCCESS ) 1300 { 1301 LCHAR value[MAX_LINE_LENGTH]; 1302 LSPRINTF( value,"V='%s';",nametagID); 1303 esr_status = SR_GrammarAddNametagToSlot ( active_grammar, slot, data->nametag, weightNumber, value ); 1304 if ( esr_status != ESR_SUCCESS ) 1305 { 1306 enroll_status = -1; 1307 /* Log Here */ 1308 } 1309 } 1310 else 1311 { 1312 enroll_status = -1; 1313 /* Log Here */ 1314 } 1315 } 1316 else 1317 { 1318 enroll_status = -1; 1319 /* Log Here */ 1320 } 1321 } 1322 else 1323 { 1324 /* Log Here */ 1325 } 1326 } 1327 else 1328 { 1329 enroll_status = -1; 1330 /* Log Here */ 1331 } 1332 } 1333 else 1334 { 1335 enroll_status = -1; 1336 srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, "No Grammar Activated" ); 1337 } 1338 return ( enroll_status ); 1339 } 1340 1341 1342 1343int srec_test_load_nametags ( ApplicationData *data, LCHAR *command_text ) 1344 { 1345 int load_status; 1346 ESR_ReturnCode esr_status; 1347 LCHAR file_name [P_PATH_MAX]; 1348 1349 load_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, file_name, NULL ); 1350 1351 if ( load_status == 0 ) 1352 { 1353 esr_status = SR_NametagsLoad ( data->nametags, file_name ); 1354 1355 if ( esr_status != ESR_SUCCESS ) 1356 { 1357 load_status = -1; 1358 /* Log Here */ 1359 } 1360 } 1361 else 1362 { 1363 load_status = -1; 1364 /* Log Here */ 1365 } 1366 return ( load_status ); 1367 } 1368 1369 1370 1371int srec_test_save_nametags ( ApplicationData *data, LCHAR *command_text ) 1372 { 1373 int save_status; 1374 ESR_ReturnCode esr_status; 1375 LCHAR file_name [P_PATH_MAX]; 1376 1377 save_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, file_name, NULL ); 1378 1379 if ( save_status == 0 ) 1380 { 1381 esr_status = SR_NametagsSave ( data->nametags, file_name ); 1382 1383 if ( esr_status != ESR_SUCCESS ) 1384 { 1385 save_status = -1; 1386 /* Log Here */ 1387 } 1388 } 1389 else 1390 { 1391 save_status = -1; 1392 /* Log Here */ 1393 } 1394 return ( save_status ); 1395 } 1396 1397 1398 1399int srec_test_clear_nametags ( ApplicationData *data ) 1400 { 1401 int clear_status; 1402 ESR_ReturnCode esr_status; 1403 1404 clear_status = 0; 1405 esr_status = SR_NametagsDestroy ( data->nametags ); 1406 1407 if ( esr_status == ESR_SUCCESS ) 1408 { 1409 data->nametags = NULL; 1410 esr_status = SR_NametagsCreate ( &data->nametags ); 1411 1412 if ( esr_status != ESR_SUCCESS ) 1413 { 1414 clear_status = -1; 1415 /* Log Here */ 1416 } 1417 } 1418 else 1419 { 1420 clear_status = -1; 1421 /* Log Here */ 1422 } 1423 return ( clear_status ); 1424 } 1425 1426 1427 1428int srec_test_add_to_nametags ( ApplicationData *data, LCHAR *command_text ) 1429 { 1430 int add_status; 1431 ESR_ReturnCode esr_status; 1432 LCHAR nametagID [P_PATH_MAX]; 1433 1434 if ( data->nametag != NULL ) 1435 { 1436 add_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, nametagID, NULL ); 1437 1438 if ( add_status == 0 ) 1439 { 1440 esr_status = SR_NametagSetID ( data->nametag, nametagID ); 1441 1442 if ( esr_status == ESR_SUCCESS ) 1443 { 1444 esr_status = SR_NametagsAdd ( data->nametags, data->nametag ); 1445 1446 if ( esr_status == ESR_SUCCESS ) 1447 { 1448 data->nametag = NULL; 1449 } 1450 else 1451 { 1452 add_status = -1; 1453 /* Log Here */ 1454 } 1455 } 1456 else 1457 { 1458 add_status = -1; 1459 /* Log Here */ 1460 } 1461 } 1462 else 1463 { 1464 add_status = -1; 1465 /* Log Here */ 1466 } 1467 } 1468 else 1469 { 1470 add_status = -1; 1471 /* Log Here */ 1472 } 1473 return ( add_status ); 1474 } 1475 1476 1477int srec_test_voice_enroll_nametags ( SR_Grammar *active_grammar, ApplicationData *data, LCHAR *command_text ) 1478 { 1479 int enroll_status; 1480 ESR_ReturnCode esr_status; 1481 LCHAR slot [P_PATH_MAX]; 1482 SR_Nametag *nametag; 1483 size_t nametags_size; 1484 size_t current_nametag; 1485 1486 if ( active_grammar != NULL ) 1487 { 1488 enroll_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, slot, NULL ); 1489 1490 if ( enroll_status == 0 ) 1491 { 1492 esr_status = SR_NametagsGetSize (data->nametags, &nametags_size ); 1493 1494 if ( esr_status == ESR_SUCCESS ) 1495 { 1496 current_nametag = 0; 1497 1498 while ( ( current_nametag < nametags_size ) && ( esr_status == ESR_SUCCESS ) ) 1499 { 1500 esr_status = SR_NametagsGetAtIndex ( data->nametags, current_nametag, &nametag ); 1501 1502 if ( esr_status == ESR_SUCCESS ) 1503 { 1504 esr_status = SR_GrammarAddNametagToSlot ( active_grammar, slot, nametag, 0, NULL ); 1505 1506 if ( esr_status == ESR_SUCCESS ) 1507 { 1508 current_nametag++; 1509 } 1510 else 1511 { 1512 enroll_status = -1; 1513 /* Log Here */ 1514 } 1515 } 1516 else 1517 { 1518 enroll_status = -1; 1519 /* Log Here */ 1520 } 1521 } 1522 } 1523 else 1524 { 1525 enroll_status = -1; 1526 /* Log Here */ 1527 } 1528 } 1529 else 1530 { 1531 /* Log Here */ 1532 } 1533 } 1534 else 1535 { 1536 enroll_status = -1; 1537 srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, "No Grammar Activated" ); 1538 } 1539 return ( enroll_status ); 1540 } 1541 1542 1543 1544int srec_test_set_audio_size ( ApplicationData *data, LCHAR *command_text ) 1545 { 1546 int set_status; 1547 LCHAR new_size [P_PATH_MAX]; 1548 int size_as_number; 1549 int convert_string_to_num; 1550 1551 set_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, new_size, NULL ); 1552 1553 if ( set_status == 0 ) 1554 { 1555 convert_string_to_num = lstrtoi ( new_size, &size_as_number, 10 ); 1556 1557 if ( convert_string_to_num == 0 ) 1558 { 1559 if ( ( size_as_number > 0 ) && ( size_as_number <= MAX_AUDIO_BUFFER_SIZE ) ) 1560 { 1561 data->audio_buffer_requested_size = size_as_number; 1562 LPRINTF ( L("Audio Size Set To %d\n"), size_as_number ); 1563 } 1564 else 1565 { 1566 set_status = -1; 1567 /* Log Here */ 1568 } 1569 } 1570 else 1571 { 1572 set_status = -1; 1573 /* Log Here */ 1574 } 1575 } 1576 return ( set_status ); 1577 } 1578 1579 1580#ifdef _WIN32 1581int srec_test_sleep ( LCHAR *command_text ) 1582 { 1583 1584 return ( - 1 ); 1585 } 1586#else 1587int srec_test_sleep ( LCHAR *command_text ) 1588 { 1589 int sleep_status; 1590 struct timeval sleep_time_struct; 1591 LCHAR sleep_time [P_PATH_MAX]; 1592 int time_as_number; 1593 int convert_string_to_num; 1594 1595 sleep_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, sleep_time, NULL ); 1596 1597 if ( sleep_status == 0 ) 1598 { 1599 convert_string_to_num = lstrtoi ( sleep_time, &time_as_number, 10 ); 1600 1601 if ( convert_string_to_num == 0 ) 1602 { 1603 sleep_time_struct.tv_sec = time_as_number; 1604 sleep_time_struct.tv_usec = 0; 1605 select ( 0, NULL, NULL, NULL, &sleep_time_struct ); 1606 } 1607 else 1608 { 1609 sleep_status = -1; 1610 /* Log Here */ 1611 } 1612 } 1613 return ( sleep_status ); 1614 } 1615#endif 1616 1617 1618int srec_test_change_sample_rate ( ApplicationData *data, LCHAR *command_text ) 1619 { 1620 int change_status; 1621 ESR_ReturnCode esr_status; 1622 LCHAR new_rate [P_PATH_MAX]; 1623 int rate_as_number; 1624 int convert_string_to_num; 1625 1626 change_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, new_rate, NULL ); 1627 1628 if ( change_status == 0 ) 1629 { 1630 convert_string_to_num = lstrtoi ( new_rate, &rate_as_number, 10 ); 1631 1632 if ( convert_string_to_num == 0 ) 1633 { 1634 esr_status = SR_Recognizer_Change_Sample_Rate ( data->recognizer, rate_as_number ); 1635 1636 if ( esr_status != ESR_SUCCESS ) 1637 { 1638 change_status = -1; 1639 /* Log Here */ 1640 } 1641 } 1642 else 1643 { 1644 change_status = -1; 1645 /* Log Here */ 1646 } 1647 } 1648 if ( change_status == 0 ) 1649 printf ( "\nSuccessfully Set Sample Rate To : %s\n", new_rate ); 1650 else 1651 printf ( "\nFailed To Set Sample Rate To : %s\n", new_rate ); 1652 return ( change_status ); 1653 } 1654 1655 1656 1657int srec_test_set_acousticstate ( ApplicationData *data, LCHAR *command_text ) 1658 { 1659 int set_status; 1660 ESR_ReturnCode esr_status; 1661 LCHAR state_string [P_PATH_MAX]; 1662 1663 set_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, state_string, NULL ); 1664 1665 if ( set_status == 0 ) 1666 { 1667 esr_status = SR_AcousticStateSet ( data->recognizer, state_string ); 1668 1669 if ( esr_status != ESR_SUCCESS ) 1670 { 1671 set_status = -1; 1672 /* Log Here */ 1673 } 1674 } 1675 else 1676 { 1677 /* Log Here */ 1678 } 1679 return ( set_status ); 1680 } 1681 1682 1683 1684int srec_test_get_acousticstate ( ApplicationData *data ) 1685 { 1686 int get_status; 1687 ESR_ReturnCode esr_status; 1688 const LCHAR *state_string; 1689 1690 get_status = 0; 1691 esr_status = SR_AcousticStateGet ( data->recognizer, &state_string ); 1692 1693 if ( esr_status == ESR_SUCCESS ) 1694 { 1695 printf ( "Acoustic State :\n%s\n", state_string ); 1696 } 1697 else 1698 { 1699 get_status = -1; 1700 /* Log Here */ 1701 } 1702 return ( get_status ); 1703 } 1704 1705 1706 1707int srec_test_reset_acousticstate ( ApplicationData *data ) 1708 { 1709 int reset_status; 1710 ESR_ReturnCode esr_status; 1711 1712 reset_status = 0; 1713 esr_status = SR_AcousticStateReset ( data->recognizer ); 1714 1715 if ( esr_status != ESR_SUCCESS ) 1716 { 1717 reset_status = -1; 1718 /* Log Here */ 1719 } 1720 return ( reset_status ); 1721 } 1722 1723 1724 1725int srec_test_set_forced_rec_mode ( ApplicationData *data, LCHAR *command_text ) 1726 { 1727 int set_status; 1728 LCHAR mode[P_PATH_MAX]; 1729 1730 set_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, mode, NULL ); 1731 1732 if ( set_status == 0 ) 1733 { 1734 if ( LSTRCMP ( mode, L("one_time") ) == 0 ) 1735 { 1736 data->forced_rec_mode = ForcedRecModeOneTime; 1737 } 1738 else if ( LSTRCMP ( mode, L("off") ) == 0 ) 1739 { 1740 data->forced_rec_mode = ForcedRecModeOff; 1741 } 1742 else if ( LSTRCMP ( mode, L("on") ) == 0 ) 1743 { 1744 data->forced_rec_mode = ForcedRecModeOn; 1745 } 1746 else 1747 { 1748 set_status = -1; 1749 /* Log Here */ 1750 } 1751 } 1752 return ( set_status ); 1753 } 1754 1755 1756 1757int srec_test_execute_command ( ApplicationData *data, LCHAR *text, FILE *results_file, size_t *recognition_count ) 1758 { 1759 int execute_status; 1760 LCHAR *current_command_start; 1761 LCHAR *current_end_command; 1762 LCHAR command [MAX_LINE_LENGTH]; 1763 SR_Grammar *active_grammar; 1764 LCHAR log_buffer [LOG_BUFFER_SIZE]; 1765 1766 srec_test_get_active_grammar ( data, &active_grammar ); 1767 current_command_start = text; 1768 execute_status = srec_test_get_one_command_item ( current_command_start, MAX_LINE_LENGTH, command, ¤t_end_command ); 1769 1770 if ( execute_status == 0 ) 1771 { 1772 if( data->nametag 1773 && ((LSTRCMP(command, "recognize_nist")==0) || (LSTRCMP(command, "recognize_pcm")==0)) ) { 1774 /* if there is a nametag held in memory, and we don't make use of it, then 1775 let's destroy it here */ 1776 SR_NametagDestroy( data->nametag); 1777 data->nametag = NULL; 1778 } 1779 1780 if ( LSTRCMP ( command, L("recognize_nist") ) == 0 ) 1781 execute_status = srec_test_recognize_nist_file ( active_grammar, data, results_file, 1782 current_end_command, recognition_count ); 1783 else if ( LSTRCMP ( command, L("recognize_pcm") ) == 0 ) 1784 execute_status = srec_test_recognize_pcm_file ( active_grammar, data, results_file, 1785 current_end_command, recognition_count ); 1786 else if ( LSTRCMP ( command, L("context_load") ) == 0 ) 1787 execute_status = srec_test_load_context ( data, current_end_command ); 1788 else if ( LSTRCMP ( command, L("context_use") ) == 0 ) 1789 execute_status = srec_test_use_context ( active_grammar, data, current_end_command ); 1790 else if ( LSTRCMP ( command, L("context_free") ) == 0 ) 1791 execute_status = srec_test_free_context ( active_grammar, data, current_end_command ); 1792 else if ( LSTRCMP ( command, L("context_unload") ) == 0 ) 1793 execute_status = srec_test_unload_context ( data, current_end_command ); 1794 else if ( LSTRCMP ( command, L("addwords_from_nametags") ) == 0 ) 1795 execute_status = srec_test_voice_enroll_nametags ( active_grammar, data, current_end_command ); 1796 else if ( LSTRCMP ( command, L("resetslots") ) == 0 ) 1797 execute_status = srec_test_reset_slots ( active_grammar ); 1798 else if ( LSTRCMP ( command, L("addword") ) == 0 ) 1799 execute_status = srec_test_add_word ( active_grammar, current_end_command ); 1800 else if ( LSTRCMP ( command, L("context_compile") ) == 0 ) 1801 execute_status = srec_test_compile_active_context ( active_grammar ); 1802 else if ( LSTRCMP ( command, L("context_save") ) == 0 ) 1803 execute_status = srec_test_save_context ( active_grammar, current_end_command ); 1804 else if ( LSTRCMP ( command, L("addword_from_last_nametag") ) == 0 ) 1805 execute_status = srec_test_voice_enroll ( active_grammar, data, current_end_command ); 1806 else if ( LSTRCMP ( command, L("load_nametags") ) == 0 ) 1807 execute_status = srec_test_load_nametags ( data, current_end_command ); 1808 else if ( LSTRCMP ( command, L("save_nametags") ) == 0 ) 1809 execute_status = srec_test_save_nametags ( data, current_end_command ); 1810 else if ( LSTRCMP ( command, L("clear_nametags") ) ==0 ) 1811 execute_status = srec_test_clear_nametags ( data ); 1812 else if ( LSTRCMP ( command, L("add_to_nametags") ) == 0 ) 1813 execute_status = srec_test_add_to_nametags ( data, current_end_command ); 1814 else if ( LSTRCMP ( command, L("acousticstate_set") ) == 0 ) 1815 execute_status = srec_test_set_acousticstate ( data, current_end_command ); 1816 else if ( LSTRCMP ( command, L("acousticstate_get") ) == 0 ) 1817 execute_status = srec_test_get_acousticstate ( data ); 1818 else if ( LSTRCMP ( command, L("acousticstate_reset") ) == 0 ) 1819 execute_status = srec_test_reset_acousticstate ( data ); 1820 else if ( LSTRCMP ( command, L("forced_rec") ) == 0 ) 1821 execute_status = srec_test_set_forced_rec_mode ( data, current_end_command ); 1822 else if ( LSTRCMP ( command, L("change_sample_rate") ) == 0 ) 1823 execute_status = srec_test_change_sample_rate ( data, current_end_command ); 1824 else if ( LSTRCMP ( command, L("set_size_t_parameter") ) == 0 ) 1825 execute_status = srec_test_set_size_t_parameter ( data, current_end_command ); 1826 else if ( LSTRCMP ( command, L("get_size_t_parameter") ) == 0 ) 1827 execute_status = srec_test_get_size_t_parameter ( data, current_end_command ); 1828 else if ( LSTRCMP ( command, L("set_audio_size") ) == 0 ) 1829 execute_status = srec_test_set_audio_size ( data, current_end_command ); 1830 else if ( LSTRCMP ( command, L("sleep") ) == 0 ) 1831 execute_status = srec_test_sleep ( current_end_command ); 1832 else if ( *( command ) == L('#') ) 1833 execute_status = 0; /* Comment in file just skip */ 1834 else 1835 { 1836 execute_status = -1; 1837 LSPRINTF ( log_buffer, L("Unknown Command %s"), command ); 1838 srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, log_buffer ); 1839 } 1840 } 1841 else 1842 { 1843 /* Log Here */ 1844 } 1845 return ( execute_status ); 1846 } 1847 1848 1849 1850int srec_test_open_command_file ( PFile **command_file ) 1851 { 1852 int open_status; 1853 ESR_ReturnCode esr_status; 1854 LCHAR file_name [P_PATH_MAX]; 1855 size_t len; 1856 1857 open_status = 0; 1858 len = P_PATH_MAX; 1859 esr_status = ESR_SessionGetLCHAR ( L("cmdline.tcp"), file_name, &len ); 1860 1861 if ( esr_status == ESR_SUCCESS ) 1862 { 1863 len = P_PATH_MAX; 1864 esr_status = ESR_SessionPrefixWithBaseDirectory ( file_name, &len ); 1865 1866 if ( esr_status == ESR_SUCCESS ) 1867 { 1868 *command_file = pfopen ( file_name, L("r") ); 1869 1870 if ( *command_file == NULL ) 1871 { 1872 open_status = -1; 1873 LPRINTF(L("\nError opening command file %s\n"), file_name); 1874 } 1875 } 1876 else 1877 { 1878 open_status = -1; 1879 LPRINTF(L("\nError finding command file\n")); 1880 } 1881 } 1882 else 1883 { 1884 open_status = -1; 1885 /* Log Here */ 1886 } 1887 return ( open_status ); 1888 } 1889 1890 1891 1892void srec_test_close_command_file ( PFile *command_file ) 1893 { 1894 1895 pfclose ( command_file ); 1896 } 1897 1898 1899 1900void srec_test_close_audio_file ( PFile *audio_file ) 1901 { 1902 1903 pfclose ( audio_file ); 1904 } 1905 1906 1907 1908int srec_test_open_results_file ( FILE **results_file ) 1909 { 1910 int open_status; 1911 ESR_ReturnCode esr_status; 1912 LCHAR file_name [P_PATH_MAX]; 1913 size_t len; 1914 1915 open_status = 0; 1916 len = P_PATH_MAX; 1917 esr_status = ESR_SessionGetLCHAR ( L("cmdline.results"), file_name, &len ); 1918 1919 if ( esr_status == ESR_SUCCESS ) 1920 { 1921 *results_file = fopen ( file_name, L("w") ); 1922 1923 if ( ( *results_file ) == NULL ) 1924 { 1925 open_status = -1; 1926 /* Log Here */ 1927 } 1928 } 1929 else 1930 { 1931 open_status = -1; 1932 /* Log Here */ 1933 } 1934 return ( open_status ); 1935 } 1936 1937 1938 1939void srec_test_close_results_file ( FILE *results_file ) 1940 { 1941 1942 fclose ( results_file ); 1943 } 1944 1945 1946 1947int srec_test_process_commands ( ApplicationData *data ) 1948 { 1949 int process_status; 1950 PFile *command_file; 1951 FILE *results_file; 1952 LCHAR *got_line_ok; 1953 LCHAR linebuffer [MAX_LINE_LENGTH]; 1954 size_t recognition_count; 1955 1956 recognition_count = 0; 1957 process_status = srec_test_open_command_file ( &command_file ); 1958 1959 if ( process_status == 0 ) 1960 { 1961 process_status = srec_test_open_results_file ( &results_file ); 1962 1963 if ( process_status == 0 ) 1964 { 1965 do 1966 { 1967 got_line_ok = pfgets ( linebuffer, MAX_LINE_LENGTH, command_file ); 1968 1969 if ( got_line_ok != NULL ) 1970 srec_test_execute_command ( data, linebuffer, results_file, &recognition_count ); 1971 } 1972 while ( ( got_line_ok != NULL ) && ( process_status == 0 ) ); 1973 if( data->nametag ) { 1974 SR_NametagDestroy( data->nametag); 1975 data->nametag = NULL; 1976 } 1977 1978 1979 srec_test_close_results_file ( results_file ); 1980 } 1981 srec_test_close_command_file ( command_file ); 1982 } 1983 return ( process_status ); 1984 } 1985 1986 1987 1988int srec_test_open_pcm_file ( LCHAR *file_name, PFile **pcm_file ) 1989 { 1990 int open_status; 1991 ESR_ReturnCode esr_status; 1992 int lstr_result; 1993 LCHAR file_path [P_PATH_MAX]; 1994 size_t len; 1995 1996 open_status = 0; 1997 len = P_PATH_MAX; 1998 esr_status = ESR_SessionGetLCHAR ( L("cmdline.datapath"), file_path, &len ); 1999 2000 if ( esr_status == ESR_SUCCESS ) 2001 { 2002 len = P_PATH_MAX; 2003 esr_status = ESR_SessionPrefixWithBaseDirectory ( file_path, &len ); 2004 2005 if ( esr_status == ESR_SUCCESS ) 2006 { 2007 esr_status = pf_convert_backslashes_to_forwardslashes ( file_path ); 2008 2009 if ( esr_status == ESR_SUCCESS ) 2010 { 2011 len = P_PATH_MAX; 2012 lstr_result = lstrinsert ( file_path, file_name, 0, &len ); 2013 2014 if ( lstr_result == 0 ) 2015 { 2016 *pcm_file = pfopen ( file_name, L("rb") ); 2017 2018 if ( ( *pcm_file ) == NULL ) 2019 { 2020 open_status = -1; 2021 } 2022 } 2023 else 2024 { 2025 open_status = -1; 2026 /* Log Here */ 2027 } 2028 } 2029 else 2030 { 2031 open_status = -1; 2032 /* Log Here */ 2033 } 2034 } 2035 else 2036 { 2037 open_status = -1; 2038 /* Log Here */ 2039 } 2040 } 2041 else 2042 { 2043 open_status = -1; 2044 /* Log Here */ 2045 } 2046 if(open_status) PLogError( ("Error: while opening %s\n"), file_name); 2047 return ( open_status ); 2048 } 2049 2050 2051 2052int srec_test_open_nist_file ( LCHAR *file_name, PFile **nist_file ) 2053 { 2054 int open_status; 2055 ESR_ReturnCode esr_status; 2056 int lstr_result; 2057 LCHAR file_path [P_PATH_MAX]; 2058 size_t len; 2059 2060 open_status = 0; 2061 len = P_PATH_MAX; 2062 esr_status = ESR_SessionGetLCHAR ( L("cmdline.datapath"), file_path, &len ); 2063 2064 if ( esr_status == ESR_SUCCESS ) 2065 { 2066 len = P_PATH_MAX; 2067 esr_status = ESR_SessionPrefixWithBaseDirectory ( file_path, &len ); 2068 2069 if ( esr_status == ESR_SUCCESS ) 2070 { 2071 esr_status = pf_convert_backslashes_to_forwardslashes ( file_path ); 2072 2073 if(file_path[ LSTRLEN(file_path)-1] != L('/')) 2074 LSTRCAT(file_path,L("/")); 2075 2076 if ( esr_status == ESR_SUCCESS ) 2077 { 2078 len = P_PATH_MAX; 2079 lstr_result = lstrinsert ( file_path, file_name, 0, &len ); 2080 2081 if ( lstr_result == 0 ) 2082 { 2083 *nist_file = pfopen ( file_name, L("rb") ); 2084 2085 if ( ( *nist_file ) != NULL ) 2086 { 2087 esr_status = pfseek ( *nist_file, 1024, SEEK_SET ); 2088 2089 if ( esr_status != ESR_SUCCESS ) 2090 { 2091 open_status = -1; 2092 } 2093 } 2094 else 2095 { 2096 open_status = -1; 2097 } 2098 } 2099 else 2100 { 2101 open_status = -1; 2102 /* Log Here */ 2103 } 2104 } 2105 else 2106 { 2107 open_status = -1; 2108 /* Log Here */ 2109 } 2110 } 2111 else 2112 { 2113 open_status = -1; 2114 /* Log Here */ 2115 } 2116 } 2117 else 2118 { 2119 open_status = -1; 2120 /* Log Here */ 2121 } 2122 if(open_status) PLogError( ("Error: while opening %s\n"), file_name); 2123 return ( open_status ); 2124 } 2125 2126 2127 2128int srec_test_get_audio_from_file ( PFile *audio_file, ApplicationData *data, ESR_BOOL *hit_eof ) 2129 { 2130 int get_status; 2131 int eof_status; 2132 2133 get_status = 0; 2134 data->num_samples_read = pfread ( data->audio_buffer, sizeof ( asr_int16_t ), data->audio_buffer_requested_size, audio_file ); 2135 2136 if ( data->num_samples_read > 0 ) 2137 { 2138 *hit_eof = ESR_FALSE; 2139 } 2140 else 2141 { 2142 eof_status = pfeof ( audio_file ); 2143 2144 if ( eof_status == 0 ) 2145 { 2146 get_status = -1; 2147 /* Log Here */ 2148 } 2149 else 2150 { 2151 *hit_eof = ESR_TRUE; 2152 } 2153 } 2154 return ( get_status ); 2155 } 2156 2157 2158 2159int srec_test_feed_recognizer ( ApplicationData *data, ESR_BOOL hit_eof, SR_RecognizerStatus *esr_recog_status, SR_RecognizerResultType *result_type ) 2160 { 2161 int feed_status; 2162 ESR_ReturnCode esr_status; 2163 2164 feed_status = 0; 2165 esr_status = SR_RecognizerPutAudio ( data->recognizer, data->audio_buffer, &data->num_samples_read, hit_eof ); 2166 2167 if ( esr_status == ESR_SUCCESS ) 2168 { 2169 do 2170 { 2171 esr_status = SR_RecognizerAdvance ( data->recognizer, esr_recog_status, result_type, &data->result ); 2172 2173 if ( esr_status != ESR_SUCCESS ) 2174 { 2175 feed_status = -1; 2176 *esr_recog_status = SR_RECOGNIZER_EVENT_STOPPED; 2177 *result_type = SR_RECOGNIZER_RESULT_TYPE_COMPLETE; 2178 /* Log Here */ 2179 } 2180 } 2181 while ( ( *esr_recog_status ) == SR_RECOGNIZER_EVENT_INCOMPLETE ); 2182 } 2183 else 2184 { 2185 feed_status = -1; 2186 /* Log Here */ 2187 } 2188 return ( feed_status ); 2189 } 2190 2191 2192 2193int srec_test_flush_audio ( ApplicationData *data, SR_RecognizerStatus *esr_recog_status, SR_RecognizerResultType *result_type ) 2194 { 2195 int flush_status; 2196 ESR_ReturnCode esr_status; 2197 2198 flush_status = 0; 2199 2200 while ( ( *result_type ) != SR_RECOGNIZER_RESULT_TYPE_COMPLETE ) 2201 { 2202 esr_status = SR_RecognizerAdvance ( data->recognizer, esr_recog_status, result_type, &data->result ); 2203 2204 if ( esr_status != ESR_SUCCESS ) 2205 { 2206 flush_status = -1; 2207 *esr_recog_status = SR_RECOGNIZER_EVENT_STOPPED; 2208 *result_type = SR_RECOGNIZER_RESULT_TYPE_COMPLETE; 2209 /* Log Here */ 2210 } 2211 } 2212 return ( flush_status ); 2213 } 2214 2215 2216 2217int srec_test_process_start_timeout ( ApplicationData *data, FILE *results_file ) 2218 { 2219 int process_status; 2220 ESR_ReturnCode esr_status; 2221 size_t utterance_timeout; 2222 2223 process_status = 0; 2224 esr_status = ESR_SessionGetSize_t ( L("SREC.Recognizer.utterance_timeout"), &utterance_timeout ); 2225 2226 if ( esr_status == ESR_SUCCESS ) 2227 { 2228 LFPRINTF ( results_file, L("E: utterance_timeout %lu\n"), (unsigned long)utterance_timeout ); 2229 LFPRINTF ( results_file, L("R: <FAILED>\n") ); 2230 LPRINTF ( L("\n-------------------------------------\n") ); 2231 LPRINTF ( L("E: utterance_timeout %lu\n"), (unsigned long)utterance_timeout ); 2232 LPRINTF ( L("R: <FAILED>\n") ); 2233 LPRINTF ( L("-------------------------------------\n\n") ); 2234 } 2235 else 2236 { 2237 process_status = -1; 2238 /* Log Here */ 2239 } 2240 srec_test_log_recognition_failure ( data ); 2241 2242 return ( process_status ); 2243 } 2244 2245 2246int srec_test_write_and_log_wave_form_file ( ApplicationData *data, size_t recognition_count ) 2247 { 2248 int write_status; 2249 ESR_ReturnCode esr_status; 2250 LCHAR *wave_filename; 2251 2252 write_status = 0; 2253 LSPRINTF ( data->raw_waveform_filename, L("a%lu__%s.raw"), (unsigned long)recognition_count, data->transcription ); 2254 wave_filename = data->raw_waveform_filename; 2255 2256 while ( *wave_filename ) 2257 { 2258 if ( *wave_filename == ' ') 2259 *wave_filename = '_'; 2260 wave_filename++; 2261 } 2262/* just write to file for now... for testing purposes */ 2263 LPRINTF ( L("Dumping audio waveform to file %s\n"), data->raw_waveform_filename ); 2264 data->raw_waveform_file = pfopen ( data->raw_waveform_filename, L("wb") ); 2265 2266 if ( data->raw_waveform_file != NULL ) 2267 { 2268 pfwrite ( (void*)data->raw_waveform, 1, data->raw_waveform_size, data->raw_waveform_file ); 2269 pfclose ( data->raw_waveform_file ); 2270/* log the filename in the ESR log */ 2271 esr_status = SR_RecognizerLogToken ( data->recognizer, "WVFMFILENAME", data->raw_waveform_filename ); 2272 2273 if ( esr_status != ESR_SUCCESS ) 2274 { 2275 write_status = -1; 2276 /* Log Here */ 2277 } 2278 } 2279 else 2280 { 2281 write_status = -1; 2282 /* Log Here */ 2283 } 2284 return ( write_status ); 2285 } 2286 2287 2288 2289int srec_test_process_nbest_list ( ApplicationData *data, FILE *results_file ) 2290{ 2291 int process_status; 2292 ESR_ReturnCode esr_status; 2293 size_t nbestSize; 2294 size_t nbest_num; 2295 LCHAR linebuffer [MAX_LINE_LENGTH]; 2296 size_t line_length; 2297 2298 process_status = 0; 2299 /* At least one semantic result exists */ 2300 LPRINTF ( L("\n\n----------------------------------------------\n") ); 2301 2302 LPRINTF ( L("TRANSCRIPTION : '%s'\n"), data->transcription ); 2303 esr_status = SR_RecognizerResultGetSize (data->result, &nbestSize ); 2304 2305 if ( esr_status == ESR_SUCCESS ) 2306 { 2307 for ( nbest_num = 0; nbest_num < nbestSize; nbest_num++ ) 2308 { 2309 line_length = MAX_LINE_LENGTH; 2310 esr_status = SR_RecognizerResultGetValue ( data->result, nbest_num, L("literal"), linebuffer, &line_length ); 2311 2312 if ( esr_status == ESR_SUCCESS ) 2313 { 2314 LPRINTF ( L("LITERAL[%2lu] : '%s'\n"), (unsigned long)nbest_num, linebuffer ); 2315 line_length = MAX_LINE_LENGTH; 2316 esr_status = SR_RecognizerResultGetValue ( data->result, nbest_num, L("meaning"), linebuffer, &line_length ); 2317 2318 if ( esr_status != ESR_SUCCESS ) 2319 { 2320 linebuffer [0] = L('\0') ; 2321 } 2322 LPRINTF ( L("MEANING[%2lu] : '%s'\n"), (unsigned long)nbest_num, linebuffer ); 2323 2324 line_length = MAX_LINE_LENGTH; 2325 esr_status = SR_RecognizerResultGetValue (data->result, nbest_num , L("conf"), linebuffer, &line_length ); 2326 if ( esr_status == ESR_SUCCESS ) 2327 { 2328 LPRINTF ( L("CONFIDENCE VALUE : '%s'\n"), linebuffer ); 2329 } 2330 else 2331 { 2332 process_status = -1; 2333 /* Log Here */ 2334 } 2335 2336 line_length = MAX_LINE_LENGTH; 2337 esr_status = SR_RecognizerResultGetValue (data->result, nbest_num, L("raws"), linebuffer, &line_length ); 2338 2339 if ( esr_status == ESR_SUCCESS ) 2340 { 2341 LPRINTF ( L("RAW SCORE[%2lu] : '%s'\n"), (unsigned long)nbest_num, linebuffer); 2342 } 2343 else 2344 { 2345 process_status = -1; 2346 /* Log Here */ 2347 } 2348#ifdef SREC_ENGINE_VERBOSE_LOGGING 2349 if(1) { 2350 /* get ALL key value pairs */ 2351 LCHAR *keys[32], keysdata[128*32]; 2352 size_t k,key_count = sizeof(keys)/sizeof(keys[0]); 2353 for(k=0;k<key_count;k++) keys[k] = &keysdata[128*k]; 2354 // esr_status = SR_RecognizerResultGetKeyCount(data->result, nbest_num, &key_count); 2355 esr_status = SR_RecognizerResultGetKeyList(data->result, nbest_num, (LCHAR**)keys, &key_count); 2356 if(esr_status == ESR_SUCCESS) { 2357 for (k=0; k<key_count; k++) { 2358 LCHAR value[128]; 2359 size_t valuelen = sizeof(value)/sizeof(value[0]); 2360 esr_status = SR_RecognizerResultGetValue( data->result, nbest_num, keys[k], value, &valuelen); 2361 if(esr_status==ESR_SUCCESS) 2362 LPRINTF( L("%s[%2lu] : '%s'\n"), keys[k], (unsigned long)nbest_num, value); 2363 } 2364 } 2365 } 2366#endif 2367 LPRINTF ( L("\n")); 2368 } 2369 else 2370 { 2371 process_status = -1; 2372 /* Log Here */ 2373 } 2374 } 2375 } 2376 else 2377 { 2378 process_status = -1; 2379 /* Log Here */ 2380 } 2381 2382 LPRINTF ( L("----------------------------------------------\n\n") ); 2383 return ( process_status ); 2384} 2385 2386 2387 2388int srec_test_process_recognition ( ApplicationData *data, FILE *results_file, size_t recognition_count ) 2389{ 2390 int process_status; 2391 ESR_ReturnCode esr_status; 2392 APPL_GRAMMAR_DATA *grammar_data; 2393 LCHAR linebuffer [MAX_LINE_LENGTH]; 2394 size_t line_length; 2395 2396 process_status = 0; 2397 srec_test_get_active_grammar_data ( data, &grammar_data ); 2398 2399 do { 2400 line_length = MAX_LINE_LENGTH; 2401 esr_status = SR_RecognizerResultGetValue ( data->result, 0, L("literal"), linebuffer, &line_length ); 2402 2403 if ( esr_status != ESR_SUCCESS ) break; 2404 LFPRINTF ( results_file, L("R: %s\n"), linebuffer ); 2405 line_length = MAX_LINE_LENGTH; 2406 esr_status = SR_RecognizerResultGetValue ( data->result, 0, L("conf"), linebuffer, &line_length ); 2407 2408 if ( esr_status != ESR_SUCCESS ) break; 2409 2410 LFPRINTF ( results_file, L("S: %s\n"), linebuffer); 2411 LPRINTF ( L("S: %s\n"), linebuffer); 2412 srec_test_process_nbest_list ( data, results_file ); 2413 /* 2414 * SR_RecognizerResultGetWaveform will return pointer to buffer holding 2415 * audio data in it. This buffer is NOT under the application's control 2416 * and MUST only be read from. 2417 */ 2418 if ( grammar_data->is_ve_grammar == ESR_TRUE ) 2419 { 2420 esr_status = SR_NametagCreate ( data->result, L("dummyID"), &data->nametag ); 2421 if(esr_status != ESR_SUCCESS) { 2422 LPRINTF ( L("VoiceEnrollment=>FAILURE\n") ); 2423 break; 2424 } 2425 LPRINTF ( L("VoiceEnrollment=>SUCCESS\n") ); 2426 if(1) { 2427 const char* value = NULL; 2428 size_t len, len2; 2429 char* value2; 2430 /* this is to mimic what might happen in a real application */ 2431 esr_status = SR_NametagGetValue( data->nametag, &value, &len); 2432 value2 = (char*)CALLOC(len, sizeof(char), "srectest.nametag"); 2433 len2 = len; 2434 memcpy( value2, value, len); 2435 esr_status = SR_NametagDestroy(data->nametag); 2436 data->nametag = NULL; 2437 if(esr_status != ESR_SUCCESS) break; 2438 /* in a real application we would save the nametag value someplace 2439 along with the data->raw_waveform, then retrieve it and create a 2440 nametag from that value */ 2441 esr_status = SR_NametagCreateFromValue( L("dummyID"), value2, len2, &data->nametag); 2442 FREE( value2); 2443 value2 = NULL; 2444 } 2445 if ( esr_status != ESR_SUCCESS ) break; 2446 2447 esr_status = SR_RecognizerResultGetWaveform ( data->result, &data->raw_waveform, &data->raw_waveform_size ); 2448 if( esr_status != ESR_SUCCESS ) break; 2449 2450 if ( data->raw_waveform ) 2451 { 2452 process_status = srec_test_write_and_log_wave_form_file ( data, recognition_count ); 2453 2454 if ( process_status == 0 ) 2455 { 2456 esr_status = SR_RecognizerLogEvent ( data->recognizer, "ESRve" ); 2457 } 2458 else { 2459 esr_status = ESR_INVALID_STATE; 2460 } 2461 } 2462 } 2463 process_status = 0; 2464 } while(0); 2465 if( esr_status != ESR_SUCCESS) { 2466 process_status = -1; 2467 /* Log Here */ 2468 } 2469 return ( process_status ); 2470} 2471 2472 2473 2474int srec_test_process_recognition_fail ( ApplicationData *data ) 2475 { 2476 int process_status; 2477 ESR_ReturnCode esr_status; 2478 APPL_GRAMMAR_DATA *grammar_data; 2479 ESR_BOOL reason_status; 2480 2481 process_status = 0; 2482 srec_test_get_active_grammar_data ( data, &grammar_data ); 2483 LPRINTF(L("*** no match in recognition***\n")); 2484 2485 if ( grammar_data->is_ve_grammar == ESR_TRUE ) 2486 { 2487 data->nametag = NULL; 2488 LPRINTF ( L("VoiceEnrollement = FAILED : \n") ); 2489 } 2490 esr_status = SR_RecognizerIsSignalClipping ( data->recognizer, &reason_status ); 2491 2492 if ( esr_status == ESR_SUCCESS ) 2493 { 2494 if ( reason_status == ESR_TRUE ) 2495 LPRINTF ( L("- Signal is clipping\n") ); 2496 } 2497 else 2498 { 2499 process_status = -1; 2500 /* Log Here */ 2501 } 2502 esr_status = SR_RecognizerIsSignalDCOffset ( data->recognizer, &reason_status ); 2503 2504 if ( esr_status == ESR_SUCCESS ) 2505 { 2506 if ( reason_status == ESR_TRUE ) 2507 LPRINTF ( L("- Signal is DC-offset\n") ); 2508 } 2509 else 2510 { 2511 process_status = -1; 2512 /* Log Here */ 2513 } 2514 esr_status = SR_RecognizerIsSignalNoisy ( data->recognizer, &reason_status ); 2515 2516 if ( esr_status == ESR_SUCCESS ) 2517 { 2518 if ( reason_status == ESR_TRUE ) 2519 LPRINTF ( L("- Signal is noisy\n") ); 2520 } 2521 else 2522 { 2523 process_status = -1; 2524 /* Log Here */ 2525 } 2526 esr_status = SR_RecognizerIsSignalTooFewSamples ( data->recognizer, &reason_status ); 2527 2528 if ( esr_status == ESR_SUCCESS ) 2529 { 2530 if ( reason_status == ESR_TRUE ) 2531 LPRINTF ( L("- Signal has too few samples\n") ); 2532 } 2533 else 2534 { 2535 process_status = -1; 2536 /* Log Here */ 2537 } 2538 esr_status = SR_RecognizerIsSignalTooManySamples ( data->recognizer, &reason_status ); 2539 2540 if ( esr_status == ESR_SUCCESS ) 2541 { 2542 if ( reason_status == ESR_TRUE ) 2543 LPRINTF ( L("- Signal has too many samples\n") ); 2544 } 2545 else 2546 { 2547 process_status = -1; 2548 /* Log Here */ 2549 } 2550 esr_status = SR_RecognizerIsSignalTooQuiet ( data->recognizer, &reason_status ); 2551 2552 if ( esr_status == ESR_SUCCESS ) 2553 { 2554 if ( reason_status == ESR_TRUE ) 2555 LPRINTF ( L("- Signal is too quiet\n") ); 2556 } 2557 else 2558 { 2559 process_status = -1; 2560 /* Log Here */ 2561 } 2562 srec_test_log_recognition_failure ( data ); 2563 return ( process_status ); 2564 } 2565 2566 2567 2568int srec_test_process_recognition_unsupported_case ( ApplicationData *data, FILE *results_file ) 2569 { 2570 int process_status; 2571 2572 process_status = 0; 2573 LFPRINTF ( results_file, L("E: No results available\n") ); 2574 LFPRINTF ( results_file, L("R: <FAILED>\n") ); 2575 srec_test_log_recognition_failure ( data ); 2576 2577 return ( process_status ); 2578 } 2579 2580 2581 2582static void srec_test_log_recognition_failure ( ApplicationData *data ) 2583 { 2584 2585 LPRINTF(L("----------------------------------------------\n")); 2586 LPRINTF(L("TRANSCRIPTION : '%s'\n"), data->transcription); 2587 LPRINTF(L("<NO-RESULTS>\n")); 2588 LPRINTF(L("----------------------------------------------\n\n")); 2589 } 2590 2591 2592 2593int srec_test_process_results ( ApplicationData *data, SR_RecognizerStatus esr_recog_status, 2594 FILE *results_file, size_t recognition_count ) 2595 { 2596 int process_status; 2597 2598 switch ( esr_recog_status ) 2599 { 2600 case SR_RECOGNIZER_EVENT_START_OF_UTTERANCE_TIMEOUT: 2601 process_status = srec_test_process_start_timeout ( data, results_file ); 2602 break; 2603 2604 case SR_RECOGNIZER_EVENT_RECOGNITION_RESULT: 2605 process_status = srec_test_process_recognition ( data, results_file, recognition_count ); 2606 break; 2607 2608 case SR_RECOGNIZER_EVENT_NO_MATCH: 2609 process_status = srec_test_process_recognition_fail ( data ); 2610 break; 2611 2612 default: 2613 process_status = srec_test_process_recognition_unsupported_case ( data, results_file ); 2614 break; 2615 } 2616 return ( process_status ); 2617 } 2618 2619 2620 2621int srec_test_log_reco_from_file_data ( SR_Grammar *active_grammar, ApplicationData *data, LCHAR *waveform, LCHAR *bos, LCHAR *eos, LCHAR *transcription ) 2622 { 2623 int log_status; 2624 ESR_ReturnCode esr_status; 2625 size_t result_count; 2626 ESR_BOOL got_results; 2627 size_t transcription_length; 2628 2629 log_status = 0; 2630 LSPRINTF ( data->transcription, "%s", transcription ); 2631 transcription_length = LSTRLEN ( data->transcription ); 2632 2633 while ( ( *( data->transcription + transcription_length - 1 ) == '\n' ) || 2634 ( *( data->transcription + transcription_length - 1 ) == '\r' ) ) 2635 { 2636 *( data->transcription + transcription_length - 1 ) = '\0'; 2637 transcription_length--; 2638 } 2639 LPRINTF ( L("D: %s\nC: %s\n"), waveform, data->transcription ); 2640 2641 esr_status = SR_GrammarCheckParse ( active_grammar, data->transcription, 0, &result_count ); 2642 2643 if ( esr_status == ESR_SUCCESS ) 2644 { 2645 if ( result_count > 0 ) 2646 { 2647#ifdef ACCURACY_TESTING 2648 LCHAR meaning[MAX_LINE_LENGTH] = { 0 }; 2649 size_t len = MAX_LINE_LENGTH; 2650 got_results = ESR_TRUE; 2651 LPRINTF ( L("Sem (%lu): invocab=1\n"), (unsigned long)result_count ); 2652 srec_test_parse ( active_grammar, transcription, meaning, &len); 2653 if(LSTRLEN(meaning)>0) 2654 LPRINTF( L("CSem: %s\n"), meaning); 2655#else 2656 got_results = ESR_TRUE; 2657 LPRINTF ( L("Sem (%lu): invocab=1\n"), (unsigned long)result_count ); 2658#endif 2659 } 2660 else 2661 { 2662 got_results = ESR_FALSE; 2663 LPRINTF ( L("Sem: <NO INTERPRETATION FOUND>\n") ); 2664 LPRINTF ( L("CSem: <NO INTERPRETATION FOUND>\n") ); 2665 } 2666 esr_status = SR_RecognizerLogWaveformData ( data->recognizer, waveform, data->transcription, atof ( bos ), atof ( eos ), got_results ); 2667 2668 if ( esr_status != ESR_SUCCESS ) 2669 { 2670 log_status = -1; 2671 /* Log Here */ 2672 } 2673 } 2674 else 2675 { 2676 log_status = -1; 2677 /* Log Here */ 2678 } 2679 2680 return ( log_status ); 2681 } 2682 2683 2684 2685static int srec_test_recognize_pcm_file ( SR_Grammar *active_grammar, ApplicationData *data, FILE *results_file, 2686 LCHAR *command_text, size_t *recognition_count ) 2687 { 2688 int recognize_status; 2689 ESR_ReturnCode esr_status; 2690 SR_RecognizerStatus esr_recog_status; 2691 SR_RecognizerResultType result_type; 2692 LCHAR *transcription; 2693 LCHAR waveform [MAX_LINE_LENGTH]; 2694 LCHAR bos [MAX_LINE_LENGTH]; 2695 LCHAR eos [MAX_LINE_LENGTH]; 2696 PFile *waveform_file; 2697 ESR_BOOL hit_eof; 2698 2699 if ( active_grammar != NULL ) 2700 { 2701 recognize_status = srec_test_get_three_command_items ( command_text, MAX_LINE_LENGTH, waveform, 2702 MAX_LINE_LENGTH, bos, MAX_LINE_LENGTH, 2703 eos, NULL, &transcription ); 2704 while( *transcription == ' ' ) transcription++; 2705 if ( recognize_status == 0 ) 2706 { 2707 recognize_status = srec_test_log_reco_from_file_data ( active_grammar, data, waveform, bos, eos, transcription ); 2708 2709 if ( recognize_status == 0 ) 2710 { 2711 if(LSTRCMP(bos,"0") || LSTRCMP(eos,"0")) { 2712 PLogError( ("Error: non-zero BOS and non-zero EOS are not supported\n")); 2713 return -1; 2714 } 2715 recognize_status = srec_test_open_pcm_file ( waveform, &waveform_file ); 2716 2717 if ( recognize_status == 0 ) 2718 { 2719 if ( ( data->forced_rec_mode == ForcedRecModeOn ) || ( data->forced_rec_mode == ForcedRecModeOneTime ) ) 2720 SR_GrammarAllowOnly ( active_grammar, data->transcription ); 2721 esr_status = SR_RecognizerStart ( data->recognizer ); 2722 2723 if ( esr_status == ESR_SUCCESS ) 2724 { 2725 ( *recognition_count )++; 2726 hit_eof = ESR_FALSE; 2727 2728 do 2729 { 2730 recognize_status = srec_test_get_audio_from_file ( waveform_file, data, &hit_eof ); 2731 2732 if ( recognize_status == 0 ) 2733 recognize_status = srec_test_feed_recognizer ( data, hit_eof, &esr_recog_status, &result_type ); 2734 } 2735 while ( ( hit_eof == ESR_FALSE ) && ( result_type != SR_RECOGNIZER_RESULT_TYPE_COMPLETE ) && ( recognize_status == 0 ) ); 2736 2737 if ( recognize_status == 0 ) 2738 { 2739 recognize_status = srec_test_flush_audio ( data, &esr_recog_status, &result_type ); 2740 2741 if ( recognize_status == 0 ) 2742 { 2743 recognize_status = srec_test_process_results ( data, esr_recog_status, 2744 results_file, *recognition_count ); 2745 } 2746 } 2747 esr_status = SR_RecognizerStop ( data->recognizer ); 2748 2749 if (esr_status == ESR_SUCCESS ) 2750 { 2751 LPRINTF ( L("Recognizer has been stopped\n") ); 2752 } 2753 else 2754 { 2755 recognize_status = -1; 2756 LPRINTF ( L("Recognizer has failed to stop\n") ); 2757 } 2758 } 2759 if ( data->forced_rec_mode == ForcedRecModeOneTime ) 2760 { 2761 data->forced_rec_mode = ForcedRecModeOff; 2762 SR_GrammarAllowAll ( active_grammar ); 2763 } 2764 srec_test_close_audio_file ( waveform_file ); 2765 } 2766 } 2767 } 2768 } 2769 else 2770 { 2771 recognize_status = -1; 2772 /* Log Here */ 2773 } 2774 return ( recognize_status ); 2775 } 2776 2777 2778 2779static int srec_test_recognize_nist_file ( SR_Grammar *active_grammar, ApplicationData *data, FILE *results_file, 2780 LCHAR *command_text, size_t *recognition_count ) 2781 { 2782 int recognize_status; 2783 ESR_ReturnCode esr_status; 2784 SR_RecognizerStatus esr_recog_status; 2785 SR_RecognizerResultType result_type; 2786 LCHAR *transcription; 2787 LCHAR waveform [MAX_LINE_LENGTH]; 2788 LCHAR bos [MAX_LINE_LENGTH]; 2789 LCHAR eos [MAX_LINE_LENGTH]; 2790 PFile *waveform_file; 2791 ESR_BOOL hit_eof; 2792 2793 if ( active_grammar != NULL ) 2794 { 2795 recognize_status = srec_test_get_three_command_items ( command_text, MAX_LINE_LENGTH, waveform, 2796 MAX_LINE_LENGTH, bos, MAX_LINE_LENGTH, 2797 eos, NULL, &transcription ); 2798 while( *transcription == ' ' ) transcription++; 2799 if ( recognize_status == 0 ) 2800 { 2801 recognize_status = srec_test_log_reco_from_file_data ( active_grammar, data, waveform, bos, eos, transcription ); 2802 2803 if ( recognize_status == 0 ) 2804 { 2805 if(LSTRCMP(bos,"0") || LSTRCMP(eos,"0")) { 2806 PLogError( ("Error: non-zero BOS and non-zero EOS are not supported\n")); 2807 return -1; 2808 } 2809 recognize_status = srec_test_open_nist_file ( waveform, &waveform_file ); 2810 2811 if ( recognize_status == 0 ) 2812 { 2813 if ( ( data->forced_rec_mode == ForcedRecModeOn ) || ( data->forced_rec_mode == ForcedRecModeOneTime ) ) 2814 SR_GrammarAllowOnly ( active_grammar, data->transcription ); 2815 esr_status = SR_RecognizerStart ( data->recognizer ); 2816 2817 if ( esr_status == ESR_SUCCESS ) 2818 { 2819 ( *recognition_count )++; 2820 hit_eof = ESR_FALSE; 2821 2822 do 2823 { 2824 recognize_status = srec_test_get_audio_from_file ( waveform_file, data, &hit_eof ); 2825 2826 if ( recognize_status == 0 ) 2827 recognize_status = srec_test_feed_recognizer ( data, hit_eof, &esr_recog_status, &result_type ); 2828 } 2829 while ( ( hit_eof == ESR_FALSE ) && ( result_type != SR_RECOGNIZER_RESULT_TYPE_COMPLETE ) && ( recognize_status == 0 ) ); 2830 2831 if ( recognize_status == 0 ) 2832 { 2833 recognize_status = srec_test_flush_audio ( data, &esr_recog_status, &result_type ); 2834 2835 if ( recognize_status == 0 ) 2836 { 2837 recognize_status = srec_test_process_results ( data, esr_recog_status, 2838 results_file, *recognition_count ); 2839 } 2840 } 2841 } 2842 esr_status = SR_RecognizerStop ( data->recognizer ); 2843 2844 if (esr_status == ESR_SUCCESS ) 2845 { 2846 LPRINTF ( L("Recognizer has been stopped\n") ); 2847 } 2848 else 2849 { 2850 recognize_status = -1; 2851 LPRINTF ( L("Recognizer has failed to stop\n") ); 2852 } 2853 if ( data->forced_rec_mode == ForcedRecModeOneTime ) 2854 { 2855 data->forced_rec_mode = ForcedRecModeOff; 2856 SR_GrammarAllowAll ( active_grammar ); 2857 } 2858 srec_test_close_audio_file ( waveform_file ); 2859 } 2860 } 2861 } 2862 } 2863 else 2864 { 2865 recognize_status = -1; 2866 /* Log Here */ 2867 } 2868 return ( recognize_status ); 2869 } 2870 2871 2872 2873#define STACK_SIZE (1024 * 200) 2874 2875#ifdef _WIN32 2876/* disable optimization for the next functions as the compiler optimizes away the assignment to mySTACK[i] */ 2877#pragma optimize("", off) 2878 2879static void initStack() 2880{ 2881 int mySTACK[STACK_SIZE]; 2882 { 2883 /* This extra block is to ensure that local variables of the function occur after buffer mySTACK. */ 2884 int i; 2885 2886 for (i = 0; i < STACK_SIZE; ++i) 2887 { 2888 mySTACK[i] = 0xDEADBEEF; 2889 } 2890 } 2891} 2892 2893static int analyzeStack() 2894{ 2895 int mySTACK[STACK_SIZE]; 2896 { 2897 2898 /* This extra block is to ensure that local variables of the function occur after buffer mySTACK. */ 2899 int i, j; 2900 2901 for (i = STACK_SIZE - 1; i >= 0; --i) 2902 { 2903 if (mySTACK[i] == 0xDEADBEEF) 2904 { 2905 /* This might be a candidate for the end of stack marker, or it could be 2906 some value that is equal to our marker. To ensure reliability of 2907 this candidate, we will make sure that all remaining entries int the 2908 stack are also equal to DEADBEEF. 2909 */ 2910 for (j = i - 1; j >= 0; --j) 2911 { 2912 if (mySTACK[j] != 0xDEADBEEF) 2913 { 2914 i = j; 2915 break; 2916 } 2917 } 2918 if (j < 0) break; 2919 } 2920 } 2921 2922 if (i < 0) 2923 return -1; 2924 else 2925 return (STACK_SIZE - 1 - i) * sizeof(int); 2926 } 2927} 2928 2929/* restore optmization settings to what they used to be. */ 2930#pragma optimize("", on) 2931#endif 2932 2933 2934#ifdef ACCURACY_TESTING 2935static void srec_test_clean_up_sentence ( char* sentence ) 2936 { 2937 int clean_up_status; 2938 int sentence_finished; 2939 SENTENCE_CLEANING_STATES current_state; 2940 char *current_input; 2941 char *current_output; 2942 2943 clean_up_status = 0; 2944 sentence_finished = 0; 2945 current_state = SENTENCE_BEGIN; 2946 current_input = sentence; 2947 current_output = sentence; 2948 strtok( sentence, "\n\r"); 2949 2950 do 2951 { 2952 switch ( *current_input ) 2953 { 2954 case '\0': 2955 switch ( current_state ) 2956 { 2957 case SENTENCE_BEGIN: 2958 break; 2959 2960 case SENTENCE_BEGIN_BRACKET_BEGIN: /* Is this error condition */ 2961 *current_output = '\0'; 2962 clean_up_status = -1; 2963 break; 2964 2965 case SENTENCE_BEGIN_BRACKET_END: 2966 *current_output = '\0'; 2967 break; 2968 2969 case SENTENCE_MIDDLE: 2970 *current_output = '\0'; 2971 break; 2972 2973 case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */ 2974 *( current_output - 1 ) = '\0'; 2975 clean_up_status = -1; 2976 break; 2977 2978 case SENTENCE_MIDDLE_BRACKET_END: 2979 *( current_output - 1 ) = '\0'; 2980 break; 2981 2982 case SENTENCE_MIDDLE_WITH_SPACE: 2983 *( current_output - 1 ) = '\0'; 2984 break; 2985 2986 default: 2987 *current_output = '\0'; 2988 /* Log error */ 2989 break; 2990 } 2991 sentence_finished = 1; 2992 break; 2993 2994 case ' ': 2995 switch ( current_state ) 2996 { 2997 case SENTENCE_BEGIN: 2998 break; 2999 3000 case SENTENCE_BEGIN_BRACKET_BEGIN: 3001 break; 3002 3003 case SENTENCE_BEGIN_BRACKET_END: 3004 break; 3005 3006 case SENTENCE_MIDDLE: 3007 *current_output = ' '; 3008 current_output++; 3009 current_state = SENTENCE_MIDDLE_WITH_SPACE; 3010 break; 3011 3012 case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */ 3013 break; 3014 3015 case SENTENCE_MIDDLE_BRACKET_END: 3016 break; 3017 3018 case SENTENCE_MIDDLE_WITH_SPACE: 3019 break; 3020 3021 default: 3022 *current_output = '\0'; 3023 clean_up_status = -1; 3024 /* Log error */ 3025 break; 3026 } 3027 current_input++; 3028 break; 3029 3030 case '[': 3031 switch ( current_state ) 3032 { 3033 case SENTENCE_BEGIN: 3034 current_state = SENTENCE_BEGIN_BRACKET_BEGIN; 3035 break; 3036 3037 case SENTENCE_BEGIN_BRACKET_BEGIN: 3038 *current_output = '\0'; 3039 clean_up_status = -1; 3040 /* Log error */ 3041 break; 3042 3043 case SENTENCE_BEGIN_BRACKET_END: 3044 current_state = SENTENCE_BEGIN_BRACKET_BEGIN; 3045 break; 3046 3047 case SENTENCE_MIDDLE: 3048 *current_output = ' '; 3049 current_output++; 3050 current_state = SENTENCE_MIDDLE_BRACKET_BEGIN; 3051 break; 3052 3053 case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */ 3054 *current_output = '\0'; 3055 clean_up_status = -1; 3056 /* Log error */ 3057 break; 3058 3059 case SENTENCE_MIDDLE_BRACKET_END: 3060 current_state = SENTENCE_MIDDLE_BRACKET_BEGIN; 3061 break; 3062 3063 case SENTENCE_MIDDLE_WITH_SPACE: 3064 current_state = SENTENCE_MIDDLE_BRACKET_BEGIN; 3065 break; 3066 3067 default: 3068 *current_output = '\0'; 3069 clean_up_status = -1; 3070 /* Log error */ 3071 break; 3072 } 3073 current_input++; 3074 break; 3075 3076 case ']': 3077 switch ( current_state ) 3078 { 3079 case SENTENCE_BEGIN: 3080 *current_output = '\0'; 3081 clean_up_status = -1; 3082 /* Log error */ 3083 break; 3084 3085 case SENTENCE_BEGIN_BRACKET_BEGIN: 3086 current_state = SENTENCE_BEGIN_BRACKET_END; 3087 break; 3088 3089 case SENTENCE_BEGIN_BRACKET_END: 3090 *current_output = '\0'; 3091 clean_up_status = -1; 3092 /* Log error */ 3093 break; 3094 3095 case SENTENCE_MIDDLE: 3096 *current_output = '\0'; 3097 clean_up_status = -1; 3098 /* Log error */ 3099 break; 3100 3101 case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */ 3102 current_state = SENTENCE_MIDDLE_BRACKET_END; 3103 break; 3104 3105 case SENTENCE_MIDDLE_BRACKET_END: 3106 *current_output = '\0'; 3107 clean_up_status = -1; 3108 /* Log error */ 3109 break; 3110 3111 case SENTENCE_MIDDLE_WITH_SPACE: 3112 *current_output = '\0'; 3113 clean_up_status = -1; 3114 /* Log error */ 3115 break; 3116 3117 default: 3118 *current_output = '\0'; 3119 clean_up_status = -1; 3120 /* Log error */ 3121 break; 3122 } 3123 current_input++; 3124 break; 3125 3126 default: 3127 switch ( current_state ) 3128 { 3129 case SENTENCE_BEGIN: 3130 *current_output = *current_input; 3131 current_output++; 3132 current_state = SENTENCE_MIDDLE; 3133 break; 3134 3135 case SENTENCE_BEGIN_BRACKET_BEGIN: 3136 break; 3137 3138 case SENTENCE_BEGIN_BRACKET_END: 3139 *current_output = *current_input; 3140 current_output++; 3141 current_state = SENTENCE_MIDDLE; 3142 break; 3143 3144 case SENTENCE_MIDDLE: 3145 *current_output = *current_input; 3146 current_output++; 3147 current_state = SENTENCE_MIDDLE; 3148 break; 3149 3150 case SENTENCE_MIDDLE_BRACKET_BEGIN: 3151 break; 3152 3153 case SENTENCE_MIDDLE_BRACKET_END: 3154 *current_output = *current_input; 3155 current_output++; 3156 current_state = SENTENCE_MIDDLE; 3157 break; 3158 3159 case SENTENCE_MIDDLE_WITH_SPACE: 3160 *current_output = *current_input; 3161 current_output++; 3162 current_state = SENTENCE_MIDDLE; 3163 break; 3164 3165 default: 3166 *current_output = '\0'; 3167 clean_up_status = -1; 3168 /* Log error */ 3169 break; 3170 } 3171 current_input++; 3172 break; 3173 } 3174 } 3175 while ( ( sentence_finished == 0 ) && ( clean_up_status == 0 ) ); 3176 } 3177 3178 3179 3180ESR_ReturnCode srec_test_parse ( SR_Grammar* grammar, const LCHAR* trans, LCHAR* meaning, size_t *len ) 3181 { 3182 ESR_ReturnCode parse_status; 3183 char cleaned_trans[TRANSCRIPTION_SIZE]; 3184 SR_SemanticResult *semanticResults; 3185 size_t result_count; 3186 3187 result_count = 0; 3188 strcpy( cleaned_trans, trans ); 3189 srec_test_clean_up_sentence ( cleaned_trans ); 3190 3191 /* create the result holders, initially not greater than MAX */ 3192 parse_status = SR_SemanticResultCreate ( &semanticResults ); 3193 3194 if ( parse_status == ESR_SUCCESS ) 3195 { 3196 result_count = 1; 3197 parse_status = SR_SemanticProcessor_Flush( ( (SR_GrammarImpl*)grammar )->semproc ); 3198 3199 if ( parse_status == ESR_SUCCESS ) 3200 { 3201 parse_status = SR_SemanticProcessor_SetParam( ((SR_GrammarImpl*)grammar)->semproc, L("literal"), cleaned_trans ); 3202 3203 if ( parse_status == ESR_SUCCESS ) 3204 { 3205 parse_status = SR_GrammarCheckParse( grammar, cleaned_trans, &semanticResults, (size_t*)&result_count ); 3206 3207 if ( parse_status == ESR_SUCCESS ) 3208 { 3209 if ( result_count < 1 ) 3210 { 3211 LSTRCPY ( meaning, L("") ); 3212 SR_SemanticResultDestroy( semanticResults); 3213 parse_status = ESR_NO_MATCH_ERROR; 3214 } 3215 else 3216 { 3217 parse_status = SR_SemanticResultGetValue ( semanticResults, "meaning", meaning, len); 3218 SR_SemanticResultDestroy( semanticResults); 3219 } 3220 } 3221 } 3222 } 3223 } 3224 return ( parse_status ); 3225 } 3226#endif 3227 3228 3229static int srec_test_get_run_params ( unsigned int *num_shutdown_loops, unsigned int *num_continuous_run_loops ) 3230 { 3231 int get_status; 3232 3233 get_status = get_num_srec_test_shutdown_times ( num_shutdown_loops ); 3234 3235 if ( get_status == 0 ) 3236 get_status = get_num_srec_test_continuous_loops ( num_continuous_run_loops ); 3237 3238 return ( get_status ); 3239 } 3240 3241 3242 3243static int srec_test_shutdown_application_data ( ApplicationData *applicationData ) 3244 { 3245 int init_status; 3246 int i; 3247 3248 init_status = 0; 3249 3250 applicationData->argc = 0; 3251 applicationData->argv = NULL; 3252 applicationData->locale = -1; 3253 3254 if (applicationData->recognizer != NULL) 3255 { 3256 srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup Recognizer") ); 3257/* SR_RecognizerLogSessionEnd ( applicationData->recognizer ); 3258 SR_RecognizerDestroy ( applicationData->recognizer );*/ 3259 applicationData->recognizer = NULL; 3260 applicationData->result = NULL; // this was deallocated by SR_RecognizerDestroy() 3261 } 3262 3263 if (applicationData->vocabulary != NULL) 3264 { 3265 srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup Vocabulary") ); 3266/* SR_VocabularyDestroy(applicationData->vocabulary); 3267 applicationData->vocabulary = NULL;*/ 3268 } 3269 3270 if (applicationData->nametag != NULL) 3271 { 3272 srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup NameTag") ); 3273/* SR_NametagDestroy(applicationData->nametag); 3274 applicationData->nametag = NULL;*/ 3275 } 3276 3277 if (applicationData->nametags != NULL) 3278 { 3279 srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup NameTagSet") ); 3280/* SR_NametagsDestroy(applicationData->nametags); 3281 applicationData->nametags = NULL;*/ 3282 } 3283 3284 for (i = 0; i < applicationData->grammarCount; ++i) 3285 srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup Grammar") ); 3286/* 3287 if ( applicationData->grammars [i] != NULL ) 3288 { 3289 printf ( "!!!!!!!! %d Grammars Not Destroyed !!!!!!!!!!!!\n", i ); 3290 SR_GrammarDestroy(applicationData->grammars[i]); 3291 applicationData->grammars[i] = NULL; 3292 } 3293 } 3294 3295 applicationData->activeGrammar = -1; 3296 applicationData->activeRule = NULL; 3297 applicationData->voiceEnrollment = NULL; 3298*/ 3299 applicationData->raw_waveform = NULL; 3300 applicationData->raw_waveform_filename [0] = L( '\0' ); 3301 applicationData->raw_waveform_file = NULL; 3302 applicationData->transcription[0] = L( '\0' ); 3303 applicationData->forced_rec_mode = ForcedRecModeNotSet; 3304 3305 return ( init_status ); 3306 } 3307 3308 3309 3310static int srec_test_init_application_data ( ApplicationData *applicationData, int arg_count, LCHAR *arg_vals [] ) 3311 { 3312 int init_status; 3313 3314 init_status = 0; 3315 applicationData->argc = arg_count; 3316 applicationData->argv = arg_vals; 3317 applicationData->locale = -1; 3318 applicationData->recognizer = NULL; 3319 applicationData->result = NULL; 3320 applicationData->vocabulary = NULL; 3321 applicationData->nametag = NULL; 3322 applicationData->nametags = NULL; 3323 applicationData->grammarCount = 0; /* No need to initialize arrays, index is 0 */ 3324 applicationData->active_grammar_num = -1; 3325/* applicationData->activeRule = NULL; 3326 applicationData-> = applicationData->voiceEnrollment = NULL;*/ 3327 applicationData->audio_buffer_requested_size = DEFAULT_AUDIO_BUFFER_SIZE; 3328 applicationData->raw_waveform = NULL; 3329 applicationData->raw_waveform_filename [0] = L( '\0' ); 3330 applicationData->raw_waveform_file = NULL; 3331 applicationData->transcription[0] = L( '\0' ); 3332 applicationData->forced_rec_mode = ForcedRecModeNotSet; 3333 return ( init_status ); 3334 } 3335 3336 3337static int srec_test_run_test_shutdown_session ( ApplicationData *applicationData ) 3338 { 3339 int shutdown_status; 3340 3341 shutdown_status = 0; 3342 3343 SR_RecognizerUnsetup ( applicationData->recognizer); // releases acoustic models 3344 SR_RecognizerDestroy ( applicationData->recognizer ); 3345 applicationData->recognizer = NULL; 3346 ShutdownSession ( ); 3347 3348 return ( shutdown_status ); 3349 } 3350 3351 3352 3353static int srec_test_run_test_init_session ( ApplicationData *applicationData ) 3354{ 3355 int run_status; 3356 ESR_ReturnCode esr_status; 3357 3358 run_status = 0; 3359 LPRINTF(L("\nCreate Recognizer:\n")); 3360 LPRINTF(L(" InitSession()\n")); 3361 esr_status = InitSession ( applicationData->argc, applicationData->argv ); 3362 3363 if ( esr_status == ESR_SUCCESS ) 3364 { 3365 LPRINTF(L(" SR_RecognizerCreate()\n")); 3366 esr_status = SR_RecognizerCreate ( &applicationData->recognizer ); 3367 3368 if ( esr_status != ESR_SUCCESS ) 3369 { 3370 ShutdownSession ( ); 3371 run_status = -1; 3372 /* Log Here */ 3373 } else { 3374 LPRINTF(L(" SR_RecognizerSetup()\n")); 3375 esr_status = SR_RecognizerSetup ( applicationData->recognizer); 3376 if ( esr_status != ESR_SUCCESS ) 3377 { 3378 ShutdownSession ( ); 3379 run_status = -1; 3380 /* Log Here */ 3381 } 3382 } 3383 } 3384 return ( run_status ); 3385} 3386 3387 3388static int srec_test_run_test_shutdown_vocab_grammar ( ApplicationData *applicationData ) 3389 { 3390 int shutdown_status; 3391 3392 shutdown_status = 0; 3393 SR_RecognizerLogSessionEnd ( applicationData->recognizer ); 3394/* SR_GrammarDestroy ( applicationData->grammars [0].grammar ); 3395 applicationData->grammars [0].grammar = NULL;*/ 3396 SR_VocabularyDestroy ( applicationData->vocabulary ); 3397 applicationData->vocabulary = NULL; 3398 3399 return ( shutdown_status ); 3400 } 3401 3402 3403 3404static int srec_test_run_test_init_vocab_grammar ( ApplicationData *applicationData ) 3405 { 3406 int run_status; 3407 ESR_ReturnCode esr_status; 3408 LCHAR filename[P_PATH_MAX]; 3409 size_t len; 3410 3411 run_status = 0; 3412 /* Create vocabulary object and associate with grammar */ 3413 LPRINTF(L("Create vocabulary object and associate with grammar:\n")); 3414 len = P_PATH_MAX; 3415 esr_status = ESR_SessionGetLCHAR ( L("cmdline.vocabulary"), filename, &len ); 3416 3417 if ( esr_status == ESR_SUCCESS ) 3418 { 3419 LPRINTF(L(" SR_VocabularyLoad()\n")); 3420 esr_status = SR_VocabularyLoad ( filename, &applicationData->vocabulary ); 3421 3422 if ( esr_status == ESR_SUCCESS ) 3423 { 3424 LPRINTF(L(" SR_VocabularyGetLanguage()\n")); 3425 esr_status = SR_VocabularyGetLanguage ( applicationData->vocabulary, &applicationData->locale ); 3426 3427 if ( esr_status == ESR_SUCCESS ) 3428 { 3429 /* start a new log session */ 3430 LPRINTF( L("Start a new log session:\n") ); 3431 LPRINTF( L(" SR_RecognizerLogSessionStart()\n") ); 3432 esr_status = SR_RecognizerLogSessionStart ( applicationData->recognizer, L("SRecTest.session1") ); 3433 3434 if ( esr_status != ESR_SUCCESS ) 3435 { 3436 SR_VocabularyDestroy ( applicationData->vocabulary ); 3437 applicationData->vocabulary = NULL; 3438 run_status = -1; 3439 /* Log here */ 3440 } 3441 } 3442 else 3443 { 3444 SR_VocabularyDestroy ( applicationData->vocabulary ); 3445 applicationData->vocabulary = NULL; 3446 run_status = -1; 3447 /* Log Here */ 3448 } 3449 } 3450 else 3451 { 3452 run_status = -1; 3453 /* Log Here */ 3454 } 3455 } 3456 else 3457 { 3458 run_status = -1; 3459 /* Log Here */ 3460 } 3461 return ( run_status ); 3462 } 3463 3464 3465 3466static int srec_test_run_test_shutdown ( ApplicationData *applicationData ) 3467 { 3468 int shutdown_status; 3469 3470 shutdown_status = srec_test_run_test_shutdown_vocab_grammar ( applicationData ); 3471 3472 if ( shutdown_status == 0 ) 3473 shutdown_status = srec_test_run_test_shutdown_session ( applicationData ); 3474 return ( shutdown_status ); 3475 } 3476 3477 3478static int srec_test_run_test_init ( ApplicationData *applicationData ) 3479{ 3480 int run_status; 3481 3482 run_status = srec_test_run_test_init_session ( applicationData ); 3483 3484 if ( run_status == 0 ) 3485 { 3486 run_status = srec_test_run_test_init_vocab_grammar ( applicationData ); 3487 3488 if ( run_status != 0 ) 3489 { 3490 srec_test_run_test_shutdown_session ( applicationData ); 3491 } 3492 } 3493 else 3494 { 3495 srec_test_run_test_shutdown_session ( applicationData ); 3496 } 3497 return ( run_status ); 3498} 3499 3500 3501 3502static int srec_test_run_test_execute ( ApplicationData *applicationData ) 3503 { 3504 int run_status; 3505 ESR_ReturnCode esr_status; 3506 3507 run_status = 0; 3508 applicationData->nametag = NULL; 3509 3510 LPRINTF(L("Recognize:\n")); 3511 LPRINTF(L(" SR_NametagsCreate()\n")); 3512 esr_status = SR_NametagsCreate ( &applicationData->nametags ); 3513 3514 if ( esr_status == ESR_SUCCESS ) 3515 { 3516 run_status = srec_test_process_commands ( applicationData ); 3517 SR_NametagsDestroy ( applicationData->nametags ); 3518 applicationData->nametags = NULL; 3519 3520 if ( run_status != 0 ) 3521 { 3522 /* Log Here */ 3523 } 3524 } 3525 else 3526 { 3527 run_status = -1; 3528 /* Log Here */ 3529 } 3530 return ( run_status ); 3531 } 3532 3533 3534 3535static int srec_test_run_test ( ApplicationData *applicationData ) 3536 { 3537 int run_status; 3538 3539 run_status = srec_test_run_test_init ( applicationData ); 3540 3541 if ( run_status == 0 ) 3542 { 3543 run_status = srec_test_run_test_execute ( applicationData ); 3544 srec_test_run_test_shutdown ( applicationData ); 3545 } 3546 return ( run_status ); 3547 } 3548 3549 3550 3551static int srec_test_shutdown_memory_system ( void ) 3552 { 3553 int shutdown_status; 3554 3555 shutdown_status = 0; 3556 PMemShutdown ( ); 3557 3558 return ( shutdown_status ); 3559 } 3560 3561 3562 3563static int srec_test_init_memory_system ( unsigned int srec_test_heap_size ) 3564 { 3565 int init_status; 3566 ESR_ReturnCode esr_status; 3567 3568 init_status = 0; 3569 esr_status = PMemInit ( ); 3570 3571 if ( esr_status == ESR_SUCCESS ) 3572 { 3573 PSTACK_SIZE_INIT ( ); /* I don't know what this is, it should probably have a status */ 3574 } 3575 else 3576 { 3577 init_status = -1; 3578 /* Log Here */ 3579 } 3580 return ( init_status ); 3581 } 3582 3583 3584 3585static int srec_test_shutdown_file_system ( void ) 3586 { 3587 int shutdown_status; 3588 3589 shutdown_status = 0; 3590 3591 return ( shutdown_status ); 3592 } 3593 3594 3595 3596static int srec_test_init_file_system ( int arg_count, LCHAR *arg_vals [] ) 3597 { 3598 int init_status; 3599 3600 init_status = 0; 3601 return ( init_status ); 3602 } 3603 3604 3605 3606static int srec_test_shutdown_logging_system ( PLogger *logger ) 3607 { 3608 int shutdown_status; 3609 3610 shutdown_status = 0; 3611 PLogShutdown ( ); 3612 3613 return ( shutdown_status ); 3614 } 3615 3616 3617 3618static int srec_test_init_logging_system ( int arg_count, LCHAR *arg_vals [], PLogger *logger ) 3619 { 3620 int init_status; 3621 ESR_ReturnCode esr_status; 3622 3623 init_status = 0; 3624 esr_status = PLogCreateFileLogger ( PSTDOUT, &logger ); 3625 3626 if ( esr_status == ESR_SUCCESS ) 3627 { 3628 esr_status = PLogInit ( logger, 0 ); 3629 3630 if ( esr_status != ESR_SUCCESS ) 3631 { 3632/* pfclose ( (struct PFile_t *)logger ); I'm not sure this is correct, check with Gili */ 3633 init_status = -1; 3634 /* Log Here */ 3635 } 3636 } 3637 else 3638 { 3639 init_status = -1; 3640 /* Log Here */ 3641 } 3642 return ( init_status ); 3643 } 3644 3645 3646 3647static int srec_test_shutdown_system ( PLogger *logger ) 3648 { 3649 int shutdown_status; 3650 3651 shutdown_status = srec_test_shutdown_logging_system ( logger ); 3652 3653 if ( shutdown_status == 0 ) 3654 { 3655 shutdown_status = srec_test_shutdown_file_system ( ); 3656 3657 if ( shutdown_status == 0 ) 3658 shutdown_status = srec_test_shutdown_memory_system ( ); 3659 } 3660 return ( shutdown_status ); 3661 } 3662 3663 3664 3665 3666static int srec_test_init_system ( unsigned int srec_test_heap_size, PLogger* logger, int arg_count, LCHAR *arg_vals [] ) 3667 { 3668 int init_status; 3669 3670 /* register signal handler so cleanup works on CTRL-C (Win32) */ 3671#ifdef _WIN32 3672 signal(SIGINT, signal_handler_SIGINT); 3673#endif 3674 init_status = srec_test_init_memory_system ( srec_test_heap_size ); 3675 3676 if ( init_status == 0 ) 3677 { 3678 init_status = srec_test_init_file_system ( arg_count, arg_vals ); 3679 3680 if ( init_status == 0 ) 3681 { 3682 init_status = srec_test_init_logging_system ( arg_count, arg_vals, logger ); 3683 3684 if ( init_status != 0 ) 3685 { 3686 srec_test_shutdown_file_system ( ); 3687 srec_test_shutdown_memory_system ( ); 3688 } 3689 } 3690 else 3691 { 3692 srec_test_shutdown_memory_system ( ); 3693 } 3694 } 3695 return ( init_status ); 3696 } 3697 3698 3699 3700#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32) 3701_CrtMemState s0,s1,s2; 3702 3703 3704 3705void OutputDivider( void ) 3706 { 3707 3708 _RPT1(_CRT_WARN, "%s", "********************************************************************\n"); 3709 } 3710 3711 3712 3713void OutputNewline( void ) 3714 { 3715 3716 _RPT1(_CRT_WARN, "%s", "\n"); 3717 } 3718#endif 3719 3720 3721 3722#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32) 3723void initialize_heap_logging ( void ) 3724 { 3725 3726 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); 3727 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT); 3728 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE); 3729 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT); 3730 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE); 3731 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT); 3732 _CrtMemCheckpoint(&s0); 3733 } 3734#endif 3735 3736 3737 3738#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32) 3739void execute_heap_logging ( void ) 3740 { 3741 OutputNewline(); 3742 OutputDivider(); 3743 _RPT1(_CRT_WARN, "%s", "After cleanup, before exiting main()\n"); 3744 OutputDivider(); 3745 _CrtMemCheckpoint(&s1); 3746 _CrtMemDifference(&s2, &s0, &s1); 3747 _CrtMemDumpStatistics(&s2); 3748 OutputNewline(); 3749 OutputDivider(); 3750 _RPT1(_CRT_WARN, "%s", "Checking for memory leaks...\n"); 3751 OutputDivider(); 3752 _CrtDumpMemoryLeaks(); 3753 } 3754#endif 3755 3756 3757 3758int main(int argc, LCHAR* argv [] ) 3759 { 3760 int test_status; 3761 unsigned int num_shutdown_loops; 3762 unsigned int current_shutdown_loop; 3763 unsigned int num_continuous_run_loops; 3764 unsigned int current_continuous_run_loop; 3765 unsigned int srec_test_heap_size; 3766 3767#ifdef _WIN32 3768 initStack(); 3769#endif 3770 { 3771 /* This extra block is there to get a more precise estimate of the stack 3772 depth. This way we also make sure that the local variables of main are 3773 taken into acount when estimating the stack size. 3774 */ 3775 ApplicationData applicationData; 3776 PLogger* logger; 3777 3778 srec_test_heap_size = ( 4 * 1024 * 1024 ); 3779 logger = NULL; 3780 test_status = srec_test_get_run_params ( &num_shutdown_loops, &num_continuous_run_loops ); 3781 3782 if ( test_status == 0 ) 3783 { 3784 current_shutdown_loop = 0; 3785 3786 while ( ( current_shutdown_loop < num_shutdown_loops )/* && ( test_status == 0 )*/ ) 3787 { 3788#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32) 3789 initialize_heap_logging ( ); 3790#endif 3791 test_status = srec_test_init_system ( srec_test_heap_size, logger, argc, argv ); 3792 3793 if ( test_status == 0 ) 3794 { 3795 current_continuous_run_loop = 0; 3796 3797 while ( ( current_continuous_run_loop < num_continuous_run_loops ) && ( test_status == 0 ) ) 3798 { 3799 test_status = srec_test_init_application_data ( &applicationData, argc, argv ); 3800 3801 if ( test_status == 0 ) 3802 { 3803 test_status = srec_test_run_test ( &applicationData ); 3804 srec_test_shutdown_application_data ( &applicationData ); 3805 } 3806 current_continuous_run_loop++; 3807 } 3808 test_status = srec_test_shutdown_system ( logger ); 3809 } 3810 current_shutdown_loop++; 3811#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32) 3812 execute_heap_logging ( ); 3813#endif 3814 } 3815 } 3816 } 3817 3818 return ( test_status ); 3819 } 3820 3821 3822