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