SRecTest.c revision 2ebd1d2ce82c4f10284c70a28ea62535349a7334
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 = ESR_SessionSetBool("enableGetWaveform", ESR_FALSE);
997                    //esr_status = SR_RecognizerSetBoolParameter ( data->recognizer, L("enableGetWaveform"), ESR_FALSE );
998
999                    if ( esr_status != ESR_SUCCESS )
1000                        {
1001                        free_status = -1;
1002                        /* Log Here */
1003                        }
1004                    }
1005                else
1006                    {
1007                    esr_status = ESR_SUCCESS;
1008                    }
1009                if ( esr_status == ESR_SUCCESS )
1010                    {
1011                    esr_status = SR_RecognizerDeactivateRule ( data->recognizer, data->grammars [data->active_grammar_num].grammar,
1012                                                               data->grammars [data->active_grammar_num].ruleName );
1013                    if ( esr_status == ESR_SUCCESS )
1014                        {
1015                          data->active_grammar_num = -1;
1016                        }
1017                    else
1018                        {
1019                        free_status = -1;
1020                        /* Log Here */
1021                        }
1022                    }
1023                }
1024            else
1025                {
1026                free_status = -1;
1027                /* Log Here */
1028                }
1029            }
1030        else
1031            {
1032            free_status = -1;
1033            /* Log Here */
1034            }
1035        }
1036    else
1037        {
1038        /* Log Here */
1039        }
1040    return ( free_status );
1041    }
1042
1043
1044
1045int srec_test_unload_context ( ApplicationData *data, LCHAR *command_text )
1046    {
1047    int             unload_status;
1048    ESR_ReturnCode  esr_status;
1049    int             grammar_num;
1050    ESR_BOOL            found_grammar;
1051    ESR_BOOL            grammar_is_active;
1052    ESR_BOOL            grammar_is_ve;
1053    LCHAR           grammar_id [P_PATH_MAX];
1054
1055   unload_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, grammar_id, NULL );
1056
1057   if ( unload_status == 0 )
1058        {
1059        found_grammar = srec_test_get_grammar_from_id ( data, grammar_id, &grammar_num, &grammar_is_active, &grammar_is_ve );
1060
1061        if ( found_grammar == ESR_TRUE )
1062            {
1063            if ( grammar_is_active == ESR_FALSE )
1064                {
1065                esr_status = SR_GrammarDestroy ( data->grammars [grammar_num].grammar );
1066
1067                if ( esr_status != ESR_SUCCESS )
1068                    {
1069                    unload_status = -1;
1070                    /* Log Here */
1071                    }
1072                srec_test_delete_grammar ( data, grammar_num );
1073                }
1074            else
1075                {
1076                unload_status = -1;
1077                /* Log Here */
1078                }
1079            }
1080        else
1081            {
1082            unload_status = -1;
1083            /* Log Here */
1084            }
1085            }
1086    else
1087        {
1088        /* Log Here */
1089        }
1090    return ( unload_status );
1091    }
1092
1093
1094
1095int srec_test_use_context ( SR_Grammar *active_grammar, ApplicationData *data, LCHAR *command_text )
1096    {
1097    int             use_status;
1098    ESR_ReturnCode  esr_status;
1099    int             grammar_num;
1100    ESR_BOOL            found_grammar;
1101    ESR_BOOL            grammar_is_active;
1102    ESR_BOOL            grammar_is_ve;
1103    LCHAR           grammar_id [P_PATH_MAX];
1104
1105    if ( active_grammar == NULL )
1106        {
1107        use_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, grammar_id, NULL );
1108
1109        if ( use_status == 0 )
1110            {
1111            found_grammar = srec_test_get_grammar_from_id ( data, grammar_id, &grammar_num, &grammar_is_active, &grammar_is_ve );
1112
1113            if ( found_grammar == ESR_TRUE )
1114                {
1115                    esr_status = SR_RecognizerActivateRule ( data->recognizer, data->grammars [grammar_num].grammar,
1116                                                             data->grammars [grammar_num].ruleName, 1 );
1117                    if ( esr_status == ESR_SUCCESS )
1118                        {
1119                        if ( data->grammars [grammar_num].is_ve_grammar == ESR_TRUE )
1120                            {
1121                            esr_status = ESR_SessionSetBool("enableGetWaveform", ESR_TRUE);
1122                            //esr_status = SR_RecognizerSetBoolParameter ( data->recognizer, L("enableGetWaveform"), ESR_TRUE );
1123
1124                            if ( esr_status == ESR_SUCCESS )
1125                                {
1126                                data->active_grammar_num = (int)grammar_num;
1127                                }
1128                            else
1129                                {
1130                                use_status = -1;
1131                                /* Log Here */
1132                                SR_RecognizerDeactivateRule ( data->recognizer, data->grammars [grammar_num].grammar,
1133                                                              data->grammars [grammar_num].ruleName );
1134                                }
1135                            }
1136                        else
1137                            {
1138                            data->active_grammar_num = (int)grammar_num;
1139                            }
1140                        }
1141                    else
1142                        {
1143                        use_status = -1;
1144                        /* Log Here */
1145                        }
1146                }
1147            else
1148                {
1149                use_status = -1;
1150                /* Log Here */
1151                }
1152            }
1153        else
1154            {
1155            use_status = -1;
1156            /* Log Here */
1157            }
1158        }
1159    else
1160        {
1161        use_status = -1;
1162		PLogError("ERROR: context_use ignored, use context_free first!");
1163        /* Log Here */
1164        }
1165    return ( use_status );
1166    }
1167
1168
1169
1170int srec_test_save_context ( SR_Grammar *active_grammar, LCHAR *command_text )
1171    {
1172    int             save_status;
1173    ESR_ReturnCode  esr_status;
1174    LCHAR           file_name [P_PATH_MAX];
1175
1176    if ( active_grammar != NULL )
1177        {
1178        save_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, file_name, NULL );
1179
1180        if ( save_status == 0 )
1181            {
1182            esr_status = SR_GrammarSave ( active_grammar, file_name );
1183
1184            if ( esr_status != ESR_SUCCESS )
1185                {
1186                save_status = -1;
1187                /* Log Here */
1188                }
1189            }
1190        else
1191            {
1192            /* Log Here */
1193            }
1194        }
1195    else
1196        {
1197        save_status = -1;
1198        srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, "No Grammar Activated" );
1199        }
1200    return ( save_status );
1201    }
1202
1203
1204
1205int srec_test_get_size_t_parameter ( ApplicationData *data, LCHAR *command_text )
1206    {
1207    int             get_status;
1208    ESR_ReturnCode  esr_status;
1209    LCHAR           key [P_PATH_MAX];
1210    size_t          value;
1211
1212    get_status = srec_test_get_one_command_item ( command_text, P_PATH_MAX, key, NULL );
1213
1214    if ( get_status == 0 )
1215        {
1216        esr_status = SR_RecognizerGetSize_tParameter ( data->recognizer, key, &value );
1217
1218        if ( esr_status == ESR_SUCCESS )
1219            {
1220            printf ( "Get Parameter  :  %s  :  %u\n", key, value );
1221        }
1222    else
1223            {
1224            get_status = -1;
1225            /* Log Here */
1226            }
1227        }
1228    return ( get_status );
1229    }
1230
1231
1232
1233int srec_test_set_size_t_parameter ( ApplicationData *data, LCHAR *command_text )
1234    {
1235    int             set_status;
1236    ESR_ReturnCode  esr_status;
1237    LCHAR           key [P_PATH_MAX];
1238    LCHAR           value [MAX_UINT_DIGITS+1];
1239    int             value_as_number;
1240    int             convert_string_to_num;
1241
1242    set_status = srec_test_get_two_command_items ( command_text, P_PATH_MAX, key,
1243                                                   MAX_UINT_DIGITS + 1, value, NULL, NULL );
1244    if ( set_status == 0 )
1245        {
1246        convert_string_to_num = lstrtoi ( value, &value_as_number, 10 );
1247
1248        if ( convert_string_to_num == 0 )
1249            {
1250            if ( value_as_number > 0 )
1251                {
1252                esr_status = SR_RecognizerSetSize_tParameter ( data->recognizer, key, (size_t)value_as_number );
1253
1254                if ( esr_status != ESR_SUCCESS )
1255                    {
1256                    set_status = -1;
1257                    /* Log Here */
1258                    }
1259                }
1260            else
1261                {
1262                set_status = -1;
1263                /* Log Here */
1264                }
1265            }
1266        else
1267            {
1268            set_status = -1;
1269            /* Log Here */
1270            }
1271        }
1272    return ( set_status );
1273    }
1274
1275
1276
1277int srec_test_voice_enroll ( SR_Grammar *active_grammar, ApplicationData *data, LCHAR *command_text )
1278    {
1279    int             enroll_status;
1280    ESR_ReturnCode  esr_status;
1281    LCHAR           slot [P_PATH_MAX];
1282    LCHAR           nametagID [P_PATH_MAX];
1283    LCHAR           weight [MAX_UINT_DIGITS+1];
1284    int             weightNumber;
1285    int             convert_string_to_num;
1286
1287    if ( active_grammar != NULL )
1288        {
1289        if ( data->nametag != NULL )
1290            {
1291            enroll_status = srec_test_get_three_command_items ( command_text, P_PATH_MAX, slot, P_PATH_MAX, nametagID,
1292                                                                MAX_UINT_DIGITS + 1, weight, NULL, NULL );
1293            if ( enroll_status == 0 )
1294                {
1295                convert_string_to_num = lstrtoi ( weight, &weightNumber, 10 );
1296
1297                if ( convert_string_to_num == 0 )
1298                    {
1299                    esr_status = SR_NametagSetID ( data->nametag, nametagID );
1300
1301                    if ( esr_status == ESR_SUCCESS )
1302                        {
1303			  LCHAR value[MAX_LINE_LENGTH];
1304			  LSPRINTF( value,"V='%s';",nametagID);
1305                        esr_status = SR_GrammarAddNametagToSlot ( active_grammar, slot, data->nametag, weightNumber, value );
1306                        if ( esr_status != ESR_SUCCESS )
1307                            {
1308                            enroll_status = -1;
1309                            /* Log Here */
1310                            }
1311                        }
1312                    else
1313                        {
1314                        enroll_status = -1;
1315                        /* Log Here */
1316                        }
1317                    }
1318                else
1319                    {
1320                    enroll_status = -1;
1321                    /* Log Here */
1322                    }
1323                }
1324            else
1325                {
1326                /* Log Here */
1327                }
1328            }
1329        else
1330            {
1331            enroll_status = -1;
1332            /* Log Here */
1333            }
1334        }
1335    else
1336        {
1337        enroll_status = -1;
1338        srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, "No Grammar Activated" );
1339        }
1340    return ( enroll_status );
1341    }
1342
1343
1344
1345int srec_test_load_nametags ( ApplicationData *data, LCHAR *command_text )
1346    {
1347    int             load_status;
1348    ESR_ReturnCode  esr_status;
1349    LCHAR           file_name [P_PATH_MAX];
1350
1351    load_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, file_name, NULL );
1352
1353    if ( load_status == 0 )
1354        {
1355        esr_status = SR_NametagsLoad ( data->nametags, file_name );
1356
1357        if ( esr_status != ESR_SUCCESS )
1358            {
1359            load_status = -1;
1360            /* Log Here */
1361            }
1362        }
1363    else
1364        {
1365        load_status = -1;
1366        /* Log Here */
1367        }
1368    return ( load_status );
1369    }
1370
1371
1372
1373int srec_test_save_nametags ( ApplicationData *data, LCHAR *command_text )
1374    {
1375    int             save_status;
1376    ESR_ReturnCode  esr_status;
1377    LCHAR           file_name [P_PATH_MAX];
1378
1379    save_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, file_name, NULL );
1380
1381    if ( save_status == 0 )
1382        {
1383        esr_status = SR_NametagsSave ( data->nametags, file_name );
1384
1385        if ( esr_status != ESR_SUCCESS )
1386            {
1387            save_status = -1;
1388            /* Log Here */
1389            }
1390        }
1391    else
1392        {
1393        save_status = -1;
1394        /* Log Here */
1395        }
1396    return ( save_status );
1397    }
1398
1399
1400
1401int srec_test_clear_nametags ( ApplicationData *data )
1402    {
1403    int             clear_status;
1404    ESR_ReturnCode  esr_status;
1405
1406    clear_status = 0;
1407    esr_status = SR_NametagsDestroy ( data->nametags );
1408
1409    if ( esr_status == ESR_SUCCESS )
1410        {
1411        data->nametags = NULL;
1412        esr_status = SR_NametagsCreate ( &data->nametags );
1413
1414        if ( esr_status != ESR_SUCCESS )
1415            {
1416            clear_status = -1;
1417            /* Log Here */
1418            }
1419        }
1420    else
1421        {
1422        clear_status = -1;
1423        /* Log Here */
1424        }
1425    return ( clear_status );
1426    }
1427
1428
1429
1430int srec_test_add_to_nametags ( ApplicationData *data, LCHAR *command_text )
1431    {
1432    int             add_status;
1433    ESR_ReturnCode  esr_status;
1434    LCHAR           nametagID [P_PATH_MAX];
1435
1436    if ( data->nametag != NULL )
1437        {
1438        add_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, nametagID, NULL );
1439
1440        if ( add_status == 0 )
1441            {
1442            esr_status = SR_NametagSetID ( data->nametag, nametagID );
1443
1444            if ( esr_status == ESR_SUCCESS )
1445                {
1446                esr_status = SR_NametagsAdd ( data->nametags, data->nametag );
1447
1448                if ( esr_status == ESR_SUCCESS )
1449                    {
1450                    data->nametag = NULL;
1451                    }
1452                else
1453                    {
1454                    add_status = -1;
1455                    /* Log Here */
1456                    }
1457                }
1458            else
1459                {
1460                add_status = -1;
1461                /* Log Here */
1462                }
1463            }
1464        else
1465            {
1466            add_status = -1;
1467            /* Log Here */
1468            }
1469        }
1470    else
1471        {
1472        add_status = -1;
1473        /* Log Here */
1474        }
1475    return ( add_status );
1476    }
1477
1478
1479int srec_test_voice_enroll_nametags ( SR_Grammar *active_grammar, ApplicationData *data, LCHAR *command_text )
1480    {
1481    int             enroll_status;
1482    ESR_ReturnCode  esr_status;
1483    LCHAR           slot [P_PATH_MAX];
1484    SR_Nametag      *nametag;
1485    size_t          nametags_size;
1486    size_t          current_nametag;
1487
1488    if ( active_grammar != NULL )
1489        {
1490        enroll_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, slot, NULL );
1491
1492        if ( enroll_status == 0 )
1493            {
1494            esr_status = SR_NametagsGetSize (data->nametags, &nametags_size );
1495
1496            if ( esr_status == ESR_SUCCESS )
1497                {
1498                current_nametag = 0;
1499
1500                while ( ( current_nametag < nametags_size ) && ( esr_status == ESR_SUCCESS ) )
1501                    {
1502                    esr_status = SR_NametagsGetAtIndex ( data->nametags, current_nametag, &nametag );
1503
1504                    if ( esr_status == ESR_SUCCESS )
1505                        {
1506                        esr_status = SR_GrammarAddNametagToSlot ( active_grammar, slot, nametag, 0, NULL );
1507
1508                        if ( esr_status == ESR_SUCCESS )
1509                            {
1510                            current_nametag++;
1511                            }
1512                        else
1513                            {
1514                            enroll_status = -1;
1515                            /* Log Here */
1516                            }
1517                        }
1518                    else
1519                        {
1520                        enroll_status = -1;
1521                        /* Log Here */
1522                        }
1523                    }
1524                }
1525            else
1526                {
1527                enroll_status = -1;
1528                /* Log Here */
1529                }
1530            }
1531        else
1532            {
1533            /* Log Here */
1534            }
1535        }
1536    else
1537        {
1538        enroll_status = -1;
1539        srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, "No Grammar Activated" );
1540        }
1541    return ( enroll_status );
1542    }
1543
1544
1545
1546int srec_test_set_audio_size ( ApplicationData *data, LCHAR *command_text )
1547    {
1548    int             set_status;
1549    LCHAR           new_size [P_PATH_MAX];
1550    int             size_as_number;
1551    int             convert_string_to_num;
1552
1553    set_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, new_size, NULL );
1554
1555    if ( set_status == 0 )
1556        {
1557        convert_string_to_num = lstrtoi ( new_size, &size_as_number, 10 );
1558
1559        if ( convert_string_to_num == 0 )
1560            {
1561            if ( ( size_as_number > 0 ) && ( size_as_number <= MAX_AUDIO_BUFFER_SIZE ) )
1562                {
1563                data->audio_buffer_requested_size = size_as_number;
1564                LPRINTF ( L("Audio Size Set To %d\n"), size_as_number );
1565                }
1566            else
1567                {
1568                set_status = -1;
1569                /* Log Here */
1570                }
1571            }
1572        else
1573            {
1574            set_status = -1;
1575            /* Log Here */
1576            }
1577        }
1578    return ( set_status );
1579    }
1580
1581
1582#ifdef _WIN32
1583int srec_test_sleep ( LCHAR *command_text )
1584    {
1585
1586    return ( - 1 );
1587    }
1588#else
1589int srec_test_sleep ( LCHAR *command_text )
1590    {
1591    int         sleep_status;
1592    struct timeval  sleep_time_struct;
1593    LCHAR           sleep_time [P_PATH_MAX];
1594    int             time_as_number;
1595    int             convert_string_to_num;
1596
1597    sleep_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, sleep_time, NULL );
1598
1599    if ( sleep_status == 0 )
1600        {
1601        convert_string_to_num = lstrtoi ( sleep_time, &time_as_number, 10 );
1602
1603        if ( convert_string_to_num == 0 )
1604            {
1605        sleep_time_struct.tv_sec = time_as_number;
1606        sleep_time_struct.tv_usec = 0;
1607        select ( 0, NULL, NULL, NULL, &sleep_time_struct );
1608        }
1609    else
1610        {
1611        sleep_status = -1;
1612        /* Log Here */
1613        }
1614        }
1615    return ( sleep_status );
1616    }
1617#endif
1618
1619
1620int srec_test_change_sample_rate ( ApplicationData *data, LCHAR *command_text )
1621    {
1622    int             change_status;
1623    ESR_ReturnCode  esr_status;
1624    LCHAR           new_rate [P_PATH_MAX];
1625    int             rate_as_number;
1626    int             convert_string_to_num;
1627
1628    change_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, new_rate, NULL );
1629
1630    if ( change_status == 0 )
1631        {
1632        convert_string_to_num = lstrtoi ( new_rate, &rate_as_number, 10 );
1633
1634        if ( convert_string_to_num == 0 )
1635            {
1636            esr_status = SR_Recognizer_Change_Sample_Rate ( data->recognizer, rate_as_number );
1637
1638            if ( esr_status != ESR_SUCCESS )
1639                {
1640                change_status = -1;
1641                /* Log Here */
1642                }
1643            }
1644        else
1645            {
1646            change_status = -1;
1647            /* Log Here */
1648            }
1649        }
1650    if ( change_status == 0 )
1651        printf ( "\nSuccessfully Set Sample Rate To  :  %s\n", new_rate );
1652    else
1653        printf ( "\nFailed To Set Sample Rate To  :  %s\n", new_rate );
1654    return ( change_status );
1655    }
1656
1657
1658
1659int srec_test_set_acousticstate ( ApplicationData *data, LCHAR *command_text )
1660    {
1661    int             set_status;
1662    ESR_ReturnCode  esr_status;
1663    LCHAR           state_string [P_PATH_MAX];
1664
1665    set_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, state_string, NULL );
1666
1667    if ( set_status == 0 )
1668        {
1669        esr_status = SR_AcousticStateSet ( data->recognizer, state_string );
1670
1671        if ( esr_status != ESR_SUCCESS )
1672            {
1673            set_status = -1;
1674            /* Log Here */
1675            }
1676        }
1677    else
1678        {
1679        /* Log Here */
1680        }
1681    return ( set_status );
1682    }
1683
1684
1685
1686int srec_test_get_acousticstate ( ApplicationData *data )
1687    {
1688    int             get_status;
1689    ESR_ReturnCode  esr_status;
1690    const LCHAR     *state_string;
1691
1692    get_status = 0;
1693    esr_status = SR_AcousticStateGet ( data->recognizer, &state_string );
1694
1695    if ( esr_status == ESR_SUCCESS )
1696        {
1697        printf ( "Acoustic State :\n%s\n", state_string );
1698        }
1699    else
1700        {
1701        get_status = -1;
1702        /* Log Here */
1703        }
1704    return ( get_status );
1705    }
1706
1707
1708
1709int srec_test_reset_acousticstate ( ApplicationData *data )
1710    {
1711    int             reset_status;
1712    ESR_ReturnCode  esr_status;
1713
1714    reset_status = 0;
1715    esr_status = SR_AcousticStateReset ( data->recognizer );
1716
1717    if ( esr_status != ESR_SUCCESS )
1718        {
1719        reset_status = -1;
1720        /* Log Here */
1721        }
1722    return ( reset_status );
1723    }
1724
1725
1726
1727int srec_test_set_forced_rec_mode ( ApplicationData *data, LCHAR *command_text )
1728    {
1729    int         set_status;
1730    LCHAR       mode[P_PATH_MAX];
1731
1732    set_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, mode, NULL );
1733
1734    if ( set_status == 0 )
1735        {
1736        if ( LSTRCMP ( mode, L("one_time") ) == 0 )
1737            {
1738            data->forced_rec_mode = ForcedRecModeOneTime;
1739            }
1740        else if ( LSTRCMP ( mode, L("off") ) == 0 )
1741            {
1742            data->forced_rec_mode = ForcedRecModeOff;
1743            }
1744        else if ( LSTRCMP ( mode, L("on") ) == 0 )
1745            {
1746            data->forced_rec_mode = ForcedRecModeOn;
1747            }
1748        else
1749            {
1750            set_status = -1;
1751            /* Log Here */
1752            }
1753        }
1754    return ( set_status );
1755    }
1756
1757
1758
1759int srec_test_execute_command ( ApplicationData *data, LCHAR *text, FILE *results_file, size_t *recognition_count )
1760    {
1761    int         execute_status;
1762    LCHAR       *current_command_start;
1763    LCHAR       *current_end_command;
1764    LCHAR       command [MAX_LINE_LENGTH];
1765    SR_Grammar  *active_grammar;
1766    LCHAR       log_buffer [LOG_BUFFER_SIZE];
1767
1768    srec_test_get_active_grammar ( data, &active_grammar );
1769    current_command_start = text;
1770    execute_status = srec_test_get_one_command_item ( current_command_start, MAX_LINE_LENGTH, command, &current_end_command );
1771
1772    if ( execute_status == 0 )
1773        {
1774        if( data->nametag
1775            && ((LSTRCMP(command, "recognize_nist")==0) || (LSTRCMP(command, "recognize_pcm")==0)) ) {
1776                /* if there is a nametag held in memory, and we don't make use of it, then
1777                let's destroy it here */
1778            SR_NametagDestroy(  data->nametag);
1779            data->nametag = NULL;
1780        }
1781
1782        if ( LSTRCMP ( command, L("recognize_nist") ) == 0 )
1783            execute_status = srec_test_recognize_nist_file ( active_grammar, data, results_file,
1784                                                             current_end_command, recognition_count );
1785        else if ( LSTRCMP ( command, L("recognize_pcm") ) == 0 )
1786            execute_status = srec_test_recognize_pcm_file ( active_grammar, data, results_file,
1787                                                            current_end_command, recognition_count );
1788        else if ( LSTRCMP ( command, L("context_load") ) == 0 )
1789            execute_status = srec_test_load_context ( data, current_end_command );
1790        else if ( LSTRCMP ( command, L("context_use") ) == 0 )
1791            execute_status = srec_test_use_context ( active_grammar, data, current_end_command );
1792        else if ( LSTRCMP ( command, L("context_free") ) == 0 )
1793            execute_status = srec_test_free_context ( active_grammar, data, current_end_command );
1794        else if ( LSTRCMP ( command, L("context_unload") ) == 0 )
1795            execute_status = srec_test_unload_context ( data, current_end_command );
1796        else if ( LSTRCMP ( command, L("addwords_from_nametags") ) == 0 )
1797            execute_status = srec_test_voice_enroll_nametags ( active_grammar, data, current_end_command );
1798        else if ( LSTRCMP ( command, L("resetslots") ) == 0 )
1799            execute_status = srec_test_reset_slots ( active_grammar );
1800        else  if ( LSTRCMP ( command, L("addword") ) == 0 )
1801            execute_status = srec_test_add_word ( active_grammar, current_end_command );
1802        else if ( LSTRCMP ( command, L("context_compile") ) == 0 )
1803            execute_status = srec_test_compile_active_context ( active_grammar );
1804        else if ( LSTRCMP ( command, L("context_save") ) == 0 )
1805            execute_status = srec_test_save_context ( active_grammar, current_end_command );
1806        else if ( LSTRCMP ( command, L("addword_from_last_nametag") ) == 0 )
1807            execute_status = srec_test_voice_enroll ( active_grammar, data, current_end_command );
1808        else if ( LSTRCMP ( command, L("load_nametags") ) == 0 )
1809            execute_status = srec_test_load_nametags ( data, current_end_command );
1810        else if ( LSTRCMP ( command, L("save_nametags") ) == 0 )
1811            execute_status = srec_test_save_nametags ( data, current_end_command );
1812        else if ( LSTRCMP ( command, L("clear_nametags") ) ==0 )
1813            execute_status = srec_test_clear_nametags ( data );
1814        else if ( LSTRCMP ( command, L("add_to_nametags") ) == 0 )
1815            execute_status = srec_test_add_to_nametags ( data, current_end_command );
1816        else if ( LSTRCMP ( command, L("acousticstate_set") ) == 0 )
1817            execute_status = srec_test_set_acousticstate ( data, current_end_command );
1818        else if ( LSTRCMP ( command, L("acousticstate_get") ) == 0 )
1819            execute_status = srec_test_get_acousticstate ( data );
1820        else if ( LSTRCMP ( command, L("acousticstate_reset") ) == 0 )
1821            execute_status = srec_test_reset_acousticstate ( data );
1822        else if ( LSTRCMP ( command, L("forced_rec") ) == 0 )
1823            execute_status = srec_test_set_forced_rec_mode ( data, current_end_command );
1824        else if ( LSTRCMP ( command, L("change_sample_rate") ) == 0 )
1825            execute_status = srec_test_change_sample_rate ( data, current_end_command );
1826        else if ( LSTRCMP ( command, L("set_size_t_parameter") ) == 0 )
1827            execute_status = srec_test_set_size_t_parameter ( data, current_end_command );
1828        else if ( LSTRCMP ( command, L("get_size_t_parameter") ) == 0 )
1829            execute_status = srec_test_get_size_t_parameter ( data, current_end_command );
1830        else if ( LSTRCMP ( command, L("set_audio_size") ) == 0 )
1831            execute_status = srec_test_set_audio_size ( data, current_end_command );
1832        else if ( LSTRCMP ( command, L("sleep") ) == 0 )
1833            execute_status = srec_test_sleep ( current_end_command );
1834        else if ( *( command ) == L('#') )
1835            execute_status = 0;   /* Comment in file just skip */
1836        else
1837            {
1838            execute_status = -1;
1839            LSPRINTF ( log_buffer, L("Unknown Command %s"), command );
1840            srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, log_buffer );
1841            }
1842        }
1843    else
1844        {
1845        /* Log Here */
1846        }
1847    return ( execute_status );
1848    }
1849
1850
1851
1852int srec_test_open_command_file ( PFile **command_file )
1853    {
1854    int             open_status;
1855    ESR_ReturnCode  esr_status;
1856    LCHAR           file_name [P_PATH_MAX];
1857    size_t          len;
1858
1859    open_status = 0;
1860    len = P_PATH_MAX;
1861    esr_status = ESR_SessionGetLCHAR ( L("cmdline.tcp"), file_name, &len );
1862
1863    if ( esr_status == ESR_SUCCESS )
1864        {
1865        len = P_PATH_MAX;
1866        esr_status = ESR_SessionPrefixWithBaseDirectory ( file_name, &len );
1867
1868        if ( esr_status == ESR_SUCCESS )
1869            {
1870            *command_file = pfopen ( file_name, L("r") );
1871
1872            if ( *command_file == NULL )
1873                {
1874                open_status = -1;
1875                LPRINTF(L("\nError opening command file %s\n"), file_name);
1876                }
1877            }
1878        else
1879            {
1880            open_status = -1;
1881            LPRINTF(L("\nError finding command file\n"));
1882            }
1883        }
1884     else
1885        {
1886        open_status = -1;
1887        /* Log Here */
1888        }
1889     return ( open_status );
1890     }
1891
1892
1893
1894void srec_test_close_command_file ( PFile *command_file )
1895    {
1896
1897    pfclose ( command_file );
1898    }
1899
1900
1901
1902void srec_test_close_audio_file ( PFile *audio_file )
1903    {
1904
1905    pfclose ( audio_file );
1906    }
1907
1908
1909
1910int srec_test_open_results_file ( FILE **results_file )
1911    {
1912    int             open_status;
1913    ESR_ReturnCode  esr_status;
1914    LCHAR           file_name [P_PATH_MAX];
1915    size_t          len;
1916
1917    open_status = 0;
1918    len = P_PATH_MAX;
1919    esr_status = ESR_SessionGetLCHAR ( L("cmdline.results"), file_name, &len );
1920
1921    if ( esr_status == ESR_SUCCESS )
1922        {
1923        *results_file = fopen ( file_name, L("w") );
1924
1925        if ( ( *results_file ) == NULL )
1926            {
1927            open_status = -1;
1928            /* Log Here */
1929            }
1930        }
1931     else
1932         {
1933         open_status = -1;
1934         /* Log Here */
1935         }
1936     return ( open_status );
1937     }
1938
1939
1940
1941void srec_test_close_results_file ( FILE *results_file )
1942    {
1943
1944    fclose ( results_file );
1945    }
1946
1947
1948
1949int srec_test_process_commands ( ApplicationData *data )
1950    {
1951    int             process_status;
1952    PFile           *command_file;
1953    FILE            *results_file;
1954    LCHAR           *got_line_ok;
1955    LCHAR           linebuffer [MAX_LINE_LENGTH];
1956    size_t          recognition_count;
1957
1958    recognition_count = 0;
1959    process_status = srec_test_open_command_file ( &command_file );
1960
1961    if ( process_status == 0 )
1962        {
1963        process_status = srec_test_open_results_file ( &results_file );
1964
1965        if ( process_status == 0 )
1966            {
1967            do
1968                {
1969                got_line_ok = pfgets ( linebuffer, MAX_LINE_LENGTH, command_file );
1970
1971                if ( got_line_ok != NULL )
1972                    srec_test_execute_command ( data, linebuffer, results_file, &recognition_count );
1973                }
1974            while ( ( got_line_ok != NULL ) && ( process_status == 0 ) );
1975            if( data->nametag ) {
1976                SR_NametagDestroy(  data->nametag);
1977                data->nametag = NULL;
1978            }
1979
1980
1981            srec_test_close_results_file ( results_file );
1982            }
1983        srec_test_close_command_file ( command_file );
1984        }
1985    return ( process_status );
1986    }
1987
1988
1989
1990int srec_test_open_pcm_file ( LCHAR *file_name, PFile **pcm_file )
1991    {
1992    int             open_status;
1993    ESR_ReturnCode  esr_status;
1994    int             lstr_result;
1995    LCHAR           file_path [P_PATH_MAX];
1996    size_t          len;
1997
1998    open_status = 0;
1999    len = P_PATH_MAX;
2000    esr_status = ESR_SessionGetLCHAR ( L("cmdline.datapath"), file_path, &len );
2001
2002    if ( esr_status == ESR_SUCCESS )
2003        {
2004        len = P_PATH_MAX;
2005        esr_status = ESR_SessionPrefixWithBaseDirectory ( file_path, &len );
2006
2007        if ( esr_status == ESR_SUCCESS )
2008            {
2009            esr_status = pf_convert_backslashes_to_forwardslashes ( file_path );
2010
2011            if ( esr_status == ESR_SUCCESS )
2012                {
2013                len = P_PATH_MAX;
2014                lstr_result = lstrinsert ( file_path, file_name, 0, &len );
2015
2016                if ( lstr_result == 0 )
2017                    {
2018                    *pcm_file = pfopen ( file_name, L("rb") );
2019
2020                    if ( ( *pcm_file ) == NULL )
2021                        {
2022                        open_status = -1;
2023                        }
2024                    }
2025                else
2026                    {
2027                    open_status = -1;
2028                    /* Log Here */
2029                    }
2030                }
2031            else
2032                {
2033                open_status = -1;
2034                /* Log Here */
2035                }
2036            }
2037        else
2038            {
2039            open_status = -1;
2040            /* Log Here */
2041            }
2042        }
2043     else
2044         {
2045         open_status = -1;
2046         /* Log Here */
2047         }
2048     if(open_status) PLogError( ("Error: while opening %s\n"), file_name);
2049     return ( open_status );
2050     }
2051
2052
2053
2054int srec_test_open_nist_file ( LCHAR *file_name, PFile **nist_file )
2055    {
2056    int             open_status;
2057    ESR_ReturnCode  esr_status;
2058    int             lstr_result;
2059    LCHAR           file_path [P_PATH_MAX];
2060    size_t          len;
2061
2062    open_status = 0;
2063    len = P_PATH_MAX;
2064    esr_status = ESR_SessionGetLCHAR ( L("cmdline.datapath"), file_path, &len );
2065
2066    if ( esr_status == ESR_SUCCESS )
2067        {
2068        len = P_PATH_MAX;
2069        esr_status = ESR_SessionPrefixWithBaseDirectory ( file_path, &len );
2070
2071        if ( esr_status == ESR_SUCCESS )
2072            {
2073            esr_status = pf_convert_backslashes_to_forwardslashes ( file_path );
2074
2075        if(file_path[ LSTRLEN(file_path)-1] != L('/'))
2076            LSTRCAT(file_path,L("/"));
2077
2078            if ( esr_status == ESR_SUCCESS )
2079                {
2080                len = P_PATH_MAX;
2081                lstr_result = lstrinsert ( file_path, file_name, 0, &len );
2082
2083                if ( lstr_result == 0 )
2084                    {
2085                    *nist_file = pfopen ( file_name, L("rb") );
2086
2087                    if ( ( *nist_file ) != NULL )
2088                        {
2089                        esr_status = pfseek ( *nist_file, 1024, SEEK_SET );
2090
2091                        if ( esr_status != ESR_SUCCESS )
2092                            {
2093                            open_status = -1;
2094                            }
2095                        }
2096                    else
2097                        {
2098                        open_status = -1;
2099                        }
2100                    }
2101                else
2102                    {
2103                    open_status = -1;
2104                    /* Log Here */
2105                    }
2106                }
2107            else
2108                {
2109                open_status = -1;
2110                /* Log Here */
2111                }
2112            }
2113        else
2114            {
2115            open_status = -1;
2116            /* Log Here */
2117            }
2118        }
2119     else
2120         {
2121         open_status = -1;
2122         /* Log Here */
2123         }
2124	 if(open_status)	PLogError( ("Error: while opening %s\n"), file_name);
2125     return ( open_status );
2126     }
2127
2128
2129
2130int srec_test_get_audio_from_file ( PFile *audio_file, ApplicationData *data, ESR_BOOL *hit_eof )
2131    {
2132    int get_status;
2133    int eof_status;
2134
2135    get_status = 0;
2136    data->num_samples_read = pfread ( data->audio_buffer, sizeof ( asr_int16_t ), data->audio_buffer_requested_size, audio_file );
2137
2138    if ( data->num_samples_read > 0 )
2139        {
2140        *hit_eof = ESR_FALSE;
2141        }
2142    else
2143        {
2144        eof_status = pfeof ( audio_file );
2145
2146        if ( eof_status == 0 )
2147            {
2148            get_status = -1;
2149            /* Log Here */
2150            }
2151        else
2152            {
2153            *hit_eof = ESR_TRUE;
2154            }
2155        }
2156    return ( get_status );
2157    }
2158
2159
2160
2161int srec_test_feed_recognizer ( ApplicationData *data, ESR_BOOL hit_eof, SR_RecognizerStatus *esr_recog_status, SR_RecognizerResultType *result_type )
2162    {
2163    int             feed_status;
2164    ESR_ReturnCode  esr_status;
2165
2166    feed_status = 0;
2167    esr_status = SR_RecognizerPutAudio ( data->recognizer, data->audio_buffer, &data->num_samples_read, hit_eof );
2168
2169    if ( esr_status == ESR_SUCCESS )
2170        {
2171        do
2172            {
2173            esr_status = SR_RecognizerAdvance ( data->recognizer, esr_recog_status, result_type, &data->result );
2174
2175            if ( esr_status != ESR_SUCCESS )
2176                {
2177                feed_status = -1;
2178                *esr_recog_status = SR_RECOGNIZER_EVENT_STOPPED;
2179                *result_type = SR_RECOGNIZER_RESULT_TYPE_COMPLETE;
2180                /* Log Here */
2181                }
2182            }
2183        while ( ( *esr_recog_status ) == SR_RECOGNIZER_EVENT_INCOMPLETE );
2184        }
2185    else
2186        {
2187        feed_status = -1;
2188        /* Log Here */
2189        }
2190     return ( feed_status );
2191     }
2192
2193
2194
2195int srec_test_flush_audio ( ApplicationData *data, SR_RecognizerStatus *esr_recog_status, SR_RecognizerResultType *result_type )
2196    {
2197    int             flush_status;
2198    ESR_ReturnCode  esr_status;
2199
2200    flush_status = 0;
2201
2202    while ( ( *result_type ) != SR_RECOGNIZER_RESULT_TYPE_COMPLETE )
2203        {
2204        esr_status = SR_RecognizerAdvance ( data->recognizer, esr_recog_status, result_type, &data->result );
2205
2206        if ( esr_status != ESR_SUCCESS )
2207            {
2208            flush_status = -1;
2209            *esr_recog_status = SR_RECOGNIZER_EVENT_STOPPED;
2210            *result_type = SR_RECOGNIZER_RESULT_TYPE_COMPLETE;
2211            /* Log Here */
2212            }
2213        }
2214    return ( flush_status );
2215    }
2216
2217
2218
2219int srec_test_process_start_timeout ( ApplicationData *data, FILE *results_file )
2220    {
2221    int             process_status;
2222    ESR_ReturnCode  esr_status;
2223    size_t          utterance_timeout;
2224
2225    process_status = 0;
2226    esr_status = ESR_SessionGetSize_t ( L("SREC.Recognizer.utterance_timeout"), &utterance_timeout );
2227
2228    if ( esr_status == ESR_SUCCESS )
2229        {
2230        LFPRINTF ( results_file, L("E: utterance_timeout %lu\n"), (unsigned long)utterance_timeout );
2231        LFPRINTF ( results_file, L("R: <FAILED>\n") );
2232        LPRINTF ( L("\n-------------------------------------\n") );
2233        LPRINTF ( L("E: utterance_timeout %lu\n"), (unsigned long)utterance_timeout );
2234        LPRINTF ( L("R: <FAILED>\n") );
2235        LPRINTF ( L("-------------------------------------\n\n") );
2236        }
2237    else
2238        {
2239        process_status = -1;
2240        /* Log Here */
2241        }
2242    srec_test_log_recognition_failure ( data );
2243
2244    return ( process_status );
2245    }
2246
2247
2248int srec_test_write_and_log_wave_form_file ( ApplicationData *data, size_t recognition_count )
2249    {
2250    int                 write_status;
2251    ESR_ReturnCode      esr_status;
2252    LCHAR               *wave_filename;
2253
2254    write_status = 0;
2255    LSPRINTF ( data->raw_waveform_filename, L("a%lu__%s.raw"), (unsigned long)recognition_count, data->transcription );
2256    wave_filename = data->raw_waveform_filename;
2257
2258    while ( *wave_filename )
2259        {
2260        if ( *wave_filename == ' ')
2261            *wave_filename = '_';
2262        wave_filename++;
2263        }
2264/* just write to file for now... for testing purposes */
2265    LPRINTF ( L("Dumping audio waveform to file %s\n"), data->raw_waveform_filename );
2266    data->raw_waveform_file = pfopen ( data->raw_waveform_filename, L("wb") );
2267
2268    if ( data->raw_waveform_file != NULL )
2269        {
2270        pfwrite ( (void*)data->raw_waveform, 1, data->raw_waveform_size, data->raw_waveform_file );
2271        pfclose ( data->raw_waveform_file );
2272/* log the filename in the ESR log */
2273        esr_status = SR_RecognizerLogToken ( data->recognizer, "WVFMFILENAME", data->raw_waveform_filename );
2274
2275        if ( esr_status != ESR_SUCCESS )
2276            {
2277            write_status = -1;
2278            /* Log Here */
2279            }
2280        }
2281    else
2282        {
2283        write_status = -1;
2284        /* Log Here */
2285        }
2286    return ( write_status );
2287    }
2288
2289
2290
2291int srec_test_process_nbest_list ( ApplicationData *data, FILE *results_file )
2292{
2293  int                 process_status;
2294  ESR_ReturnCode      esr_status;
2295  size_t              nbestSize;
2296  size_t              nbest_num;
2297  LCHAR               linebuffer [MAX_LINE_LENGTH];
2298  size_t              line_length;
2299
2300  process_status = 0;
2301  /* At least one semantic result exists */
2302  LPRINTF ( L("\n\n----------------------------------------------\n") );
2303
2304  LPRINTF ( L("TRANSCRIPTION    : '%s'\n"), data->transcription );
2305  esr_status = SR_RecognizerResultGetSize (data->result, &nbestSize );
2306
2307  if ( esr_status == ESR_SUCCESS )
2308    {
2309      for ( nbest_num = 0; nbest_num < nbestSize; nbest_num++ )
2310        {
2311          line_length =  MAX_LINE_LENGTH;
2312          esr_status = SR_RecognizerResultGetValue ( data->result, nbest_num, L("literal"), linebuffer, &line_length );
2313
2314          if ( esr_status == ESR_SUCCESS )
2315            {
2316              LPRINTF ( L("LITERAL[%2lu]      : '%s'\n"), (unsigned long)nbest_num, linebuffer );
2317              line_length = MAX_LINE_LENGTH;
2318              esr_status = SR_RecognizerResultGetValue ( data->result, nbest_num, L("meaning"), linebuffer, &line_length );
2319
2320              if ( esr_status != ESR_SUCCESS )
2321                {
2322                  linebuffer [0] = L('\0') ;
2323                }
2324              LPRINTF ( L("MEANING[%2lu]      : '%s'\n"), (unsigned long)nbest_num, linebuffer );
2325
2326              line_length =  MAX_LINE_LENGTH;
2327              esr_status = SR_RecognizerResultGetValue (data->result, nbest_num , L("conf"), linebuffer, &line_length );
2328              if ( esr_status == ESR_SUCCESS )
2329                {
2330                  LPRINTF ( L("CONFIDENCE VALUE : '%s'\n"), linebuffer );
2331                }
2332              else
2333                {
2334                  process_status = -1;
2335                  /* Log Here */
2336                }
2337
2338              line_length = MAX_LINE_LENGTH;
2339              esr_status = SR_RecognizerResultGetValue (data->result, nbest_num, L("raws"), linebuffer, &line_length );
2340
2341              if ( esr_status == ESR_SUCCESS )
2342                {
2343                  LPRINTF ( L("RAW SCORE[%2lu]    : '%s'\n"), (unsigned long)nbest_num, linebuffer);
2344                }
2345              else
2346                {
2347                  process_status = -1;
2348                  /* Log Here */
2349                }
2350#ifdef SREC_ENGINE_VERBOSE_LOGGING
2351              if(1) {
2352                /* get ALL key value pairs */
2353                LCHAR *keys[32], keysdata[128*32];
2354                size_t k,key_count = sizeof(keys)/sizeof(keys[0]);
2355                for(k=0;k<key_count;k++) keys[k] = &keysdata[128*k];
2356                // esr_status = SR_RecognizerResultGetKeyCount(data->result, nbest_num, &key_count);
2357                esr_status = SR_RecognizerResultGetKeyList(data->result, nbest_num, (LCHAR**)keys, &key_count);
2358                if(esr_status == ESR_SUCCESS) {
2359                  for (k=0; k<key_count; k++) {
2360                    LCHAR value[128];
2361                    size_t valuelen = sizeof(value)/sizeof(value[0]);
2362                    esr_status = SR_RecognizerResultGetValue( data->result, nbest_num, keys[k], value, &valuelen);
2363                    if(esr_status==ESR_SUCCESS)
2364                      LPRINTF( L("%s[%2lu] : '%s'\n"), keys[k], (unsigned long)nbest_num, value);
2365                  }
2366                }
2367              }
2368#endif
2369              LPRINTF ( L("\n"));
2370            }
2371          else
2372            {
2373              process_status = -1;
2374              /* Log Here */
2375            }
2376        }
2377    }
2378  else
2379    {
2380      process_status = -1;
2381      /* Log Here */
2382    }
2383
2384  LPRINTF ( L("----------------------------------------------\n\n") );
2385  return ( process_status );
2386}
2387
2388
2389
2390int srec_test_process_recognition ( ApplicationData *data, FILE *results_file, size_t recognition_count )
2391{
2392  int                 process_status;
2393  ESR_ReturnCode      esr_status;
2394  APPL_GRAMMAR_DATA   *grammar_data;
2395  LCHAR               linebuffer [MAX_LINE_LENGTH];
2396  size_t              line_length;
2397
2398  process_status = 0;
2399  srec_test_get_active_grammar_data ( data, &grammar_data );
2400
2401  do {
2402    line_length = MAX_LINE_LENGTH;
2403    esr_status = SR_RecognizerResultGetValue ( data->result, 0, L("literal"), linebuffer, &line_length );
2404
2405    if ( esr_status != ESR_SUCCESS ) break;
2406    LFPRINTF ( results_file, L("R: %s\n"), linebuffer );
2407    line_length = MAX_LINE_LENGTH;
2408    esr_status = SR_RecognizerResultGetValue ( data->result, 0, L("conf"), linebuffer, &line_length );
2409
2410    if ( esr_status != ESR_SUCCESS ) break;
2411
2412    LFPRINTF ( results_file, L("S: %s\n"), linebuffer);
2413    LPRINTF ( L("S: %s\n"), linebuffer);
2414    srec_test_process_nbest_list ( data, results_file );
2415    /*
2416     * SR_RecognizerResultGetWaveform will return pointer to buffer holding
2417     * audio data in it. This buffer is NOT under the application's control
2418     * and MUST only be read from.
2419     */
2420    if ( grammar_data->is_ve_grammar == ESR_TRUE )
2421      {
2422	esr_status = SR_NametagCreate ( data->result, L("dummyID"), &data->nametag );
2423	if(esr_status != ESR_SUCCESS) {
2424	  LPRINTF ( L("VoiceEnrollment=>FAILURE\n") );
2425	  break;
2426	}
2427	LPRINTF ( L("VoiceEnrollment=>SUCCESS\n") );
2428	if(1) {
2429	  const char* value = NULL;
2430	  size_t len, len2;
2431	  char* value2;
2432	  /* this is to mimic what might happen in a real application */
2433	  esr_status = SR_NametagGetValue( data->nametag, &value, &len);
2434	  value2 = (char*)CALLOC(len, sizeof(char), "srectest.nametag");
2435	  len2 = len;
2436	  memcpy( value2, value, len);
2437	  esr_status = SR_NametagDestroy(data->nametag);
2438	  data->nametag = NULL;
2439	  if(esr_status != ESR_SUCCESS) break;
2440	  /* in a real application we would save the nametag value someplace
2441	     along with the data->raw_waveform, then retrieve it and create a
2442	     nametag from that value */
2443	  esr_status = SR_NametagCreateFromValue( L("dummyID"),  value2, len2, &data->nametag);
2444	  FREE( value2);
2445	  value2 = NULL;
2446	}
2447	if ( esr_status != ESR_SUCCESS ) break;
2448
2449	esr_status = SR_RecognizerResultGetWaveform ( data->result, &data->raw_waveform, &data->raw_waveform_size );
2450	if( esr_status != ESR_SUCCESS ) break;
2451
2452	if ( data->raw_waveform )
2453	  {
2454	    process_status = srec_test_write_and_log_wave_form_file ( data, recognition_count );
2455
2456	    if ( process_status == 0 )
2457	      {
2458		esr_status = SR_RecognizerLogEvent ( data->recognizer, "ESRve" );
2459	      }
2460	    else {
2461	      esr_status = ESR_INVALID_STATE;
2462	    }
2463	  }
2464      }
2465    process_status = 0;
2466  } while(0);
2467  if( esr_status != ESR_SUCCESS) {
2468        process_status = -1;
2469        /* Log Here */
2470  }
2471  return ( process_status );
2472}
2473
2474
2475
2476int srec_test_process_recognition_fail ( ApplicationData *data )
2477    {
2478    int                 process_status;
2479    ESR_ReturnCode      esr_status;
2480    APPL_GRAMMAR_DATA   *grammar_data;
2481    ESR_BOOL                reason_status;
2482
2483    process_status = 0;
2484    srec_test_get_active_grammar_data ( data, &grammar_data );
2485    LPRINTF(L("*** no match in recognition***\n"));
2486
2487    if ( grammar_data->is_ve_grammar == ESR_TRUE )
2488        {
2489        data->nametag = NULL;
2490        LPRINTF ( L("VoiceEnrollement = FAILED : \n") );
2491        }
2492    esr_status = SR_RecognizerIsSignalClipping ( data->recognizer, &reason_status );
2493
2494    if ( esr_status == ESR_SUCCESS )
2495        {
2496        if ( reason_status == ESR_TRUE )
2497            LPRINTF ( L("- Signal is clipping\n") );
2498        }
2499    else
2500        {
2501        process_status = -1;
2502        /* Log Here */
2503        }
2504    esr_status = SR_RecognizerIsSignalDCOffset ( data->recognizer, &reason_status );
2505
2506    if ( esr_status == ESR_SUCCESS )
2507        {
2508        if ( reason_status == ESR_TRUE )
2509            LPRINTF ( L("- Signal is DC-offset\n") );
2510        }
2511    else
2512        {
2513        process_status = -1;
2514        /* Log Here */
2515        }
2516    esr_status = SR_RecognizerIsSignalNoisy ( data->recognizer, &reason_status );
2517
2518    if ( esr_status == ESR_SUCCESS )
2519        {
2520        if ( reason_status == ESR_TRUE )
2521            LPRINTF ( L("- Signal is noisy\n") );
2522        }
2523    else
2524        {
2525        process_status = -1;
2526        /* Log Here */
2527        }
2528    esr_status = SR_RecognizerIsSignalTooFewSamples ( data->recognizer, &reason_status );
2529
2530    if ( esr_status == ESR_SUCCESS )
2531        {
2532        if ( reason_status == ESR_TRUE )
2533            LPRINTF ( L("- Signal has too few samples\n") );
2534        }
2535    else
2536        {
2537        process_status = -1;
2538        /* Log Here */
2539        }
2540    esr_status = SR_RecognizerIsSignalTooManySamples ( data->recognizer, &reason_status );
2541
2542    if ( esr_status == ESR_SUCCESS )
2543        {
2544        if ( reason_status == ESR_TRUE )
2545            LPRINTF ( L("- Signal has too many samples\n") );
2546        }
2547    else
2548        {
2549        process_status = -1;
2550        /* Log Here */
2551        }
2552    esr_status = SR_RecognizerIsSignalTooQuiet ( data->recognizer, &reason_status );
2553
2554    if ( esr_status == ESR_SUCCESS )
2555        {
2556        if ( reason_status == ESR_TRUE )
2557            LPRINTF ( L("- Signal is too quiet\n") );
2558        }
2559    else
2560        {
2561        process_status = -1;
2562        /* Log Here */
2563        }
2564    srec_test_log_recognition_failure ( data );
2565    return ( process_status );
2566    }
2567
2568
2569
2570int srec_test_process_recognition_unsupported_case ( ApplicationData *data, FILE *results_file )
2571    {
2572    int process_status;
2573
2574    process_status = 0;
2575    LFPRINTF ( results_file, L("E: No results available\n") );
2576    LFPRINTF ( results_file, L("R: <FAILED>\n") );
2577    srec_test_log_recognition_failure ( data );
2578
2579    return ( process_status );
2580    }
2581
2582
2583
2584static void srec_test_log_recognition_failure ( ApplicationData *data )
2585    {
2586
2587    LPRINTF(L("----------------------------------------------\n"));
2588    LPRINTF(L("TRANSCRIPTION    : '%s'\n"), data->transcription);
2589    LPRINTF(L("<NO-RESULTS>\n"));
2590    LPRINTF(L("----------------------------------------------\n\n"));
2591    }
2592
2593
2594
2595int srec_test_process_results ( ApplicationData *data, SR_RecognizerStatus esr_recog_status,
2596                                FILE *results_file, size_t recognition_count )
2597    {
2598    int process_status;
2599
2600    switch ( esr_recog_status )
2601        {
2602        case SR_RECOGNIZER_EVENT_START_OF_UTTERANCE_TIMEOUT:
2603            process_status = srec_test_process_start_timeout ( data, results_file );
2604            break;
2605
2606        case SR_RECOGNIZER_EVENT_RECOGNITION_RESULT:
2607            process_status = srec_test_process_recognition ( data, results_file, recognition_count );
2608            break;
2609
2610        case SR_RECOGNIZER_EVENT_NO_MATCH:
2611            process_status = srec_test_process_recognition_fail ( data );
2612            break;
2613
2614        default:
2615            process_status = srec_test_process_recognition_unsupported_case ( data, results_file );
2616            break;
2617        }
2618    return ( process_status );
2619    }
2620
2621
2622
2623int srec_test_log_reco_from_file_data ( SR_Grammar *active_grammar, ApplicationData *data, LCHAR *waveform, LCHAR *bos, LCHAR *eos, LCHAR *transcription )
2624    {
2625    int             log_status;
2626    ESR_ReturnCode  esr_status;
2627    size_t          result_count;
2628    ESR_BOOL            got_results;
2629    size_t          transcription_length;
2630
2631    log_status = 0;
2632    LSPRINTF ( data->transcription, "%s", transcription );
2633    transcription_length = LSTRLEN ( data->transcription );
2634
2635    while ( ( *( data->transcription + transcription_length - 1 ) == '\n' ) ||
2636            ( *( data->transcription + transcription_length - 1 ) == '\r' ) )
2637        {
2638	  *( data->transcription + transcription_length - 1 ) = '\0';
2639	  transcription_length--;
2640        }
2641  LPRINTF ( L("D: %s\nC: %s\n"), waveform, data->transcription );
2642
2643    esr_status = SR_GrammarCheckParse ( active_grammar, data->transcription, 0, &result_count );
2644
2645    if ( esr_status == ESR_SUCCESS )
2646        {
2647        if ( result_count > 0 )
2648            {
2649#ifdef ACCURACY_TESTING
2650	      LCHAR meaning[MAX_LINE_LENGTH] = { 0 };
2651	      size_t len  = MAX_LINE_LENGTH;
2652	      got_results = ESR_TRUE;
2653	      LPRINTF ( L("Sem (%lu):  invocab=1\n"), (unsigned long)result_count );
2654	      srec_test_parse ( active_grammar, transcription, meaning, &len);
2655	      if(LSTRLEN(meaning)>0)
2656		LPRINTF( L("CSem: %s\n"), meaning);
2657#else
2658	      got_results = ESR_TRUE;
2659	      LPRINTF ( L("Sem (%lu):  invocab=1\n"), (unsigned long)result_count );
2660#endif
2661            }
2662        else
2663            {
2664            got_results = ESR_FALSE;
2665            LPRINTF ( L("Sem:  <NO INTERPRETATION FOUND>\n") );
2666            LPRINTF ( L("CSem: <NO INTERPRETATION FOUND>\n") );
2667	    }
2668        esr_status = SR_RecognizerLogWaveformData ( data->recognizer, waveform, data->transcription, atof ( bos ), atof ( eos ), got_results );
2669
2670        if ( esr_status != ESR_SUCCESS )
2671            {
2672            log_status = -1;
2673            /* Log Here */
2674            }
2675        }
2676    else
2677        {
2678        log_status = -1;
2679        /* Log Here */
2680        }
2681
2682    return ( log_status );
2683    }
2684
2685
2686
2687static int srec_test_recognize_pcm_file ( SR_Grammar *active_grammar, ApplicationData *data, FILE *results_file,
2688                                          LCHAR *command_text, size_t *recognition_count )
2689    {
2690    int                     recognize_status;
2691    ESR_ReturnCode          esr_status;
2692    SR_RecognizerStatus     esr_recog_status;
2693    SR_RecognizerResultType result_type;
2694    LCHAR                   *transcription;
2695    LCHAR                   waveform [MAX_LINE_LENGTH];
2696    LCHAR                   bos [MAX_LINE_LENGTH];
2697    LCHAR                   eos [MAX_LINE_LENGTH];
2698    PFile                   *waveform_file;
2699    ESR_BOOL                    hit_eof;
2700
2701    if ( active_grammar != NULL )
2702        {
2703        recognize_status = srec_test_get_three_command_items ( command_text, MAX_LINE_LENGTH, waveform,
2704                                                              MAX_LINE_LENGTH, bos, MAX_LINE_LENGTH,
2705                                                              eos, NULL, &transcription );
2706	while( *transcription == ' ' ) transcription++;
2707        if ( recognize_status == 0 )
2708            {
2709            recognize_status = srec_test_log_reco_from_file_data ( active_grammar, data, waveform, bos, eos, transcription );
2710
2711            if ( recognize_status == 0 )
2712                {
2713				if(LSTRCMP(bos,"0") || LSTRCMP(eos,"0")) {
2714					PLogError( ("Error: non-zero BOS and non-zero EOS are not supported\n"));
2715					return -1;
2716				}
2717                recognize_status = srec_test_open_pcm_file ( waveform, &waveform_file );
2718
2719                if ( recognize_status == 0 )
2720                    {
2721                    if ( ( data->forced_rec_mode == ForcedRecModeOn ) || ( data->forced_rec_mode == ForcedRecModeOneTime ) )
2722                        SR_GrammarAllowOnly ( active_grammar, data->transcription );
2723                    esr_status = SR_RecognizerStart ( data->recognizer );
2724
2725                    if ( esr_status == ESR_SUCCESS )
2726                        {
2727                        ( *recognition_count )++;
2728                        hit_eof = ESR_FALSE;
2729
2730                        do
2731                            {
2732                            recognize_status = srec_test_get_audio_from_file ( waveform_file, data, &hit_eof );
2733
2734                            if ( recognize_status == 0 )
2735                                recognize_status = srec_test_feed_recognizer ( data, hit_eof, &esr_recog_status, &result_type  );
2736                            }
2737                        while ( ( hit_eof == ESR_FALSE ) && ( result_type != SR_RECOGNIZER_RESULT_TYPE_COMPLETE ) && ( recognize_status == 0 ) );
2738
2739                        if ( recognize_status == 0 )
2740                            {
2741                            recognize_status = srec_test_flush_audio ( data, &esr_recog_status, &result_type );
2742
2743                            if ( recognize_status == 0 )
2744                                {
2745                                recognize_status = srec_test_process_results ( data, esr_recog_status,
2746                                                                               results_file, *recognition_count );
2747                                }
2748                            }
2749                        esr_status = SR_RecognizerStop ( data->recognizer );
2750
2751                        if (esr_status == ESR_SUCCESS )
2752                            {
2753                            LPRINTF ( L("Recognizer has been stopped\n") );
2754                            }
2755                        else
2756                            {
2757                            recognize_status = -1;
2758                            LPRINTF ( L("Recognizer has failed to stop\n") );
2759                            }
2760                        }
2761                    if ( data->forced_rec_mode == ForcedRecModeOneTime )
2762                        {
2763                        data->forced_rec_mode = ForcedRecModeOff;
2764                        SR_GrammarAllowAll ( active_grammar );
2765                        }
2766                    srec_test_close_audio_file ( waveform_file );
2767                    }
2768                }
2769            }
2770        }
2771     else
2772         {
2773         recognize_status = -1;
2774         /* Log Here */
2775         }
2776     return ( recognize_status );
2777     }
2778
2779
2780
2781static int srec_test_recognize_nist_file ( SR_Grammar *active_grammar, ApplicationData *data, FILE *results_file,
2782                                           LCHAR *command_text, size_t *recognition_count )
2783    {
2784    int                     recognize_status;
2785    ESR_ReturnCode          esr_status;
2786    SR_RecognizerStatus     esr_recog_status;
2787    SR_RecognizerResultType result_type;
2788    LCHAR                   *transcription;
2789    LCHAR                   waveform [MAX_LINE_LENGTH];
2790    LCHAR                   bos [MAX_LINE_LENGTH];
2791    LCHAR                   eos [MAX_LINE_LENGTH];
2792    PFile                   *waveform_file;
2793    ESR_BOOL                    hit_eof;
2794
2795    if ( active_grammar != NULL )
2796        {
2797        recognize_status = srec_test_get_three_command_items ( command_text, MAX_LINE_LENGTH, waveform,
2798                                                              MAX_LINE_LENGTH, bos, MAX_LINE_LENGTH,
2799                                                              eos, NULL, &transcription );
2800	while( *transcription == ' ' ) transcription++;
2801        if ( recognize_status == 0 )
2802            {
2803            recognize_status = srec_test_log_reco_from_file_data ( active_grammar, data, waveform, bos, eos, transcription );
2804
2805            if ( recognize_status == 0 )
2806                {
2807				if(LSTRCMP(bos,"0") || LSTRCMP(eos,"0")) {
2808					PLogError( ("Error: non-zero BOS and non-zero EOS are not supported\n"));
2809					return -1;
2810				}
2811                recognize_status = srec_test_open_nist_file ( waveform, &waveform_file );
2812
2813                if ( recognize_status == 0 )
2814                    {
2815                    if ( ( data->forced_rec_mode == ForcedRecModeOn ) || ( data->forced_rec_mode == ForcedRecModeOneTime ) )
2816                        SR_GrammarAllowOnly ( active_grammar, data->transcription );
2817                    esr_status = SR_RecognizerStart ( data->recognizer );
2818
2819                    if ( esr_status == ESR_SUCCESS )
2820                        {
2821                        ( *recognition_count )++;
2822                        hit_eof = ESR_FALSE;
2823
2824                        do
2825                            {
2826                            recognize_status = srec_test_get_audio_from_file ( waveform_file, data, &hit_eof );
2827
2828                            if ( recognize_status == 0 )
2829                                recognize_status = srec_test_feed_recognizer ( data, hit_eof, &esr_recog_status, &result_type  );
2830                            }
2831                        while ( ( hit_eof == ESR_FALSE ) && ( result_type != SR_RECOGNIZER_RESULT_TYPE_COMPLETE ) && ( recognize_status == 0 ) );
2832
2833                        if ( recognize_status == 0 )
2834                            {
2835                            recognize_status = srec_test_flush_audio ( data, &esr_recog_status, &result_type );
2836
2837                            if ( recognize_status == 0 )
2838                                {
2839                                recognize_status = srec_test_process_results ( data, esr_recog_status,
2840                                                                               results_file, *recognition_count );
2841                                }
2842                            }
2843                        }
2844                        esr_status = SR_RecognizerStop ( data->recognizer );
2845
2846                        if (esr_status == ESR_SUCCESS )
2847                            {
2848                            LPRINTF ( L("Recognizer has been stopped\n") );
2849                            }
2850                        else
2851                            {
2852                            recognize_status = -1;
2853                            LPRINTF ( L("Recognizer has failed to stop\n") );
2854                            }
2855                    if ( data->forced_rec_mode == ForcedRecModeOneTime )
2856                        {
2857                        data->forced_rec_mode = ForcedRecModeOff;
2858                        SR_GrammarAllowAll ( active_grammar );
2859                        }
2860                    srec_test_close_audio_file ( waveform_file );
2861                    }
2862                }
2863            }
2864        }
2865     else
2866         {
2867         recognize_status = -1;
2868         /* Log Here */
2869         }
2870     return ( recognize_status );
2871     }
2872
2873
2874
2875#define STACK_SIZE (1024 * 200)
2876
2877#ifdef _WIN32
2878/* disable optimization for the next functions as the compiler optimizes away the assignment to mySTACK[i] */
2879#pragma optimize("", off)
2880
2881static void initStack()
2882{
2883  int mySTACK[STACK_SIZE];
2884 {
2885   /* This extra block is to ensure that local variables of the function occur after buffer mySTACK. */
2886   int i;
2887
2888   for (i = 0; i < STACK_SIZE; ++i)
2889   {
2890     mySTACK[i] = 0xDEADBEEF;
2891   }
2892 }
2893}
2894
2895static int analyzeStack()
2896{
2897  int mySTACK[STACK_SIZE];
2898 {
2899
2900   /* This extra block is to ensure that local variables of the function occur after buffer mySTACK. */
2901   int i, j;
2902
2903   for (i = STACK_SIZE - 1; i >= 0; --i)
2904   {
2905     if (mySTACK[i] == 0xDEADBEEF)
2906     {
2907       /* This might be a candidate for the end of stack marker, or it could be
2908          some value that is equal to our marker.  To ensure reliability of
2909          this candidate, we will make sure that all remaining entries int the
2910          stack are also equal to DEADBEEF.
2911       */
2912       for (j = i - 1; j >= 0; --j)
2913       {
2914         if (mySTACK[j] != 0xDEADBEEF)
2915         {
2916           i = j;
2917           break;
2918         }
2919       }
2920       if (j < 0) break;
2921     }
2922   }
2923
2924   if (i < 0)
2925     return -1;
2926   else
2927     return (STACK_SIZE - 1 - i) * sizeof(int);
2928 }
2929}
2930
2931/* restore optmization settings to what they used to be. */
2932#pragma optimize("", on)
2933#endif
2934
2935
2936#ifdef ACCURACY_TESTING
2937static void srec_test_clean_up_sentence ( char* sentence )
2938    {
2939    int                         clean_up_status;
2940    int                         sentence_finished;
2941    SENTENCE_CLEANING_STATES    current_state;
2942    char                        *current_input;
2943    char                        *current_output;
2944
2945    clean_up_status = 0;
2946    sentence_finished = 0;
2947    current_state = SENTENCE_BEGIN;
2948    current_input = sentence;
2949    current_output = sentence;
2950    strtok( sentence, "\n\r");
2951
2952    do
2953        {
2954        switch ( *current_input )
2955            {
2956            case '\0':
2957                switch ( current_state )
2958                    {
2959                    case SENTENCE_BEGIN:
2960                        break;
2961
2962                    case SENTENCE_BEGIN_BRACKET_BEGIN:  /* Is this error condition */
2963                        *current_output = '\0';
2964                        clean_up_status = -1;
2965                        break;
2966
2967                    case SENTENCE_BEGIN_BRACKET_END:
2968                        *current_output = '\0';
2969                        break;
2970
2971                    case SENTENCE_MIDDLE:
2972                        *current_output = '\0';
2973                        break;
2974
2975                    case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */
2976                        *( current_output - 1 ) = '\0';
2977                        clean_up_status = -1;
2978                        break;
2979
2980                    case SENTENCE_MIDDLE_BRACKET_END:
2981                        *( current_output - 1 ) = '\0';
2982                        break;
2983
2984                    case SENTENCE_MIDDLE_WITH_SPACE:
2985                        *( current_output - 1 ) = '\0';
2986                        break;
2987
2988                    default:
2989                        *current_output = '\0';
2990                        /* Log error */
2991                        break;
2992                    }
2993                sentence_finished = 1;
2994                break;
2995
2996            case ' ':
2997                switch ( current_state )
2998                    {
2999                    case SENTENCE_BEGIN:
3000                        break;
3001
3002                    case SENTENCE_BEGIN_BRACKET_BEGIN:
3003                        break;
3004
3005                    case SENTENCE_BEGIN_BRACKET_END:
3006                        break;
3007
3008                    case SENTENCE_MIDDLE:
3009                        *current_output = ' ';
3010                        current_output++;
3011                        current_state = SENTENCE_MIDDLE_WITH_SPACE;
3012                        break;
3013
3014                    case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */
3015                        break;
3016
3017                    case SENTENCE_MIDDLE_BRACKET_END:
3018                        break;
3019
3020                    case SENTENCE_MIDDLE_WITH_SPACE:
3021                        break;
3022
3023                    default:
3024                        *current_output = '\0';
3025                        clean_up_status = -1;
3026                        /* Log error */
3027                        break;
3028                    }
3029                current_input++;
3030                break;
3031
3032            case '[':
3033                switch ( current_state )
3034                    {
3035                    case SENTENCE_BEGIN:
3036                        current_state = SENTENCE_BEGIN_BRACKET_BEGIN;
3037                        break;
3038
3039                    case SENTENCE_BEGIN_BRACKET_BEGIN:
3040                        *current_output = '\0';
3041                        clean_up_status = -1;
3042                        /* Log error */
3043                        break;
3044
3045                    case SENTENCE_BEGIN_BRACKET_END:
3046                        current_state = SENTENCE_BEGIN_BRACKET_BEGIN;
3047                        break;
3048
3049                    case SENTENCE_MIDDLE:
3050                        *current_output = ' ';
3051                        current_output++;
3052                        current_state = SENTENCE_MIDDLE_BRACKET_BEGIN;
3053                        break;
3054
3055                    case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */
3056                        *current_output = '\0';
3057                        clean_up_status = -1;
3058                        /* Log error */
3059                        break;
3060
3061                    case SENTENCE_MIDDLE_BRACKET_END:
3062                        current_state = SENTENCE_MIDDLE_BRACKET_BEGIN;
3063                        break;
3064
3065                    case SENTENCE_MIDDLE_WITH_SPACE:
3066                        current_state = SENTENCE_MIDDLE_BRACKET_BEGIN;
3067                        break;
3068
3069                    default:
3070                        *current_output = '\0';
3071                        clean_up_status = -1;
3072                        /* Log error */
3073                        break;
3074                    }
3075                current_input++;
3076                break;
3077
3078            case ']':
3079                switch ( current_state )
3080                    {
3081                    case SENTENCE_BEGIN:
3082                        *current_output = '\0';
3083                        clean_up_status = -1;
3084                        /* Log error */
3085                        break;
3086
3087                    case SENTENCE_BEGIN_BRACKET_BEGIN:
3088                        current_state = SENTENCE_BEGIN_BRACKET_END;
3089                        break;
3090
3091                    case SENTENCE_BEGIN_BRACKET_END:
3092                        *current_output = '\0';
3093                        clean_up_status = -1;
3094                        /* Log error */
3095                        break;
3096
3097                    case SENTENCE_MIDDLE:
3098                        *current_output = '\0';
3099                        clean_up_status = -1;
3100                        /* Log error */
3101                        break;
3102
3103                    case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */
3104                        current_state = SENTENCE_MIDDLE_BRACKET_END;
3105                        break;
3106
3107                    case SENTENCE_MIDDLE_BRACKET_END:
3108                        *current_output = '\0';
3109                        clean_up_status = -1;
3110                        /* Log error */
3111                        break;
3112
3113                    case SENTENCE_MIDDLE_WITH_SPACE:
3114                        *current_output = '\0';
3115                        clean_up_status = -1;
3116                        /* Log error */
3117                        break;
3118
3119                    default:
3120                        *current_output = '\0';
3121                        clean_up_status = -1;
3122                        /* Log error */
3123                        break;
3124                    }
3125                current_input++;
3126                break;
3127
3128            default:
3129                switch ( current_state )
3130                    {
3131                    case SENTENCE_BEGIN:
3132                        *current_output = *current_input;
3133                        current_output++;
3134                        current_state = SENTENCE_MIDDLE;
3135                        break;
3136
3137                    case SENTENCE_BEGIN_BRACKET_BEGIN:
3138                        break;
3139
3140                    case SENTENCE_BEGIN_BRACKET_END:
3141                        *current_output = *current_input;
3142                        current_output++;
3143                        current_state = SENTENCE_MIDDLE;
3144                        break;
3145
3146                    case SENTENCE_MIDDLE:
3147                        *current_output = *current_input;
3148                        current_output++;
3149                        current_state = SENTENCE_MIDDLE;
3150                        break;
3151
3152                    case SENTENCE_MIDDLE_BRACKET_BEGIN:
3153                        break;
3154
3155                    case SENTENCE_MIDDLE_BRACKET_END:
3156                        *current_output = *current_input;
3157                        current_output++;
3158                        current_state = SENTENCE_MIDDLE;
3159                        break;
3160
3161                    case SENTENCE_MIDDLE_WITH_SPACE:
3162                        *current_output = *current_input;
3163                        current_output++;
3164                        current_state = SENTENCE_MIDDLE;
3165                        break;
3166
3167                    default:
3168                        *current_output = '\0';
3169                        clean_up_status = -1;
3170                        /* Log error */
3171                        break;
3172                    }
3173                current_input++;
3174                break;
3175            }
3176        }
3177    while ( ( sentence_finished == 0 ) && ( clean_up_status == 0 ) );
3178    }
3179
3180
3181
3182ESR_ReturnCode srec_test_parse ( SR_Grammar* grammar, const LCHAR* trans, LCHAR* meaning, size_t *len )
3183    {
3184    ESR_ReturnCode      parse_status;
3185    char                cleaned_trans[TRANSCRIPTION_SIZE];
3186    SR_SemanticResult   *semanticResults;
3187    size_t              result_count;
3188
3189    result_count = 0;
3190    strcpy( cleaned_trans, trans );
3191    srec_test_clean_up_sentence ( cleaned_trans );
3192
3193  /* create the result holders, initially not greater than MAX */
3194    parse_status = SR_SemanticResultCreate ( &semanticResults );
3195
3196    if ( parse_status == ESR_SUCCESS )
3197        {
3198        result_count = 1;
3199        parse_status = SR_SemanticProcessor_Flush( ( (SR_GrammarImpl*)grammar )->semproc );
3200
3201        if ( parse_status == ESR_SUCCESS )
3202            {
3203            parse_status = SR_SemanticProcessor_SetParam( ((SR_GrammarImpl*)grammar)->semproc, L("literal"), cleaned_trans );
3204
3205            if ( parse_status == ESR_SUCCESS )
3206                {
3207                parse_status = SR_GrammarCheckParse( grammar, cleaned_trans, &semanticResults, (size_t*)&result_count );
3208
3209                if ( parse_status == ESR_SUCCESS )
3210                    {
3211                    if ( result_count < 1 )
3212                        {
3213                        LSTRCPY ( meaning, L("") );
3214                        SR_SemanticResultDestroy( semanticResults);
3215                        parse_status = ESR_NO_MATCH_ERROR;
3216                        }
3217                    else
3218                        {
3219                        parse_status = SR_SemanticResultGetValue ( semanticResults, "meaning", meaning, len);
3220                        SR_SemanticResultDestroy( semanticResults);
3221                        }
3222                    }
3223                }
3224            }
3225        }
3226    return ( parse_status );
3227    }
3228#endif
3229
3230
3231static int srec_test_get_run_params ( unsigned int *num_shutdown_loops, unsigned int *num_continuous_run_loops )
3232    {
3233    int get_status;
3234
3235    get_status = get_num_srec_test_shutdown_times ( num_shutdown_loops );
3236
3237    if ( get_status == 0 )
3238                get_status = get_num_srec_test_continuous_loops ( num_continuous_run_loops );
3239
3240    return ( get_status );
3241    }
3242
3243
3244
3245static int srec_test_shutdown_application_data ( ApplicationData *applicationData )
3246    {
3247    int init_status;
3248    int i;
3249
3250    init_status = 0;
3251
3252    applicationData->argc = 0;
3253    applicationData->argv = NULL;
3254    applicationData->locale = -1;
3255
3256    if (applicationData->recognizer != NULL)
3257        {
3258        srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup Recognizer") );
3259/*        SR_RecognizerLogSessionEnd ( applicationData->recognizer );
3260        SR_RecognizerDestroy ( applicationData->recognizer );*/
3261        applicationData->recognizer = NULL;
3262        applicationData->result = NULL;                 // this was deallocated by SR_RecognizerDestroy()
3263        }
3264
3265    if (applicationData->vocabulary != NULL)
3266        {
3267        srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup Vocabulary") );
3268/*        SR_VocabularyDestroy(applicationData->vocabulary);
3269        applicationData->vocabulary = NULL;*/
3270        }
3271
3272    if (applicationData->nametag != NULL)
3273        {
3274        srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup NameTag") );
3275/*        SR_NametagDestroy(applicationData->nametag);
3276        applicationData->nametag = NULL;*/
3277        }
3278
3279    if (applicationData->nametags != NULL)
3280        {
3281        srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup NameTagSet") );
3282/*        SR_NametagsDestroy(applicationData->nametags);
3283        applicationData->nametags = NULL;*/
3284        }
3285
3286    for (i = 0; i < applicationData->grammarCount; ++i)
3287        srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup Grammar") );
3288/*
3289        if ( applicationData->grammars [i] != NULL )
3290            {
3291            printf ( "!!!!!!!!  %d Grammars Not Destroyed !!!!!!!!!!!!\n", i );
3292            SR_GrammarDestroy(applicationData->grammars[i]);
3293            applicationData->grammars[i] = NULL;
3294            }
3295        }
3296
3297    applicationData->activeGrammar = -1;
3298    applicationData->activeRule = NULL;
3299    applicationData->voiceEnrollment = NULL;
3300*/
3301    applicationData->raw_waveform = NULL;
3302    applicationData->raw_waveform_filename [0] = L( '\0' );
3303    applicationData->raw_waveform_file = NULL;
3304    applicationData->transcription[0] = L( '\0' );
3305    applicationData->forced_rec_mode = ForcedRecModeNotSet;
3306
3307    return ( init_status );
3308    }
3309
3310
3311
3312static int srec_test_init_application_data ( ApplicationData *applicationData, int arg_count, LCHAR *arg_vals [] )
3313    {
3314    int init_status;
3315
3316    init_status = 0;
3317    applicationData->argc = arg_count;
3318    applicationData->argv = arg_vals;
3319    applicationData->locale = -1;
3320    applicationData->recognizer = NULL;
3321    applicationData->result = NULL;
3322    applicationData->vocabulary = NULL;
3323    applicationData->nametag = NULL;
3324    applicationData->nametags = NULL;
3325    applicationData->grammarCount = 0;  /* No need to initialize arrays, index is 0 */
3326    applicationData->active_grammar_num = -1;
3327/*    applicationData->activeRule = NULL;
3328    applicationData-> = applicationData->voiceEnrollment = NULL;*/
3329    applicationData->audio_buffer_requested_size = DEFAULT_AUDIO_BUFFER_SIZE;
3330    applicationData->raw_waveform = NULL;
3331    applicationData->raw_waveform_filename [0] = L( '\0' );
3332    applicationData->raw_waveform_file = NULL;
3333    applicationData->transcription[0] = L( '\0' );
3334    applicationData->forced_rec_mode = ForcedRecModeNotSet;
3335    return ( init_status );
3336    }
3337
3338
3339static int srec_test_run_test_shutdown_session ( ApplicationData *applicationData )
3340    {
3341    int shutdown_status;
3342
3343    shutdown_status = 0;
3344
3345    SR_RecognizerUnsetup ( applicationData->recognizer); // releases acoustic models
3346    SR_RecognizerDestroy ( applicationData->recognizer );
3347    applicationData->recognizer = NULL;
3348    ShutdownSession ( );
3349
3350    return ( shutdown_status );
3351    }
3352
3353
3354
3355static int srec_test_run_test_init_session ( ApplicationData *applicationData )
3356{
3357  int             run_status;
3358  ESR_ReturnCode  esr_status;
3359
3360  run_status = 0;
3361  LPRINTF(L("\nCreate Recognizer:\n"));
3362  LPRINTF(L("    InitSession()\n"));
3363  esr_status = InitSession ( applicationData->argc, applicationData->argv );
3364
3365  if ( esr_status == ESR_SUCCESS )
3366    {
3367      LPRINTF(L("    SR_RecognizerCreate()\n"));
3368      esr_status = SR_RecognizerCreate ( &applicationData->recognizer );
3369
3370      if ( esr_status != ESR_SUCCESS )
3371    {
3372      ShutdownSession ( );
3373      run_status = -1;
3374      /* Log Here */
3375    } else {
3376      LPRINTF(L("    SR_RecognizerSetup()\n"));
3377      esr_status = SR_RecognizerSetup ( applicationData->recognizer);
3378      if ( esr_status != ESR_SUCCESS )
3379        {
3380          ShutdownSession ( );
3381          run_status = -1;
3382          /* Log Here */
3383        }
3384    }
3385    }
3386  return ( run_status );
3387}
3388
3389
3390static int srec_test_run_test_shutdown_vocab_grammar ( ApplicationData *applicationData )
3391    {
3392    int shutdown_status;
3393
3394    shutdown_status = 0;
3395    SR_RecognizerLogSessionEnd ( applicationData->recognizer );
3396/*    SR_GrammarDestroy ( applicationData->grammars [0].grammar );
3397    applicationData->grammars [0].grammar = NULL;*/
3398    SR_VocabularyDestroy ( applicationData->vocabulary );
3399    applicationData->vocabulary = NULL;
3400
3401    return ( shutdown_status );
3402    }
3403
3404
3405
3406static int srec_test_run_test_init_vocab_grammar ( ApplicationData *applicationData )
3407    {
3408    int             run_status;
3409    ESR_ReturnCode  esr_status;
3410    LCHAR           filename[P_PATH_MAX];
3411    size_t          len;
3412
3413    run_status = 0;
3414   /* Create vocabulary object and associate with grammar */
3415    LPRINTF(L("Create vocabulary object and associate with grammar:\n"));
3416    len = P_PATH_MAX;
3417    esr_status = ESR_SessionGetLCHAR ( L("cmdline.vocabulary"), filename, &len );
3418
3419    if ( esr_status == ESR_SUCCESS )
3420        {
3421        LPRINTF(L("    SR_VocabularyLoad()\n"));
3422        esr_status = SR_VocabularyLoad ( filename, &applicationData->vocabulary );
3423
3424        if ( esr_status == ESR_SUCCESS )
3425            {
3426            LPRINTF(L("    SR_VocabularyGetLanguage()\n"));
3427            esr_status =  SR_VocabularyGetLanguage ( applicationData->vocabulary, &applicationData->locale );
3428
3429            if ( esr_status == ESR_SUCCESS )
3430                {
3431                /* start a new log session */
3432                LPRINTF( L("Start a new log session:\n") );
3433                LPRINTF( L("    SR_RecognizerLogSessionStart()\n") );
3434                esr_status = SR_RecognizerLogSessionStart ( applicationData->recognizer, L("SRecTest.session1") );
3435
3436                if ( esr_status != ESR_SUCCESS )
3437                    {
3438                    SR_VocabularyDestroy ( applicationData->vocabulary );
3439                    applicationData->vocabulary = NULL;
3440                    run_status = -1;
3441                    /* Log here */
3442                    }
3443                }
3444            else
3445                {
3446                SR_VocabularyDestroy ( applicationData->vocabulary );
3447                applicationData->vocabulary = NULL;
3448                run_status = -1;
3449                /* Log Here */
3450                }
3451            }
3452        else
3453            {
3454            run_status = -1;
3455            /* Log Here */
3456            }
3457        }
3458    else
3459        {
3460        run_status = -1;
3461        /* Log Here */
3462        }
3463    return ( run_status );
3464    }
3465
3466
3467
3468static int srec_test_run_test_shutdown ( ApplicationData *applicationData )
3469    {
3470    int shutdown_status;
3471
3472    shutdown_status = srec_test_run_test_shutdown_vocab_grammar ( applicationData );
3473
3474        if ( shutdown_status == 0 )
3475            shutdown_status = srec_test_run_test_shutdown_session ( applicationData );
3476    return ( shutdown_status );
3477    }
3478
3479
3480static int srec_test_run_test_init ( ApplicationData *applicationData )
3481{
3482  int run_status;
3483
3484  run_status = srec_test_run_test_init_session ( applicationData );
3485
3486  if ( run_status == 0 )
3487    {
3488      run_status = srec_test_run_test_init_vocab_grammar ( applicationData );
3489
3490      if ( run_status != 0 )
3491    {
3492      srec_test_run_test_shutdown_session ( applicationData );
3493    }
3494    }
3495  else
3496    {
3497      srec_test_run_test_shutdown_session ( applicationData );
3498    }
3499  return ( run_status );
3500}
3501
3502
3503
3504static int srec_test_run_test_execute ( ApplicationData *applicationData )
3505    {
3506    int             run_status;
3507    ESR_ReturnCode  esr_status;
3508
3509    run_status = 0;
3510    applicationData->nametag = NULL;
3511
3512    LPRINTF(L("Recognize:\n"));
3513    LPRINTF(L("    SR_NametagsCreate()\n"));
3514    esr_status = SR_NametagsCreate ( &applicationData->nametags );
3515
3516    if ( esr_status == ESR_SUCCESS )
3517        {
3518        run_status = srec_test_process_commands ( applicationData );
3519        SR_NametagsDestroy ( applicationData->nametags );
3520        applicationData->nametags = NULL;
3521
3522        if ( run_status != 0 )
3523            {
3524            /* Log Here */
3525            }
3526        }
3527    else
3528        {
3529        run_status = -1;
3530        /* Log Here */
3531        }
3532    return ( run_status );
3533    }
3534
3535
3536
3537static int srec_test_run_test ( ApplicationData *applicationData )
3538    {
3539    int run_status;
3540
3541    run_status = srec_test_run_test_init ( applicationData );
3542
3543    if ( run_status == 0 )
3544        {
3545        run_status = srec_test_run_test_execute ( applicationData );
3546        srec_test_run_test_shutdown ( applicationData );
3547        }
3548    return ( run_status );
3549    }
3550
3551
3552
3553static int srec_test_shutdown_memory_system ( void )
3554    {
3555    int shutdown_status;
3556
3557    shutdown_status = 0;
3558    PMemShutdown ( );
3559
3560    return ( shutdown_status );
3561    }
3562
3563
3564
3565static int srec_test_init_memory_system ( unsigned int srec_test_heap_size )
3566    {
3567    int             init_status;
3568    ESR_ReturnCode  esr_status;
3569
3570    init_status = 0;
3571    esr_status = PMemInit ( );
3572
3573    if ( esr_status == ESR_SUCCESS )
3574        {
3575        PSTACK_SIZE_INIT ( );   /* I don't know what this is, it should probably have a status */
3576        }
3577    else
3578        {
3579        init_status = -1;
3580        /* Log Here */
3581        }
3582    return ( init_status );
3583    }
3584
3585
3586
3587static int srec_test_shutdown_file_system ( void )
3588    {
3589    int shutdown_status;
3590
3591    shutdown_status = 0;
3592
3593    return ( shutdown_status );
3594    }
3595
3596
3597
3598static int srec_test_init_file_system ( int arg_count, LCHAR *arg_vals [] )
3599    {
3600    int             init_status;
3601
3602    init_status = 0;
3603    return ( init_status );
3604    }
3605
3606
3607
3608static int srec_test_shutdown_logging_system ( PLogger *logger )
3609    {
3610    int shutdown_status;
3611
3612    shutdown_status = 0;
3613    PLogShutdown ( );
3614
3615    return ( shutdown_status );
3616    }
3617
3618
3619
3620static int srec_test_init_logging_system ( int arg_count, LCHAR *arg_vals [], PLogger *logger )
3621    {
3622    int             init_status;
3623    ESR_ReturnCode  esr_status;
3624
3625    init_status = 0;
3626    esr_status = PLogCreateFileLogger ( PSTDOUT, &logger );
3627
3628    if ( esr_status == ESR_SUCCESS )
3629        {
3630        esr_status = PLogInit ( logger, 0 );
3631
3632        if ( esr_status != ESR_SUCCESS )
3633            {
3634/*          pfclose ( (struct PFile_t *)logger );       I'm not sure this is correct, check with Gili */
3635            init_status = -1;
3636            /* Log Here */
3637            }
3638        }
3639    else
3640        {
3641        init_status = -1;
3642        /* Log Here */
3643        }
3644    return ( init_status );
3645    }
3646
3647
3648
3649static int srec_test_shutdown_system ( PLogger *logger )
3650    {
3651    int shutdown_status;
3652
3653    shutdown_status = srec_test_shutdown_logging_system ( logger );
3654
3655    if ( shutdown_status == 0 )
3656        {
3657        shutdown_status = srec_test_shutdown_file_system ( );
3658
3659        if ( shutdown_status == 0 )
3660            shutdown_status = srec_test_shutdown_memory_system ( );
3661        }
3662    return ( shutdown_status );
3663    }
3664
3665
3666
3667
3668static int srec_test_init_system ( unsigned int srec_test_heap_size, PLogger* logger, int arg_count, LCHAR *arg_vals [] )
3669    {
3670    int init_status;
3671
3672   /* register signal handler so cleanup works on CTRL-C (Win32) */
3673#ifdef _WIN32
3674    signal(SIGINT, signal_handler_SIGINT);
3675#endif
3676    init_status = srec_test_init_memory_system ( srec_test_heap_size );
3677
3678    if ( init_status == 0 )
3679        {
3680        init_status = srec_test_init_file_system ( arg_count, arg_vals );
3681
3682        if ( init_status == 0 )
3683            {
3684            init_status = srec_test_init_logging_system ( arg_count, arg_vals, logger );
3685
3686            if ( init_status != 0 )
3687                {
3688                srec_test_shutdown_file_system ( );
3689                srec_test_shutdown_memory_system ( );
3690                }
3691            }
3692        else
3693            {
3694            srec_test_shutdown_memory_system ( );
3695            }
3696        }
3697    return ( init_status );
3698    }
3699
3700
3701
3702#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32)
3703_CrtMemState s0,s1,s2;
3704
3705
3706
3707void OutputDivider( void )
3708    {
3709
3710    _RPT1(_CRT_WARN, "%s", "********************************************************************\n");
3711    }
3712
3713
3714
3715void OutputNewline( void )
3716    {
3717
3718    _RPT1(_CRT_WARN, "%s", "\n");
3719    }
3720#endif
3721
3722
3723
3724#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32)
3725void initialize_heap_logging ( void )
3726    {
3727
3728     _CrtSetReportMode(_CRT_WARN,   _CRTDBG_MODE_FILE);
3729     _CrtSetReportFile(_CRT_WARN,   _CRTDBG_FILE_STDOUT);
3730     _CrtSetReportMode(_CRT_ERROR,  _CRTDBG_MODE_FILE);
3731     _CrtSetReportFile(_CRT_ERROR,  _CRTDBG_FILE_STDOUT);
3732     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
3733     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
3734     _CrtMemCheckpoint(&s0);
3735     }
3736#endif
3737
3738
3739
3740#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32)
3741void execute_heap_logging ( void )
3742    {
3743    OutputNewline();
3744    OutputDivider();
3745    _RPT1(_CRT_WARN, "%s", "After cleanup, before exiting main()\n");
3746    OutputDivider();
3747    _CrtMemCheckpoint(&s1);
3748    _CrtMemDifference(&s2, &s0, &s1);
3749    _CrtMemDumpStatistics(&s2);
3750    OutputNewline();
3751    OutputDivider();
3752    _RPT1(_CRT_WARN, "%s", "Checking for memory leaks...\n");
3753    OutputDivider();
3754    _CrtDumpMemoryLeaks();
3755    }
3756#endif
3757
3758
3759
3760int main(int argc, LCHAR* argv [] )
3761    {
3762     int             test_status;
3763     unsigned int    num_shutdown_loops;
3764     unsigned int    current_shutdown_loop;
3765     unsigned int    num_continuous_run_loops;
3766     unsigned int    current_continuous_run_loop;
3767     unsigned int    srec_test_heap_size;
3768
3769#ifdef _WIN32
3770    initStack();
3771#endif
3772        {
3773    /* This extra block is there to get a more precise estimate of the stack
3774      depth.  This way we also make sure that the local variables of main are
3775      taken into acount when estimating the stack size.
3776   */
3777        ApplicationData applicationData;
3778        PLogger* logger;
3779
3780        srec_test_heap_size = ( 4 * 1024 * 1024 );
3781        logger = NULL;
3782        test_status = srec_test_get_run_params ( &num_shutdown_loops, &num_continuous_run_loops );
3783
3784        if ( test_status == 0 )
3785            {
3786            current_shutdown_loop = 0;
3787
3788            while ( ( current_shutdown_loop < num_shutdown_loops )/* && ( test_status == 0 )*/ )
3789                {
3790#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32)
3791                initialize_heap_logging ( );
3792#endif
3793                test_status = srec_test_init_system ( srec_test_heap_size, logger, argc, argv );
3794
3795                if ( test_status == 0 )
3796                    {
3797                    current_continuous_run_loop = 0;
3798
3799                    while ( ( current_continuous_run_loop < num_continuous_run_loops ) && ( test_status == 0 ) )
3800                        {
3801                        test_status = srec_test_init_application_data ( &applicationData, argc, argv );
3802
3803                        if ( test_status == 0 )
3804                            {
3805                            test_status = srec_test_run_test ( &applicationData );
3806                            srec_test_shutdown_application_data ( &applicationData );
3807                            }
3808                        current_continuous_run_loop++;
3809                        }
3810                    test_status = srec_test_shutdown_system ( logger );
3811                    }
3812                current_shutdown_loop++;
3813#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32)
3814                execute_heap_logging ( );
3815#endif
3816                }
3817            }
3818        }
3819
3820    return ( test_status );
3821    }
3822
3823
3824