1/*---------------------------------------------------------------------------*
2 *  Recognizer.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#include "SR_Recognizer.h"
21#include "SR_RecognizerImpl.h"
22#include "ESR_Session.h"
23#include "plog.h"
24#include "pmemory.h"
25
26#define COUNT_INTERVAL      20
27
28ESR_ReturnCode SR_RecognizerStart(SR_Recognizer* self)
29{
30  if (self == NULL)
31  {
32    PLogError(L("ESR_INVALID_ARGUMENT"));
33    return ESR_INVALID_ARGUMENT;
34  }
35#ifdef SREC_ENGINE_TRACK_RECOGNITION
36  PLogMessage ( "Entering Recognizer Start\n" );
37#endif
38  return self->start(self);
39}
40
41ESR_ReturnCode SR_RecognizerStop(SR_Recognizer* self)
42{
43  if (self == NULL)
44  {
45    PLogError(L("ESR_INVALID_ARGUMENT"));
46    return ESR_INVALID_ARGUMENT;
47  }
48#ifdef SREC_ENGINE_TRACK_RECOGNITION
49  PLogMessage ( "Entering Recognizer Stop\n" );
50#endif
51  return self->stop(self);
52}
53
54ESR_ReturnCode SR_RecognizerDestroy(SR_Recognizer* self)
55{
56  if (self == NULL)
57  {
58    PLogError(L("ESR_INVALID_ARGUMENT"));
59    return ESR_INVALID_ARGUMENT;
60  }
61  return self->destroy(self);
62}
63
64ESR_ReturnCode SR_RecognizerSetup(SR_Recognizer* self)
65{
66  if (self == NULL)
67  {
68    PLogError(L("ESR_INVALID_ARGUMENT"));
69    return ESR_INVALID_ARGUMENT;
70  }
71  return self->setup(self);
72}
73
74ESR_ReturnCode SR_RecognizerUnsetup(SR_Recognizer* self)
75{
76  if (self == NULL)
77  {
78    PLogError(L("ESR_INVALID_ARGUMENT"));
79    return ESR_INVALID_ARGUMENT;
80  }
81  return self->unsetup(self);
82}
83
84ESR_ReturnCode SR_RecognizerIsSetup(SR_Recognizer* self, ESR_BOOL* isSetup)
85{
86  if (self == NULL)
87  {
88    PLogError(L("ESR_INVALID_ARGUMENT"));
89    return ESR_INVALID_ARGUMENT;
90  }
91  return self->isSetup(self, isSetup);
92}
93
94ESR_ReturnCode SR_RecognizerGetParameter(SR_Recognizer* self, const LCHAR* name, LCHAR* value, size_t* len)
95{
96  if (self == NULL)
97  {
98    PLogError(L("ESR_INVALID_ARGUMENT"));
99    return ESR_INVALID_ARGUMENT;
100  }
101  return self->getParameter(self, name, value, len);
102}
103
104ESR_ReturnCode SR_RecognizerGetSize_tParameter(SR_Recognizer* self, const LCHAR* name, size_t* value)
105{
106  if (self == NULL)
107  {
108    PLogError(L("ESR_INVALID_ARGUMENT"));
109    return ESR_INVALID_ARGUMENT;
110  }
111  return self->getSize_tParameter(self, name, value);
112}
113
114ESR_ReturnCode SR_RecognizerGetBoolParameter(SR_Recognizer* self, const LCHAR* name, ESR_BOOL* value)
115{
116  if (self == NULL)
117  {
118    PLogError(L("ESR_INVALID_ARGUMENT"));
119    return ESR_INVALID_ARGUMENT;
120  }
121  return self->getBoolParameter(self, name, value);
122}
123
124ESR_ReturnCode SR_RecognizerSetParameter(SR_Recognizer* self, const LCHAR* name, LCHAR* value)
125{
126  if (self == NULL)
127  {
128    PLogError(L("ESR_INVALID_ARGUMENT"));
129    return ESR_INVALID_ARGUMENT;
130  }
131  return self->setParameter(self, name, value);
132}
133
134ESR_ReturnCode SR_RecognizerSetSize_tParameter(SR_Recognizer* self, const LCHAR* name, size_t value)
135{
136  if (self == NULL)
137  {
138    PLogError(L("ESR_INVALID_ARGUMENT"));
139    return ESR_INVALID_ARGUMENT;
140  }
141  if ( LSTRCMP( L("CREC.Frontend.samplerate"), name ) == 0 )
142    return SR_Recognizer_Change_Sample_Rate ( self, value );
143  else
144    return self->setSize_tParameter(self, name, value);
145}
146
147ESR_ReturnCode SR_RecognizerSetBoolParameter(SR_Recognizer* self, const LCHAR* name, ESR_BOOL value)
148{
149  if (self == NULL)
150  {
151    PLogError(L("ESR_INVALID_ARGUMENT"));
152    return ESR_INVALID_ARGUMENT;
153  }
154  return self->setBoolParameter(self, name, value);
155}
156
157ESR_ReturnCode SR_RecognizerSetupRule(SR_Recognizer* self, SR_Grammar* grammar,
158    const LCHAR* ruleName)
159{
160  if (self == NULL)
161  {
162    PLogError(L("ESR_INVALID_ARGUMENT"));
163    return ESR_INVALID_ARGUMENT;
164  }
165  return self->setupRule(self, grammar, ruleName);
166}
167
168ESR_ReturnCode SR_RecognizerHasSetupRules(SR_Recognizer* self,
169    ESR_BOOL* hasSetupRules)
170{
171  if (self == NULL)
172  {
173    PLogError(L("ESR_INVALID_ARGUMENT"));
174    return ESR_INVALID_ARGUMENT;
175  }
176  return self->hasSetupRules(self, hasSetupRules);
177}
178
179ESR_ReturnCode SR_RecognizerActivateRule(SR_Recognizer* self, SR_Grammar* grammar,
180    const LCHAR* ruleName, unsigned int weight)
181{
182  if (self == NULL)
183  {
184    PLogError(L("ESR_INVALID_ARGUMENT"));
185    return ESR_INVALID_ARGUMENT;
186  }
187  return self->activateRule(self, grammar, ruleName, weight);
188}
189
190ESR_ReturnCode SR_RecognizerDeactivateRule(SR_Recognizer* self, SR_Grammar* grammar,
191    const LCHAR* ruleName)
192{
193  if (self == NULL)
194  {
195    PLogError(L("ESR_INVALID_ARGUMENT"));
196    return ESR_INVALID_ARGUMENT;
197  }
198  return self->deactivateRule(self, grammar, ruleName);
199}
200
201ESR_ReturnCode SR_RecognizerDeactivateAllRules(SR_Recognizer* self)
202{
203  if (self == NULL)
204  {
205    PLogError(L("ESR_INVALID_ARGUMENT"));
206    return ESR_INVALID_ARGUMENT;
207  }
208  return self->deactivateAllRules(self);
209}
210
211ESR_ReturnCode SR_RecognizerIsActiveRule(SR_Recognizer* self, SR_Grammar* grammar,
212    const LCHAR* ruleName, ESR_BOOL* isActiveRule)
213{
214  if (self == NULL)
215  {
216    PLogError(L("ESR_INVALID_ARGUMENT"));
217    return ESR_INVALID_ARGUMENT;
218  }
219  return self->isActiveRule(self, grammar, ruleName, isActiveRule);
220}
221
222ESR_ReturnCode SR_RecognizerCheckGrammarConsistency(SR_Recognizer* self, SR_Grammar* grammar,
223    ESR_BOOL* isConsistent)
224{
225  if (self == NULL)
226  {
227    PLogError(L("ESR_INVALID_ARGUMENT"));
228    return ESR_INVALID_ARGUMENT;
229  }
230  return self->checkGrammarConsistency(self, grammar, isConsistent);
231}
232
233ESR_ReturnCode SR_RecognizerGetModels(SR_Recognizer* self, SR_AcousticModels** pmodels)
234{
235  if (self == NULL)
236  {
237    PLogError(L("ESR_INVALID_ARGUMENT"));
238    return ESR_INVALID_ARGUMENT;
239  }
240  return self->getModels(self, pmodels);
241}
242
243ESR_ReturnCode SR_RecognizerPutAudio(SR_Recognizer* self, asr_int16_t* buffer, size_t* bufferSize,
244                                     ESR_BOOL isLast)
245{
246#ifdef SREC_ENGINE_TRACK_RECOGNITION
247  static int counter = 0;
248#endif
249
250  if (self == NULL)
251  {
252    PLogError(L("ESR_INVALID_ARGUMENT"));
253    return ESR_INVALID_ARGUMENT;
254  }
255#ifdef SREC_ENGINE_TRACK_RECOGNITION
256  if ( ( counter % COUNT_INTERVAL ) == 0 )
257    PLogMessage ( "Entering Recognizer Put Audio %d Times\n", counter );
258  counter++;
259#endif
260  return self->putAudio(self, buffer, bufferSize, isLast);
261}
262
263ESR_ReturnCode SR_RecognizerAdvance(SR_Recognizer* self, SR_RecognizerStatus* status,
264                                    SR_RecognizerResultType* type,
265                                    SR_RecognizerResult** result)
266{
267#ifdef SREC_ENGINE_TRACK_RECOGNITION
268  static int counter = 0;
269#endif
270
271  if (self == NULL)
272  {
273    PLogError(L("ESR_INVALID_ARGUMENT"));
274    return ESR_INVALID_ARGUMENT;
275  }
276#ifdef SREC_ENGINE_TRACK_RECOGNITION
277  if ( ( counter % COUNT_INTERVAL ) == 0 )
278    PLogMessage ( "Entering Recognizer Advance %d Times\n", counter );
279  counter++;
280#endif
281  return self->advance(self, status, type, result);
282}
283
284ESR_ReturnCode SR_RecognizerLoadUtterance(SR_Recognizer* self, const LCHAR* filename)
285{
286  if (self == NULL)
287  {
288    PLogError(L("ESR_INVALID_ARGUMENT"));
289    return ESR_INVALID_ARGUMENT;
290  }
291  return self->loadUtterance(self, filename);
292}
293
294ESR_ReturnCode SR_RecognizerLoadWaveFile(SR_Recognizer* self, const LCHAR* filename)
295{
296  if (self == NULL)
297  {
298    PLogError(L("ESR_INVALID_ARGUMENT"));
299    return ESR_INVALID_ARGUMENT;
300  }
301  return self->loadWaveFile(self, filename);
302}
303
304ESR_ReturnCode SR_RecognizerLogEvent(SR_Recognizer* self, const LCHAR* event)
305{
306  if (self == NULL)
307  {
308    PLogError(L("ESR_INVALID_ARGUMENT"));
309    return ESR_INVALID_ARGUMENT;
310  }
311  return self->logEvent(self, event);
312}
313
314ESR_ReturnCode SR_RecognizerLogToken(SR_Recognizer* self, const LCHAR* token, const LCHAR* value)
315{
316  if (self == NULL)
317  {
318    PLogError(L("ESR_INVALID_ARGUMENT"));
319    return ESR_INVALID_ARGUMENT;
320  }
321  return self->logToken(self, token, value);
322}
323
324ESR_ReturnCode SR_RecognizerLogTokenInt(SR_Recognizer* self, const LCHAR* token, int value)
325{
326  if (self == NULL)
327  {
328    PLogError(L("ESR_INVALID_ARGUMENT"));
329    return ESR_INVALID_ARGUMENT;
330  }
331  return self->logTokenInt(self, token, value);
332}
333
334
335ESR_ReturnCode SR_RecognizerLogSessionStart(SR_Recognizer* self, const LCHAR* sessionName)
336{
337  if (self == NULL)
338  {
339    PLogError(L("ESR_INVALID_ARGUMENT"));
340    return ESR_INVALID_ARGUMENT;
341  }
342  return self->logSessionStart(self, sessionName);
343}
344
345
346ESR_ReturnCode SR_RecognizerLogSessionEnd(SR_Recognizer* self)
347{
348  if (self == NULL)
349  {
350    PLogError(L("ESR_INVALID_ARGUMENT"));
351    return ESR_INVALID_ARGUMENT;
352  }
353  return self->logSessionEnd(self);
354}
355
356
357ESR_ReturnCode SR_RecognizerLogWaveformData(SR_Recognizer* self,
358    const LCHAR* waveformFilename,
359    const LCHAR* transcription,
360    const double bos,
361    const double eos,
362    ESR_BOOL isInvocab)
363{
364  if (self == NULL)
365  {
366    PLogError(L("ESR_INVALID_ARGUMENT"));
367    return ESR_INVALID_ARGUMENT;
368  }
369  return self->logWaveformData(self, waveformFilename, transcription, bos, eos, isInvocab);
370}
371
372
373ESR_ReturnCode SR_RecognizerSetLockFunction(SR_Recognizer* self, SR_RecognizerLockFunction function, void* data)
374{
375  if (self == NULL)
376  {
377    PLogError(L("ESR_INVALID_ARGUMENT"));
378    return ESR_INVALID_ARGUMENT;
379  }
380  return self->setLockFunction(self, function, data);
381}
382
383ESR_ReturnCode SR_RecognizerIsSignalClipping(SR_Recognizer* self, ESR_BOOL* isClipping)
384{
385  if (self == NULL)
386  {
387    PLogError(L("ESR_INVALID_ARGUMENT"));
388    return ESR_INVALID_ARGUMENT;
389  }
390  return self->isSignalClipping(self, isClipping);
391}
392
393ESR_ReturnCode SR_RecognizerIsSignalDCOffset(SR_Recognizer* self, ESR_BOOL* isDCOffset)
394{
395  if (self == NULL)
396  {
397    PLogError(L("ESR_INVALID_ARGUMENT"));
398    return ESR_INVALID_ARGUMENT;
399  }
400  return self->isSignalDCOffset(self, isDCOffset);
401}
402
403ESR_ReturnCode SR_RecognizerIsSignalNoisy(SR_Recognizer* self, ESR_BOOL* isNoisy)
404{
405  if (self == NULL)
406  {
407    PLogError(L("ESR_INVALID_ARGUMENT"));
408    return ESR_INVALID_ARGUMENT;
409  }
410  return self->isSignalNoisy(self, isNoisy);
411}
412
413ESR_ReturnCode SR_RecognizerIsSignalTooQuiet(SR_Recognizer* self, ESR_BOOL* isTooQuiet)
414{
415  if (self == NULL)
416  {
417    PLogError(L("ESR_INVALID_ARGUMENT"));
418    return ESR_INVALID_ARGUMENT;
419  }
420  return self->isSignalTooQuiet(self, isTooQuiet);
421}
422
423ESR_ReturnCode SR_RecognizerIsSignalTooFewSamples(SR_Recognizer* self, ESR_BOOL* isTooFewSamples)
424{
425  if (self == NULL)
426  {
427    PLogError(L("ESR_INVALID_ARGUMENT"));
428    return ESR_INVALID_ARGUMENT;
429  }
430  return self->isSignalTooFewSamples(self, isTooFewSamples);
431}
432
433ESR_ReturnCode SR_RecognizerIsSignalTooManySamples(SR_Recognizer* self, ESR_BOOL* isTooManySamples)
434{
435  if (self == NULL)
436  {
437    PLogError(L("ESR_INVALID_ARGUMENT"));
438    return ESR_INVALID_ARGUMENT;
439  }
440  return self->isSignalTooManySamples(self, isTooManySamples);
441}
442
443
444
445ESR_ReturnCode SR_Recognizer_Change_Sample_Rate ( SR_Recognizer *recognizer, size_t new_sample_rate )
446    {
447    ESR_ReturnCode  change_status;
448
449    if ( recognizer != NULL )
450        {
451        change_status = SR_Recognizer_Change_Sample_RateImpl ( recognizer, new_sample_rate );
452        }
453    else
454        {
455        change_status = ESR_INVALID_ARGUMENT;
456        PLogError ( L("ESR_INVALID_ARGUMENT") );
457        }
458    return ( change_status );
459    }
460
461
462