srsf/devasr/src/devasrrecognitionalgmgr.cpp
branchRCL_3
changeset 19 e36f3802f733
equal deleted inserted replaced
18:cad71a31b7fc 19:e36f3802f733
       
     1 /*
       
     2 * Copyright (c) 2004-2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  This file contains the implementation of Recognition Algorithm
       
    15 *               Manager, a submodule of DevASR.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #ifdef _DEBUG
       
    22 #include <e32svr.h>
       
    23 #endif
       
    24 #include "devasrrecognitionalgmgr.h"
       
    25 #include "rubydebug.h"
       
    26 
       
    27 // CONSTANTS
       
    28 const TInt KInitFrontEnd        = 1;
       
    29 //const TInt KInitTrainBE         = 2;
       
    30 const TInt KInitRecognizerBE    = 3;
       
    31 const TInt KLoadGrammar         = 4;
       
    32 const TInt KLoadLexicon         = 5;
       
    33 const TInt KLoadModels          = 6;
       
    34 //const TInt KUtteranceDataRcvd   = 7;
       
    35 const TInt KActivateGrammar     = 8;
       
    36 const TInt KDeActivateGrammar   = 9;
       
    37 const TInt KUnloadRule          = 10;
       
    38 const TInt KUnloadGrammar       = 11;
       
    39 
       
    40 //_LIT( KPanicDescriptor, "CRecognitionAlgMgr Panic" );
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CRecognitionAlgMgr::CRecognitionAlgMgr
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CRecognitionAlgMgr::CRecognitionAlgMgr( MRecAlgMgrObserver& aObserver )
       
    51                     : CActive( CActive::EPriorityStandard ),
       
    52                       iRecAlgMgrObserver( &aObserver ),
       
    53                       iCombinedGrammar( NULL ),
       
    54                       iCombineNeeded( ETrue ),
       
    55                       iInitialized( EFalse ),
       
    56                       iRecoHw( NULL ),
       
    57                       iAdaptHw( NULL ),
       
    58                       iSDModelBank( NULL ),
       
    59                       iSIModelBank( NULL ),
       
    60                       iSDGrammar( NULL ),
       
    61                       iSIGrammar( NULL ),
       
    62                       iSICompGrammar( NULL ),
       
    63                       iSDCompGrammar( NULL ),
       
    64                       iSDLexicon( NULL ),
       
    65                       iSILexicon( NULL ),
       
    66                       iFEState( EIdle ),
       
    67                       iBEState( EIdle ),
       
    68                       iFeatures( EFalse ),
       
    69                       iAdaptation( ETrue ),
       
    70                       iAdaptationData( NULL )
       
    71     {
       
    72     RUBY_DEBUG0( "CRecognitionAlgMgr::CRecognitionAlgMgr()" );
       
    73     }
       
    74 
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CRecognitionAlgMgr::ConstructL
       
    78 // Symbian 2nd phase constructor can leave.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 void CRecognitionAlgMgr::ConstructL()
       
    82     {
       
    83     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::ConstructL()" );
       
    84 
       
    85     iRecoHw = CASRSRecognitionHwDevice::NewL( *this );
       
    86     iRecoHw->InitializeL();
       
    87 
       
    88     iAdaptHw = CASRSAdaptHwDevice::NewL( *this );
       
    89     iAdaptHw->InitializeL();
       
    90 
       
    91     CActiveScheduler::Add(this);
       
    92     
       
    93     RUBY_DEBUG1( "CRecognitionAlgMgr::ConstructL(): RecoHwDevice instance [%x]", iRecoHw );
       
    94     }
       
    95 
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CRecognitionAlgMgr::NewL
       
    99 // Two-phased constructor.
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 CRecognitionAlgMgr* CRecognitionAlgMgr::NewL( MRecAlgMgrObserver& aObserver )
       
   103     {
       
   104     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::ConstructL()" );
       
   105     
       
   106     CRecognitionAlgMgr* self = new ( ELeave ) CRecognitionAlgMgr( aObserver );
       
   107     
       
   108     CleanupStack::PushL( self );
       
   109     self->ConstructL();
       
   110     CleanupStack::Pop( self );
       
   111     
       
   112     return self;
       
   113     }
       
   114 
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // CRecognitionAlgMgr::~CRecognitionAlgMgr
       
   118 // Destructor
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 CRecognitionAlgMgr::~CRecognitionAlgMgr()
       
   122     {
       
   123     RUBY_DEBUG0( "CRecognitionAlgMgr::~CRecognitionAlgMgr()" );
       
   124     
       
   125     iSIActiveGrammars.Reset();
       
   126     iSIDeActivatedGrammars.Reset();
       
   127     iNBestList.Reset();
       
   128     iScores.Reset();
       
   129 
       
   130     if ( iRecoHw != NULL )
       
   131         {
       
   132         iRecoHw->Clear();
       
   133         delete iRecoHw;
       
   134         }
       
   135 
       
   136     if ( iAdaptHw != NULL )
       
   137         {
       
   138         iAdaptHw->Clear();
       
   139         delete iAdaptHw;
       
   140         }
       
   141 
       
   142     delete iCombinedGrammar;
       
   143 
       
   144     iBlackList.ResetAndDestroy();
       
   145     iBlackList.Close();
       
   146 
       
   147     delete iAdaptationData;
       
   148     }
       
   149 
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CRecognitionAlgMgr::AdaptModelsL
       
   153 // Adapts models.
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 void CRecognitionAlgMgr::AdaptModelsL( const CSIResultSet& aResultSet, 
       
   157                                        TInt aResultIndex,
       
   158                                        TLanguage aLanguage )
       
   159     {
       
   160     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::AdaptModelsL()" );
       
   161 
       
   162     RUBY_DEBUG1( "CRecognitionAlgMgr::AdaptModelsL aResultIndex: %x", aResultIndex );
       
   163 
       
   164     // TODO: Not too nice to remove consts like this
       
   165     CSIResult& iCorrectResult = ( CSIResult& ) aResultSet.AtL( aResultIndex );
       
   166 
       
   167     // Check that we have a modelbank to adapt
       
   168     if ( iSIModelBank == NULL )
       
   169         {
       
   170         User::Leave( KErrNotReady );
       
   171         }
       
   172 
       
   173     CSIResultSet& iResultSet = ( CSIResultSet& ) aResultSet;
       
   174     iAdaptHw->StartAdaptationL( iResultSet.AdaptationData(), 
       
   175                                 *iSIModelBank, 
       
   176                                 iCorrectResult.Pronunciation(),
       
   177                                 aLanguage );
       
   178     }
       
   179 
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CRecognitionAlgMgr::InitFrontEnd
       
   183 // Receives the initialize frontend request and starts an asynchronous handler
       
   184 // for the request.
       
   185 // -----------------------------------------------------------------------------
       
   186 //
       
   187 void CRecognitionAlgMgr::InitFrontEnd( TRecognizerMode aMode )
       
   188     {
       
   189     RUBY_DEBUG0( "CRecognitionAlgMgr::InitFrontEnd()" );
       
   190 
       
   191     // Save arguments and set code
       
   192     iMode = aMode;
       
   193     iRequestFunction = KInitFrontEnd;
       
   194     Ready( KErrNone );
       
   195     }
       
   196 
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CRecognitionAlgMgr::HandleInitFrontEnd
       
   200 // Asynchronously handles the initialize frontend request. The front-end module
       
   201 // is started as a result.
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 void CRecognitionAlgMgr::HandleInitFrontEnd()
       
   205     {
       
   206     RUBY_DEBUG0( "CRecognitionAlgMgr::HandleInitFrontEnd()" );
       
   207     
       
   208     if ( !iInitialized )
       
   209         {
       
   210         iRecAlgMgrObserver->InitFEComplete( KErrNotReady );
       
   211         return;
       
   212         }
       
   213 
       
   214     if ( iFEState != EIdle )
       
   215         {
       
   216         iRecAlgMgrObserver->InitFEComplete( KErrInUse );
       
   217         return;
       
   218         }
       
   219     
       
   220     if ( iMode != ESiRecognition && iMode != ESiRecognitionSpeechInput )
       
   221         {
       
   222         iRecAlgMgrObserver->InitFEComplete( KErrNotSupported );
       
   223         return;
       
   224         }
       
   225 
       
   226     TInt error = KErrNone;
       
   227 
       
   228     if ( iFeatures || iAdaptation )
       
   229         {
       
   230         TRAP( error, iRecoHw->InitRecognizerFEL( ETrue ) );
       
   231         }
       
   232     else
       
   233         {
       
   234         TRAP( error, iRecoHw->InitRecognizerFEL( EFalse ) );
       
   235         }
       
   236 
       
   237     iRecAlgMgrObserver->InitFEComplete( error );
       
   238     }
       
   239 
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // CRecognitionAlgMgr::InitRecognizerBE
       
   243 // Receives the initialize recognition backend request and starts an asynchronous
       
   244 // handler for the request.
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 void CRecognitionAlgMgr::InitRecognizerBE( CSIResultSet& aResult )
       
   248     {
       
   249     RUBY_DEBUG0( "CRecognitionAlgMgr::InitRecognizerBE()" );
       
   250     
       
   251     // Save arguments and set code
       
   252     iSDResult = NULL;
       
   253     iSIResult = &aResult;
       
   254     iRequestFunction = KInitRecognizerBE;
       
   255     Ready( KErrNone );
       
   256     }
       
   257 
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // CRecognitionAlgMgr::HandleInitRecognizerBE
       
   261 // Asynchronously handles the initialize recognition backend request. The backend
       
   262 // module for recognition function is started as a result.
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CRecognitionAlgMgr::HandleInitRecognizerBE()
       
   266     {
       
   267     RUBY_DEBUG0( "CRecognitionAlgMgr::HandleInitRecognizerBE()" );
       
   268 
       
   269     if ( !iInitialized )
       
   270         {
       
   271         iRecAlgMgrObserver->InitRecognizerBEComplete( KErrNotReady );
       
   272         return;
       
   273         }
       
   274 
       
   275     if ( iBEState != EIdle )
       
   276         {
       
   277         iRecAlgMgrObserver->InitRecognizerBEComplete( KErrInUse );
       
   278         return;
       
   279         }
       
   280     TInt error( KErrNone );
       
   281     if ( iSDResult )
       
   282         {
       
   283         iRecAlgMgrObserver->InitRecognizerBEComplete( KErrNotSupported );
       
   284         return;
       
   285         }
       
   286     else if ( iSIResult )
       
   287         {
       
   288         // Call RecoHwDevice
       
   289         if ( ( iSIActiveGrammars.Count() == 0 )  || ( iSIModelBank == NULL ) )
       
   290             {
       
   291             // Guess it is ok to leave with KErrNotReady if all necessary 
       
   292             iRecAlgMgrObserver->InitRecognizerBEComplete( KErrNotReady );
       
   293             return;
       
   294             }
       
   295         else
       
   296             {
       
   297             if ( iCombineNeeded )
       
   298                 {
       
   299                 delete iCombinedGrammar;
       
   300                 iCombinedGrammar = NULL;
       
   301 
       
   302                 TRAPD( error, iRecAlgMgrObserver->CombineGrammarL( iSIActiveGrammars, iBlackList ) );  
       
   303                 if ( error != KErrNone )
       
   304                     {
       
   305                     iRecAlgMgrObserver->InitRecognizerBEComplete( error );
       
   306                     }
       
   307                 // Wait for the callback from grammar compilation!
       
   308                 return;
       
   309                 }
       
   310             // Call reco HW device
       
   311             TRAP( error, iRecoHw->InitRecognizerBEL( iCombinedGrammar->Des(), *iSIModelBank ) );
       
   312             if ( error != KErrNone )
       
   313                 {
       
   314                 iRecAlgMgrObserver->InitRecognizerBEComplete( error );
       
   315                 }
       
   316             }
       
   317         }
       
   318     else
       
   319         {
       
   320         error = KErrArgument;
       
   321         }
       
   322     
       
   323     if ( error == KErrNone )
       
   324         {
       
   325         iBEState = EProcessing;
       
   326         }
       
   327     }
       
   328 
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // CRecognitionAlgMgr::LoadGrammarL
       
   332 // Receives the load grammar request and start the asynchronous handler.
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 void CRecognitionAlgMgr::LoadGrammarL( const CSIGrammar& aGrammar )
       
   336     {
       
   337     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::LoadGrammarL()" );
       
   338 
       
   339     if ( IsActive() )
       
   340         {
       
   341         // Callback before the request function returns
       
   342         User::Leave( KErrServerBusy );
       
   343         return;
       
   344         }
       
   345     
       
   346     // Save arguments and set code
       
   347     iSIGrammar = &aGrammar;
       
   348     iRequestFunction = KLoadGrammar;
       
   349     Ready( KErrNone );
       
   350     }
       
   351 
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CRecognitionAlgMgr::LoadGrammarL
       
   355 // Receives the load grammar request and start the asynchronous handler.
       
   356 // -----------------------------------------------------------------------------
       
   357 //
       
   358 void CRecognitionAlgMgr::LoadGrammarL( const CSICompiledGrammar& aGrammar )
       
   359     {
       
   360     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::LoadGrammarL()" );
       
   361     
       
   362     if ( IsActive() )
       
   363         {
       
   364         // Callback before the request function returns
       
   365         User::Leave( KErrServerBusy );
       
   366         return;
       
   367         }
       
   368     
       
   369     // Save arguments and set code
       
   370     iSICompGrammar = &aGrammar; 
       
   371 /*    __ASSERT_DEBUG( iSDGrammar == NULL, User::Panic( KPanicDescriptor, KErrCorrupt ) );
       
   372     __ASSERT_DEBUG( iSIGrammar == NULL, User::Panic( KPanicDescriptor, KErrCorrupt ) );*/
       
   373     iRequestFunction = KLoadGrammar;
       
   374     Ready( KErrNone );
       
   375     }
       
   376 
       
   377 
       
   378 // -----------------------------------------------------------------------------
       
   379 // CRecognitionAlgMgr::HandleLoadGrammar
       
   380 // Asynchronous handler for Load Grammar request.
       
   381 // -----------------------------------------------------------------------------
       
   382 //
       
   383 void CRecognitionAlgMgr::HandleLoadGrammar()
       
   384     {
       
   385     RUBY_DEBUG0( "CRecognitionAlgMgr::HandleLoadGrammar()" );
       
   386 
       
   387     TInt error( KErrNone );
       
   388 
       
   389     if ( !iInitialized )
       
   390         {
       
   391         iRecAlgMgrObserver->LoadGrammarComplete( KErrNotReady );
       
   392         return;
       
   393         }
       
   394 
       
   395     if ( iSICompGrammar )
       
   396         {
       
   397 
       
   398         if ( iSICompGrammar->Count() < 1 )
       
   399             {
       
   400             iRecAlgMgrObserver->LoadGrammarComplete( KErrArgument );
       
   401             return;
       
   402             }
       
   403 
       
   404         error = iSIActiveGrammars.Append( iSICompGrammar );
       
   405 
       
   406         iSICompGrammar = NULL;
       
   407 
       
   408         // Mark that there is need to recombine grammars
       
   409         iCombineNeeded = ETrue;
       
   410 
       
   411         iRecAlgMgrObserver->LoadGrammarComplete( error );
       
   412  
       
   413         }
       
   414     else
       
   415         {
       
   416         iRecAlgMgrObserver->LoadGrammarComplete( KErrNotSupported );
       
   417         }
       
   418     }
       
   419 
       
   420 
       
   421 // -----------------------------------------------------------------------------
       
   422 // CRecognitionAlgMgr::UnloadGrammarL
       
   423 // Receives the unload grammar request and start the asynchronous handler.
       
   424 // -----------------------------------------------------------------------------
       
   425 //
       
   426 void CRecognitionAlgMgr::UnloadGrammarL( const CSIGrammar& aGrammar )
       
   427     {
       
   428     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::UnloadGrammarL()" );
       
   429 
       
   430     if ( IsActive() )
       
   431         {
       
   432         // Callback before the request function returns
       
   433         User::Leave( KErrServerBusy );
       
   434         return;
       
   435         }
       
   436     
       
   437     // Save arguments and set code
       
   438     iSIGrammar = &aGrammar;
       
   439     iRequestFunction = KUnloadGrammar;
       
   440     Ready( KErrNone );
       
   441     }
       
   442 
       
   443 
       
   444 // -----------------------------------------------------------------------------
       
   445 // CRecognitionAlgMgr::UnloadGrammarL
       
   446 // Receives the unload grammar request and start the asynchronous handler.
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 void CRecognitionAlgMgr::UnloadGrammarL( const CSICompiledGrammar& aGrammar )
       
   450     {
       
   451     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::UnloadGrammarL()" );
       
   452     
       
   453     if ( IsActive() )
       
   454         {
       
   455         // Callback before the request function returns
       
   456         User::Leave( KErrServerBusy );
       
   457         return;
       
   458         }
       
   459     
       
   460     // Save arguments and set code
       
   461     iSICompGrammar = &aGrammar; 
       
   462     iRequestFunction = KUnloadGrammar;
       
   463     Ready( KErrNone );
       
   464     }
       
   465 
       
   466 
       
   467 // -----------------------------------------------------------------------------
       
   468 // CRecognitionAlgMgr::HandleUnloadGrammar
       
   469 // Asynchronous handler for Load Grammar request.
       
   470 // -----------------------------------------------------------------------------
       
   471 //
       
   472 void CRecognitionAlgMgr::HandleUnloadGrammar()
       
   473     {
       
   474     RUBY_DEBUG0( "CRecognitionAlgMgr::HandleUnloadGrammar()" );
       
   475 
       
   476     CSICompiledGrammar* siActiveGrammar = NULL;
       
   477     CSICompiledGrammar* siNonActiveGrammar = NULL;
       
   478 
       
   479     if ( !iInitialized )
       
   480         {
       
   481         iRecAlgMgrObserver->UnloadGrammarComplete( KErrNotReady );
       
   482         return;
       
   483         }
       
   484 
       
   485     if ( iSICompGrammar )
       
   486         {
       
   487         
       
   488         TRAPD( error, GetGrammarL( iSICompGrammar->GrammarID(), &siActiveGrammar, &siNonActiveGrammar ) ); // Leaves if not found
       
   489         if ( error != KErrNone )
       
   490             {
       
   491             iRecAlgMgrObserver->UnloadGrammarComplete( error ); 
       
   492             return;
       
   493             }
       
   494         
       
   495         if ( siActiveGrammar )
       
   496             {
       
   497             // Remove grammar from active grammars
       
   498             iSIActiveGrammars.Remove( iSIActiveGrammars.Find( siActiveGrammar ) );
       
   499             // Mark that there is need to recombine grammars
       
   500             iCombineNeeded = ETrue;
       
   501             }
       
   502         else if ( siNonActiveGrammar )
       
   503             {
       
   504             // Remove grammar from deactive
       
   505             iSIActiveGrammars.Remove( iSIActiveGrammars.Find( siActiveGrammar ) );
       
   506             // No need to set recombination flag
       
   507             }
       
   508         else
       
   509             {
       
   510             iRecAlgMgrObserver->UnloadGrammarComplete( KErrNotFound ); 
       
   511             return;
       
   512             }
       
   513         }
       
   514     else
       
   515         {
       
   516         iRecAlgMgrObserver->UnloadGrammarComplete( KErrNotSupported );
       
   517         return;
       
   518         }
       
   519 
       
   520     iRecAlgMgrObserver->UnloadGrammarComplete( KErrNone );
       
   521     }
       
   522 
       
   523 
       
   524 // -----------------------------------------------------------------------------
       
   525 // CRecognitionAlgMgr::GetGrammarL
       
   526 // Find grammar based on identifier
       
   527 // -----------------------------------------------------------------------------
       
   528 //
       
   529 void CRecognitionAlgMgr::GetGrammarL( const TSIGrammarID aGrammarID, 
       
   530                                       CSICompiledGrammar** aSIActiveGrammar,
       
   531                                       CSICompiledGrammar** aSIDeActivatedGrammar )
       
   532     {
       
   533     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::GetGrammarL()" );
       
   534 
       
   535     // Find the grammar.
       
   536     // One of the arrays should contain the specified grammar and
       
   537     // others are empty.
       
   538     TInt i( 0 );
       
   539     
       
   540     // SI active grammars.
       
   541     for ( i = 0; i < iSIActiveGrammars.Count(); i++ )
       
   542         {
       
   543         if ( aGrammarID == iSIActiveGrammars[i]->GrammarID() )
       
   544             {
       
   545             // Grammar found.
       
   546             *aSIActiveGrammar = iSIActiveGrammars[i];
       
   547             *aSIDeActivatedGrammar = NULL;
       
   548             return;
       
   549             }
       
   550         }
       
   551     
       
   552     // SI deactived grammars.
       
   553     for ( i = 0; i < iSIDeActivatedGrammars.Count(); i++ )
       
   554         {
       
   555         if ( aGrammarID == iSIDeActivatedGrammars[i]->GrammarID() )
       
   556             {
       
   557             // Grammar found.
       
   558             *aSIDeActivatedGrammar = iSIDeActivatedGrammars[i];
       
   559             *aSIActiveGrammar = NULL;
       
   560             return;
       
   561             }
       
   562         }
       
   563 
       
   564     User::Leave( KErrNotFound );
       
   565     }
       
   566 
       
   567 
       
   568 // -----------------------------------------------------------------------------
       
   569 // CRecognitionAlgMgr::ActivateGrammarL
       
   570 // Receives the load grammar request and start the asynchronous handler.
       
   571 // -----------------------------------------------------------------------------
       
   572 //
       
   573 void CRecognitionAlgMgr::ActivateGrammarL( const TSIGrammarID aGrammarID )
       
   574     {
       
   575     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::ActivateGrammarL()" );
       
   576 
       
   577     if ( IsActive() )
       
   578         {
       
   579         User::Leave( KErrServerBusy );
       
   580         }
       
   581     
       
   582     iRequestFunction = KActivateGrammar;
       
   583     iGrammarID = aGrammarID;
       
   584     Ready( KErrNone );
       
   585     }
       
   586 
       
   587 
       
   588 // -----------------------------------------------------------------------------
       
   589 // CRecognitionAlgMgr::HandleActivateGrammar
       
   590 // Asynchronous handler for activate grammar request.
       
   591 // -----------------------------------------------------------------------------
       
   592 //
       
   593 void CRecognitionAlgMgr::HandleActivateGrammar()
       
   594     {
       
   595     RUBY_DEBUG0( "CRecognitionAlgMgr::HandleActivateGrammar()" );
       
   596 
       
   597     CSICompiledGrammar* siActiveGrammar = NULL;
       
   598     CSICompiledGrammar* siNonActiveGrammar = NULL;
       
   599 
       
   600     if ( !iInitialized )
       
   601         {
       
   602         iRecAlgMgrObserver->ActivateGrammarComplete( KErrNotReady );
       
   603         }
       
   604 
       
   605     // Find the grammar
       
   606     TRAPD( error, GetGrammarL( iGrammarID, &siActiveGrammar, &siNonActiveGrammar ) ); // Leaves if not found
       
   607     if ( error != KErrNone )
       
   608         {
       
   609         iRecAlgMgrObserver->ActivateGrammarComplete( error ); 
       
   610         }
       
   611 
       
   612     if ( siActiveGrammar )
       
   613         {
       
   614         // Do nothing since grammar is already active
       
   615         }
       
   616     else if ( siNonActiveGrammar )
       
   617         {
       
   618         // Move item from non-active grammar list to active list
       
   619         iSIActiveGrammars.Append( siNonActiveGrammar );
       
   620         iSIDeActivatedGrammars.Remove( iSIDeActivatedGrammars.Find( siNonActiveGrammar ) );
       
   621         // Mark that there is need to recombine grammars
       
   622         iCombineNeeded = ETrue;
       
   623         }
       
   624     else
       
   625         {
       
   626         iRecAlgMgrObserver->ActivateGrammarComplete( KErrNotFound );
       
   627         return;
       
   628         }
       
   629 
       
   630     iRecAlgMgrObserver->ActivateGrammarComplete( KErrNone ); 
       
   631     }
       
   632 
       
   633 
       
   634 // -----------------------------------------------------------------------------
       
   635 // CRecognitionAlgMgr::DeactivateGrammarL
       
   636 // Receives the load grammar request and start the asynchronous handler.
       
   637 // -----------------------------------------------------------------------------
       
   638 //
       
   639 void CRecognitionAlgMgr::DeactivateGrammarL( const TSIGrammarID aGrammarID )
       
   640     {
       
   641     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::DeactivateGrammarL()" );
       
   642 
       
   643     if ( IsActive() )
       
   644         {
       
   645         User::Leave( KErrServerBusy );
       
   646         }
       
   647     
       
   648     iRequestFunction = KDeActivateGrammar;
       
   649     iGrammarID = aGrammarID;
       
   650     Ready( KErrNone );    
       
   651     }
       
   652 
       
   653 
       
   654 // -----------------------------------------------------------------------------
       
   655 // CRecognitionAlgMgr::HandleDeActivateGrammar
       
   656 // Asynchronous handler for deactivate grammar request.
       
   657 // -----------------------------------------------------------------------------
       
   658 //
       
   659 void CRecognitionAlgMgr::HandleDeActivateGrammar()
       
   660     {
       
   661     RUBY_DEBUG0( "CRecognitionAlgMgr::HandleDeActivateGrammar()" );
       
   662 
       
   663     CSICompiledGrammar* siActiveGrammar = NULL;
       
   664     CSICompiledGrammar* siNonActiveGrammar = NULL;
       
   665 
       
   666     if ( !iInitialized )
       
   667         {
       
   668         iRecAlgMgrObserver->DeActivateGrammarComplete( KErrNotReady );
       
   669         }
       
   670 
       
   671     // Find the grammar
       
   672     TRAPD( error, GetGrammarL( iGrammarID, &siActiveGrammar, &siNonActiveGrammar ) ); // Leaves if not found
       
   673     if ( error != KErrNone )
       
   674         {
       
   675         iRecAlgMgrObserver->DeActivateGrammarComplete( error ); 
       
   676         }
       
   677 
       
   678     if ( siActiveGrammar )
       
   679         {
       
   680         // Move item from active grammar list to non-active list
       
   681         iSIDeActivatedGrammars.Append( siActiveGrammar );
       
   682         iSIActiveGrammars.Remove( iSIActiveGrammars.Find( siActiveGrammar ) );
       
   683         // Mark that there is need to recombine grammars
       
   684         iCombineNeeded = ETrue;
       
   685         }
       
   686     else if ( siNonActiveGrammar )
       
   687         {
       
   688         // Do nothing since grammar is already inactive
       
   689         }
       
   690     else
       
   691         {
       
   692         iRecAlgMgrObserver->DeActivateGrammarComplete( KErrNotFound ); 
       
   693         return;
       
   694         }
       
   695 
       
   696     iRecAlgMgrObserver->DeActivateGrammarComplete( KErrNone ); 
       
   697     }
       
   698 
       
   699 
       
   700 // -----------------------------------------------------------------------------
       
   701 // CRecognitionAlgMgr::LoadLexiconL
       
   702 // Receives the load lexicon request and start the asynchronous handler.
       
   703 // -----------------------------------------------------------------------------
       
   704 //
       
   705 void CRecognitionAlgMgr::LoadLexiconL( const CSILexicon& aLexicon )
       
   706     {
       
   707     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::LoadLexiconL()" );
       
   708     
       
   709     // Save arguments and set code
       
   710     if ( IsActive() )
       
   711         {
       
   712         // Callback before the request function returns
       
   713         User::Leave( KErrServerBusy );
       
   714         return;
       
   715         }
       
   716     
       
   717     iSILexicon = &aLexicon;
       
   718     
       
   719     iRequestFunction = KLoadLexicon;
       
   720     Ready( KErrNone );
       
   721     }
       
   722 
       
   723 
       
   724 // -----------------------------------------------------------------------------
       
   725 // CRecognitionAlgMgr::HandleLoadLexicon
       
   726 // Asynchronous handler for Load Lexicon request.
       
   727 // -----------------------------------------------------------------------------
       
   728 //
       
   729 void CRecognitionAlgMgr::HandleLoadLexicon()
       
   730     {
       
   731     RUBY_DEBUG0( "CRecognitionAlgMgr::HandleLoadLexicon()" );
       
   732 
       
   733     TInt error( KErrNone );
       
   734 
       
   735     if ( !iInitialized )
       
   736         {
       
   737         iRecAlgMgrObserver->LoadLexiconComplete( KErrNotReady );
       
   738         return;
       
   739         }
       
   740 
       
   741     if ( iSDLexicon )
       
   742         {
       
   743         iSDLexicon = NULL;
       
   744         iRecAlgMgrObserver->LoadLexiconComplete( KErrNotSupported );
       
   745         return;
       
   746         }
       
   747     else if ( iSILexicon )
       
   748         {
       
   749         }
       
   750     else
       
   751         {
       
   752         error = KErrArgument;
       
   753         }
       
   754     
       
   755     // Do not load empty lexicon
       
   756     if ( iSILexicon && iSILexicon->Count() == 0 )
       
   757         {
       
   758         iSILexicon = NULL;
       
   759         iRecAlgMgrObserver->LoadLexiconComplete( KErrArgument );
       
   760         return;
       
   761         }
       
   762 
       
   763 /*    __ASSERT_DEBUG( iSDLexicon == NULL, User::Panic( KPanicDescriptor, KErrCorrupt ) );
       
   764     __ASSERT_DEBUG( iSILexicon == NULL, User::Panic( KPanicDescriptor, KErrCorrupt ) );*/
       
   765     
       
   766     iRecAlgMgrObserver->LoadLexiconComplete( error );
       
   767     }
       
   768 
       
   769 
       
   770 // -----------------------------------------------------------------------------
       
   771 // CRecognitionAlgMgr::LoadModelsL
       
   772 // Receives the load models request and start the asynchronous handler.
       
   773 // -----------------------------------------------------------------------------
       
   774 //
       
   775 void CRecognitionAlgMgr::LoadModelsL( const CSDModelBank& aModels )
       
   776     {
       
   777     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::LoadModelsL()" );
       
   778     
       
   779     if ( IsActive() )
       
   780         {
       
   781         User::Leave( KErrServerBusy );
       
   782         return;
       
   783         }
       
   784 
       
   785     // Save arguments and set code
       
   786     iSDModelBank = ( CSDModelBank* ) &aModels;
       
   787     iRequestFunction = KLoadModels;
       
   788     Ready( KErrNone );
       
   789     }
       
   790 
       
   791 
       
   792 // -----------------------------------------------------------------------------
       
   793 // CRecognitionAlgMgr::LoadModelsL
       
   794 // Receives the load models request and start the asynchronous handler.
       
   795 // -----------------------------------------------------------------------------
       
   796 //
       
   797 void CRecognitionAlgMgr::LoadModelsL( const CSIModelBank& aModels )
       
   798     {
       
   799     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::LoadModelsL()" );
       
   800 
       
   801     if ( IsActive() )
       
   802         {
       
   803         User::Leave( KErrServerBusy );
       
   804         }
       
   805 
       
   806     // Save arguments and set code
       
   807     iSIModelBank = ( CSIModelBank* ) &aModels;
       
   808     iRequestFunction = KLoadModels;
       
   809     Ready( KErrNone );
       
   810     }
       
   811 
       
   812 
       
   813 // -----------------------------------------------------------------------------
       
   814 // CRecognitionAlgMgr::HandleLoadModels
       
   815 // Asynchronous handler for Load Models request.
       
   816 // -----------------------------------------------------------------------------
       
   817 //
       
   818 void CRecognitionAlgMgr::HandleLoadModels()
       
   819     {
       
   820     RUBY_DEBUG0( "CRecognitionAlgMgr::HandleLoadModels()" );
       
   821 
       
   822     TInt error( KErrNone );
       
   823 
       
   824     if ( !iInitialized )
       
   825         {
       
   826         iSDModelBank = NULL;
       
   827         iSIModelBank = NULL;
       
   828         iRecAlgMgrObserver->LoadModelsComplete( KErrNotReady );
       
   829         return;
       
   830         }
       
   831 
       
   832     if ( iSDModelBank )
       
   833         {
       
   834         // SD modelbank not supported
       
   835         iSDModelBank = NULL;
       
   836         iRecAlgMgrObserver->LoadModelsComplete( KErrNotSupported );
       
   837         return;
       
   838         }
       
   839     else if ( iSIModelBank )
       
   840         {
       
   841         }
       
   842     else
       
   843         {
       
   844         error = KErrArgument;
       
   845         }
       
   846 
       
   847     if ( iSIModelBank && iSIModelBank->Count() < 1 )
       
   848         {
       
   849         iSIModelBank = NULL;
       
   850         iRecAlgMgrObserver->LoadModelsComplete( KErrArgument );
       
   851         return;
       
   852         }
       
   853 
       
   854 /*    __ASSERT_DEBUG( iSDModelBank == NULL, User::Panic( KPanicDescriptor, KErrCorrupt ) );
       
   855     __ASSERT_DEBUG( iSIModelBank == NULL, User::Panic( KPanicDescriptor, KErrCorrupt ) );*/
       
   856     
       
   857     iRecAlgMgrObserver->LoadModelsComplete( error );
       
   858     }
       
   859 
       
   860 
       
   861 // -----------------------------------------------------------------------------
       
   862 // CRecognitionAlgMgr::GetEnginePropertiesL
       
   863 // Retreive the properties of the underlying speech recognition engine.
       
   864 // An array of values corresponding to the querried identifiers will be populated.
       
   865 // The function may leave with KErrArgument.
       
   866 // -----------------------------------------------------------------------------
       
   867 //
       
   868 void CRecognitionAlgMgr::GetEnginePropertiesL( const RArray<TInt>& aPropertyId,
       
   869                                               RArray<TInt>& aPropertyValue )
       
   870     {
       
   871     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::GetEnginePropertiesL()" );
       
   872     
       
   873     for ( TInt index = 0; index < aPropertyId.Count(); index++ )
       
   874         {
       
   875         switch( aPropertyId[index] )
       
   876             {
       
   877             case KDevASRSendFeatures:
       
   878                 if ( iFeatures )
       
   879                     {
       
   880                     aPropertyValue[index] = 1;
       
   881                     }
       
   882                 else
       
   883                     {
       
   884                     aPropertyValue[index] = 0;
       
   885                     }
       
   886                 break;
       
   887                 
       
   888             case KDevASRAdaptation:
       
   889                 if ( iAdaptation )
       
   890                     {
       
   891                     aPropertyValue[index] = 1;
       
   892                     }
       
   893                 else
       
   894                     {
       
   895                     aPropertyValue[index] = 0;
       
   896                     }
       
   897                 break;
       
   898                 
       
   899             default:
       
   900                 break;
       
   901                 
       
   902             }
       
   903         }
       
   904     }
       
   905 
       
   906 
       
   907 // -----------------------------------------------------------------------------
       
   908 // CRecognitionAlgMgr::LoadEnginePropertiesL
       
   909 // Loads properties to engine, invalid IDs are neglected.
       
   910 // -----------------------------------------------------------------------------
       
   911 //
       
   912 void CRecognitionAlgMgr::LoadEnginePropertiesL( const RArray<TInt>& aParameterId,
       
   913                                                const RArray<TInt>& aParameterValue )
       
   914     {
       
   915     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::LoadEnginePropertiesL()" );
       
   916     
       
   917     for ( TInt index = 0; index < aParameterId.Count(); index++ )
       
   918         {
       
   919         switch( aParameterId[index] )
       
   920             {
       
   921             case KDevASRSendFeatures:
       
   922                 if ( aParameterValue[index] == 0 )
       
   923                     {
       
   924                     iFeatures = EFalse;
       
   925                     }
       
   926                 else
       
   927                     {
       
   928                     iFeatures = ETrue;
       
   929                     }
       
   930                 break;
       
   931                 
       
   932             case KDevASRAdaptation:
       
   933                 if ( aParameterValue[index] == 0 )
       
   934                     {
       
   935                     iAdaptation = EFalse;
       
   936                     }
       
   937                 else
       
   938                     {
       
   939                     iAdaptation = ETrue;
       
   940                     }
       
   941                 break;
       
   942                 
       
   943             default:
       
   944                 break;
       
   945             }
       
   946         }
       
   947     }
       
   948 
       
   949 
       
   950 // -----------------------------------------------------------------------------
       
   951 // CRecognitionAlgMgr::SendSpeechData
       
   952 // Sends utterance data to recognizer.
       
   953 // -----------------------------------------------------------------------------
       
   954 //
       
   955 void CRecognitionAlgMgr::SendSpeechData( TPtrC8& aBuffer, TBool aEnd )
       
   956     {
       
   957     iRecoHw->SendSpeechData( aBuffer, aEnd );
       
   958     }
       
   959 
       
   960 
       
   961 // -----------------------------------------------------------------------------
       
   962 // CRecognitionAlgMgr::StartRecSession
       
   963 // Signal the start of a recognition sesion.
       
   964 // -----------------------------------------------------------------------------
       
   965 //
       
   966 TInt CRecognitionAlgMgr::StartRecSession( TRecognizerMode aMode )
       
   967     {
       
   968     RUBY_DEBUG0( "CRecognitionAlgMgr::StartRecSession()" );
       
   969 
       
   970     TInt error( KErrNone );
       
   971 
       
   972     if ( !iInitialized )
       
   973         {
       
   974         return KErrNotReady; 
       
   975         }
       
   976 
       
   977     if ( aMode == ESiRecognition )
       
   978         {
       
   979         iMode = aMode;
       
   980         }
       
   981     else if ( aMode == ESiRecognitionSpeechInput )
       
   982         {
       
   983         iMode = aMode;
       
   984         }
       
   985     else
       
   986         {
       
   987         return KErrNotSupported;
       
   988         }
       
   989 
       
   990     return error;
       
   991     }
       
   992 
       
   993 
       
   994 // -----------------------------------------------------------------------------
       
   995 // CRecognitionAlgMgr::StartRecognitionL
       
   996 // Start recognition.
       
   997 // -----------------------------------------------------------------------------
       
   998 //
       
   999 void CRecognitionAlgMgr::StartRecognitionL()
       
  1000     {
       
  1001     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::StartRecognitionL()" );
       
  1002 
       
  1003     if ( !iInitialized )
       
  1004         {
       
  1005         return;
       
  1006         }
       
  1007 
       
  1008     // Reset the result IDs
       
  1009     iNBestList.Reset();
       
  1010     iScores.Reset();
       
  1011 
       
  1012     iRecoHw->StartRecognitionL( iNBestList, iScores );
       
  1013     }
       
  1014 
       
  1015 
       
  1016 // -----------------------------------------------------------------------------
       
  1017 // CRecognitionAlgMgr::EndRecSession
       
  1018 // Signal the end of a recognition sesion.
       
  1019 // -----------------------------------------------------------------------------
       
  1020 //
       
  1021 void CRecognitionAlgMgr::EndRecSession()
       
  1022     {
       
  1023     RUBY_DEBUG0( "CRecognitionAlgMgr::EndRecSession()" );
       
  1024   
       
  1025     // Reset grammars etc.
       
  1026     iSIActiveGrammars.Reset();
       
  1027 	iSIDeActivatedGrammars.Reset();
       
  1028     iNBestList.Reset();
       
  1029     iScores.Reset();
       
  1030     iBlackList.ResetAndDestroy();
       
  1031 
       
  1032     delete iCombinedGrammar;
       
  1033     iCombinedGrammar = NULL;
       
  1034 
       
  1035     iCombineNeeded = ETrue;
       
  1036     iSDModelBank = NULL;
       
  1037     iSIModelBank = NULL;
       
  1038     iSDGrammar = NULL;
       
  1039     iSIGrammar = NULL;
       
  1040     iSICompGrammar = NULL;
       
  1041     iSDCompGrammar = NULL;
       
  1042     iSDLexicon = NULL;
       
  1043     iSILexicon = NULL;
       
  1044     iFEState = EIdle;
       
  1045     iBEState = EIdle;
       
  1046     }
       
  1047 
       
  1048 
       
  1049 // -----------------------------------------------------------------------------
       
  1050 // CRecognitionAlgMgr::Cancel
       
  1051 // Cancels the current and any on going requests/tasks.
       
  1052 // -----------------------------------------------------------------------------
       
  1053 //
       
  1054 void CRecognitionAlgMgr::Cancel()
       
  1055     {
       
  1056     RUBY_DEBUG0( "CRecognitionAlgMgr::Cancel()" );
       
  1057 
       
  1058     iRecoHw->StopRecognition();
       
  1059 
       
  1060     iFEState = EIdle;
       
  1061     iBEState = EIdle;
       
  1062 
       
  1063     iAdaptHw->CancelAdaptation();
       
  1064     
       
  1065     CActive::Cancel();
       
  1066     }
       
  1067 
       
  1068 
       
  1069 // -----------------------------------------------------------------------------
       
  1070 // CRecognitionAlgMgr::DoCancel
       
  1071 // Cancels the current and any on going requests/tasks.
       
  1072 // -----------------------------------------------------------------------------
       
  1073 //
       
  1074 void CRecognitionAlgMgr::DoCancel()
       
  1075     {
       
  1076     RUBY_DEBUG0( "CRecognitionAlgMgr::DoCancel()" );
       
  1077     }
       
  1078 
       
  1079 
       
  1080 // -----------------------------------------------------------------------------
       
  1081 // CRecognitionAlgMgr::GetUtteranceDuration
       
  1082 // Get start and end point of utterance.
       
  1083 // -----------------------------------------------------------------------------
       
  1084 //
       
  1085 TBool CRecognitionAlgMgr::GetUtteranceDuration( TUint32& /*aStartFrame*/,
       
  1086                                                 TUint32& /*aEndFrame*/,
       
  1087                                                 TReal& /*aFrameLength*/ )
       
  1088     {
       
  1089     RUBY_DEBUG0( "CRecognitionAlgMgr::GetUtteranceDuration()" );
       
  1090 
       
  1091     return EFalse;
       
  1092     }
       
  1093 
       
  1094 
       
  1095 // -----------------------------------------------------------------------------
       
  1096 // CRecognitionAlgMgr::UnloadRule
       
  1097 // Unloads one rule from specified grammar.
       
  1098 // -----------------------------------------------------------------------------
       
  1099 //
       
  1100 void CRecognitionAlgMgr::UnloadRule( TSIGrammarID aGrammarID, TSIRuleID aRuleID )
       
  1101     {
       
  1102     RUBY_DEBUG0( "CRecognitionAlgMgr::UnloadRule()" );
       
  1103 
       
  1104     if ( !IsActive() )
       
  1105         {
       
  1106         iGrammarID = aGrammarID;
       
  1107         iRuleID = aRuleID;
       
  1108         iRequestFunction = KUnloadRule;
       
  1109         Ready( KErrNone );
       
  1110         }
       
  1111     }
       
  1112 
       
  1113 
       
  1114 // -----------------------------------------------------------------------------
       
  1115 // CRecognitionAlgMgr::HandleUnloadRule
       
  1116 // Asynchrounous handler for rule unload.
       
  1117 // -----------------------------------------------------------------------------
       
  1118 //
       
  1119 void CRecognitionAlgMgr::HandleUnloadRule()
       
  1120     {
       
  1121     RUBY_DEBUG0( "CRecognitionAlgMgr::HandleUnloadRule()" );
       
  1122 
       
  1123     TInt error = KErrNone;
       
  1124     CSICompiledGrammar* siActiveGrammar = NULL;
       
  1125     CSICompiledGrammar* siNonActiveGrammar = NULL;
       
  1126     CSICompiledGrammar* siGrammar = NULL;
       
  1127 
       
  1128     if ( !iInitialized )
       
  1129         {
       
  1130         iRecAlgMgrObserver->UnloadRuleComplete( KErrNotReady );
       
  1131         }
       
  1132     
       
  1133     TRAP( error, GetGrammarL( iGrammarID, &siActiveGrammar, &siNonActiveGrammar ) );
       
  1134     if ( error != KErrNone )
       
  1135         {
       
  1136         iRecAlgMgrObserver->UnloadRuleComplete( error );
       
  1137         return;
       
  1138         }
       
  1139 
       
  1140     if ( siActiveGrammar != NULL )
       
  1141         {
       
  1142         siGrammar = siActiveGrammar;
       
  1143         }
       
  1144     else if ( siNonActiveGrammar != NULL )
       
  1145         {
       
  1146         siGrammar = siNonActiveGrammar;
       
  1147         }
       
  1148     else
       
  1149         {
       
  1150         iRecAlgMgrObserver->UnloadRuleComplete( KErrNotFound );
       
  1151         return;
       
  1152         }
       
  1153 
       
  1154     // Check that given rule id can be found from the given grammar
       
  1155     if ( siGrammar->Find( iRuleID ) < 0 )
       
  1156         {
       
  1157         iRecAlgMgrObserver->UnloadRuleComplete( KErrNotFound );
       
  1158         return;
       
  1159         }
       
  1160 
       
  1161     // Fetch the actual rule object based on rule id
       
  1162     TRAP_IGNORE( 
       
  1163     
       
  1164         CSIRule& blackRule = siGrammar->AtL( siGrammar->Find( iRuleID ) );
       
  1165      
       
  1166         // Loop through the rule variants in blacklisted rule
       
  1167         for ( TInt i = 0; i < blackRule.Count(); i++ )
       
  1168             {
       
  1169             // AtL should not leave in any case since i is every time
       
  1170             // within range 0..Count()-1
       
  1171             CSIRuleVariant& blackVariant = blackRule.AtL( i );
       
  1172     
       
  1173             // Create rule variant info
       
  1174             TSIRuleVariantInfo* info = new TSIRuleVariantInfo( iGrammarID, iRuleID, blackVariant.RuleVariantID() );
       
  1175             if ( !info )
       
  1176                 {
       
  1177                 iRecAlgMgrObserver->UnloadRuleComplete( KErrNoMemory );
       
  1178                 return;
       
  1179                 }
       
  1180     
       
  1181             iBlackList.Append( info );
       
  1182             }
       
  1183         ); // TRAP_IGNORE
       
  1184 
       
  1185     // Mark that new combination is needed
       
  1186     // iCombineNeeded = ETrue;
       
  1187 
       
  1188     iRecAlgMgrObserver->UnloadRuleComplete( error );
       
  1189     }
       
  1190 
       
  1191 
       
  1192 // -----------------------------------------------------------------------------
       
  1193 // CRecognitionAlgMgr::CombineComplete
       
  1194 // Notifies that grammar combination has been done.
       
  1195 // -----------------------------------------------------------------------------
       
  1196 //
       
  1197 void CRecognitionAlgMgr::CombineComplete( HBufC8* aResult, TInt aError )
       
  1198     {
       
  1199     RUBY_DEBUG0( "CRecognitionAlgMgr::CombineComplete()" );
       
  1200 
       
  1201     if ( aError != KErrNone )
       
  1202         {
       
  1203         iRecAlgMgrObserver->InitRecognizerBEComplete( aError );
       
  1204         return;
       
  1205         }
       
  1206 
       
  1207     delete iCombinedGrammar;
       
  1208     iCombinedGrammar = NULL;
       
  1209     iCombinedGrammar = aResult;
       
  1210 
       
  1211     // Do the rest of BE initialization
       
  1212     TRAPD( error, iRecoHw->InitRecognizerBEL( iCombinedGrammar->Des(), *iSIModelBank ) );
       
  1213     if ( error != KErrNone )
       
  1214         {
       
  1215         iRecAlgMgrObserver->InitRecognizerBEComplete( error );
       
  1216         }
       
  1217     }
       
  1218 
       
  1219 
       
  1220 // -----------------------------------------------------------------------------
       
  1221 // MASRAdaptationHwDeviceObserver MIXIN impl begins
       
  1222 // -----------------------------------------------------------------------------
       
  1223 
       
  1224 // -----------------------------------------------------------------------------
       
  1225 // CRecognitionAlgMgr::MaahdAdaptationComplete
       
  1226 // Model adaptation has been completed.
       
  1227 // -----------------------------------------------------------------------------
       
  1228 //
       
  1229 void CRecognitionAlgMgr::MaahdAdaptationComplete( TInt aError )
       
  1230     {
       
  1231 //    iAdaptHw->ClearAdaptation();
       
  1232     iRecAlgMgrObserver->AdaptComplete( aError );
       
  1233     }
       
  1234 
       
  1235 
       
  1236 // -----------------------------------------------------------------------------
       
  1237 // MASRAdaptationHwDeviceObserver MIXIN impl begins
       
  1238 // -----------------------------------------------------------------------------
       
  1239 
       
  1240 
       
  1241 
       
  1242 // -----------------------------------------------------------------------------
       
  1243 // MASRSRecogHwDeviceObserver MIXIN impl begins
       
  1244 // -----------------------------------------------------------------------------
       
  1245 
       
  1246 
       
  1247 // -----------------------------------------------------------------------------
       
  1248 // CRecognitionAlgMgr::MarhdoRequestSpeechData
       
  1249 // Forwards request to upper layers.
       
  1250 // -----------------------------------------------------------------------------
       
  1251 //
       
  1252 void CRecognitionAlgMgr::MarhdoRequestSpeechData()
       
  1253     {
       
  1254     iRecAlgMgrObserver->RequestSpeechData();
       
  1255     }
       
  1256 
       
  1257 
       
  1258 // -----------------------------------------------------------------------------
       
  1259 // CRecognitionAlgMgr::MarhdoInitializationComplete
       
  1260 // Initialization of Hw Device has been completed.
       
  1261 // -----------------------------------------------------------------------------
       
  1262 //
       
  1263 void CRecognitionAlgMgr::MarhdoInitializationComplete( TInt aError )
       
  1264     {
       
  1265     if ( aError == KErrNone )
       
  1266         {
       
  1267         iInitialized = ETrue;
       
  1268         }
       
  1269     }
       
  1270 
       
  1271 
       
  1272 // -----------------------------------------------------------------------------
       
  1273 // CRecognitionAlgMgr::MarhdoRecognizerFEComplete
       
  1274 // FE has been initialized.
       
  1275 // -----------------------------------------------------------------------------
       
  1276 //
       
  1277 void CRecognitionAlgMgr::MarhdoInitRecognizerFEComplete( TInt aError )
       
  1278     {
       
  1279     RUBY_DEBUG1( "CRecognitionAlgMgr::MarhdoInitRecognizerFEComplete(), Status: %d", aError );
       
  1280 
       
  1281     if ( aError == KErrNone )
       
  1282         {
       
  1283         iFEState = EIdle;
       
  1284         }
       
  1285     iRecAlgMgrObserver->InitFEComplete( aError );
       
  1286     }
       
  1287 
       
  1288 
       
  1289 // -----------------------------------------------------------------------------
       
  1290 // CRecognitionAlgMgr::MarhdoRecognizerBEComplete
       
  1291 // BE has been initialized.
       
  1292 // -----------------------------------------------------------------------------
       
  1293 //
       
  1294 void CRecognitionAlgMgr::MarhdoInitRecognizerBEComplete( TInt aError )
       
  1295     {
       
  1296     RUBY_DEBUG1( "CRecognitionAlgMgr::MarhdoInitRecognizerBEComplete(), Status: %d", aError );
       
  1297 
       
  1298     if ( aError == KErrNone )
       
  1299         {
       
  1300         iBEState = EIdle;
       
  1301         }
       
  1302     iRecAlgMgrObserver->InitRecognizerBEComplete( aError );
       
  1303     }
       
  1304 
       
  1305 
       
  1306 // -----------------------------------------------------------------------------
       
  1307 // CRecognitionAlgMgr::MarhdoRecognitionComplete
       
  1308 // Recognition has been completed
       
  1309 // -----------------------------------------------------------------------------
       
  1310 //
       
  1311 void CRecognitionAlgMgr::MarhdoRecognitionComplete( TInt aError )
       
  1312     {
       
  1313     RUBY_DEBUG1( "CRecognitionAlgMgr::MarhdoRecognitionComplete, Status: %d", aError );
       
  1314 
       
  1315     if ( iCombinedGrammar == NULL )
       
  1316         {
       
  1317         RUBY_DEBUG0( "CRecognitionAlgMgr::HandleMarhdoRecognitionComplete, Cancelled" );
       
  1318         delete iAdaptationData;
       
  1319         iAdaptationData = NULL;
       
  1320         iRecAlgMgrObserver->RecognitionComplete( KErrCancel );
       
  1321         return;
       
  1322         }
       
  1323 
       
  1324      // Resolve result
       
  1325     iRecAlgMgrObserver->ResolveResult( iNBestList, *iSIResult, iSIActiveGrammars, 
       
  1326                                        iCombinedGrammar->Des()/*, *iSIModelBank*/ );
       
  1327 
       
  1328 
       
  1329     RUBY_DEBUG1( "CRecognitionAlgMgr::MarhdoRecognitionComplete, result resolved, result count: %d", iSIResult->Count() );
       
  1330 
       
  1331     TRAP_IGNORE(
       
  1332 
       
  1333         // Put scores in place
       
  1334         for ( TInt i = 0; i < iSIResult->Count(); i++ )
       
  1335             {
       
  1336             // Should not leave since we are every time within range
       
  1337             CSIResult& result = iSIResult->AtL( i );
       
  1338 
       
  1339             if ( i < iScores.Count() )
       
  1340                 {
       
  1341                 result.SetScore( iScores[i] );
       
  1342                 }
       
  1343             else
       
  1344                 {
       
  1345                 result.SetScore( 0 );
       
  1346                 }
       
  1347             }
       
  1348             
       
  1349         ); // TRAP_IGNORE
       
  1350     
       
  1351     iScores.Reset();
       
  1352 
       
  1353     RUBY_DEBUG0( "CRecognitionAlgMgr::MarhdoRecognitionComplete, scores in place" );
       
  1354 
       
  1355     // Put adaptation data in place if needed
       
  1356     if ( iAdaptation )
       
  1357         {
       
  1358         iSIResult->SetAdaptationData( iAdaptationData );
       
  1359         // Result set gains ownership, no need to delete
       
  1360         iAdaptationData = NULL;
       
  1361         RUBY_DEBUG0( "CRecognitionAlgMgr::MarhdoRecognitionComplete, adaptation data in place" );
       
  1362         }
       
  1363 
       
  1364     iBEState = EIdle;
       
  1365     iFEState = EIdle;
       
  1366     iRecAlgMgrObserver->RecognitionComplete( aError );
       
  1367 
       
  1368     RUBY_DEBUG0( "CRecognitionAlgMgr::MarhdoRecognitionComplete, ok, event sent" );
       
  1369     }
       
  1370 
       
  1371 
       
  1372 // -----------------------------------------------------------------------------
       
  1373 // CRecognitionAlgMgr::MarhdoEouDetected
       
  1374 // Called when EOU has been detected, no need to send samples anymore.
       
  1375 // -----------------------------------------------------------------------------
       
  1376 //
       
  1377 void CRecognitionAlgMgr::MarhdoEouDetected( TInt aError )
       
  1378     {
       
  1379     RUBY_DEBUG1( "CRecognitionAlgMgr::MarhdoEouDetected(), Status: %d", aError );
       
  1380 
       
  1381     iRecAlgMgrObserver->EouDetected( aError );
       
  1382     }
       
  1383 
       
  1384 
       
  1385 // -----------------------------------------------------------------------------
       
  1386 // CRecognitionAlgMgr::MarhdoFeatureVector
       
  1387 // Feature vector sent by AsrRecognitionHwDevice
       
  1388 // -----------------------------------------------------------------------------
       
  1389 //
       
  1390 void CRecognitionAlgMgr::MarhdoFeatureVector( const TDesC8& aFV, 
       
  1391                                               TInt32 aSNR, 
       
  1392                                               TInt32 aPosition )
       
  1393     {
       
  1394     RUBY_DEBUG0( "CRecognitionAlgMgr::FeatureVectorDataRcvd()" );
       
  1395 
       
  1396     // Collect data to one buffer
       
  1397     if ( iAdaptation )
       
  1398         {
       
  1399         if ( aPosition == 0 )
       
  1400             {
       
  1401             // Clear previous data if exists
       
  1402             if ( iAdaptationData )
       
  1403                 {
       
  1404                 delete iAdaptationData;
       
  1405                 iAdaptationData = NULL;
       
  1406                 }
       
  1407             // Copy data to new buffer
       
  1408             iAdaptationData = HBufC8::New( aFV.Length() );
       
  1409             if ( !iAdaptationData )
       
  1410                 {
       
  1411                 return;
       
  1412                 }
       
  1413             TPtr8 bufferPtr( iAdaptationData->Des() );
       
  1414             bufferPtr.Copy( aFV.Ptr(), aFV.Length() );
       
  1415             }
       
  1416         else if ( aPosition > 0 )
       
  1417             {
       
  1418             if ( iAdaptationData != NULL )
       
  1419                 {
       
  1420                 // Append data to existing buffer
       
  1421                 iAdaptationData = iAdaptationData->ReAlloc( iAdaptationData->Length() + aFV.Length() );
       
  1422                 // Check that we got enough memory for new buffer
       
  1423                 if ( iAdaptationData == NULL )
       
  1424                     {
       
  1425                     return;
       
  1426                     }
       
  1427                 TPtr8 bufferPtr( iAdaptationData->Des() );
       
  1428                 bufferPtr.Append( aFV );
       
  1429                 }
       
  1430             else
       
  1431                 {
       
  1432                 // Do a new buffer since there is no existing one
       
  1433                 iAdaptationData = HBufC8::New( aFV.Length() );
       
  1434                 if ( !iAdaptationData )
       
  1435                     {
       
  1436                     return;
       
  1437                     }
       
  1438                 TPtr8 bufferPtr( iAdaptationData->Des() );
       
  1439                 bufferPtr.Copy( aFV.Ptr(), aFV.Length() );
       
  1440                 }
       
  1441             }
       
  1442         }
       
  1443     // Same callback to client if needed
       
  1444     if ( iFeatures )
       
  1445         {
       
  1446         iRecAlgMgrObserver->FeatureVectorDataRcvd( aFV, aSNR, aPosition );
       
  1447         }
       
  1448     }
       
  1449 
       
  1450 
       
  1451 // -----------------------------------------------------------------------------
       
  1452 // MASRSRecogHwDeviceObserver MIXIN impl ends
       
  1453 // -----------------------------------------------------------------------------
       
  1454 
       
  1455 
       
  1456 // -----------------------------------------------------------------------------
       
  1457 // Active object implementation begins
       
  1458 // -----------------------------------------------------------------------------
       
  1459 
       
  1460 
       
  1461 // -----------------------------------------------------------------------------
       
  1462 // CRecognitionAlgMgr::RunL
       
  1463 // Invoke by the active scheduler when a request completes.
       
  1464 // -----------------------------------------------------------------------------
       
  1465 //
       
  1466 void CRecognitionAlgMgr::RunL()
       
  1467     {
       
  1468     RUBY_DEBUG_BLOCK( "CRecognitionAlgMgr::RunL()" );
       
  1469 
       
  1470     switch(iRequestFunction)
       
  1471         {
       
  1472         case KInitFrontEnd:
       
  1473             HandleInitFrontEnd();
       
  1474             break;
       
  1475             
       
  1476         case KInitRecognizerBE:
       
  1477             HandleInitRecognizerBE();
       
  1478             break;
       
  1479             
       
  1480         case KLoadGrammar:
       
  1481             HandleLoadGrammar();
       
  1482             break;
       
  1483             
       
  1484         case KLoadLexicon:
       
  1485             HandleLoadLexicon();
       
  1486             break;
       
  1487             
       
  1488         case KLoadModels:
       
  1489             HandleLoadModels();
       
  1490             break;
       
  1491             
       
  1492         case KActivateGrammar:
       
  1493             HandleActivateGrammar();
       
  1494             break;
       
  1495 
       
  1496         case KDeActivateGrammar:
       
  1497             HandleDeActivateGrammar();
       
  1498             break;
       
  1499 
       
  1500         case KUnloadRule:
       
  1501             HandleUnloadRule();
       
  1502             break;
       
  1503 
       
  1504         case KUnloadGrammar:
       
  1505             HandleUnloadGrammar();
       
  1506             break;
       
  1507         
       
  1508         default:
       
  1509             break;
       
  1510         };
       
  1511     
       
  1512     }
       
  1513 
       
  1514 
       
  1515 // -----------------------------------------------------------------------------
       
  1516 // CRecognitionAlgMgr::Ready
       
  1517 // Utility function to post a request complete
       
  1518 // -----------------------------------------------------------------------------
       
  1519 //
       
  1520 void CRecognitionAlgMgr::Ready( const TInt aStatus )
       
  1521     {
       
  1522     TRequestStatus* stat = &iStatus;
       
  1523     User::RequestComplete( stat, aStatus );
       
  1524     SetActive();
       
  1525     }
       
  1526 
       
  1527 
       
  1528 // -----------------------------------------------------------------------------
       
  1529 // Active object impl ends
       
  1530 // -----------------------------------------------------------------------------
       
  1531 
       
  1532 
       
  1533 // -----------------------------------------------------------------------------
       
  1534 // CRecognitionAlgMgr::AlgorithmState
       
  1535 // Utility function to print out the state of the algorithms.
       
  1536 // -----------------------------------------------------------------------------
       
  1537 //
       
  1538 void CRecognitionAlgMgr::AlgorithmState()
       
  1539     {
       
  1540 #ifdef _DEBUG
       
  1541     switch ( iFEState )
       
  1542         {
       
  1543         case EIdle:
       
  1544             RUBY_DEBUG1( "Recognition algorithm is: [%d] - EIdle", iFEState );
       
  1545             break;
       
  1546             
       
  1547         case EProcessing:
       
  1548             RUBY_DEBUG1( "Recognition algorithm is: [%d] - EProcessing", iFEState );
       
  1549             break;
       
  1550             
       
  1551         case ECancel:
       
  1552             RUBY_DEBUG1( "Recognition algorithm is: [%d] - ECancel", iFEState );
       
  1553             break;
       
  1554             
       
  1555         default:
       
  1556             break;
       
  1557         };
       
  1558 #endif
       
  1559     }
       
  1560     
       
  1561 // -----------------------------------------------------------------------------
       
  1562 // CRecognitionAlgMgr::SetRejection
       
  1563 // Set rejection threshold.
       
  1564 // -----------------------------------------------------------------------------
       
  1565 //
       
  1566 void CRecognitionAlgMgr::SetRejection( TUint32 aRejection )
       
  1567     {
       
  1568     RUBY_DEBUG0( "CRecognitionAlgMgr::SetRejection()" );
       
  1569 
       
  1570     iRecoHw->SetRejection( aRejection );
       
  1571     }