SRecTest.c revision 2f8ec03aa4809cec2b0944ca9bf1c7783e11b4ae
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    LCHAR           state_string[1000];
1691    size_t len;
1692
1693    get_status = 0;
1694    len = sizeof(state_string);
1695    esr_status = SR_AcousticStateGet ( data->recognizer, state_string, &len );
1696
1697    if ( esr_status == ESR_SUCCESS )
1698        {
1699        printf ( "Acoustic State :\n%s\n", state_string );
1700        }
1701    else
1702        {
1703        get_status = -1;
1704        /* Log Here */
1705        }
1706    return ( get_status );
1707    }
1708
1709
1710
1711int srec_test_reset_acousticstate ( ApplicationData *data )
1712    {
1713    int             reset_status;
1714    ESR_ReturnCode  esr_status;
1715
1716    reset_status = 0;
1717    esr_status = SR_AcousticStateReset ( data->recognizer );
1718
1719    if ( esr_status != ESR_SUCCESS )
1720        {
1721        reset_status = -1;
1722        /* Log Here */
1723        }
1724    return ( reset_status );
1725    }
1726
1727
1728
1729int srec_test_set_forced_rec_mode ( ApplicationData *data, LCHAR *command_text )
1730    {
1731    int         set_status;
1732    LCHAR       mode[P_PATH_MAX];
1733
1734    set_status =  srec_test_get_one_command_item ( command_text, P_PATH_MAX, mode, NULL );
1735
1736    if ( set_status == 0 )
1737        {
1738        if ( LSTRCMP ( mode, L("one_time") ) == 0 )
1739            {
1740            data->forced_rec_mode = ForcedRecModeOneTime;
1741            }
1742        else if ( LSTRCMP ( mode, L("off") ) == 0 )
1743            {
1744            data->forced_rec_mode = ForcedRecModeOff;
1745            }
1746        else if ( LSTRCMP ( mode, L("on") ) == 0 )
1747            {
1748            data->forced_rec_mode = ForcedRecModeOn;
1749            }
1750        else
1751            {
1752            set_status = -1;
1753            /* Log Here */
1754            }
1755        }
1756    return ( set_status );
1757    }
1758
1759
1760
1761int srec_test_execute_command ( ApplicationData *data, LCHAR *text, FILE *results_file, size_t *recognition_count )
1762    {
1763    int         execute_status;
1764    LCHAR       *current_command_start;
1765    LCHAR       *current_end_command;
1766    LCHAR       command [MAX_LINE_LENGTH];
1767    SR_Grammar  *active_grammar;
1768    LCHAR       log_buffer [LOG_BUFFER_SIZE];
1769
1770    srec_test_get_active_grammar ( data, &active_grammar );
1771    current_command_start = text;
1772    execute_status = srec_test_get_one_command_item ( current_command_start, MAX_LINE_LENGTH, command, &current_end_command );
1773
1774    if ( execute_status == 0 )
1775        {
1776        if( data->nametag
1777            && ((LSTRCMP(command, "recognize_nist")==0) || (LSTRCMP(command, "recognize_pcm")==0)) ) {
1778                /* if there is a nametag held in memory, and we don't make use of it, then
1779                let's destroy it here */
1780            SR_NametagDestroy(  data->nametag);
1781            data->nametag = NULL;
1782        }
1783
1784        if ( LSTRCMP ( command, L("recognize_nist") ) == 0 )
1785            execute_status = srec_test_recognize_nist_file ( active_grammar, data, results_file,
1786                                                             current_end_command, recognition_count );
1787        else if ( LSTRCMP ( command, L("recognize_pcm") ) == 0 )
1788            execute_status = srec_test_recognize_pcm_file ( active_grammar, data, results_file,
1789                                                            current_end_command, recognition_count );
1790        else if ( LSTRCMP ( command, L("context_load") ) == 0 )
1791            execute_status = srec_test_load_context ( data, current_end_command );
1792        else if ( LSTRCMP ( command, L("context_use") ) == 0 )
1793            execute_status = srec_test_use_context ( active_grammar, data, current_end_command );
1794        else if ( LSTRCMP ( command, L("context_free") ) == 0 )
1795            execute_status = srec_test_free_context ( active_grammar, data, current_end_command );
1796        else if ( LSTRCMP ( command, L("context_unload") ) == 0 )
1797            execute_status = srec_test_unload_context ( data, current_end_command );
1798        else if ( LSTRCMP ( command, L("addwords_from_nametags") ) == 0 )
1799            execute_status = srec_test_voice_enroll_nametags ( active_grammar, data, current_end_command );
1800        else if ( LSTRCMP ( command, L("resetslots") ) == 0 )
1801            execute_status = srec_test_reset_slots ( active_grammar );
1802        else  if ( LSTRCMP ( command, L("addword") ) == 0 )
1803            execute_status = srec_test_add_word ( active_grammar, current_end_command );
1804        else if ( LSTRCMP ( command, L("context_compile") ) == 0 )
1805            execute_status = srec_test_compile_active_context ( active_grammar );
1806        else if ( LSTRCMP ( command, L("context_save") ) == 0 )
1807            execute_status = srec_test_save_context ( active_grammar, current_end_command );
1808        else if ( LSTRCMP ( command, L("addword_from_last_nametag") ) == 0 )
1809            execute_status = srec_test_voice_enroll ( active_grammar, data, current_end_command );
1810        else if ( LSTRCMP ( command, L("load_nametags") ) == 0 )
1811            execute_status = srec_test_load_nametags ( data, current_end_command );
1812        else if ( LSTRCMP ( command, L("save_nametags") ) == 0 )
1813            execute_status = srec_test_save_nametags ( data, current_end_command );
1814        else if ( LSTRCMP ( command, L("clear_nametags") ) ==0 )
1815            execute_status = srec_test_clear_nametags ( data );
1816        else if ( LSTRCMP ( command, L("add_to_nametags") ) == 0 )
1817            execute_status = srec_test_add_to_nametags ( data, current_end_command );
1818        else if ( LSTRCMP ( command, L("acousticstate_set") ) == 0 )
1819            execute_status = srec_test_set_acousticstate ( data, current_end_command );
1820        else if ( LSTRCMP ( command, L("acousticstate_get") ) == 0 )
1821            execute_status = srec_test_get_acousticstate ( data );
1822        else if ( LSTRCMP ( command, L("acousticstate_reset") ) == 0 )
1823            execute_status = srec_test_reset_acousticstate ( data );
1824        else if ( LSTRCMP ( command, L("forced_rec") ) == 0 )
1825            execute_status = srec_test_set_forced_rec_mode ( data, current_end_command );
1826        else if ( LSTRCMP ( command, L("change_sample_rate") ) == 0 )
1827            execute_status = srec_test_change_sample_rate ( data, current_end_command );
1828        else if ( LSTRCMP ( command, L("set_size_t_parameter") ) == 0 )
1829            execute_status = srec_test_set_size_t_parameter ( data, current_end_command );
1830        else if ( LSTRCMP ( command, L("get_size_t_parameter") ) == 0 )
1831            execute_status = srec_test_get_size_t_parameter ( data, current_end_command );
1832        else if ( LSTRCMP ( command, L("set_audio_size") ) == 0 )
1833            execute_status = srec_test_set_audio_size ( data, current_end_command );
1834        else if ( LSTRCMP ( command, L("sleep") ) == 0 )
1835            execute_status = srec_test_sleep ( current_end_command );
1836        else if ( *( command ) == L('#') )
1837            execute_status = 0;   /* Comment in file just skip */
1838        else
1839            {
1840            execute_status = -1;
1841            LSPRINTF ( log_buffer, L("Unknown Command %s"), command );
1842            srec_test_log_error ( 0, ERROR_LEVEL_MODERATE, log_buffer );
1843            }
1844        }
1845    else
1846        {
1847        /* Log Here */
1848        }
1849    return ( execute_status );
1850    }
1851
1852
1853
1854int srec_test_open_command_file ( PFile **command_file )
1855    {
1856    int             open_status;
1857    ESR_ReturnCode  esr_status;
1858    LCHAR           file_name [P_PATH_MAX];
1859    size_t          len;
1860
1861    open_status = 0;
1862    len = P_PATH_MAX;
1863    esr_status = ESR_SessionGetLCHAR ( L("cmdline.tcp"), file_name, &len );
1864
1865    if ( esr_status == ESR_SUCCESS )
1866        {
1867        len = P_PATH_MAX;
1868        esr_status = ESR_SessionPrefixWithBaseDirectory ( file_name, &len );
1869
1870        if ( esr_status == ESR_SUCCESS )
1871            {
1872            *command_file = pfopen ( file_name, L("r") );
1873
1874            if ( *command_file == NULL )
1875                {
1876                open_status = -1;
1877                LPRINTF(L("\nError opening command file %s\n"), file_name);
1878                }
1879            }
1880        else
1881            {
1882            open_status = -1;
1883            LPRINTF(L("\nError finding command file\n"));
1884            }
1885        }
1886     else
1887        {
1888        open_status = -1;
1889        /* Log Here */
1890        }
1891     return ( open_status );
1892     }
1893
1894
1895
1896void srec_test_close_command_file ( PFile *command_file )
1897    {
1898
1899    pfclose ( command_file );
1900    }
1901
1902
1903
1904void srec_test_close_audio_file ( PFile *audio_file )
1905    {
1906
1907    pfclose ( audio_file );
1908    }
1909
1910
1911
1912int srec_test_open_results_file ( FILE **results_file )
1913    {
1914    int             open_status;
1915    ESR_ReturnCode  esr_status;
1916    LCHAR           file_name [P_PATH_MAX];
1917    size_t          len;
1918
1919    open_status = 0;
1920    len = P_PATH_MAX;
1921    esr_status = ESR_SessionGetLCHAR ( L("cmdline.results"), file_name, &len );
1922
1923    if ( esr_status == ESR_SUCCESS )
1924        {
1925        *results_file = fopen ( file_name, L("w") );
1926
1927        if ( ( *results_file ) == NULL )
1928            {
1929            open_status = -1;
1930            /* Log Here */
1931            }
1932        }
1933     else
1934         {
1935         open_status = -1;
1936         /* Log Here */
1937         }
1938     return ( open_status );
1939     }
1940
1941
1942
1943void srec_test_close_results_file ( FILE *results_file )
1944    {
1945
1946    fclose ( results_file );
1947    }
1948
1949
1950
1951int srec_test_process_commands ( ApplicationData *data )
1952    {
1953    int             process_status;
1954    PFile           *command_file;
1955    FILE            *results_file;
1956    LCHAR           *got_line_ok;
1957    LCHAR           linebuffer [MAX_LINE_LENGTH];
1958    size_t          recognition_count;
1959
1960    recognition_count = 0;
1961    process_status = srec_test_open_command_file ( &command_file );
1962
1963    if ( process_status == 0 )
1964        {
1965        process_status = srec_test_open_results_file ( &results_file );
1966
1967        if ( process_status == 0 )
1968            {
1969            do
1970                {
1971                got_line_ok = pfgets ( linebuffer, MAX_LINE_LENGTH, command_file );
1972
1973                if ( got_line_ok != NULL )
1974                    srec_test_execute_command ( data, linebuffer, results_file, &recognition_count );
1975                }
1976            while ( ( got_line_ok != NULL ) && ( process_status == 0 ) );
1977            if( data->nametag ) {
1978                SR_NametagDestroy(  data->nametag);
1979                data->nametag = NULL;
1980            }
1981
1982
1983            srec_test_close_results_file ( results_file );
1984            }
1985        srec_test_close_command_file ( command_file );
1986        }
1987    return ( process_status );
1988    }
1989
1990
1991
1992int srec_test_open_pcm_file ( LCHAR *file_name, PFile **pcm_file )
1993    {
1994    int             open_status;
1995    ESR_ReturnCode  esr_status;
1996    int             lstr_result;
1997    LCHAR           file_path [P_PATH_MAX];
1998    size_t          len;
1999
2000    open_status = 0;
2001    len = P_PATH_MAX;
2002    esr_status = ESR_SessionGetLCHAR ( L("cmdline.datapath"), file_path, &len );
2003
2004    if ( esr_status == ESR_SUCCESS )
2005        {
2006        len = P_PATH_MAX;
2007        esr_status = ESR_SessionPrefixWithBaseDirectory ( file_path, &len );
2008
2009        if ( esr_status == ESR_SUCCESS )
2010            {
2011            esr_status = pf_convert_backslashes_to_forwardslashes ( file_path );
2012
2013            if ( esr_status == ESR_SUCCESS )
2014                {
2015                len = P_PATH_MAX;
2016                lstr_result = lstrinsert ( file_path, file_name, 0, &len );
2017
2018                if ( lstr_result == 0 )
2019                    {
2020                    *pcm_file = pfopen ( file_name, L("rb") );
2021
2022                    if ( ( *pcm_file ) == NULL )
2023                        {
2024                        open_status = -1;
2025                        }
2026                    }
2027                else
2028                    {
2029                    open_status = -1;
2030                    /* Log Here */
2031                    }
2032                }
2033            else
2034                {
2035                open_status = -1;
2036                /* Log Here */
2037                }
2038            }
2039        else
2040            {
2041            open_status = -1;
2042            /* Log Here */
2043            }
2044        }
2045     else
2046         {
2047         open_status = -1;
2048         /* Log Here */
2049         }
2050     if(open_status) PLogError( ("Error: while opening %s\n"), file_name);
2051     return ( open_status );
2052     }
2053
2054
2055
2056int srec_test_open_nist_file ( LCHAR *file_name, PFile **nist_file )
2057    {
2058    int             open_status;
2059    ESR_ReturnCode  esr_status;
2060    int             lstr_result;
2061    LCHAR           file_path [P_PATH_MAX];
2062    size_t          len;
2063
2064    open_status = 0;
2065    len = P_PATH_MAX;
2066    esr_status = ESR_SessionGetLCHAR ( L("cmdline.datapath"), file_path, &len );
2067
2068    if ( esr_status == ESR_SUCCESS )
2069        {
2070        len = P_PATH_MAX;
2071        esr_status = ESR_SessionPrefixWithBaseDirectory ( file_path, &len );
2072
2073        if ( esr_status == ESR_SUCCESS )
2074            {
2075            esr_status = pf_convert_backslashes_to_forwardslashes ( file_path );
2076
2077        if(file_path[ LSTRLEN(file_path)-1] != L('/'))
2078            LSTRCAT(file_path,L("/"));
2079
2080            if ( esr_status == ESR_SUCCESS )
2081                {
2082                len = P_PATH_MAX;
2083                lstr_result = lstrinsert ( file_path, file_name, 0, &len );
2084
2085                if ( lstr_result == 0 )
2086                    {
2087                    *nist_file = pfopen ( file_name, L("rb") );
2088
2089                    if ( ( *nist_file ) != NULL )
2090                        {
2091                        esr_status = pfseek ( *nist_file, 1024, SEEK_SET );
2092
2093                        if ( esr_status != ESR_SUCCESS )
2094                            {
2095                            open_status = -1;
2096                            }
2097                        }
2098                    else
2099                        {
2100                        open_status = -1;
2101                        }
2102                    }
2103                else
2104                    {
2105                    open_status = -1;
2106                    /* Log Here */
2107                    }
2108                }
2109            else
2110                {
2111                open_status = -1;
2112                /* Log Here */
2113                }
2114            }
2115        else
2116            {
2117            open_status = -1;
2118            /* Log Here */
2119            }
2120        }
2121     else
2122         {
2123         open_status = -1;
2124         /* Log Here */
2125         }
2126	 if(open_status)	PLogError( ("Error: while opening %s\n"), file_name);
2127     return ( open_status );
2128     }
2129
2130
2131
2132int srec_test_get_audio_from_file ( PFile *audio_file, ApplicationData *data, ESR_BOOL *hit_eof )
2133    {
2134    int get_status;
2135    int eof_status;
2136
2137    get_status = 0;
2138    data->num_samples_read = pfread ( data->audio_buffer, sizeof ( asr_int16_t ), data->audio_buffer_requested_size, audio_file );
2139
2140    if ( data->num_samples_read > 0 )
2141        {
2142        *hit_eof = ESR_FALSE;
2143        }
2144    else
2145        {
2146        eof_status = pfeof ( audio_file );
2147
2148        if ( eof_status == 0 )
2149            {
2150            get_status = -1;
2151            /* Log Here */
2152            }
2153        else
2154            {
2155            *hit_eof = ESR_TRUE;
2156            }
2157        }
2158    return ( get_status );
2159    }
2160
2161
2162
2163int srec_test_feed_recognizer ( ApplicationData *data, ESR_BOOL hit_eof, SR_RecognizerStatus *esr_recog_status, SR_RecognizerResultType *result_type )
2164    {
2165    int             feed_status;
2166    ESR_ReturnCode  esr_status;
2167
2168    feed_status = 0;
2169    esr_status = SR_RecognizerPutAudio ( data->recognizer, data->audio_buffer, &data->num_samples_read, hit_eof );
2170
2171    if ( esr_status == ESR_SUCCESS )
2172        {
2173        do
2174            {
2175            esr_status = SR_RecognizerAdvance ( data->recognizer, esr_recog_status, result_type, &data->result );
2176
2177            if ( esr_status != ESR_SUCCESS )
2178                {
2179                feed_status = -1;
2180                *esr_recog_status = SR_RECOGNIZER_EVENT_STOPPED;
2181                *result_type = SR_RECOGNIZER_RESULT_TYPE_COMPLETE;
2182                /* Log Here */
2183                }
2184            }
2185        while ( ( *esr_recog_status ) == SR_RECOGNIZER_EVENT_INCOMPLETE );
2186        }
2187    else
2188        {
2189        feed_status = -1;
2190        /* Log Here */
2191        }
2192     return ( feed_status );
2193     }
2194
2195
2196
2197int srec_test_flush_audio ( ApplicationData *data, SR_RecognizerStatus *esr_recog_status, SR_RecognizerResultType *result_type )
2198    {
2199    int             flush_status;
2200    ESR_ReturnCode  esr_status;
2201
2202    flush_status = 0;
2203
2204    while ( ( *result_type ) != SR_RECOGNIZER_RESULT_TYPE_COMPLETE )
2205        {
2206        esr_status = SR_RecognizerAdvance ( data->recognizer, esr_recog_status, result_type, &data->result );
2207
2208        if ( esr_status != ESR_SUCCESS )
2209            {
2210            flush_status = -1;
2211            *esr_recog_status = SR_RECOGNIZER_EVENT_STOPPED;
2212            *result_type = SR_RECOGNIZER_RESULT_TYPE_COMPLETE;
2213            /* Log Here */
2214            }
2215        }
2216    return ( flush_status );
2217    }
2218
2219
2220
2221int srec_test_process_start_timeout ( ApplicationData *data, FILE *results_file )
2222    {
2223    int             process_status;
2224    ESR_ReturnCode  esr_status;
2225    size_t          utterance_timeout;
2226
2227    process_status = 0;
2228    esr_status = ESR_SessionGetSize_t ( L("SREC.Recognizer.utterance_timeout"), &utterance_timeout );
2229
2230    if ( esr_status == ESR_SUCCESS )
2231        {
2232        LFPRINTF ( results_file, L("E: utterance_timeout %lu\n"), (unsigned long)utterance_timeout );
2233        LFPRINTF ( results_file, L("R: <FAILED>\n") );
2234        LPRINTF ( L("\n-------------------------------------\n") );
2235        LPRINTF ( L("E: utterance_timeout %lu\n"), (unsigned long)utterance_timeout );
2236        LPRINTF ( L("R: <FAILED>\n") );
2237        LPRINTF ( L("-------------------------------------\n\n") );
2238        }
2239    else
2240        {
2241        process_status = -1;
2242        /* Log Here */
2243        }
2244    srec_test_log_recognition_failure ( data );
2245
2246    return ( process_status );
2247    }
2248
2249
2250int srec_test_write_and_log_wave_form_file ( ApplicationData *data, size_t recognition_count )
2251    {
2252    int                 write_status;
2253    ESR_ReturnCode      esr_status;
2254    LCHAR               *wave_filename;
2255
2256    write_status = 0;
2257    LSPRINTF ( data->raw_waveform_filename, L("a%lu__%s.raw"), (unsigned long)recognition_count, data->transcription );
2258    wave_filename = data->raw_waveform_filename;
2259
2260    while ( *wave_filename )
2261        {
2262        if ( *wave_filename == ' ')
2263            *wave_filename = '_';
2264        wave_filename++;
2265        }
2266/* just write to file for now... for testing purposes */
2267    LPRINTF ( L("Dumping audio waveform to file %s\n"), data->raw_waveform_filename );
2268    data->raw_waveform_file = pfopen ( data->raw_waveform_filename, L("wb") );
2269
2270    if ( data->raw_waveform_file != NULL )
2271        {
2272        pfwrite ( (void*)data->raw_waveform, 1, data->raw_waveform_size, data->raw_waveform_file );
2273        pfclose ( data->raw_waveform_file );
2274/* log the filename in the ESR log */
2275        esr_status = SR_RecognizerLogToken ( data->recognizer, "WVFMFILENAME", data->raw_waveform_filename );
2276
2277        if ( esr_status != ESR_SUCCESS )
2278            {
2279            write_status = -1;
2280            /* Log Here */
2281            }
2282        }
2283    else
2284        {
2285        write_status = -1;
2286        /* Log Here */
2287        }
2288    return ( write_status );
2289    }
2290
2291
2292
2293int srec_test_process_nbest_list ( ApplicationData *data, FILE *results_file )
2294{
2295  int                 process_status;
2296  ESR_ReturnCode      esr_status;
2297  size_t              nbestSize;
2298  size_t              nbest_num;
2299  LCHAR               linebuffer [MAX_LINE_LENGTH];
2300  size_t              line_length;
2301
2302  process_status = 0;
2303  /* At least one semantic result exists */
2304  LPRINTF ( L("\n\n----------------------------------------------\n") );
2305
2306  LPRINTF ( L("TRANSCRIPTION    : '%s'\n"), data->transcription );
2307  esr_status = SR_RecognizerResultGetSize (data->result, &nbestSize );
2308
2309  if ( esr_status == ESR_SUCCESS )
2310    {
2311      for ( nbest_num = 0; nbest_num < nbestSize; nbest_num++ )
2312        {
2313          line_length =  MAX_LINE_LENGTH;
2314          esr_status = SR_RecognizerResultGetValue ( data->result, nbest_num, L("literal"), linebuffer, &line_length );
2315
2316          if ( esr_status == ESR_SUCCESS )
2317            {
2318              LPRINTF ( L("LITERAL[%2lu]      : '%s'\n"), (unsigned long)nbest_num, linebuffer );
2319              line_length = MAX_LINE_LENGTH;
2320              esr_status = SR_RecognizerResultGetValue ( data->result, nbest_num, L("meaning"), linebuffer, &line_length );
2321
2322              if ( esr_status != ESR_SUCCESS )
2323                {
2324                  linebuffer [0] = L('\0') ;
2325                }
2326              LPRINTF ( L("MEANING[%2lu]      : '%s'\n"), (unsigned long)nbest_num, linebuffer );
2327
2328              line_length =  MAX_LINE_LENGTH;
2329              esr_status = SR_RecognizerResultGetValue (data->result, nbest_num , L("conf"), linebuffer, &line_length );
2330              if ( esr_status == ESR_SUCCESS )
2331                {
2332                  LPRINTF ( L("CONFIDENCE VALUE : '%s'\n"), linebuffer );
2333                }
2334              else
2335                {
2336                  process_status = -1;
2337                  /* Log Here */
2338                }
2339
2340              line_length = MAX_LINE_LENGTH;
2341              esr_status = SR_RecognizerResultGetValue (data->result, nbest_num, L("raws"), linebuffer, &line_length );
2342
2343              if ( esr_status == ESR_SUCCESS )
2344                {
2345                  LPRINTF ( L("RAW SCORE[%2lu]    : '%s'\n"), (unsigned long)nbest_num, linebuffer);
2346                }
2347              else
2348                {
2349                  process_status = -1;
2350                  /* Log Here */
2351                }
2352#ifdef SREC_ENGINE_VERBOSE_LOGGING
2353              if(1) {
2354                /* get ALL key value pairs */
2355                LCHAR *keys[32], keysdata[128*32];
2356                size_t k,key_count = sizeof(keys)/sizeof(keys[0]);
2357                for(k=0;k<key_count;k++) keys[k] = &keysdata[128*k];
2358                // esr_status = SR_RecognizerResultGetKeyCount(data->result, nbest_num, &key_count);
2359                esr_status = SR_RecognizerResultGetKeyList(data->result, nbest_num, (LCHAR**)keys, &key_count);
2360                if(esr_status == ESR_SUCCESS) {
2361                  for (k=0; k<key_count; k++) {
2362                    LCHAR value[128];
2363                    size_t valuelen = sizeof(value)/sizeof(value[0]);
2364                    esr_status = SR_RecognizerResultGetValue( data->result, nbest_num, keys[k], value, &valuelen);
2365                    if(esr_status==ESR_SUCCESS)
2366                      LPRINTF( L("%s[%2lu] : '%s'\n"), keys[k], (unsigned long)nbest_num, value);
2367                  }
2368                }
2369              }
2370#endif
2371              LPRINTF ( L("\n"));
2372            }
2373          else
2374            {
2375              process_status = -1;
2376              /* Log Here */
2377            }
2378        }
2379    }
2380  else
2381    {
2382      process_status = -1;
2383      /* Log Here */
2384    }
2385
2386  LPRINTF ( L("----------------------------------------------\n\n") );
2387  return ( process_status );
2388}
2389
2390
2391
2392int srec_test_process_recognition ( ApplicationData *data, FILE *results_file, size_t recognition_count )
2393{
2394  int                 process_status;
2395  ESR_ReturnCode      esr_status;
2396  APPL_GRAMMAR_DATA   *grammar_data;
2397  LCHAR               linebuffer [MAX_LINE_LENGTH];
2398  size_t              line_length;
2399
2400  process_status = 0;
2401  srec_test_get_active_grammar_data ( data, &grammar_data );
2402
2403  do {
2404    line_length = MAX_LINE_LENGTH;
2405    esr_status = SR_RecognizerResultGetValue ( data->result, 0, L("literal"), linebuffer, &line_length );
2406
2407    if ( esr_status != ESR_SUCCESS ) break;
2408    LFPRINTF ( results_file, L("R: %s\n"), linebuffer );
2409    line_length = MAX_LINE_LENGTH;
2410    esr_status = SR_RecognizerResultGetValue ( data->result, 0, L("conf"), linebuffer, &line_length );
2411
2412    if ( esr_status != ESR_SUCCESS ) break;
2413
2414    LFPRINTF ( results_file, L("S: %s\n"), linebuffer);
2415    LPRINTF ( L("S: %s\n"), linebuffer);
2416    srec_test_process_nbest_list ( data, results_file );
2417    /*
2418     * SR_RecognizerResultGetWaveform will return pointer to buffer holding
2419     * audio data in it. This buffer is NOT under the application's control
2420     * and MUST only be read from.
2421     */
2422    if ( grammar_data->is_ve_grammar == ESR_TRUE )
2423      {
2424	esr_status = SR_NametagCreate ( data->result, L("dummyID"), &data->nametag );
2425	if(esr_status != ESR_SUCCESS) {
2426	  LPRINTF ( L("VoiceEnrollment=>FAILURE\n") );
2427	  break;
2428	}
2429	LPRINTF ( L("VoiceEnrollment=>SUCCESS\n") );
2430	if(1) {
2431	  const char* value = NULL;
2432	  size_t len, len2;
2433	  char* value2;
2434	  /* this is to mimic what might happen in a real application */
2435	  esr_status = SR_NametagGetValue( data->nametag, &value, &len);
2436	  value2 = (char*)CALLOC(len, sizeof(char), "srectest.nametag");
2437	  len2 = len;
2438	  memcpy( value2, value, len);
2439	  esr_status = SR_NametagDestroy(data->nametag);
2440	  data->nametag = NULL;
2441	  if(esr_status != ESR_SUCCESS) break;
2442	  /* in a real application we would save the nametag value someplace
2443	     along with the data->raw_waveform, then retrieve it and create a
2444	     nametag from that value */
2445	  esr_status = SR_NametagCreateFromValue( L("dummyID"),  value2, len2, &data->nametag);
2446	  FREE( value2);
2447	  value2 = NULL;
2448	}
2449	if ( esr_status != ESR_SUCCESS ) break;
2450
2451	esr_status = SR_RecognizerResultGetWaveform ( data->result, &data->raw_waveform, &data->raw_waveform_size );
2452	if( esr_status != ESR_SUCCESS ) break;
2453
2454	if ( data->raw_waveform )
2455	  {
2456	    process_status = srec_test_write_and_log_wave_form_file ( data, recognition_count );
2457
2458	    if ( process_status == 0 )
2459	      {
2460		esr_status = SR_RecognizerLogEvent ( data->recognizer, "ESRve" );
2461	      }
2462	    else {
2463	      esr_status = ESR_INVALID_STATE;
2464	    }
2465	  }
2466      }
2467    process_status = 0;
2468  } while(0);
2469  if( esr_status != ESR_SUCCESS) {
2470        process_status = -1;
2471        /* Log Here */
2472  }
2473  return ( process_status );
2474}
2475
2476
2477
2478int srec_test_process_recognition_fail ( ApplicationData *data )
2479    {
2480    int                 process_status;
2481    ESR_ReturnCode      esr_status;
2482    APPL_GRAMMAR_DATA   *grammar_data;
2483    ESR_BOOL                reason_status;
2484
2485    process_status = 0;
2486    srec_test_get_active_grammar_data ( data, &grammar_data );
2487    LPRINTF(L("*** no match in recognition***\n"));
2488
2489    if ( grammar_data->is_ve_grammar == ESR_TRUE )
2490        {
2491        data->nametag = NULL;
2492        LPRINTF ( L("VoiceEnrollement = FAILED : \n") );
2493        }
2494    esr_status = SR_RecognizerIsSignalClipping ( data->recognizer, &reason_status );
2495
2496    if ( esr_status == ESR_SUCCESS )
2497        {
2498        if ( reason_status == ESR_TRUE )
2499            LPRINTF ( L("- Signal is clipping\n") );
2500        }
2501    else
2502        {
2503        process_status = -1;
2504        /* Log Here */
2505        }
2506    esr_status = SR_RecognizerIsSignalDCOffset ( data->recognizer, &reason_status );
2507
2508    if ( esr_status == ESR_SUCCESS )
2509        {
2510        if ( reason_status == ESR_TRUE )
2511            LPRINTF ( L("- Signal is DC-offset\n") );
2512        }
2513    else
2514        {
2515        process_status = -1;
2516        /* Log Here */
2517        }
2518    esr_status = SR_RecognizerIsSignalNoisy ( data->recognizer, &reason_status );
2519
2520    if ( esr_status == ESR_SUCCESS )
2521        {
2522        if ( reason_status == ESR_TRUE )
2523            LPRINTF ( L("- Signal is noisy\n") );
2524        }
2525    else
2526        {
2527        process_status = -1;
2528        /* Log Here */
2529        }
2530    esr_status = SR_RecognizerIsSignalTooFewSamples ( data->recognizer, &reason_status );
2531
2532    if ( esr_status == ESR_SUCCESS )
2533        {
2534        if ( reason_status == ESR_TRUE )
2535            LPRINTF ( L("- Signal has too few samples\n") );
2536        }
2537    else
2538        {
2539        process_status = -1;
2540        /* Log Here */
2541        }
2542    esr_status = SR_RecognizerIsSignalTooManySamples ( data->recognizer, &reason_status );
2543
2544    if ( esr_status == ESR_SUCCESS )
2545        {
2546        if ( reason_status == ESR_TRUE )
2547            LPRINTF ( L("- Signal has too many samples\n") );
2548        }
2549    else
2550        {
2551        process_status = -1;
2552        /* Log Here */
2553        }
2554    esr_status = SR_RecognizerIsSignalTooQuiet ( data->recognizer, &reason_status );
2555
2556    if ( esr_status == ESR_SUCCESS )
2557        {
2558        if ( reason_status == ESR_TRUE )
2559            LPRINTF ( L("- Signal is too quiet\n") );
2560        }
2561    else
2562        {
2563        process_status = -1;
2564        /* Log Here */
2565        }
2566    srec_test_log_recognition_failure ( data );
2567    return ( process_status );
2568    }
2569
2570
2571
2572int srec_test_process_recognition_unsupported_case ( ApplicationData *data, FILE *results_file )
2573    {
2574    int process_status;
2575
2576    process_status = 0;
2577    LFPRINTF ( results_file, L("E: No results available\n") );
2578    LFPRINTF ( results_file, L("R: <FAILED>\n") );
2579    srec_test_log_recognition_failure ( data );
2580
2581    return ( process_status );
2582    }
2583
2584
2585
2586static void srec_test_log_recognition_failure ( ApplicationData *data )
2587    {
2588
2589    LPRINTF(L("----------------------------------------------\n"));
2590    LPRINTF(L("TRANSCRIPTION    : '%s'\n"), data->transcription);
2591    LPRINTF(L("<NO-RESULTS>\n"));
2592    LPRINTF(L("----------------------------------------------\n\n"));
2593    }
2594
2595
2596
2597int srec_test_process_results ( ApplicationData *data, SR_RecognizerStatus esr_recog_status,
2598                                FILE *results_file, size_t recognition_count )
2599    {
2600    int process_status;
2601
2602    switch ( esr_recog_status )
2603        {
2604        case SR_RECOGNIZER_EVENT_START_OF_UTTERANCE_TIMEOUT:
2605            process_status = srec_test_process_start_timeout ( data, results_file );
2606            break;
2607
2608        case SR_RECOGNIZER_EVENT_RECOGNITION_RESULT:
2609            process_status = srec_test_process_recognition ( data, results_file, recognition_count );
2610            break;
2611
2612        case SR_RECOGNIZER_EVENT_NO_MATCH:
2613            process_status = srec_test_process_recognition_fail ( data );
2614            break;
2615
2616        default:
2617            process_status = srec_test_process_recognition_unsupported_case ( data, results_file );
2618            break;
2619        }
2620    return ( process_status );
2621    }
2622
2623
2624
2625int srec_test_log_reco_from_file_data ( SR_Grammar *active_grammar, ApplicationData *data, LCHAR *waveform, LCHAR *bos, LCHAR *eos, LCHAR *transcription )
2626    {
2627    int             log_status;
2628    ESR_ReturnCode  esr_status;
2629    size_t          result_count;
2630    ESR_BOOL            got_results;
2631    size_t          transcription_length;
2632
2633    log_status = 0;
2634    LSPRINTF ( data->transcription, "%s", transcription );
2635    transcription_length = LSTRLEN ( data->transcription );
2636
2637    while ( ( *( data->transcription + transcription_length - 1 ) == '\n' ) ||
2638            ( *( data->transcription + transcription_length - 1 ) == '\r' ) )
2639        {
2640	  *( data->transcription + transcription_length - 1 ) = '\0';
2641	  transcription_length--;
2642        }
2643  LPRINTF ( L("D: %s\nC: %s\n"), waveform, data->transcription );
2644
2645    esr_status = SR_GrammarCheckParse ( active_grammar, data->transcription, 0, &result_count );
2646
2647    if ( esr_status == ESR_SUCCESS )
2648        {
2649        if ( result_count > 0 )
2650            {
2651#ifdef ACCURACY_TESTING
2652	      LCHAR meaning[MAX_LINE_LENGTH] = { 0 };
2653	      size_t len  = MAX_LINE_LENGTH;
2654	      got_results = ESR_TRUE;
2655	      LPRINTF ( L("Sem (%lu):  invocab=1\n"), (unsigned long)result_count );
2656	      srec_test_parse ( active_grammar, transcription, meaning, &len);
2657	      if(LSTRLEN(meaning)>0)
2658		LPRINTF( L("CSem: %s\n"), meaning);
2659#else
2660	      got_results = ESR_TRUE;
2661	      LPRINTF ( L("Sem (%lu):  invocab=1\n"), (unsigned long)result_count );
2662#endif
2663            }
2664        else
2665            {
2666            got_results = ESR_FALSE;
2667            LPRINTF ( L("Sem:  <NO INTERPRETATION FOUND>\n") );
2668            LPRINTF ( L("CSem: <NO INTERPRETATION FOUND>\n") );
2669	    }
2670        esr_status = SR_RecognizerLogWaveformData ( data->recognizer, waveform, data->transcription, atof ( bos ), atof ( eos ), got_results );
2671
2672        if ( esr_status != ESR_SUCCESS )
2673            {
2674            log_status = -1;
2675            /* Log Here */
2676            }
2677        }
2678    else
2679        {
2680        log_status = -1;
2681        /* Log Here */
2682        }
2683
2684    return ( log_status );
2685    }
2686
2687
2688
2689static int srec_test_recognize_pcm_file ( SR_Grammar *active_grammar, ApplicationData *data, FILE *results_file,
2690                                          LCHAR *command_text, size_t *recognition_count )
2691    {
2692    int                     recognize_status;
2693    ESR_ReturnCode          esr_status;
2694    SR_RecognizerStatus     esr_recog_status;
2695    SR_RecognizerResultType result_type;
2696    LCHAR                   *transcription;
2697    LCHAR                   waveform [MAX_LINE_LENGTH];
2698    LCHAR                   bos [MAX_LINE_LENGTH];
2699    LCHAR                   eos [MAX_LINE_LENGTH];
2700    PFile                   *waveform_file;
2701    ESR_BOOL                    hit_eof;
2702
2703    if ( active_grammar != NULL )
2704        {
2705        recognize_status = srec_test_get_three_command_items ( command_text, MAX_LINE_LENGTH, waveform,
2706                                                              MAX_LINE_LENGTH, bos, MAX_LINE_LENGTH,
2707                                                              eos, NULL, &transcription );
2708	while( *transcription == ' ' ) transcription++;
2709        if ( recognize_status == 0 )
2710            {
2711            recognize_status = srec_test_log_reco_from_file_data ( active_grammar, data, waveform, bos, eos, transcription );
2712
2713            if ( recognize_status == 0 )
2714                {
2715				if(LSTRCMP(bos,"0") || LSTRCMP(eos,"0")) {
2716					PLogError( ("Error: non-zero BOS and non-zero EOS are not supported\n"));
2717					return -1;
2718				}
2719                recognize_status = srec_test_open_pcm_file ( waveform, &waveform_file );
2720
2721                if ( recognize_status == 0 )
2722                    {
2723                    if ( ( data->forced_rec_mode == ForcedRecModeOn ) || ( data->forced_rec_mode == ForcedRecModeOneTime ) )
2724                        SR_GrammarAllowOnly ( active_grammar, data->transcription );
2725                    esr_status = SR_RecognizerStart ( data->recognizer );
2726
2727                    if ( esr_status == ESR_SUCCESS )
2728                        {
2729                        ( *recognition_count )++;
2730                        hit_eof = ESR_FALSE;
2731
2732                        do
2733                            {
2734                            recognize_status = srec_test_get_audio_from_file ( waveform_file, data, &hit_eof );
2735
2736                            if ( recognize_status == 0 )
2737                                recognize_status = srec_test_feed_recognizer ( data, hit_eof, &esr_recog_status, &result_type  );
2738                            }
2739                        while ( ( hit_eof == ESR_FALSE ) && ( result_type != SR_RECOGNIZER_RESULT_TYPE_COMPLETE ) && ( recognize_status == 0 ) );
2740
2741                        if ( recognize_status == 0 )
2742                            {
2743                            recognize_status = srec_test_flush_audio ( data, &esr_recog_status, &result_type );
2744
2745                            if ( recognize_status == 0 )
2746                                {
2747                                recognize_status = srec_test_process_results ( data, esr_recog_status,
2748                                                                               results_file, *recognition_count );
2749                                }
2750                            }
2751                        esr_status = SR_RecognizerStop ( data->recognizer );
2752
2753                        if (esr_status == ESR_SUCCESS )
2754                            {
2755                            LPRINTF ( L("Recognizer has been stopped\n") );
2756                            }
2757                        else
2758                            {
2759                            recognize_status = -1;
2760                            LPRINTF ( L("Recognizer has failed to stop\n") );
2761                            }
2762                        }
2763                    if ( data->forced_rec_mode == ForcedRecModeOneTime )
2764                        {
2765                        data->forced_rec_mode = ForcedRecModeOff;
2766                        SR_GrammarAllowAll ( active_grammar );
2767                        }
2768                    srec_test_close_audio_file ( waveform_file );
2769                    }
2770                }
2771            }
2772        }
2773     else
2774         {
2775         recognize_status = -1;
2776         /* Log Here */
2777         }
2778     return ( recognize_status );
2779     }
2780
2781
2782
2783static int srec_test_recognize_nist_file ( SR_Grammar *active_grammar, ApplicationData *data, FILE *results_file,
2784                                           LCHAR *command_text, size_t *recognition_count )
2785    {
2786    int                     recognize_status;
2787    ESR_ReturnCode          esr_status;
2788    SR_RecognizerStatus     esr_recog_status;
2789    SR_RecognizerResultType result_type;
2790    LCHAR                   *transcription;
2791    LCHAR                   waveform [MAX_LINE_LENGTH];
2792    LCHAR                   bos [MAX_LINE_LENGTH];
2793    LCHAR                   eos [MAX_LINE_LENGTH];
2794    PFile                   *waveform_file;
2795    ESR_BOOL                    hit_eof;
2796
2797    if ( active_grammar != NULL )
2798        {
2799        recognize_status = srec_test_get_three_command_items ( command_text, MAX_LINE_LENGTH, waveform,
2800                                                              MAX_LINE_LENGTH, bos, MAX_LINE_LENGTH,
2801                                                              eos, NULL, &transcription );
2802	while( *transcription == ' ' ) transcription++;
2803        if ( recognize_status == 0 )
2804            {
2805            recognize_status = srec_test_log_reco_from_file_data ( active_grammar, data, waveform, bos, eos, transcription );
2806
2807            if ( recognize_status == 0 )
2808                {
2809				if(LSTRCMP(bos,"0") || LSTRCMP(eos,"0")) {
2810					PLogError( ("Error: non-zero BOS and non-zero EOS are not supported\n"));
2811					return -1;
2812				}
2813                recognize_status = srec_test_open_nist_file ( waveform, &waveform_file );
2814
2815                if ( recognize_status == 0 )
2816                    {
2817                    if ( ( data->forced_rec_mode == ForcedRecModeOn ) || ( data->forced_rec_mode == ForcedRecModeOneTime ) )
2818                        SR_GrammarAllowOnly ( active_grammar, data->transcription );
2819                    esr_status = SR_RecognizerStart ( data->recognizer );
2820
2821                    if ( esr_status == ESR_SUCCESS )
2822                        {
2823                        ( *recognition_count )++;
2824                        hit_eof = ESR_FALSE;
2825
2826                        do
2827                            {
2828                            recognize_status = srec_test_get_audio_from_file ( waveform_file, data, &hit_eof );
2829
2830                            if ( recognize_status == 0 )
2831                                recognize_status = srec_test_feed_recognizer ( data, hit_eof, &esr_recog_status, &result_type  );
2832                            }
2833                        while ( ( hit_eof == ESR_FALSE ) && ( result_type != SR_RECOGNIZER_RESULT_TYPE_COMPLETE ) && ( recognize_status == 0 ) );
2834
2835                        if ( recognize_status == 0 )
2836                            {
2837                            recognize_status = srec_test_flush_audio ( data, &esr_recog_status, &result_type );
2838
2839                            if ( recognize_status == 0 )
2840                                {
2841                                recognize_status = srec_test_process_results ( data, esr_recog_status,
2842                                                                               results_file, *recognition_count );
2843                                }
2844                            }
2845                        }
2846                        esr_status = SR_RecognizerStop ( data->recognizer );
2847
2848                        if (esr_status == ESR_SUCCESS )
2849                            {
2850                            LPRINTF ( L("Recognizer has been stopped\n") );
2851                            }
2852                        else
2853                            {
2854                            recognize_status = -1;
2855                            LPRINTF ( L("Recognizer has failed to stop\n") );
2856                            }
2857                    if ( data->forced_rec_mode == ForcedRecModeOneTime )
2858                        {
2859                        data->forced_rec_mode = ForcedRecModeOff;
2860                        SR_GrammarAllowAll ( active_grammar );
2861                        }
2862                    srec_test_close_audio_file ( waveform_file );
2863                    }
2864                }
2865            }
2866        }
2867     else
2868         {
2869         recognize_status = -1;
2870         /* Log Here */
2871         }
2872     return ( recognize_status );
2873     }
2874
2875
2876
2877#define STACK_SIZE (1024 * 200)
2878
2879#ifdef _WIN32
2880/* disable optimization for the next functions as the compiler optimizes away the assignment to mySTACK[i] */
2881#pragma optimize("", off)
2882
2883static void initStack()
2884{
2885  int mySTACK[STACK_SIZE];
2886 {
2887   /* This extra block is to ensure that local variables of the function occur after buffer mySTACK. */
2888   int i;
2889
2890   for (i = 0; i < STACK_SIZE; ++i)
2891   {
2892     mySTACK[i] = 0xDEADBEEF;
2893   }
2894 }
2895}
2896
2897static int analyzeStack()
2898{
2899  int mySTACK[STACK_SIZE];
2900 {
2901
2902   /* This extra block is to ensure that local variables of the function occur after buffer mySTACK. */
2903   int i, j;
2904
2905   for (i = STACK_SIZE - 1; i >= 0; --i)
2906   {
2907     if (mySTACK[i] == 0xDEADBEEF)
2908     {
2909       /* This might be a candidate for the end of stack marker, or it could be
2910          some value that is equal to our marker.  To ensure reliability of
2911          this candidate, we will make sure that all remaining entries int the
2912          stack are also equal to DEADBEEF.
2913       */
2914       for (j = i - 1; j >= 0; --j)
2915       {
2916         if (mySTACK[j] != 0xDEADBEEF)
2917         {
2918           i = j;
2919           break;
2920         }
2921       }
2922       if (j < 0) break;
2923     }
2924   }
2925
2926   if (i < 0)
2927     return -1;
2928   else
2929     return (STACK_SIZE - 1 - i) * sizeof(int);
2930 }
2931}
2932
2933/* restore optmization settings to what they used to be. */
2934#pragma optimize("", on)
2935#endif
2936
2937
2938#ifdef ACCURACY_TESTING
2939static void srec_test_clean_up_sentence ( char* sentence )
2940    {
2941    int                         clean_up_status;
2942    int                         sentence_finished;
2943    SENTENCE_CLEANING_STATES    current_state;
2944    char                        *current_input;
2945    char                        *current_output;
2946
2947    clean_up_status = 0;
2948    sentence_finished = 0;
2949    current_state = SENTENCE_BEGIN;
2950    current_input = sentence;
2951    current_output = sentence;
2952    strtok( sentence, "\n\r");
2953
2954    do
2955        {
2956        switch ( *current_input )
2957            {
2958            case '\0':
2959                switch ( current_state )
2960                    {
2961                    case SENTENCE_BEGIN:
2962                        break;
2963
2964                    case SENTENCE_BEGIN_BRACKET_BEGIN:  /* Is this error condition */
2965                        *current_output = '\0';
2966                        clean_up_status = -1;
2967                        break;
2968
2969                    case SENTENCE_BEGIN_BRACKET_END:
2970                        *current_output = '\0';
2971                        break;
2972
2973                    case SENTENCE_MIDDLE:
2974                        *current_output = '\0';
2975                        break;
2976
2977                    case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */
2978                        *( current_output - 1 ) = '\0';
2979                        clean_up_status = -1;
2980                        break;
2981
2982                    case SENTENCE_MIDDLE_BRACKET_END:
2983                        *( current_output - 1 ) = '\0';
2984                        break;
2985
2986                    case SENTENCE_MIDDLE_WITH_SPACE:
2987                        *( current_output - 1 ) = '\0';
2988                        break;
2989
2990                    default:
2991                        *current_output = '\0';
2992                        /* Log error */
2993                        break;
2994                    }
2995                sentence_finished = 1;
2996                break;
2997
2998            case ' ':
2999                switch ( current_state )
3000                    {
3001                    case SENTENCE_BEGIN:
3002                        break;
3003
3004                    case SENTENCE_BEGIN_BRACKET_BEGIN:
3005                        break;
3006
3007                    case SENTENCE_BEGIN_BRACKET_END:
3008                        break;
3009
3010                    case SENTENCE_MIDDLE:
3011                        *current_output = ' ';
3012                        current_output++;
3013                        current_state = SENTENCE_MIDDLE_WITH_SPACE;
3014                        break;
3015
3016                    case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */
3017                        break;
3018
3019                    case SENTENCE_MIDDLE_BRACKET_END:
3020                        break;
3021
3022                    case SENTENCE_MIDDLE_WITH_SPACE:
3023                        break;
3024
3025                    default:
3026                        *current_output = '\0';
3027                        clean_up_status = -1;
3028                        /* Log error */
3029                        break;
3030                    }
3031                current_input++;
3032                break;
3033
3034            case '[':
3035                switch ( current_state )
3036                    {
3037                    case SENTENCE_BEGIN:
3038                        current_state = SENTENCE_BEGIN_BRACKET_BEGIN;
3039                        break;
3040
3041                    case SENTENCE_BEGIN_BRACKET_BEGIN:
3042                        *current_output = '\0';
3043                        clean_up_status = -1;
3044                        /* Log error */
3045                        break;
3046
3047                    case SENTENCE_BEGIN_BRACKET_END:
3048                        current_state = SENTENCE_BEGIN_BRACKET_BEGIN;
3049                        break;
3050
3051                    case SENTENCE_MIDDLE:
3052                        *current_output = ' ';
3053                        current_output++;
3054                        current_state = SENTENCE_MIDDLE_BRACKET_BEGIN;
3055                        break;
3056
3057                    case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */
3058                        *current_output = '\0';
3059                        clean_up_status = -1;
3060                        /* Log error */
3061                        break;
3062
3063                    case SENTENCE_MIDDLE_BRACKET_END:
3064                        current_state = SENTENCE_MIDDLE_BRACKET_BEGIN;
3065                        break;
3066
3067                    case SENTENCE_MIDDLE_WITH_SPACE:
3068                        current_state = SENTENCE_MIDDLE_BRACKET_BEGIN;
3069                        break;
3070
3071                    default:
3072                        *current_output = '\0';
3073                        clean_up_status = -1;
3074                        /* Log error */
3075                        break;
3076                    }
3077                current_input++;
3078                break;
3079
3080            case ']':
3081                switch ( current_state )
3082                    {
3083                    case SENTENCE_BEGIN:
3084                        *current_output = '\0';
3085                        clean_up_status = -1;
3086                        /* Log error */
3087                        break;
3088
3089                    case SENTENCE_BEGIN_BRACKET_BEGIN:
3090                        current_state = SENTENCE_BEGIN_BRACKET_END;
3091                        break;
3092
3093                    case SENTENCE_BEGIN_BRACKET_END:
3094                        *current_output = '\0';
3095                        clean_up_status = -1;
3096                        /* Log error */
3097                        break;
3098
3099                    case SENTENCE_MIDDLE:
3100                        *current_output = '\0';
3101                        clean_up_status = -1;
3102                        /* Log error */
3103                        break;
3104
3105                    case SENTENCE_MIDDLE_BRACKET_BEGIN: /* Is this error condition */
3106                        current_state = SENTENCE_MIDDLE_BRACKET_END;
3107                        break;
3108
3109                    case SENTENCE_MIDDLE_BRACKET_END:
3110                        *current_output = '\0';
3111                        clean_up_status = -1;
3112                        /* Log error */
3113                        break;
3114
3115                    case SENTENCE_MIDDLE_WITH_SPACE:
3116                        *current_output = '\0';
3117                        clean_up_status = -1;
3118                        /* Log error */
3119                        break;
3120
3121                    default:
3122                        *current_output = '\0';
3123                        clean_up_status = -1;
3124                        /* Log error */
3125                        break;
3126                    }
3127                current_input++;
3128                break;
3129
3130            default:
3131                switch ( current_state )
3132                    {
3133                    case SENTENCE_BEGIN:
3134                        *current_output = *current_input;
3135                        current_output++;
3136                        current_state = SENTENCE_MIDDLE;
3137                        break;
3138
3139                    case SENTENCE_BEGIN_BRACKET_BEGIN:
3140                        break;
3141
3142                    case SENTENCE_BEGIN_BRACKET_END:
3143                        *current_output = *current_input;
3144                        current_output++;
3145                        current_state = SENTENCE_MIDDLE;
3146                        break;
3147
3148                    case SENTENCE_MIDDLE:
3149                        *current_output = *current_input;
3150                        current_output++;
3151                        current_state = SENTENCE_MIDDLE;
3152                        break;
3153
3154                    case SENTENCE_MIDDLE_BRACKET_BEGIN:
3155                        break;
3156
3157                    case SENTENCE_MIDDLE_BRACKET_END:
3158                        *current_output = *current_input;
3159                        current_output++;
3160                        current_state = SENTENCE_MIDDLE;
3161                        break;
3162
3163                    case SENTENCE_MIDDLE_WITH_SPACE:
3164                        *current_output = *current_input;
3165                        current_output++;
3166                        current_state = SENTENCE_MIDDLE;
3167                        break;
3168
3169                    default:
3170                        *current_output = '\0';
3171                        clean_up_status = -1;
3172                        /* Log error */
3173                        break;
3174                    }
3175                current_input++;
3176                break;
3177            }
3178        }
3179    while ( ( sentence_finished == 0 ) && ( clean_up_status == 0 ) );
3180    }
3181
3182
3183
3184ESR_ReturnCode srec_test_parse ( SR_Grammar* grammar, const LCHAR* trans, LCHAR* meaning, size_t *len )
3185    {
3186    ESR_ReturnCode      parse_status;
3187    char                cleaned_trans[TRANSCRIPTION_SIZE];
3188    SR_SemanticResult   *semanticResults;
3189    size_t              result_count;
3190
3191    result_count = 0;
3192    strcpy( cleaned_trans, trans );
3193    srec_test_clean_up_sentence ( cleaned_trans );
3194
3195  /* create the result holders, initially not greater than MAX */
3196    parse_status = SR_SemanticResultCreate ( &semanticResults );
3197
3198    if ( parse_status == ESR_SUCCESS )
3199        {
3200        result_count = 1;
3201        parse_status = SR_SemanticProcessor_Flush( ( (SR_GrammarImpl*)grammar )->semproc );
3202
3203        if ( parse_status == ESR_SUCCESS )
3204            {
3205            parse_status = SR_SemanticProcessor_SetParam( ((SR_GrammarImpl*)grammar)->semproc, L("literal"), cleaned_trans );
3206
3207            if ( parse_status == ESR_SUCCESS )
3208                {
3209                parse_status = SR_GrammarCheckParse( grammar, cleaned_trans, &semanticResults, (size_t*)&result_count );
3210
3211                if ( parse_status == ESR_SUCCESS )
3212                    {
3213                    if ( result_count < 1 )
3214                        {
3215                        LSTRCPY ( meaning, L("") );
3216                        SR_SemanticResultDestroy( semanticResults);
3217                        parse_status = ESR_NO_MATCH_ERROR;
3218                        }
3219                    else
3220                        {
3221                        parse_status = SR_SemanticResultGetValue ( semanticResults, "meaning", meaning, len);
3222                        SR_SemanticResultDestroy( semanticResults);
3223                        }
3224                    }
3225                }
3226            }
3227        }
3228    return ( parse_status );
3229    }
3230#endif
3231
3232
3233static int srec_test_get_run_params ( unsigned int *num_shutdown_loops, unsigned int *num_continuous_run_loops )
3234    {
3235    int get_status;
3236
3237    get_status = get_num_srec_test_shutdown_times ( num_shutdown_loops );
3238
3239    if ( get_status == 0 )
3240                get_status = get_num_srec_test_continuous_loops ( num_continuous_run_loops );
3241
3242    return ( get_status );
3243    }
3244
3245
3246
3247static int srec_test_shutdown_application_data ( ApplicationData *applicationData )
3248    {
3249    int init_status;
3250    int i;
3251
3252    init_status = 0;
3253
3254    applicationData->argc = 0;
3255    applicationData->argv = NULL;
3256    applicationData->locale = -1;
3257
3258    if (applicationData->recognizer != NULL)
3259        {
3260        srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup Recognizer") );
3261/*        SR_RecognizerLogSessionEnd ( applicationData->recognizer );
3262        SR_RecognizerDestroy ( applicationData->recognizer );*/
3263        applicationData->recognizer = NULL;
3264        applicationData->result = NULL;                 // this was deallocated by SR_RecognizerDestroy()
3265        }
3266
3267    if (applicationData->vocabulary != NULL)
3268        {
3269        srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup Vocabulary") );
3270/*        SR_VocabularyDestroy(applicationData->vocabulary);
3271        applicationData->vocabulary = NULL;*/
3272        }
3273
3274    if (applicationData->nametag != NULL)
3275        {
3276        srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup NameTag") );
3277/*        SR_NametagDestroy(applicationData->nametag);
3278        applicationData->nametag = NULL;*/
3279        }
3280
3281    if (applicationData->nametags != NULL)
3282        {
3283        srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup NameTagSet") );
3284/*        SR_NametagsDestroy(applicationData->nametags);
3285        applicationData->nametags = NULL;*/
3286        }
3287
3288    for (i = 0; i < applicationData->grammarCount; ++i)
3289        srec_test_log_error ( 0, ERROR_LEVEL_HIGH, L("Failed To Cleanup Grammar") );
3290/*
3291        if ( applicationData->grammars [i] != NULL )
3292            {
3293            printf ( "!!!!!!!!  %d Grammars Not Destroyed !!!!!!!!!!!!\n", i );
3294            SR_GrammarDestroy(applicationData->grammars[i]);
3295            applicationData->grammars[i] = NULL;
3296            }
3297        }
3298
3299    applicationData->activeGrammar = -1;
3300    applicationData->activeRule = NULL;
3301    applicationData->voiceEnrollment = NULL;
3302*/
3303    applicationData->raw_waveform = NULL;
3304    applicationData->raw_waveform_filename [0] = L( '\0' );
3305    applicationData->raw_waveform_file = NULL;
3306    applicationData->transcription[0] = L( '\0' );
3307    applicationData->forced_rec_mode = ForcedRecModeNotSet;
3308
3309    return ( init_status );
3310    }
3311
3312
3313
3314static int srec_test_init_application_data ( ApplicationData *applicationData, int arg_count, LCHAR *arg_vals [] )
3315    {
3316    int init_status;
3317
3318    init_status = 0;
3319    applicationData->argc = arg_count;
3320    applicationData->argv = arg_vals;
3321    applicationData->locale = -1;
3322    applicationData->recognizer = NULL;
3323    applicationData->result = NULL;
3324    applicationData->vocabulary = NULL;
3325    applicationData->nametag = NULL;
3326    applicationData->nametags = NULL;
3327    applicationData->grammarCount = 0;  /* No need to initialize arrays, index is 0 */
3328    applicationData->active_grammar_num = -1;
3329/*    applicationData->activeRule = NULL;
3330    applicationData-> = applicationData->voiceEnrollment = NULL;*/
3331    applicationData->audio_buffer_requested_size = DEFAULT_AUDIO_BUFFER_SIZE;
3332    applicationData->raw_waveform = NULL;
3333    applicationData->raw_waveform_filename [0] = L( '\0' );
3334    applicationData->raw_waveform_file = NULL;
3335    applicationData->transcription[0] = L( '\0' );
3336    applicationData->forced_rec_mode = ForcedRecModeNotSet;
3337    return ( init_status );
3338    }
3339
3340
3341static int srec_test_run_test_shutdown_session ( ApplicationData *applicationData )
3342    {
3343    int shutdown_status;
3344
3345    shutdown_status = 0;
3346
3347    SR_RecognizerUnsetup ( applicationData->recognizer); // releases acoustic models
3348    SR_RecognizerDestroy ( applicationData->recognizer );
3349    applicationData->recognizer = NULL;
3350    ShutdownSession ( );
3351
3352    return ( shutdown_status );
3353    }
3354
3355
3356
3357static int srec_test_run_test_init_session ( ApplicationData *applicationData )
3358{
3359  int             run_status;
3360  ESR_ReturnCode  esr_status;
3361
3362  run_status = 0;
3363  LPRINTF(L("\nCreate Recognizer:\n"));
3364  LPRINTF(L("    InitSession()\n"));
3365  esr_status = InitSession ( applicationData->argc, applicationData->argv );
3366
3367  if ( esr_status == ESR_SUCCESS )
3368    {
3369      LPRINTF(L("    SR_RecognizerCreate()\n"));
3370      esr_status = SR_RecognizerCreate ( &applicationData->recognizer );
3371
3372      if ( esr_status != ESR_SUCCESS )
3373    {
3374      ShutdownSession ( );
3375      run_status = -1;
3376      /* Log Here */
3377    } else {
3378      LPRINTF(L("    SR_RecognizerSetup()\n"));
3379      esr_status = SR_RecognizerSetup ( applicationData->recognizer);
3380      if ( esr_status != ESR_SUCCESS )
3381        {
3382          ShutdownSession ( );
3383          run_status = -1;
3384          /* Log Here */
3385        }
3386    }
3387    }
3388  return ( run_status );
3389}
3390
3391
3392static int srec_test_run_test_shutdown_vocab_grammar ( ApplicationData *applicationData )
3393    {
3394    int shutdown_status;
3395
3396    shutdown_status = 0;
3397    SR_RecognizerLogSessionEnd ( applicationData->recognizer );
3398/*    SR_GrammarDestroy ( applicationData->grammars [0].grammar );
3399    applicationData->grammars [0].grammar = NULL;*/
3400    SR_VocabularyDestroy ( applicationData->vocabulary );
3401    applicationData->vocabulary = NULL;
3402
3403    return ( shutdown_status );
3404    }
3405
3406
3407
3408static int srec_test_run_test_init_vocab_grammar ( ApplicationData *applicationData )
3409    {
3410    int             run_status;
3411    ESR_ReturnCode  esr_status;
3412    LCHAR           filename[P_PATH_MAX];
3413    size_t          len;
3414
3415    run_status = 0;
3416   /* Create vocabulary object and associate with grammar */
3417    LPRINTF(L("Create vocabulary object and associate with grammar:\n"));
3418    len = P_PATH_MAX;
3419    esr_status = ESR_SessionGetLCHAR ( L("cmdline.vocabulary"), filename, &len );
3420
3421    if ( esr_status == ESR_SUCCESS )
3422        {
3423        LPRINTF(L("    SR_VocabularyLoad()\n"));
3424        esr_status = SR_VocabularyLoad ( filename, &applicationData->vocabulary );
3425
3426        if ( esr_status == ESR_SUCCESS )
3427            {
3428            LPRINTF(L("    SR_VocabularyGetLanguage()\n"));
3429            esr_status =  SR_VocabularyGetLanguage ( applicationData->vocabulary, &applicationData->locale );
3430
3431            if ( esr_status == ESR_SUCCESS )
3432                {
3433                /* start a new log session */
3434                LPRINTF( L("Start a new log session:\n") );
3435                LPRINTF( L("    SR_RecognizerLogSessionStart()\n") );
3436                esr_status = SR_RecognizerLogSessionStart ( applicationData->recognizer, L("SRecTest.session1") );
3437
3438                if ( esr_status != ESR_SUCCESS )
3439                    {
3440                    SR_VocabularyDestroy ( applicationData->vocabulary );
3441                    applicationData->vocabulary = NULL;
3442                    run_status = -1;
3443                    /* Log here */
3444                    }
3445                }
3446            else
3447                {
3448                SR_VocabularyDestroy ( applicationData->vocabulary );
3449                applicationData->vocabulary = NULL;
3450                run_status = -1;
3451                /* Log Here */
3452                }
3453            }
3454        else
3455            {
3456            run_status = -1;
3457            /* Log Here */
3458            }
3459        }
3460    else
3461        {
3462        run_status = -1;
3463        /* Log Here */
3464        }
3465    return ( run_status );
3466    }
3467
3468
3469
3470static int srec_test_run_test_shutdown ( ApplicationData *applicationData )
3471    {
3472    int shutdown_status;
3473
3474    shutdown_status = srec_test_run_test_shutdown_vocab_grammar ( applicationData );
3475
3476        if ( shutdown_status == 0 )
3477            shutdown_status = srec_test_run_test_shutdown_session ( applicationData );
3478    return ( shutdown_status );
3479    }
3480
3481
3482static int srec_test_run_test_init ( ApplicationData *applicationData )
3483{
3484  int run_status;
3485
3486  run_status = srec_test_run_test_init_session ( applicationData );
3487
3488  if ( run_status == 0 )
3489    {
3490      run_status = srec_test_run_test_init_vocab_grammar ( applicationData );
3491
3492      if ( run_status != 0 )
3493    {
3494      srec_test_run_test_shutdown_session ( applicationData );
3495    }
3496    }
3497  else
3498    {
3499      srec_test_run_test_shutdown_session ( applicationData );
3500    }
3501  return ( run_status );
3502}
3503
3504
3505
3506static int srec_test_run_test_execute ( ApplicationData *applicationData )
3507    {
3508    int             run_status;
3509    ESR_ReturnCode  esr_status;
3510
3511    run_status = 0;
3512    applicationData->nametag = NULL;
3513
3514    LPRINTF(L("Recognize:\n"));
3515    LPRINTF(L("    SR_NametagsCreate()\n"));
3516    esr_status = SR_NametagsCreate ( &applicationData->nametags );
3517
3518    if ( esr_status == ESR_SUCCESS )
3519        {
3520        run_status = srec_test_process_commands ( applicationData );
3521        SR_NametagsDestroy ( applicationData->nametags );
3522        applicationData->nametags = NULL;
3523
3524        if ( run_status != 0 )
3525            {
3526            /* Log Here */
3527            }
3528        }
3529    else
3530        {
3531        run_status = -1;
3532        /* Log Here */
3533        }
3534    return ( run_status );
3535    }
3536
3537
3538
3539static int srec_test_run_test ( ApplicationData *applicationData )
3540    {
3541    int run_status;
3542
3543    run_status = srec_test_run_test_init ( applicationData );
3544
3545    if ( run_status == 0 )
3546        {
3547        run_status = srec_test_run_test_execute ( applicationData );
3548        srec_test_run_test_shutdown ( applicationData );
3549        }
3550    return ( run_status );
3551    }
3552
3553
3554
3555static int srec_test_shutdown_memory_system ( void )
3556    {
3557    int shutdown_status;
3558
3559    shutdown_status = 0;
3560    PMemShutdown ( );
3561
3562    return ( shutdown_status );
3563    }
3564
3565
3566
3567static int srec_test_init_memory_system ( unsigned int srec_test_heap_size )
3568    {
3569    int             init_status;
3570    ESR_ReturnCode  esr_status;
3571
3572    init_status = 0;
3573    esr_status = PMemInit ( );
3574
3575    if ( esr_status == ESR_SUCCESS )
3576        {
3577        PSTACK_SIZE_INIT ( );   /* I don't know what this is, it should probably have a status */
3578        }
3579    else
3580        {
3581        init_status = -1;
3582        /* Log Here */
3583        }
3584    return ( init_status );
3585    }
3586
3587
3588
3589static int srec_test_shutdown_file_system ( void )
3590    {
3591    int shutdown_status;
3592
3593    shutdown_status = 0;
3594
3595    return ( shutdown_status );
3596    }
3597
3598
3599
3600static int srec_test_init_file_system ( int arg_count, LCHAR *arg_vals [] )
3601    {
3602    int             init_status;
3603
3604    init_status = 0;
3605    return ( init_status );
3606    }
3607
3608
3609
3610static int srec_test_shutdown_logging_system ( PLogger *logger )
3611    {
3612    int shutdown_status;
3613
3614    shutdown_status = 0;
3615    PLogShutdown ( );
3616
3617    return ( shutdown_status );
3618    }
3619
3620
3621
3622static int srec_test_init_logging_system ( int arg_count, LCHAR *arg_vals [], PLogger *logger )
3623    {
3624    int             init_status;
3625    ESR_ReturnCode  esr_status;
3626
3627    init_status = 0;
3628    esr_status = PLogCreateFileLogger ( PSTDOUT, &logger );
3629
3630    if ( esr_status == ESR_SUCCESS )
3631        {
3632        esr_status = PLogInit ( logger, 0 );
3633
3634        if ( esr_status != ESR_SUCCESS )
3635            {
3636/*          pfclose ( (struct PFile_t *)logger );       I'm not sure this is correct, check with Gili */
3637            init_status = -1;
3638            /* Log Here */
3639            }
3640        }
3641    else
3642        {
3643        init_status = -1;
3644        /* Log Here */
3645        }
3646    return ( init_status );
3647    }
3648
3649
3650
3651static int srec_test_shutdown_system ( PLogger *logger )
3652    {
3653    int shutdown_status;
3654
3655    shutdown_status = srec_test_shutdown_logging_system ( logger );
3656
3657    if ( shutdown_status == 0 )
3658        {
3659        shutdown_status = srec_test_shutdown_file_system ( );
3660
3661        if ( shutdown_status == 0 )
3662            shutdown_status = srec_test_shutdown_memory_system ( );
3663        }
3664    return ( shutdown_status );
3665    }
3666
3667
3668
3669
3670static int srec_test_init_system ( unsigned int srec_test_heap_size, PLogger* logger, int arg_count, LCHAR *arg_vals [] )
3671    {
3672    int init_status;
3673
3674   /* register signal handler so cleanup works on CTRL-C (Win32) */
3675#ifdef _WIN32
3676    signal(SIGINT, signal_handler_SIGINT);
3677#endif
3678    init_status = srec_test_init_memory_system ( srec_test_heap_size );
3679
3680    if ( init_status == 0 )
3681        {
3682        init_status = srec_test_init_file_system ( arg_count, arg_vals );
3683
3684        if ( init_status == 0 )
3685            {
3686            init_status = srec_test_init_logging_system ( arg_count, arg_vals, logger );
3687
3688            if ( init_status != 0 )
3689                {
3690                srec_test_shutdown_file_system ( );
3691                srec_test_shutdown_memory_system ( );
3692                }
3693            }
3694        else
3695            {
3696            srec_test_shutdown_memory_system ( );
3697            }
3698        }
3699    return ( init_status );
3700    }
3701
3702
3703
3704#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32)
3705_CrtMemState s0,s1,s2;
3706
3707
3708
3709void OutputDivider( void )
3710    {
3711
3712    _RPT1(_CRT_WARN, "%s", "********************************************************************\n");
3713    }
3714
3715
3716
3717void OutputNewline( void )
3718    {
3719
3720    _RPT1(_CRT_WARN, "%s", "\n");
3721    }
3722#endif
3723
3724
3725
3726#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32)
3727void initialize_heap_logging ( void )
3728    {
3729
3730     _CrtSetReportMode(_CRT_WARN,   _CRTDBG_MODE_FILE);
3731     _CrtSetReportFile(_CRT_WARN,   _CRTDBG_FILE_STDOUT);
3732     _CrtSetReportMode(_CRT_ERROR,  _CRTDBG_MODE_FILE);
3733     _CrtSetReportFile(_CRT_ERROR,  _CRTDBG_FILE_STDOUT);
3734     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
3735     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
3736     _CrtMemCheckpoint(&s0);
3737     }
3738#endif
3739
3740
3741
3742#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32)
3743void execute_heap_logging ( void )
3744    {
3745    OutputNewline();
3746    OutputDivider();
3747    _RPT1(_CRT_WARN, "%s", "After cleanup, before exiting main()\n");
3748    OutputDivider();
3749    _CrtMemCheckpoint(&s1);
3750    _CrtMemDifference(&s2, &s0, &s1);
3751    _CrtMemDumpStatistics(&s2);
3752    OutputNewline();
3753    OutputDivider();
3754    _RPT1(_CRT_WARN, "%s", "Checking for memory leaks...\n");
3755    OutputDivider();
3756    _CrtDumpMemoryLeaks();
3757    }
3758#endif
3759
3760
3761
3762int main(int argc, LCHAR* argv [] )
3763    {
3764     int             test_status;
3765     unsigned int    num_shutdown_loops;
3766     unsigned int    current_shutdown_loop;
3767     unsigned int    num_continuous_run_loops;
3768     unsigned int    current_continuous_run_loop;
3769     unsigned int    srec_test_heap_size;
3770
3771#ifdef _WIN32
3772    initStack();
3773#endif
3774        {
3775    /* This extra block is there to get a more precise estimate of the stack
3776      depth.  This way we also make sure that the local variables of main are
3777      taken into acount when estimating the stack size.
3778   */
3779        ApplicationData applicationData;
3780        PLogger* logger;
3781
3782        srec_test_heap_size = ( 4 * 1024 * 1024 );
3783        logger = NULL;
3784        test_status = srec_test_get_run_params ( &num_shutdown_loops, &num_continuous_run_loops );
3785
3786        if ( test_status == 0 )
3787            {
3788            current_shutdown_loop = 0;
3789
3790            while ( ( current_shutdown_loop < num_shutdown_loops )/* && ( test_status == 0 )*/ )
3791                {
3792#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32)
3793                initialize_heap_logging ( );
3794#endif
3795                test_status = srec_test_init_system ( srec_test_heap_size, logger, argc, argv );
3796
3797                if ( test_status == 0 )
3798                    {
3799                    current_continuous_run_loop = 0;
3800
3801                    while ( ( current_continuous_run_loop < num_continuous_run_loops ) && ( test_status == 0 ) )
3802                        {
3803                        test_status = srec_test_init_application_data ( &applicationData, argc, argv );
3804
3805                        if ( test_status == 0 )
3806                            {
3807                            test_status = srec_test_run_test ( &applicationData );
3808                            srec_test_shutdown_application_data ( &applicationData );
3809                            }
3810                        current_continuous_run_loop++;
3811                        }
3812                    test_status = srec_test_shutdown_system ( logger );
3813                    }
3814                current_shutdown_loop++;
3815#if defined(_DEBUGHEAP) && defined(_DEBUG) && defined(WIN32)
3816                execute_heap_logging ( );
3817#endif
3818                }
3819            }
3820        }
3821
3822    return ( test_status );
3823    }
3824
3825
3826