srsf/devasr/src/devasrsrsalgorithmmanager.cpp
branchRCL_3
changeset 19 e36f3802f733
parent 0 bf1d17376201
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 Algorithm Manager,
       
    15 *               a submodule of DevASR.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <mmfdatabuffer.h>
       
    22 #include <AudioPreference.h>
       
    23 #include <nssdevasr.h>
       
    24 #include "devasrsrsalgorithmmanager.h"
       
    25 #include "devasrvmalgorithmmanager.h"
       
    26 #include "rubydebug.h"
       
    27 #include "devasrcenrep.h"
       
    28 #include "srsfbldvariant.hrh"
       
    29 
       
    30 // CONSTANTS
       
    31 const TInt KPlay = 1;
       
    32 const TInt KRecord = KPlay + 1;
       
    33 
       
    34 // Extra error values
       
    35 const TInt KErrTooEarly = -100; // if speech was started too early
       
    36 const TInt KErrNoSpeech = -101; // if it was silent 
       
    37 const TInt KErrTooLong  = -102; // if speech did not stop before timeout
       
    38 const TInt KErrTooShort = -103; // if speech was too short
       
    39 
       
    40 // Default priority for recording
       
    41 const TInt KDefaultPriority = KAudioPriorityVoiceDial;
       
    42 
       
    43 // ============================ MEMBER FUNCTIONS ===============================
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CSRSAlgorithmManager::CSRSAlgorithmManager
       
    47 // C++ default constructor can NOT contain any code, that
       
    48 // might leave.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 CSRSAlgorithmManager::CSRSAlgorithmManager( MDevASRObserver& aObserver )
       
    52                                            : CActive( CActive::EPriorityStandard ),
       
    53                                              iDevASRObserver( &aObserver ),
       
    54                                              iDevSound( NULL ),
       
    55                                              iCurrentVolume( 0 ),
       
    56                                              iCurrentGain( 0 ),
       
    57                                              iRecognitionAlgMgr( NULL ),
       
    58                                              iVMAlgorithmManager( NULL ),
       
    59                                              iStartRecognition( EFalse ),
       
    60                                              iPendingRequestSpeechData( EFalse ),
       
    61                                              iStartPoint( 0 ),
       
    62                                              iStopPoint( 0 ),
       
    63                                              iAudioBuffer( NULL ),
       
    64                                              iPtr( NULL, 0, 0 ),
       
    65                                              iOverallLength( 0 ),
       
    66                                              iOverallSampled( 0 ),
       
    67                                              iProcessingUtterance( EFalse ),
       
    68                                              iEndFlag( 0 ),
       
    69                                              iStartFrame( 0 ),
       
    70                                              iEndFrame( 0 ),
       
    71                                              iFrameLength( 0 ),
       
    72                                              iBufferStartPoint( 0 ),
       
    73                                              iBufferEndPoint( 0 ),
       
    74                                              iDevASRState( EDASRIdle ),
       
    75                                              iDevSoundState( ESoundDeviceNotInitialized ),
       
    76                                              iRequestFunction( 0 ),
       
    77                                              iBufferUnderConstruction( NULL ),
       
    78                                              iConstructionPoint( 0 ),
       
    79                                              iPreSamplingStarted( EFalse )
       
    80     {
       
    81     RUBY_DEBUG0( "CSRSAlgorithmManager::CSRSAlgorithmManager()" );
       
    82 #ifdef AUDIOBUFFER_TO_FILE
       
    83     iFileCreated = EFalse;
       
    84 #endif
       
    85 
       
    86     }
       
    87 
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CSRSAlgorithmManager::ConstructL
       
    91 // Symbian 2nd phase constructor can leave.
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void CSRSAlgorithmManager::ConstructL()
       
    95     {
       
    96     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::ConstructL()" );
       
    97 
       
    98     // Resource handler reads the DevASR resource file
       
    99     iResourceHandler = CDevASRResourceHandler::NewL();
       
   100 
       
   101     iRecognitionAlgMgr = CRecognitionAlgMgr::NewL( *this );
       
   102     iVMAlgorithmManager = CVMAlgorithmManager::NewL( *iDevASRObserver, *this );
       
   103 
       
   104     // Queue containing audio buffers
       
   105     iAudioBufferQue = new ( ELeave ) TSglQue<CQueItem>( _FOFF( CQueItem, iLink ) );
       
   106 
       
   107     // Set default priority settings
       
   108     iPrioritySettings.iState = EMMFStateRecording;
       
   109     iPrioritySettings.iPriority = KDefaultPriority;
       
   110     iPrioritySettings.iPref = ( TMdaPriorityPreference ) KAudioPrefVocosRecog;
       
   111 
       
   112     // CenRep utility class
       
   113     CDevAsrCenRep* cenRep = CDevAsrCenRep::NewL();
       
   114     CleanupStack::PushL( cenRep );
       
   115     
       
   116     iRecognitionAlgMgr->SetRejection( cenRep->RejectionValue() );
       
   117     
       
   118     CleanupStack::PopAndDestroy( cenRep );
       
   119 
       
   120     CActiveScheduler::Add( this );
       
   121 
       
   122     RUBY_DEBUG1( "associated DevSound instance [%x]", iDevSound );
       
   123     RUBY_DEBUG1( "associated Recognition AlgMgr instance [%x]", iRecognitionAlgMgr );
       
   124     RUBY_DEBUG1( "associated Vocabulary AlgMgr instance [%x]", iRecognitionAlgMgr );
       
   125     }
       
   126 
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CSRSAlgorithmManager::NewL
       
   130 // Two-phased constructor.
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 CSRSAlgorithmManager* CSRSAlgorithmManager::NewL( MDevASRObserver& aObserver )
       
   134     {
       
   135     CSRSAlgorithmManager* self = new( ELeave ) CSRSAlgorithmManager( aObserver );
       
   136 
       
   137     CleanupStack::PushL( self );
       
   138     self->ConstructL();
       
   139     CleanupStack::Pop( self );
       
   140 
       
   141     return self;
       
   142     }
       
   143 
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CSRSAlgorithmManager::~CSRSAlgorithmManager
       
   147 // Destructor.
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 CSRSAlgorithmManager::~CSRSAlgorithmManager()
       
   151     {
       
   152     RUBY_DEBUG0( "CSRSAlgorithmManager::~CSRSAlgorithmManager()" );
       
   153 
       
   154     //delete iAudioBuffer;
       
   155     delete iQueItem;
       
   156 
       
   157     // Empty audio queue if there happens to be something
       
   158     if ( iAudioBufferQue )
       
   159         {
       
   160         while ( !iAudioBufferQue->IsEmpty() )
       
   161             {
       
   162             iQueItem = iAudioBufferQue->First();
       
   163             iAudioBufferQue->Remove( *iQueItem );
       
   164             delete iQueItem;
       
   165             }
       
   166 
       
   167         delete iAudioBufferQue;
       
   168         }
       
   169 
       
   170     delete iBufferUnderConstruction;
       
   171     iBufferUnderConstruction = NULL;
       
   172     iConstructionPoint = 0;
       
   173 
       
   174     delete iDevSound;
       
   175     delete iRecognitionAlgMgr;
       
   176     delete iVMAlgorithmManager;
       
   177     delete iResourceHandler;
       
   178     }
       
   179 
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CSRSAlgorithmManager::AdaptL
       
   183 // Adapts vocabulary and models based on correct result.
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 void CSRSAlgorithmManager::AdaptL( const CSIResultSet& aResultSet, 
       
   187                                    TInt aResultIndex )
       
   188     {
       
   189     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::AdaptL()" );
       
   190 
       
   191     // Adapt vocab
       
   192     iVMAlgorithmManager->AdaptVocabL( aResultSet, aResultIndex );
       
   193 
       
   194     // Find language of correct result
       
   195     const CSIResult& iCorrectResult = aResultSet.AtL( aResultIndex );
       
   196     TSIGrammarID grammarID = iCorrectResult.GrammarID();
       
   197     TSIRuleID ruleID = iCorrectResult.RuleID();
       
   198     TSIRuleVariantID ruleVariantID = iCorrectResult.RuleVariantID();
       
   199     CSICompiledGrammar* siActiveGrammar = NULL;
       
   200     CSICompiledGrammar* siNonActiveGrammar = NULL;
       
   201     CSICompiledGrammar* siGrammar = NULL;
       
   202   
       
   203     TRAPD( error, GetGrammarL( grammarID, &siActiveGrammar, &siNonActiveGrammar ) );
       
   204     User::LeaveIfError( error );
       
   205 
       
   206     TInt index = 0;
       
   207 
       
   208     if ( siActiveGrammar != NULL )
       
   209         {
       
   210         siGrammar = siActiveGrammar;
       
   211         }
       
   212     else if ( siNonActiveGrammar != NULL )
       
   213         {
       
   214         siGrammar = siNonActiveGrammar;
       
   215         }
       
   216     else
       
   217         {
       
   218         User::Leave( KErrNotFound );	
       
   219         }
       
   220     
       
   221     index = siGrammar->Find( ruleID );
       
   222     User::LeaveIfError( index );
       
   223     CSIRule& rule = siGrammar->AtL( index );
       
   224 
       
   225     index = rule.Find( ruleVariantID );
       
   226     User::LeaveIfError( index );
       
   227     CSIRuleVariant& ruleVariant = rule.AtL( index );
       
   228 
       
   229     // Adapt models
       
   230     iRecognitionAlgMgr->AdaptModelsL( aResultSet, aResultIndex, ruleVariant.Language() );
       
   231     }
       
   232 
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CSRSAlgorithmManager::CompileGrammarL
       
   236 // Forward call to CVMAlgorithmManager.
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 void CSRSAlgorithmManager::CompileGrammarL( CSICompiledGrammar& aGrammar/*,
       
   240                                             CSIModelBank& aModelBank*/ )
       
   241     {
       
   242     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::CompileGrammarL()" );
       
   243 
       
   244     iVMAlgorithmManager->CompileGrammarL( aGrammar/*, aModelBank*/ );
       
   245     }
       
   246 
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CSRSAlgorithmManager::ComputeNewGrammarSizeL
       
   250 // Forward call to CVMAlgorithmManager.
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 void CSRSAlgorithmManager::ComputeNewGrammarSizeL( const CSIGrammar& aGrammar, 
       
   254                                                    const TUint32 aTargetNRuleVariants, 
       
   255                                                    const TUint32 aMaxNRuleVariants, 
       
   256                                                    const RArray<TUint>& aNewRuleScores, 
       
   257                                                    RArray<TUint>& aNNeNRuleVariants, 
       
   258                                                    TUint32& aNPrune )
       
   259     {
       
   260     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::ComputeNewGrammarSizeL()" );
       
   261 
       
   262     iVMAlgorithmManager->ComputeNewGrammarSizeL( aGrammar,
       
   263                                                  aTargetNRuleVariants,
       
   264                                                  aMaxNRuleVariants,
       
   265                                                  aNewRuleScores,
       
   266                                                  aNNeNRuleVariants,
       
   267                                                  aNPrune );
       
   268     }
       
   269 
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // CSRSAlgorithmManager::PruneGrammar
       
   273 // Forward call to CVMAlgorithmManager.
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 TBool CSRSAlgorithmManager::PruneGrammar( const CSIGrammar& aGrammar, 
       
   277                                           const TUint32 aMinNumber, 
       
   278                                           RArray<TSIRuleVariantInfo>& aPrunedRuleVariants )
       
   279     {
       
   280     return iVMAlgorithmManager->PruneGrammar( aGrammar, aMinNumber, aPrunedRuleVariants );
       
   281     }
       
   282 
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CSRSAlgorithmManager::StartTrainingFromTextL
       
   286 // Forward call to CVMAlgorithmManager.
       
   287 // -----------------------------------------------------------------------------
       
   288 // 
       
   289 void CSRSAlgorithmManager::StartTrainingFromTextL( CSITtpWordList& aWordList, 
       
   290                                                    const RArray<TLanguage>& aDefaultLanguage, 
       
   291                                                    const RArray<TUint32>& aMaxNPronunsForWord )
       
   292     {
       
   293     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::StartTrainingFromTextL()" );
       
   294 
       
   295     iVMAlgorithmManager->TrainFromTextL( aWordList, aDefaultLanguage, aMaxNPronunsForWord );
       
   296     }
       
   297 
       
   298 
       
   299 // -----------------------------------------------------------------------------
       
   300 // CSRSAlgorithmManager::ResolveResult
       
   301 // Forward call to CVMAlgorithmManager.
       
   302 // -----------------------------------------------------------------------------
       
   303 // 
       
   304 void CSRSAlgorithmManager::ResolveResult( const RArray<TUint>& aNBestIDs,
       
   305                                           CSIResultSet& aSIResultSet,
       
   306                                           const RPointerArray<CSICompiledGrammar>& aSICompiledGrammar,
       
   307                                           const TDesC8& aCombinedData/*,
       
   308                                           CSIModelBank& iSIModelBank*/ )
       
   309     {
       
   310     iVMAlgorithmManager->ResolveResult( aNBestIDs, aSIResultSet, 
       
   311                                         aSICompiledGrammar, aCombinedData/*,
       
   312                                         iSIModelBank*/ );
       
   313     }
       
   314 
       
   315 
       
   316 // -----------------------------------------------------------------------------
       
   317 // CSRSAlgorithmManager::CombineGrammarL
       
   318 // Forward call to CVMAlgorithmManager.
       
   319 // -----------------------------------------------------------------------------
       
   320 // 
       
   321 void CSRSAlgorithmManager::CombineGrammarL( const RPointerArray<CSICompiledGrammar>& aCompiledGrammars,
       
   322                                             const RPointerArray<TSIRuleVariantInfo>& aExcludedRules )
       
   323     {
       
   324     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::CombineGrammarL()" );
       
   325 
       
   326     iVMAlgorithmManager->CombineGrammarL( aCompiledGrammars, aExcludedRules );
       
   327     }
       
   328 
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // CSRSAlgorithmManager::ConfigureSoundDeviceL
       
   332 // Configure sound device according to resource file parameters
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 void CSRSAlgorithmManager::ConfigureSoundDeviceL()
       
   336     {
       
   337     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::ConfigureSoundDeviceL()" );
       
   338 
       
   339 	// Fetch the default configuration from DevSound
       
   340     iDevSoundCapabilities = iDevSound->Config();
       
   341 
       
   342 	// Now, configure it according to resource file parameters
       
   343     switch ( iResourceHandler->iSamplingRate )
       
   344 		{
       
   345 		case 8000:
       
   346 			iDevSoundCapabilities.iRate = EMMFSampleRate8000Hz;
       
   347 			break;
       
   348 		case 11025:
       
   349 			iDevSoundCapabilities.iRate = EMMFSampleRate11025Hz;
       
   350 			break;
       
   351 		case 16000:
       
   352 			iDevSoundCapabilities.iRate = EMMFSampleRate16000Hz;
       
   353 			break;
       
   354 		case 22050:
       
   355 			iDevSoundCapabilities.iRate = EMMFSampleRate22050Hz;
       
   356 			break;
       
   357 		case 32000:
       
   358 			iDevSoundCapabilities.iRate = EMMFSampleRate32000Hz;
       
   359 			break;
       
   360 		case 44100:
       
   361 			iDevSoundCapabilities.iRate = EMMFSampleRate44100Hz;
       
   362 			break;
       
   363 		case 48000:
       
   364 			iDevSoundCapabilities.iRate = EMMFSampleRate48000Hz;
       
   365 			break;
       
   366 		case 88200:
       
   367 			iDevSoundCapabilities.iRate = EMMFSampleRate88200Hz;
       
   368 			break;
       
   369 		case 96000:
       
   370 			iDevSoundCapabilities.iRate = EMMFSampleRate96000Hz;
       
   371 			break;
       
   372 		default:
       
   373 			iDevSoundCapabilities.iRate = EMMFSampleRate8000Hz;
       
   374 			break;
       
   375 		}
       
   376 
       
   377     iDevSoundCapabilities.iBufferSize = iResourceHandler->iBufferSize;
       
   378 
       
   379     if ( iResourceHandler->iBitsPerSample == 8 )
       
   380         {
       
   381         iDevSoundCapabilities.iEncoding = EMMFSoundEncoding8BitPCM;
       
   382         }
       
   383     else
       
   384         {
       
   385         iDevSoundCapabilities.iEncoding = EMMFSoundEncoding16BitPCM;
       
   386         }
       
   387 
       
   388 	// Now, configure DevSound according to new parameters.
       
   389     iDevSound->SetConfigL( iDevSoundCapabilities );
       
   390 
       
   391     RUBY_DEBUG1( "DevSound Max Volume = %d", iDevSound->MaxVolume() );
       
   392     RUBY_DEBUG1( "DevSound Max Gain = %d", iDevSound->MaxGain() );
       
   393 
       
   394     // In order to avoid truncation, computation must be done this way.
       
   395     iCurrentVolume = ( ( iDevSound->MaxVolume() * iResourceHandler->iSpeakerVolume ) + 50 ) / 100;
       
   396     iCurrentGain = ( ( iDevSound->MaxGain() * iResourceHandler->iMicrophoneGain ) + 50 ) / 100;
       
   397 
       
   398     // The speaker volume is set to the percentage of max volume supported.
       
   399     iDevSound->SetVolume( iCurrentVolume );
       
   400     // The gain is set to the percentage of max gain supported.
       
   401     iDevSound->SetGain( iCurrentGain );
       
   402 
       
   403     iDevSoundCapabilities = iDevSound->Config();
       
   404 
       
   405     RUBY_DEBUG1( "DevSound Set Volume = %d", iCurrentVolume );
       
   406     RUBY_DEBUG1( "DevSound Set Gain = %d", iCurrentGain );
       
   407     RUBY_DEBUG1( "DevSound Buffer Size = %d", iDevSoundCapabilities.iBufferSize );
       
   408     }
       
   409 
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CSRSAlgorithmManager::GetEnginePropertiesL
       
   413 // Retreive the properties of the underlying speech recognition engine.
       
   414 // An array of values corresponding to the querried identifiers will be populated.
       
   415 // The function may leave with KErrArgument.
       
   416 // -----------------------------------------------------------------------------
       
   417 //
       
   418 void CSRSAlgorithmManager::GetEnginePropertiesL( const RArray<TInt>& aPropertyId,
       
   419                                                  RArray<TInt>& aPropertyValue )
       
   420     {
       
   421     RUBY_DEBUG_BLOCK( "SRSAlgorithmManager::EnginePropertiesL()" );
       
   422 
       
   423     // Check that arrays are ok
       
   424     if ( aPropertyId.Count() != aPropertyValue.Count() )
       
   425         {
       
   426         User::Leave( KErrArgument );
       
   427         }
       
   428 
       
   429 	// Run through the engine properties and fill in known values
       
   430     for ( TInt index = 0; index < aPropertyId.Count(); index++)
       
   431         {
       
   432         switch( aPropertyId[index] )
       
   433             {
       
   434             case KSamplingRate:
       
   435                 aPropertyValue[index] = iResourceHandler->iSamplingRate;
       
   436                 break;
       
   437 
       
   438             case KBitsPerSample:
       
   439                 aPropertyValue[index] = iResourceHandler->iBitsPerSample;
       
   440                 break;
       
   441 
       
   442             default:
       
   443                 break;
       
   444 
       
   445             }
       
   446         }
       
   447 	// Get engine properties from other sources.
       
   448     iRecognitionAlgMgr->GetEnginePropertiesL( aPropertyId, aPropertyValue );
       
   449     iVMAlgorithmManager->GetEnginePropertiesL( aPropertyId, aPropertyValue );
       
   450     }
       
   451 
       
   452 
       
   453 // -----------------------------------------------------------------------------
       
   454 // CSRSAlgorithmManager::InitFrontEnd
       
   455 // Initializes the recognition front end.
       
   456 // -----------------------------------------------------------------------------
       
   457 //
       
   458 void CSRSAlgorithmManager::InitFrontEnd( TRecognizerMode aMode )
       
   459     {
       
   460     RUBY_DEBUG0( "CSRSAlgorithmManager::InitFrontEnd()" );
       
   461 
       
   462 	iProcessingUtterance = EFalse;
       
   463 
       
   464     if ( iMode != aMode )
       
   465         {
       
   466         return;
       
   467         }
       
   468 
       
   469     StateTransition( EDASRInitRecognition );
       
   470 
       
   471     iRecognitionAlgMgr->InitFrontEnd( aMode );
       
   472     }
       
   473 
       
   474 
       
   475 // -----------------------------------------------------------------------------
       
   476 // CSRSAlgorithmManager::InitRecognizerBE
       
   477 // Initializes the recognition back-end. The module responsible for recognition
       
   478 // function is started as a result.
       
   479 // -----------------------------------------------------------------------------
       
   480 //
       
   481 void CSRSAlgorithmManager::InitRecognizerBE( CSIResultSet& aResult )
       
   482     {
       
   483     RUBY_DEBUG0( "CSRSAlgorithmManager::InitRecognizerBE()" );
       
   484 
       
   485     iRecognitionAlgMgr->InitRecognizerBE( aResult );
       
   486     }
       
   487 
       
   488 
       
   489 // -----------------------------------------------------------------------------
       
   490 // CSRSAlgorithmManager::IsGrammarLoaded
       
   491 // Checks if grammar has been loaded.
       
   492 // -----------------------------------------------------------------------------
       
   493 //
       
   494 TBool CSRSAlgorithmManager::IsGrammarLoaded( TSIGrammarID aGrammarID )
       
   495     {
       
   496     CSICompiledGrammar* siActiveGrammar = NULL;
       
   497     CSICompiledGrammar* siNonActiveGrammar = NULL;
       
   498   
       
   499     TRAPD( error, GetGrammarL( aGrammarID, &siActiveGrammar, &siNonActiveGrammar ) );
       
   500     if ( error != KErrNone )
       
   501         {
       
   502         return EFalse;
       
   503         }
       
   504 
       
   505     if ( siActiveGrammar != NULL )
       
   506         {
       
   507         return ETrue;
       
   508         }
       
   509     else if ( siNonActiveGrammar != NULL )
       
   510         {
       
   511         return ETrue;
       
   512         }
       
   513 
       
   514     return EFalse;
       
   515     }
       
   516 
       
   517 
       
   518 // -----------------------------------------------------------------------------
       
   519 // CSRSAlgorithmManager::IsGrammarActive
       
   520 // Checks if grammar is currently active.
       
   521 // -----------------------------------------------------------------------------
       
   522 //
       
   523 TBool CSRSAlgorithmManager::IsGrammarActive( TSIGrammarID aGrammarID )
       
   524     {
       
   525     RUBY_DEBUG0( "CSRSAlgorithmManager::IsGrammarActive()" );
       
   526 
       
   527     CSICompiledGrammar* siActiveGrammar = NULL;
       
   528     CSICompiledGrammar* siNonActiveGrammar = NULL;
       
   529   
       
   530     TRAPD( error, GetGrammarL( aGrammarID, &siActiveGrammar, &siNonActiveGrammar ) );
       
   531     if ( error != KErrNone )
       
   532         {
       
   533         return EFalse;
       
   534         }
       
   535 
       
   536     if ( siActiveGrammar != NULL )
       
   537         {
       
   538         return ETrue;
       
   539         }
       
   540     else if ( siNonActiveGrammar != NULL )
       
   541         {
       
   542         return EFalse;
       
   543         }
       
   544 
       
   545     return EFalse;
       
   546     }
       
   547 
       
   548 
       
   549 // -----------------------------------------------------------------------------
       
   550 // CSRSAlgorithmManager::LoadGrammarL
       
   551 // Load the specified grammar into the recognizer.
       
   552 // -----------------------------------------------------------------------------
       
   553 //
       
   554 void CSRSAlgorithmManager::LoadGrammarL( const CSIGrammar& aGrammar )
       
   555     {
       
   556     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::LoadGrammarL(si non-compiled)" );
       
   557 
       
   558     iRecognitionAlgMgr->LoadGrammarL( aGrammar );
       
   559     }
       
   560 
       
   561 
       
   562 // -----------------------------------------------------------------------------
       
   563 // CSRSAlgorithmManager::LoadGrammarL
       
   564 // Load the specified grammar into the recognizer.
       
   565 // -----------------------------------------------------------------------------
       
   566 //
       
   567 void CSRSAlgorithmManager::LoadGrammarL( const CSICompiledGrammar& aGrammar )
       
   568     {
       
   569     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::LoadGrammarL(si compiled)" );
       
   570 
       
   571     iRecognitionAlgMgr->LoadGrammarL( aGrammar );
       
   572     }
       
   573 
       
   574 
       
   575 // -----------------------------------------------------------------------------
       
   576 // CSRSAlgorithmManager::LoadGrammarL
       
   577 // Unloads the specified grammar from the recognizer.
       
   578 // -----------------------------------------------------------------------------
       
   579 //
       
   580 void CSRSAlgorithmManager::UnloadGrammarL( const CSIGrammar& aGrammar )
       
   581     {
       
   582     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::UnloadGrammarL(si non-compiled)" );
       
   583 
       
   584     iRecognitionAlgMgr->UnloadGrammarL( aGrammar );
       
   585     }
       
   586 
       
   587 
       
   588 // -----------------------------------------------------------------------------
       
   589 // CSRSAlgorithmManager::UnloadGrammarL
       
   590 // Unloads the specified grammar from the recognizer.
       
   591 // -----------------------------------------------------------------------------
       
   592 //
       
   593 void CSRSAlgorithmManager::UnloadGrammarL( const CSICompiledGrammar& aGrammar )
       
   594     {
       
   595     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::UnloadGrammarL(si compiled)" );
       
   596 
       
   597     iRecognitionAlgMgr->UnloadGrammarL( aGrammar );
       
   598     }
       
   599 
       
   600 
       
   601 // -----------------------------------------------------------------------------
       
   602 // CSRSAlgorithmManager::ActivateGrammarL
       
   603 // Unload the specified grammar from the recognizer.
       
   604 // -----------------------------------------------------------------------------
       
   605 //
       
   606 void CSRSAlgorithmManager::ActivateGrammarL( const TSIGrammarID aGrammarID )
       
   607     {
       
   608     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::ActivateGrammarL()" );
       
   609 
       
   610     iRecognitionAlgMgr->ActivateGrammarL( aGrammarID );
       
   611     }
       
   612 
       
   613 
       
   614 // -----------------------------------------------------------------------------
       
   615 // CSRSAlgorithmManager::DeactivateGrammarL
       
   616 // Unload the specified grammar from the recognizer.
       
   617 // -----------------------------------------------------------------------------
       
   618 //
       
   619 void CSRSAlgorithmManager::DeactivateGrammarL( const TSIGrammarID aGrammarID )
       
   620     {
       
   621     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::DeactivateGrammarL()" );
       
   622 
       
   623     iRecognitionAlgMgr->DeactivateGrammarL( aGrammarID );
       
   624     }
       
   625 
       
   626 
       
   627 // -----------------------------------------------------------------------------
       
   628 // CSRSRecognitionAlgMgr::GetGrammar
       
   629 // Find grammar based on identifier
       
   630 // -----------------------------------------------------------------------------
       
   631 //
       
   632 void CSRSAlgorithmManager::GetGrammarL( const TSIGrammarID aGrammarID, 
       
   633                                         CSICompiledGrammar** aSIActiveGrammar,
       
   634                                         CSICompiledGrammar** aSIDeActivatedGrammar )
       
   635     {
       
   636     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::GetGrammarL()" );
       
   637 
       
   638     iRecognitionAlgMgr->GetGrammarL( aGrammarID, 
       
   639                                      aSIActiveGrammar,
       
   640                                      aSIDeActivatedGrammar );
       
   641     }
       
   642 
       
   643 
       
   644 // -----------------------------------------------------------------------------
       
   645 // CSRSAlgorithmManager::UnloadRule
       
   646 // Request to unload rule.
       
   647 // -----------------------------------------------------------------------------
       
   648 //
       
   649 void CSRSAlgorithmManager::UnloadRule( TSIGrammarID aGrammarID,
       
   650                                        TSIRuleID aRuleID )
       
   651     {
       
   652     RUBY_DEBUG0( "CSRSAlgorithmManager::UnloadRule()" );
       
   653 
       
   654     iRecognitionAlgMgr->UnloadRule( aGrammarID, aRuleID );
       
   655     }
       
   656 
       
   657 
       
   658 // -----------------------------------------------------------------------------
       
   659 // CSRSAlgorithmManager::LoadEngineParametersL
       
   660 // Load the specified recognizer parameter(s).
       
   661 // -----------------------------------------------------------------------------
       
   662 //
       
   663 void CSRSAlgorithmManager::LoadEnginePropertiesL( const RArray<TInt>& aParameterId,
       
   664                                                   const RArray<TInt>& aParameterValue )
       
   665     {
       
   666     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::LoadEnginePropertiesL()" );
       
   667 
       
   668     // Check that parameter ids are valid, settable values
       
   669     for ( TInt i = 0; i < aParameterId.Count(); i++ )
       
   670         {
       
   671         if ( ( aParameterId[i] != KDevASRSendFeatures ) && 
       
   672              ( aParameterId[i] != KDevASRAdaptation ) )
       
   673             {
       
   674             User::Leave( KErrNotSupported );
       
   675             }
       
   676         }
       
   677     iRecognitionAlgMgr->LoadEnginePropertiesL( aParameterId, aParameterValue );
       
   678     iVMAlgorithmManager->LoadEnginePropertiesL( aParameterId, aParameterValue );
       
   679     }
       
   680 
       
   681 
       
   682 // -----------------------------------------------------------------------------
       
   683 // CSRSAlgorithmManager::LoadLexiconL
       
   684 // Load the specified lexicion into the recognizer.
       
   685 // -----------------------------------------------------------------------------
       
   686 //
       
   687 void CSRSAlgorithmManager::LoadLexiconL( const CSILexicon& aLexicon )
       
   688     {
       
   689     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::LoadLexiconL()" );
       
   690 
       
   691     iSILexicon = &aLexicon;
       
   692 
       
   693     iRecognitionAlgMgr->LoadLexiconL( aLexicon );
       
   694     }
       
   695 
       
   696 
       
   697 // -----------------------------------------------------------------------------
       
   698 // CSRSAlgorithmManager::LoadModelsL
       
   699 // Load the specified models into the recognizer.
       
   700 // -----------------------------------------------------------------------------
       
   701 //
       
   702 void CSRSAlgorithmManager::LoadModelsL( const CSIModelBank& aModels )
       
   703     {
       
   704     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::LoadModelsL()" );
       
   705 
       
   706     iSIModelBank = &aModels;
       
   707 
       
   708     iRecognitionAlgMgr->LoadModelsL( aModels );
       
   709     }
       
   710 
       
   711 
       
   712 // -----------------------------------------------------------------------------
       
   713 // CSRSAlgorithmManager::UtteranceData
       
   714 // Get the recorded audio data accumulated during training.
       
   715 // -----------------------------------------------------------------------------
       
   716 //
       
   717 void CSRSAlgorithmManager::GetUtteranceData( TDes8* /*aBuffer*/, 
       
   718                                              TTimeIntervalMicroSeconds32& /*aDuration*/ )
       
   719     {
       
   720     RUBY_DEBUG0( "CSRSAlgorithmManager::GetUtteranceData()" );
       
   721     // Not supported
       
   722     }
       
   723 
       
   724 
       
   725 // -----------------------------------------------------------------------------
       
   726 // CSRSAlgorithmManager::HandlePlay
       
   727 // Asynchronously handles the play request.
       
   728 // -----------------------------------------------------------------------------
       
   729 //
       
   730 void CSRSAlgorithmManager::HandlePlayL()
       
   731     {
       
   732     RUBY_DEBUG0( "CSRSAlgorithmManager::HandlePlayL()" );
       
   733     }
       
   734 
       
   735 
       
   736 // -----------------------------------------------------------------------------
       
   737 // CSRSAlgorithmManager::StartRecognition
       
   738 // Request to begin recording.
       
   739 // -----------------------------------------------------------------------------
       
   740 //
       
   741 void CSRSAlgorithmManager::StartRecognition( TTimeIntervalMicroSeconds32 aRecordDuration )
       
   742     {
       
   743     RUBY_DEBUG0( "CSRSAlgorithmManager::StartRecognition()" );
       
   744 
       
   745     iEndFlag = EFalse;
       
   746     iRecordDuration = aRecordDuration;
       
   747     iRequestFunction = KRecord;
       
   748     Ready( KErrNone );
       
   749     }
       
   750 
       
   751 // -----------------------------------------------------------------------------
       
   752 // CSRSAlgorithmManager::PreStartSamplingL
       
   753 // Pre-starts sampling
       
   754 // -----------------------------------------------------------------------------
       
   755 //
       
   756 void CSRSAlgorithmManager::PreStartSamplingL()
       
   757     {
       
   758     RUBY_DEBUG_BLOCK( "" );
       
   759 #ifdef __FULLDUPLEX_CHANGE
       
   760     InitializeDevSoundL();
       
   761     StartDevSoundL();
       
   762     iPreSamplingStarted = ETrue;
       
   763 #endif // __FULLDUPLEX_CHANGE
       
   764     }
       
   765 
       
   766 // -----------------------------------------------------------------------------
       
   767 // CSRSAlgorithmManager::StopRecognition
       
   768 // Request to stop recording.
       
   769 // -----------------------------------------------------------------------------
       
   770 //
       
   771 void CSRSAlgorithmManager::StopRecognition()
       
   772     {
       
   773     RUBY_DEBUG0( "CSRSAlgorithmManager::StopRecognition()" );
       
   774 
       
   775     iEndFlag = ETrue;
       
   776     }
       
   777 
       
   778 
       
   779 // -----------------------------------------------------------------------------
       
   780 // CSRSAlgorithmManager::HandleRecord
       
   781 // Asynchronously handles the recording request.
       
   782 // -----------------------------------------------------------------------------
       
   783 //
       
   784 void CSRSAlgorithmManager::HandleRecordL()
       
   785     {
       
   786     RUBY_DEBUG_BLOCK( "CSRSAlgorithmManager::HandleRecordL()" );
       
   787 
       
   788     if ( StateTransition( EDASRSampling ) ) // Attempt to change state
       
   789         {
       
   790         // Allocate a buffer big enough for recording.
       
   791 
       
   792         if ( iRecordDuration.Int() == 0 )
       
   793             {
       
   794             iOverallLength = 0;
       
   795             }
       
   796         else
       
   797             {
       
   798             TInt bytesPerSecond = ( TInt ) ( iResourceHandler->iSamplingRate * ( iResourceHandler->iBitsPerSample / 8 ) );
       
   799             iOverallLength = ( TInt) ( iRecordDuration.Int() * 0.000001 * bytesPerSecond );
       
   800             }
       
   801 
       
   802         iOverallSampled = 0;
       
   803 
       
   804         TInt err = KErrNone;
       
   805 
       
   806 
       
   807         // Start the devSound only if client does not provide utterance data.
       
   808         if ( iMode == ESiRecognition )
       
   809             {
       
   810 #ifdef __FULLDUPLEX_CHANGE
       
   811             if ( !iPreSamplingStarted )
       
   812                 {
       
   813                 TRAP( err, StartDevSoundL() );
       
   814                 }
       
   815 #else            
       
   816             iDevSound->SetPrioritySettings( iPrioritySettings );
       
   817             TRAP( err, iDevSound->RecordInitL() );
       
   818 #endif // __FULLDUPLEX_CHANGE
       
   819 
       
   820             // Mark that recognition should be started when first buffer arrives
       
   821             iStartRecognition = ETrue;
       
   822             }
       
   823         else
       
   824             {
       
   825             TRAPD ( error, iRecognitionAlgMgr->StartRecognitionL() );
       
   826             if ( error != KErrNone )
       
   827                 {
       
   828                 iRecognitionAlgMgr->Cancel();
       
   829                 StateTransition( EDASRIdle );
       
   830                 iDevASRObserver->DevASREvent( EDevASRRecord, error );
       
   831                 }
       
   832             }
       
   833 
       
   834         if ( err == KErrNone )
       
   835             {
       
   836             iDevSoundState = ESoundDeviceRecord;
       
   837             iDevASRObserver->DevASREvent( EDevASRRecordStarted, KErrNone );
       
   838             
       
   839             // Empty audio buffer queue if there happens to be some garbage 
       
   840             // from previous recognition.
       
   841             while ( !iAudioBufferQue->IsEmpty() )
       
   842                 {
       
   843                 iQueItem = iAudioBufferQue->First();
       
   844                 iAudioBufferQue->Remove( *iQueItem );
       
   845                 delete iQueItem;
       
   846                 iQueItem = NULL;
       
   847                 }
       
   848             delete iBufferUnderConstruction;
       
   849             iBufferUnderConstruction = NULL;
       
   850             iConstructionPoint = 0;
       
   851             iPendingRequestSpeechData = EFalse;
       
   852             }
       
   853         else
       
   854             {
       
   855             RUBY_DEBUG1( "RecordInitL() Error: %d", err );
       
   856             iRecognitionAlgMgr->Cancel();
       
   857             StateTransition( EDASRIdle );
       
   858             iDevASRObserver->DevASREvent( EDevASRRecord, err );
       
   859             }
       
   860         }
       
   861     else // Wrong state to handle this request
       
   862         {
       
   863         iDevASRObserver->DevASREvent( EDevASRRecord, KErrAsrInvalidState );
       
   864         
       
   865         RUBY_DEBUG1( "HandleRecord in wrong state: %d", iDevASRState );
       
   866         }
       
   867 
       
   868     }
       
   869 
       
   870 
       
   871 // -----------------------------------------------------------------------------
       
   872 // CSRSAlgorithmManager::StartRecSession
       
   873 // Signal the start of a recognition sesion.
       
   874 // -----------------------------------------------------------------------------
       
   875 //
       
   876 TInt CSRSAlgorithmManager::StartRecSession( TRecognizerMode aMode )
       
   877     {
       
   878     RUBY_DEBUG0( "CSRSAlgorithmManager::StartRecSession()" );
       
   879 
       
   880     TInt err( KErrNone );
       
   881 
       
   882 #ifdef __FULLDUPLEX_CHANGE
       
   883     if ( !iPreSamplingStarted )
       
   884         {
       
   885         TRAP( err, InitializeDevSoundL() );
       
   886         }
       
   887 #else
       
   888     TRAP( err, InitializeDevSoundL() );
       
   889 #endif // __FULLDUPLEX_CHANGE
       
   890 
       
   891     if ( err == KErrNone )
       
   892         {
       
   893         iMode = aMode;
       
   894 
       
   895         err = iRecognitionAlgMgr->StartRecSession( aMode );
       
   896         if ( err == KErrNone )
       
   897             {
       
   898             StateTransition( EDASRPreRecognition );
       
   899             }
       
   900         }
       
   901 
       
   902     return err;
       
   903     }
       
   904 
       
   905 
       
   906 // -----------------------------------------------------------------------------
       
   907 // CSRSAlgorithmManager::EndRecSession
       
   908 // Signal the end of a recognition sesion.
       
   909 // -----------------------------------------------------------------------------
       
   910 //
       
   911 void CSRSAlgorithmManager::EndRecSession()
       
   912     {
       
   913     RUBY_DEBUG0( "CSRSAlgorithmManager::EndRecSession()" );
       
   914 
       
   915     iRecognitionAlgMgr->EndRecSession();
       
   916     StateTransition( EDASRIdle );
       
   917     if ( iDevASRState == EDASRIdle )
       
   918         {
       
   919 	    iSIModelBank = NULL;
       
   920 		iSILexicon = NULL;
       
   921         }
       
   922     
       
   923     delete iDevSound;
       
   924     iDevSound = NULL;
       
   925     }
       
   926 
       
   927 
       
   928 // -----------------------------------------------------------------------------
       
   929 // CSRSAlgorithmManager::SendSpeechData
       
   930 // Gives data buffer to recognizer.
       
   931 // -----------------------------------------------------------------------------
       
   932 //
       
   933 void CSRSAlgorithmManager::SendSpeechData( TPtrC8& aBuffer, TBool aEnd )
       
   934     {
       
   935     if ( iMode == ESiRecognitionSpeechInput )
       
   936         {
       
   937         iRecognitionAlgMgr->SendSpeechData( aBuffer, aEnd );
       
   938         }
       
   939     }
       
   940 
       
   941 
       
   942 // -----------------------------------------------------------------------------
       
   943 // CSRSAlgorithmManager::Cancel
       
   944 // Cancels the current and any on going requests/tasks.
       
   945 // -----------------------------------------------------------------------------
       
   946 //
       
   947 void CSRSAlgorithmManager::Cancel()
       
   948     {
       
   949     RUBY_DEBUG0( "CSRSAlgorithmManager::Cancel()" );
       
   950 
       
   951     iVMAlgorithmManager->Cancel();
       
   952 
       
   953     iRecognitionAlgMgr->Cancel();
       
   954 
       
   955     if( iDevASRState != EDASRIdle )
       
   956     	{
       
   957         //StateTransition( EDASRPreRecognition );
       
   958         // Force to pre recognition state
       
   959         iDevASRState = EDASRPreRecognition;
       
   960     	}
       
   961 
       
   962     if ( iDevSoundState != ESoundDeviceNotInitialized )
       
   963     	{
       
   964         RUBY_DEBUG0( "CSRSAlgorithmManager::Cancel(), Stopping DevSound..." );
       
   965 
       
   966         if ( iMode == ESiRecognition )
       
   967             {
       
   968             if ( iDevSound )
       
   969                 {
       
   970                 iDevSound->Stop();
       
   971                 }
       
   972             }
       
   973 		iDevSoundState = ESoundDeviceStopped;
       
   974     	}
       
   975 
       
   976     CActive::Cancel();
       
   977     }
       
   978 
       
   979 
       
   980 // -----------------------------------------------------------------------------
       
   981 // CRecognitionAlgMgr::DoCancel
       
   982 // Cancels the current and any on going CActive requests/tasks.
       
   983 // -----------------------------------------------------------------------------
       
   984 //
       
   985 void CSRSAlgorithmManager::DoCancel()
       
   986     {
       
   987     RUBY_DEBUG0( "CSRSAlgorithmManager::DoCancel()" );
       
   988     }
       
   989 
       
   990 
       
   991 // -----------------------------------------------------------------------------
       
   992 // CSRSAlgorithmManager::EmptyBuffer
       
   993 // Copies the contents of the received buffer to the holding buffer. Subsequent
       
   994 // call to EmptyBuffer will append to existing buffer.
       
   995 // -----------------------------------------------------------------------------
       
   996 //
       
   997 TInt CSRSAlgorithmManager::EmptyBuffer( CMMFDataBuffer& aBuffer,
       
   998                                         TInt aBufferLength )
       
   999     {
       
  1000     RUBY_DEBUG1( "CSRSAlgorithmManager::EmptyBuffer(): Len=%d bytes", aBufferLength );
       
  1001 
       
  1002     TInt i = 0;
       
  1003     TInt status = KErrNone;
       
  1004     TBool iBufferCreated = EFalse;
       
  1005 #ifdef __WINS__
       
  1006     TInt KNumberOfBuffers = iResourceHandler->iBuffersInWins;
       
  1007 #else
       
  1008     TInt KNumberOfBuffers = iResourceHandler->iBuffersInThumb;
       
  1009 #endif
       
  1010 
       
  1011     TDes8& ReceivedData = aBuffer.Data();
       
  1012     TInt iSize = ReceivedData.Length();
       
  1013 
       
  1014 #ifdef __FULLDUPLEX_CHANGE
       
  1015     // If state is not EDASRSampling, then end immediately
       
  1016     // Samples will be negelected until real recognition starts
       
  1017     if ( iDevASRState != EDASRRecognitionSampling )
       
  1018         {
       
  1019         iDevSound->RecordData();
       
  1020         return status;
       
  1021         }
       
  1022     else
       
  1023         {
       
  1024         // Reset pre-sampling flag
       
  1025         iPreSamplingStarted = EFalse; 
       
  1026         }
       
  1027 #endif // __FULLDUPLEX_CHANGE
       
  1028 
       
  1029     // If DevSound has not filled the whole buffer, something 
       
  1030     // is wrong in the sampling
       
  1031     if( aBufferLength < ReceivedData.MaxLength() )
       
  1032         {
       
  1033         status = KErrEof;
       
  1034         }
       
  1035 
       
  1036     // iOverallLength checks how much is sampled during all the callbacks
       
  1037     if ( iOverallLength != 0 )
       
  1038         {
       
  1039         if ( iOverallSampled >= iOverallLength )
       
  1040             {
       
  1041             status = KErrOverflow;
       
  1042             }
       
  1043         }
       
  1044 
       
  1045     TInt silentFrames = iResourceHandler->iSilenceFrames;
       
  1046 
       
  1047     // Create new buffer if previous one has been queued
       
  1048     if ( iBufferUnderConstruction == NULL )
       
  1049         {
       
  1050         iBufferUnderConstruction = new TUint8[ iSize * KNumberOfBuffers ];
       
  1051         if ( !iBufferUnderConstruction )
       
  1052             {
       
  1053             return KErrNoMemory;
       
  1054             }
       
  1055         iConstructionPoint = 0;
       
  1056         }
       
  1057 
       
  1058     // Copy data, put silence into the beginning of buffer if needed
       
  1059     for ( i = 0; i < iSize; i++ )
       
  1060         {
       
  1061         if ( ( iOverallSampled + i ) < silentFrames )
       
  1062             {
       
  1063             // Do not copy zeros
       
  1064             //iBufferUnderConstruction[ iConstructionPoint ] = 0;
       
  1065             }
       
  1066         else
       
  1067             {
       
  1068             iBufferUnderConstruction[ iConstructionPoint ] = ReceivedData[ i ];
       
  1069             iConstructionPoint++;
       
  1070             }
       
  1071         }
       
  1072 
       
  1073     // Count the number of sampled bytes
       
  1074     iOverallSampled = iOverallSampled + iSize;
       
  1075 
       
  1076     // If internal buffer overflowed or EOF, mark this the final block of data
       
  1077     // Mark as the final block also if EndRecord() has been explicitly called
       
  1078     if ( ( status == KErrOverflow ) || ( status == KErrEof ) || iEndFlag )
       
  1079         {
       
  1080         CQueItem* qItem( NULL );
       
  1081         TRAPD( err, qItem = CQueItem::NewL( &iBufferUnderConstruction[0], iConstructionPoint ) );
       
  1082         if ( err != KErrNone )
       
  1083             {
       
  1084             return err;
       
  1085             }
       
  1086         qItem->iFinalBlock = ETrue;
       
  1087         iEndFlag = EFalse;
       
  1088         iAudioBufferQue->AddLast( *qItem );
       
  1089         iBufferUnderConstruction = NULL;
       
  1090         iConstructionPoint = 0;
       
  1091         iBufferCreated = ETrue;
       
  1092 
       
  1093         RUBY_DEBUG0( "CSRSAlgorithmManager::EmptyBuffer(): Sampling ended due to timeout!!" );
       
  1094         }
       
  1095     else
       
  1096         {
       
  1097         // Notify devsound
       
  1098         iDevSound->RecordData();
       
  1099 
       
  1100         // If next buffer does not fit to the buffer under construction
       
  1101         // put it to queue and start new when next callback arrives
       
  1102         if ( iConstructionPoint + iSize > iSize * KNumberOfBuffers )
       
  1103             {
       
  1104             // Put data to queue
       
  1105             CQueItem* qItem( NULL );
       
  1106             TRAPD( err, qItem = CQueItem::NewL( &iBufferUnderConstruction[0], iConstructionPoint ) );
       
  1107             if ( err != KErrNone )
       
  1108                 {
       
  1109                 return err;
       
  1110                 }
       
  1111             qItem->iFinalBlock = EFalse;
       
  1112             iAudioBufferQue->AddLast( *qItem );
       
  1113             iBufferCreated = ETrue;
       
  1114             }
       
  1115         }
       
  1116 
       
  1117     if ( iBufferCreated )
       
  1118         {
       
  1119 
       
  1120 #ifdef AUDIOBUFFER_TO_FILE
       
  1121         TInt connectError = iFs.Connect();
       
  1122         if ( connectError != KErrNone )
       
  1123             {
       
  1124             return connectError;
       
  1125             }
       
  1126         _LIT( KBufferFileName, "C:\\documents\\devasraudiobuffer.raw" );
       
  1127         if ( !iFileCreated )
       
  1128             {
       
  1129             iBufferDataFile.Replace( iFs, KBufferFileName, EFileWrite );
       
  1130             iFileCreated = ETrue;
       
  1131             }
       
  1132         else
       
  1133             {
       
  1134             TInt openError = iBufferDataFile.Open( iFs, KBufferFileName, EFileWrite );
       
  1135             if ( openError != KErrNone )
       
  1136                 {
       
  1137                 return openError;
       
  1138                 }
       
  1139             }
       
  1140         
       
  1141         TPtr8 ptr( &iBufferUnderConstruction[0], iConstructionPoint, iConstructionPoint );
       
  1142         TInt pos = 0;
       
  1143         iBufferDataFile.Seek( ESeekEnd, pos );
       
  1144         iBufferDataFile.Write( pos, ptr );
       
  1145         iBufferDataFile.Close();
       
  1146         iFs.Close();
       
  1147 #endif
       
  1148 
       
  1149         iBufferUnderConstruction = NULL;
       
  1150         iConstructionPoint = 0;
       
  1151         
       
  1152         // Start recognition if it has not been already started
       
  1153         if ( iStartRecognition )
       
  1154             {
       
  1155             TRAP( status, iRecognitionAlgMgr->StartRecognitionL() );
       
  1156             iStartRecognition = EFalse;
       
  1157             }
       
  1158         
       
  1159         // If RequestSpeechData() gets called when there is no data in queue, we
       
  1160         // should call it again to deliver the new data to recognizer
       
  1161         if ( iPendingRequestSpeechData )
       
  1162             {
       
  1163             iPendingRequestSpeechData = EFalse;
       
  1164             RequestSpeechData();
       
  1165             }
       
  1166         }
       
  1167     
       
  1168     return status;
       
  1169     }
       
  1170     
       
  1171     
       
  1172 // -----------------------------------------------------------------------------
       
  1173 // CSRSAlgorithmManager::FillBuffer
       
  1174 // Copies a portion of the holding buffer to the received buffer.
       
  1175 // -----------------------------------------------------------------------------
       
  1176 //
       
  1177 TInt CSRSAlgorithmManager::FillBuffer( CMMFDataBuffer& /*aBuffer*/, 
       
  1178                                        TInt /*aBufferLength*/ )
       
  1179     {
       
  1180     RUBY_DEBUG0( "CSRSAlgorithmManager::FillBuffer()" );
       
  1181 
       
  1182     return KErrNotSupported;
       
  1183     }
       
  1184 
       
  1185 
       
  1186 // -----------------------------------------------------------------------------
       
  1187 // CSRSAlgorithmManager::SetPrioritySettings
       
  1188 // Store the priority setting for the sound device.
       
  1189 // -----------------------------------------------------------------------------
       
  1190 //
       
  1191 void CSRSAlgorithmManager::SetPrioritySettings( const TMMFPrioritySettings& aPrioritySettings )
       
  1192     {
       
  1193     iPrioritySettings = aPrioritySettings;
       
  1194     // Always use preference meant for recognition
       
  1195     iPrioritySettings.iPref = ( TMdaPriorityPreference ) KAudioPrefVocosRecog;
       
  1196     }
       
  1197 
       
  1198 
       
  1199 // -----------------------------------------------------------------------------
       
  1200 // CSRSAlgorithmManager::InitializeDevSoundL
       
  1201 // 
       
  1202 // -----------------------------------------------------------------------------
       
  1203 //
       
  1204 void CSRSAlgorithmManager::InitializeDevSoundL()
       
  1205     {
       
  1206     if ( !iDevSound )
       
  1207         {
       
  1208         // Instance
       
  1209         iDevSound = CMMFDevSound::NewL();
       
  1210         }
       
  1211     
       
  1212     // Initialization        
       
  1213     iDevSound->InitializeL( *this, EMMFStateRecording );
       
  1214     
       
  1215     // Wait for initialization callback
       
  1216     if ( !iAudioWait.IsStarted() )
       
  1217         {
       
  1218         iAudioWait.Start();
       
  1219         }    
       
  1220 
       
  1221     // Configuration
       
  1222     ConfigureSoundDeviceL();
       
  1223     }
       
  1224         
       
  1225         
       
  1226 // -----------------------------------------------------------------------------
       
  1227 // CSRSAlgorithmManager::StartDevSoundL
       
  1228 // 
       
  1229 // -----------------------------------------------------------------------------
       
  1230 //
       
  1231 void CSRSAlgorithmManager::StartDevSoundL()
       
  1232     {
       
  1233     RUBY_DEBUG_BLOCK( "" );
       
  1234     iDevSound->SetPrioritySettings( iPrioritySettings );
       
  1235     iDevSound->RecordInitL();
       
  1236     iDevSoundState = ESoundDeviceRecord;
       
  1237     }
       
  1238 
       
  1239 
       
  1240 // -----------------------------------------------------------------------------
       
  1241 // CSRSAlgorithmManager::StateTransition
       
  1242 // Change to the specified state if possible. If state change is invalid,
       
  1243 // EFalse is returned, otherwise, ETrue.
       
  1244 // -----------------------------------------------------------------------------
       
  1245 //
       
  1246 TBool CSRSAlgorithmManager::StateTransition( TDevASRState aTargetState )
       
  1247     {
       
  1248 
       
  1249     TBool result = ETrue;
       
  1250 
       
  1251 #ifdef _DEBUG
       
  1252     DevASRState();
       
  1253 #endif
       
  1254 
       
  1255     switch ( aTargetState )
       
  1256         {
       
  1257         case EDASRIdle:
       
  1258             iDevASRState = aTargetState;
       
  1259             break;
       
  1260 
       
  1261         case EDASRInitTrain:
       
  1262             if ( iDevASRState == EDASRIdle )
       
  1263                 {
       
  1264                 iDevASRState = aTargetState;
       
  1265                 }
       
  1266             else
       
  1267                 {
       
  1268                 result = EFalse;
       
  1269                 }
       
  1270             break;
       
  1271 
       
  1272         case EDASRTrain:
       
  1273             if ( ( iDevASRState == EDASRTrain ) ||
       
  1274                 ( iDevASRState == EDASRInitTrain ) ||
       
  1275                 ( iDevASRState == EDASRTrainSampling ) )
       
  1276                 {
       
  1277                 iDevASRState = aTargetState;
       
  1278                 }
       
  1279             else
       
  1280                 {
       
  1281                 result = EFalse;
       
  1282                 }
       
  1283             break;
       
  1284 
       
  1285         case EDASRSampling:
       
  1286             if ( iDevASRState == EDASRTrain )
       
  1287                 {
       
  1288                 iDevASRState = EDASRTrainSampling;
       
  1289                 }
       
  1290             else if ( iDevASRState == EDASRRecognition )
       
  1291                 {
       
  1292                 iDevASRState = EDASRRecognitionSampling;
       
  1293                 }
       
  1294             else
       
  1295                 {
       
  1296                 result = EFalse;
       
  1297                 }
       
  1298             break;
       
  1299 
       
  1300         case EDASRPlayback:
       
  1301             if ( iDevASRState == EDASRIdle )
       
  1302                 {
       
  1303                 iDevASRState = EDASRPlayback;
       
  1304                 }
       
  1305             else if ( iDevASRState == EDASRPreRecognition )
       
  1306                 {
       
  1307                 iDevASRState = EDASRRecognitionPlayback;
       
  1308                 }
       
  1309             else
       
  1310                 {
       
  1311                 result = EFalse;
       
  1312                 }
       
  1313             break;
       
  1314 
       
  1315         case EDASRPreRecognition:
       
  1316             if ( ( iDevASRState == EDASRIdle ) ||
       
  1317                 ( iDevASRState == EDASRRecognition ) ||
       
  1318                 ( iDevASRState == EDASRRecognitionPlayback ) )
       
  1319                 {
       
  1320                 iDevASRState = aTargetState;
       
  1321                 }
       
  1322             else
       
  1323                 {
       
  1324                 result = EFalse;
       
  1325                 }
       
  1326             break;
       
  1327 
       
  1328         case EDASRRecognitionPlayback:
       
  1329             if (iDevASRState == EDASRPreRecognition)
       
  1330                 {
       
  1331                 iDevASRState = aTargetState;
       
  1332                 }
       
  1333             else
       
  1334                 {
       
  1335                 result=EFalse;
       
  1336                 }
       
  1337             break;
       
  1338 
       
  1339         case EDASRInitRecognition:
       
  1340             if ((iDevASRState == EDASRInitRecognition) ||
       
  1341                 (iDevASRState == EDASRPreRecognition))
       
  1342                 {
       
  1343                 iDevASRState = aTargetState;
       
  1344                 }
       
  1345             else
       
  1346                 {
       
  1347                 result=EFalse;
       
  1348                 }
       
  1349             break;
       
  1350 
       
  1351         case EDASRRecognition:
       
  1352             if ( ( iDevASRState == EDASRInitRecognition ) ||
       
  1353                 ( iDevASRState == EDASRRecognitionSampling ) )
       
  1354                 {
       
  1355                 iDevASRState = aTargetState;
       
  1356                 }
       
  1357             else
       
  1358                 {
       
  1359                 result = EFalse;
       
  1360                 }
       
  1361             break;
       
  1362 
       
  1363         case EDASRCancel:
       
  1364             break;
       
  1365 
       
  1366         default:
       
  1367             RUBY_DEBUG1( "Invalid state: %d", aTargetState );
       
  1368             break;
       
  1369         };
       
  1370 
       
  1371 #ifdef _DEBUG
       
  1372     DevASRState();
       
  1373     iRecognitionAlgMgr->AlgorithmState();
       
  1374 #endif
       
  1375     return result;
       
  1376     }
       
  1377 
       
  1378 
       
  1379 // -----------------------------------------------------------------------------
       
  1380 // CSRSAlgorithmManager::TranslateError
       
  1381 // Translate internal errors to system errors
       
  1382 // -----------------------------------------------------------------------------
       
  1383 //
       
  1384 TInt CSRSAlgorithmManager::TranslateError( TInt aError )
       
  1385     {
       
  1386     RUBY_DEBUG1( "CSRSAlgorithmManager::TranslateError(%d)", aError );
       
  1387 
       
  1388     TInt error = aError;
       
  1389 
       
  1390      switch ( aError )
       
  1391         {
       
  1392         case KErrTooEarly:
       
  1393             error = KErrAsrSpeechTooEarly;
       
  1394             break;
       
  1395         case KErrNoSpeech:
       
  1396             error = KErrAsrNoSpeech;
       
  1397             break;
       
  1398         case KErrTooLong:
       
  1399             error = KErrAsrSpeechTooLong;
       
  1400             break;
       
  1401         case KErrTooShort:
       
  1402             error = KErrAsrSpeechTooShort;
       
  1403             break;
       
  1404         default:
       
  1405             RUBY_DEBUG1( "Unmapped Error: %d", aError );
       
  1406             break;
       
  1407         };
       
  1408 
       
  1409     return error;
       
  1410     }
       
  1411 
       
  1412 
       
  1413 // -----------------------------------------------------------------------------
       
  1414 // DevSound Observer MIXIN implementation begins
       
  1415 // -----------------------------------------------------------------------------
       
  1416 
       
  1417 // -----------------------------------------------------------------------------
       
  1418 // CSRSAlgorithmManager::InitializeComplete
       
  1419 // Initialization of DevSound is complete.
       
  1420 // -----------------------------------------------------------------------------
       
  1421 //
       
  1422 void CSRSAlgorithmManager::InitializeComplete( TInt aError )
       
  1423     {
       
  1424     RUBY_DEBUG1( "InitializeComplete[%d]", aError );
       
  1425 
       
  1426     if ( aError == KErrNone  )
       
  1427         {
       
  1428 		iDevSoundState = ESoundDeviceInitialized;
       
  1429 		}
       
  1430 		
       
  1431     // Free wait loop
       
  1432     if ( iAudioWait.IsStarted() )
       
  1433         {
       
  1434         iAudioWait.AsyncStop();
       
  1435         }                       
       
  1436     }
       
  1437 
       
  1438 // -----------------------------------------------------------------------------
       
  1439 // CSRSAlgorithmManager::ToneFinished
       
  1440 // Finished playing tones
       
  1441 // -----------------------------------------------------------------------------
       
  1442 //
       
  1443 void CSRSAlgorithmManager::ToneFinished( TInt /*aError*/ )
       
  1444     {
       
  1445     // Should never be called because no tone services is required.
       
  1446     }
       
  1447 
       
  1448 
       
  1449 // -----------------------------------------------------------------------------
       
  1450 // CSRSAlgorithmManager::BufferToBeFilled
       
  1451 // Buffer from DevSound to be filled with audio data.
       
  1452 // -----------------------------------------------------------------------------
       
  1453 //
       
  1454 void CSRSAlgorithmManager::BufferToBeFilled( CMMFBuffer* aBuffer )
       
  1455     {
       
  1456     CMMFDataBuffer* buffer = STATIC_CAST( CMMFDataBuffer*, aBuffer );
       
  1457 
       
  1458     if ( FillBuffer( *buffer, aBuffer->RequestSize() ) == KErrNone )
       
  1459         {
       
  1460         iDevSound->PlayData();
       
  1461         }
       
  1462     }
       
  1463 
       
  1464 // -----------------------------------------------------------------------------
       
  1465 // CSRSAlgorithmManager::PlayError
       
  1466 // Playing of an audio sample has completed successfully or otherwise.
       
  1467 // -----------------------------------------------------------------------------
       
  1468 //
       
  1469 void CSRSAlgorithmManager::PlayError( TInt aError )
       
  1470     {
       
  1471     RUBY_DEBUG1( "CSRSAlgorithmManager::PlayError[%d]", aError );
       
  1472 
       
  1473     if ( iDevASRState != EDASRIdle )
       
  1474 		{
       
  1475 
       
  1476 		if ( iDevASRState == EDASRRecognitionPlayback )
       
  1477 			{
       
  1478 			StateTransition( EDASRPreRecognition );
       
  1479 			}
       
  1480 		else
       
  1481 			{
       
  1482 			StateTransition( EDASRIdle );
       
  1483 			}
       
  1484 
       
  1485 		if ( aError == KErrUnderflow )
       
  1486 			{
       
  1487 			iDevASRObserver->DevASREvent( EDevASRPlay, KErrNone );
       
  1488 			}
       
  1489 		else
       
  1490 			{
       
  1491 			iDevASRObserver->DevASREvent( EDevASRPlay, aError );
       
  1492 			}
       
  1493 
       
  1494 		}
       
  1495 
       
  1496     }
       
  1497 
       
  1498 // -----------------------------------------------------------------------------
       
  1499 // CSRSAlgorithmManager::BufferToBeEmptied
       
  1500 // An audio buffer is ready to be emptied.
       
  1501 // -----------------------------------------------------------------------------
       
  1502 //
       
  1503 void CSRSAlgorithmManager::BufferToBeEmptied( CMMFBuffer* aBuffer )
       
  1504     {
       
  1505     RUBY_DEBUG0( "" );    
       
  1506     if ( iDevASRState == EDASRIdle && !iPreSamplingStarted )
       
  1507         {
       
  1508         RUBY_DEBUG0( "stopping devsound" );
       
  1509         iDevSound->Stop();
       
  1510 		iDevSoundState = ESoundDeviceStopped;
       
  1511         return;
       
  1512         }
       
  1513 
       
  1514     RUBY_DEBUG0( "processing callback" );
       
  1515     CMMFDataBuffer* buffer = STATIC_CAST( CMMFDataBuffer*, aBuffer );
       
  1516 	EmptyBuffer( *buffer, aBuffer->RequestSize() );
       
  1517     }
       
  1518 
       
  1519 
       
  1520 // -----------------------------------------------------------------------------
       
  1521 // CSRSAlgorithmManager::RecordError
       
  1522 // Recording has completed successfully or otherwise.
       
  1523 // -----------------------------------------------------------------------------
       
  1524 //
       
  1525 void CSRSAlgorithmManager::RecordError( TInt aError )
       
  1526     {
       
  1527     RUBY_DEBUG1( "CSRSAlgorithmManager::RecordError[%d]", aError );
       
  1528 
       
  1529     iPreSamplingStarted = EFalse;
       
  1530     iRecognitionAlgMgr->Cancel();
       
  1531     StateTransition( EDASRIdle );
       
  1532     iDevASRObserver->DevASREvent( EDevASRRecord, aError );
       
  1533     }
       
  1534 
       
  1535 
       
  1536 // -----------------------------------------------------------------------------
       
  1537 // CSRSAlgorithmManager::ConvertError
       
  1538 // Conversion has completed successfully or otherwise.
       
  1539 // -----------------------------------------------------------------------------
       
  1540 //
       
  1541 void CSRSAlgorithmManager::ConvertError( TInt /*aError*/ )
       
  1542     {
       
  1543     // This method should never be called.
       
  1544     }
       
  1545 
       
  1546 
       
  1547 // -----------------------------------------------------------------------------
       
  1548 // CSRSAlgorithmManager::RecordError
       
  1549 // Callback for custom commands
       
  1550 // -----------------------------------------------------------------------------
       
  1551 //
       
  1552 void CSRSAlgorithmManager::DeviceMessage( TUid /*aMessageType*/, 
       
  1553                                           const TDesC8& /*aMsg*/ )
       
  1554     {
       
  1555     RUBY_DEBUG0( "CSRSAlgorithmManager::DeviceMessage" );
       
  1556     }
       
  1557 
       
  1558 // -----------------------------------------------------------------------------
       
  1559 // CSRSAlgorithmManager::SendEventToClient
       
  1560 // Receives and event from DevSound send it to the appropriate handler.
       
  1561 // -----------------------------------------------------------------------------
       
  1562 //
       
  1563 void CSRSAlgorithmManager::SendEventToClient( const TMMFEvent& aEvent )
       
  1564     {
       
  1565     RUBY_DEBUG0( "CSRSAlgorithmManager::SendEventToClient()" );
       
  1566 
       
  1567 	if ( aEvent.iErrorCode != KErrNone )
       
  1568 		{
       
  1569 		if ( iDevSoundState == ESoundDeviceRecord )
       
  1570 			{
       
  1571 			RecordError( aEvent.iErrorCode );
       
  1572 			}
       
  1573 		else if ( iDevSoundState == ESoundDevicePlayback )
       
  1574 			{
       
  1575 			PlayError( aEvent.iErrorCode );
       
  1576 			}
       
  1577 		}
       
  1578 
       
  1579     }
       
  1580 
       
  1581 
       
  1582 // -----------------------------------------------------------------------------
       
  1583 // DevSound Observer MIXIN impl ends
       
  1584 // -----------------------------------------------------------------------------
       
  1585 
       
  1586 
       
  1587 // -----------------------------------------------------------------------------
       
  1588 // RecognitionAlgMgr Observer MIXIN implementation begins
       
  1589 // -----------------------------------------------------------------------------
       
  1590 
       
  1591 // -----------------------------------------------------------------------------
       
  1592 // CSRSAlgorithmManager::AdaptComplete
       
  1593 // Model adaptation has finished.
       
  1594 // -----------------------------------------------------------------------------
       
  1595 //
       
  1596 void CSRSAlgorithmManager::AdaptComplete( TInt aResultCode )
       
  1597     {
       
  1598     RUBY_DEBUG1( "CSRSAlgorithmManager::AdaptComplete(%d)", aResultCode );
       
  1599 
       
  1600     iDevASRObserver->DevASREvent( EDevASRAdapt, aResultCode );
       
  1601     }
       
  1602 
       
  1603 
       
  1604 // -----------------------------------------------------------------------------
       
  1605 // CSRSAlgorithmManager::InitFEComplete
       
  1606 // Initialization of frontend algorithm has completed.
       
  1607 // -----------------------------------------------------------------------------
       
  1608 //
       
  1609 void CSRSAlgorithmManager::InitFEComplete( TInt aResultCode )
       
  1610     {
       
  1611     RUBY_DEBUG1( "CSRSAlgorithmManager::InitFEComplete(%d)", aResultCode );
       
  1612 
       
  1613     // Ignore events if we're idle.
       
  1614     if ( ( iDevASRState == EDASRInitTrain ) || ( iDevASRState == EDASRInitRecognition ) )
       
  1615         {
       
  1616         if ( aResultCode == KErrNone )
       
  1617             {
       
  1618             if ( iDevASRState == EDASRInitTrain )
       
  1619                 {
       
  1620                 iDevASRState = EDASRTrain;
       
  1621                 }
       
  1622             else if ( iDevASRState == EDASRInitRecognition )
       
  1623                 {
       
  1624                 iDevASRState = EDASRRecognition;
       
  1625                 }
       
  1626             iDevASRObserver->DevASREvent( EDevASRInitFrontend, aResultCode );
       
  1627             }
       
  1628         else
       
  1629             {
       
  1630             StateTransition( EDASRIdle );
       
  1631             iDevASRObserver->DevASREvent( EDevASRInitFrontend, TranslateError( aResultCode ) );
       
  1632             }
       
  1633         }
       
  1634     }
       
  1635 
       
  1636 // -----------------------------------------------------------------------------
       
  1637 // CSRSAlgorithmManager::InitRecognizerBEComplete
       
  1638 // Initialization of backend algorithm for recognition has completed.
       
  1639 // -----------------------------------------------------------------------------
       
  1640 //
       
  1641 void CSRSAlgorithmManager::InitRecognizerBEComplete( TInt aResultCode )
       
  1642     {
       
  1643     RUBY_DEBUG1( "CSRSAlgorithmManager::InitRecognizerBEComplete(%d)", aResultCode );
       
  1644 
       
  1645     if ( StateTransition( EDASRInitRecognition ) )
       
  1646         {
       
  1647         if ( aResultCode == KErrNone )
       
  1648             {
       
  1649             iDevASRObserver->DevASREvent( EDevASRInitRecognitionBackend, aResultCode );
       
  1650             }
       
  1651         else
       
  1652             {
       
  1653             StateTransition( EDASRIdle );
       
  1654             iDevASRObserver->DevASREvent( EDevASRInitRecognitionBackend, TranslateError( aResultCode ) );
       
  1655             }
       
  1656         }
       
  1657 
       
  1658     }
       
  1659 
       
  1660 // -----------------------------------------------------------------------------
       
  1661 // CSRSAlgorithmManager::LoadGrammarComplete
       
  1662 // Loading of grammar into recognizer has completed.
       
  1663 // -----------------------------------------------------------------------------
       
  1664 //
       
  1665 void CSRSAlgorithmManager::LoadGrammarComplete( TInt aResultCode )
       
  1666     {
       
  1667     RUBY_DEBUG1( "CSRSAlgorithmManager::LoadGrammarComplete(%d)", aResultCode );
       
  1668 
       
  1669 	TDevASREvent event = EDevASRLoadGrammar;
       
  1670     
       
  1671     if ( aResultCode == KErrNone )
       
  1672         {
       
  1673         iDevASRObserver->DevASREvent( event, aResultCode );
       
  1674         }
       
  1675     else
       
  1676         {
       
  1677         //StateTransition( EDASRIdle );
       
  1678         iDevASRObserver->DevASREvent( event, TranslateError( aResultCode ) );
       
  1679         }
       
  1680     }
       
  1681 
       
  1682 
       
  1683 // -----------------------------------------------------------------------------
       
  1684 // CSRSAlgorithmManager::UnloadGrammarComplete
       
  1685 // Grammar has beed unloaded.
       
  1686 // -----------------------------------------------------------------------------
       
  1687 //
       
  1688 void CSRSAlgorithmManager::UnloadGrammarComplete( TInt aResultCode )
       
  1689     {
       
  1690     RUBY_DEBUG1( "CSRSAlgorithmManager::UnloadGrammarComplete(%d)", aResultCode );
       
  1691 
       
  1692 	TDevASREvent event = EDevASRUnloadGrammar;
       
  1693     
       
  1694     if ( aResultCode == KErrNone )
       
  1695         {
       
  1696         iDevASRObserver->DevASREvent( event, aResultCode );
       
  1697         }
       
  1698     else
       
  1699         {
       
  1700         iDevASRObserver->DevASREvent( event, TranslateError( aResultCode ) );
       
  1701         }
       
  1702     }
       
  1703 
       
  1704 
       
  1705 // -----------------------------------------------------------------------------
       
  1706 // CSRSAlgorithmManager::ActivateGrammarComplete
       
  1707 // Grammar activation has been completed.
       
  1708 // -----------------------------------------------------------------------------
       
  1709 //
       
  1710 void CSRSAlgorithmManager::ActivateGrammarComplete( TInt aResultCode )
       
  1711     {
       
  1712     RUBY_DEBUG1( "CSRSAlgorithmManager::ActivateGrammarComplete(%d)", aResultCode );
       
  1713 
       
  1714     if ( iDevASRState == EDASRPreRecognition )
       
  1715         {
       
  1716         iDevASRObserver->DevASREvent( EDevASRActivateGrammar, aResultCode );
       
  1717         }  
       
  1718     }
       
  1719 
       
  1720 
       
  1721 // -----------------------------------------------------------------------------
       
  1722 // CSRSAlgorithmManager::DeActivateGrammarComplete
       
  1723 // Grammar deactivation has been completed.
       
  1724 // -----------------------------------------------------------------------------
       
  1725 //
       
  1726 void CSRSAlgorithmManager::DeActivateGrammarComplete( TInt aResultCode )
       
  1727     {
       
  1728     RUBY_DEBUG1( "CSRSAlgorithmManager::DeActivateGrammarComplete(%d)", aResultCode );
       
  1729 
       
  1730     if ( iDevASRState == EDASRPreRecognition )
       
  1731         {
       
  1732         iDevASRObserver->DevASREvent( EDevASRDeactivateGrammar, aResultCode );
       
  1733         }
       
  1734     }
       
  1735 
       
  1736 
       
  1737 // -----------------------------------------------------------------------------
       
  1738 // CSRSAlgorithmManager::LoadLexiconComplete
       
  1739 // Loading of lexicon into recognizer has completed.
       
  1740 // -----------------------------------------------------------------------------
       
  1741 //
       
  1742 void CSRSAlgorithmManager::LoadLexiconComplete( TInt aResultCode )
       
  1743     {
       
  1744     RUBY_DEBUG1( "CSRSAlgorithmManager::LoadLexiconComplete(%d)", aResultCode );
       
  1745     
       
  1746     if ( aResultCode == KErrNone )
       
  1747         {
       
  1748         iDevASRObserver->DevASREvent( EDevASRLoadLexicon, aResultCode );
       
  1749         }
       
  1750     else
       
  1751         {
       
  1752         //StateTransition( EDASRIdle );
       
  1753         iDevASRObserver->DevASREvent( EDevASRLoadLexicon, TranslateError( aResultCode ) );
       
  1754         }
       
  1755     }
       
  1756 
       
  1757 
       
  1758 // -----------------------------------------------------------------------------
       
  1759 // CSRSAlgorithmManager::LoadModelsComplete
       
  1760 // Loading of models into recognizer has completed.
       
  1761 // -----------------------------------------------------------------------------
       
  1762 //
       
  1763 void CSRSAlgorithmManager::LoadModelsComplete( TInt aResultCode )
       
  1764     {
       
  1765     RUBY_DEBUG1( "CSRSAlgorithmManager::LoadModelsComplete(%d)", aResultCode );
       
  1766     
       
  1767     if ( aResultCode == KErrNone )
       
  1768         {
       
  1769         iDevASRObserver->DevASREvent( EDevASRLoadModels, aResultCode );
       
  1770         }
       
  1771     else
       
  1772         {
       
  1773         //StateTransition( EDASRIdle );
       
  1774         iDevASRObserver->DevASREvent( EDevASRLoadModels, TranslateError( aResultCode ) );
       
  1775         }
       
  1776     }
       
  1777 
       
  1778 
       
  1779 // -----------------------------------------------------------------------------
       
  1780 // CSRSAlgorithmManager::RequestSpeechData
       
  1781 // Speech data is needed.
       
  1782 // -----------------------------------------------------------------------------
       
  1783 //
       
  1784 void CSRSAlgorithmManager::RequestSpeechData()
       
  1785     {
       
  1786     RUBY_DEBUG0( "CSRSAlgorithmManager::RequestSpeechData()" );
       
  1787 
       
  1788     if ( iMode == ESiRecognitionSpeechInput )
       
  1789         {
       
  1790         iDevASRObserver->RequestSpeechData();
       
  1791         }
       
  1792     else
       
  1793         {
       
  1794         // The processed sample descriptor can be deleted
       
  1795         delete iQueItem; 
       
  1796         iQueItem = NULL;
       
  1797         
       
  1798         if ( ( iDevASRState == EDASRTrainSampling ) || ( iDevASRState == EDASRRecognitionSampling ) )
       
  1799             {
       
  1800             // Check if there's more data to send and if so, send the next block of data
       
  1801             if ( iAudioBufferQue->IsEmpty() )
       
  1802                 {
       
  1803                 iPendingRequestSpeechData = ETrue;
       
  1804                 iProcessingUtterance = EFalse;
       
  1805                 }
       
  1806             else
       
  1807                 {
       
  1808                 iQueItem = iAudioBufferQue->First();
       
  1809                 iRecognitionAlgMgr->SendSpeechData( iQueItem->iDataBlock, iQueItem->iFinalBlock );
       
  1810                 iAudioBufferQue->Remove( *iQueItem );
       
  1811                 }        
       
  1812             }
       
  1813         }
       
  1814     }
       
  1815 
       
  1816 
       
  1817 // -----------------------------------------------------------------------------
       
  1818 // CSRSAlgorithmManager::FeatureVectorDataRcvd
       
  1819 // Feature vector data received from frontend.
       
  1820 // -----------------------------------------------------------------------------
       
  1821 //
       
  1822 void CSRSAlgorithmManager::FeatureVectorDataRcvd( const TDesC8& aFV,
       
  1823                                                   TInt32 aSNR,
       
  1824                                                   TInt32 aPosition )
       
  1825     {
       
  1826     iDevASRObserver->FeatureVectorDataRcvd( aFV, aSNR, aPosition );
       
  1827     }
       
  1828 
       
  1829 
       
  1830 // -----------------------------------------------------------------------------
       
  1831 // CSRSAlgorithmManager::EouDetected
       
  1832 // Frontend finished extracting feature vectors from PCM data.
       
  1833 // -----------------------------------------------------------------------------
       
  1834 //
       
  1835 void CSRSAlgorithmManager::EouDetected( TInt aResultCode )
       
  1836     {
       
  1837     RUBY_DEBUG1( "CSRSAlgorithmManager::EouDetected(%d)", aResultCode );
       
  1838 
       
  1839     if ( iDevASRState == EDASRTrainSampling )
       
  1840         {
       
  1841         
       
  1842         if ( aResultCode != KErrNone )
       
  1843             {
       
  1844             RUBY_DEBUG1( "CSRSAlgorithmManager::EouDetected(): Training ERROR %d", aResultCode );
       
  1845 
       
  1846             StateTransition( EDASRIdle );
       
  1847             iDevSound->Stop();
       
  1848             iDevSoundState = ESoundDeviceStopped;
       
  1849             iDevASRObserver->DevASREvent( EDevASRTrain, TranslateError( aResultCode ) );
       
  1850             return;
       
  1851             }
       
  1852         
       
  1853         // If we fail to get the utterance duration, there's probably not enough frames to calculate
       
  1854         // utterance duration. We will fail training with "too short".
       
  1855         if ( !iRecognitionAlgMgr->GetUtteranceDuration(iStartFrame, iEndFrame, iFrameLength) )
       
  1856             {
       
  1857             StateTransition(EDASRIdle);
       
  1858             iDevSound->Stop();
       
  1859             iDevSoundState = ESoundDeviceStopped;
       
  1860             iDevASRObserver->DevASREvent( EDevASRTrain, KErrAsrSpeechTooShort );
       
  1861             return;
       
  1862             }
       
  1863         
       
  1864         RUBY_DEBUG1( "Start frame: %d", iStartFrame );
       
  1865         RUBY_DEBUG1( "End frame: %d", iEndFrame );
       
  1866         RUBY_DEBUG1( "Frame Length: %f", iFrameLength );
       
  1867 
       
  1868         // Now, calculate the start and end point in the audio buffer for encoding
       
  1869         
       
  1870         RUBY_DEBUG1( "Padding start frames: %d", iResourceHandler->iPadStartFrames );
       
  1871 
       
  1872         // Pad additional frame to start and end to prevent clipping during utterance playback.
       
  1873         
       
  1874         if( (iStartFrame - iResourceHandler->iPadStartFrames) <= 0 )
       
  1875             {
       
  1876             iStartFrame = 0;
       
  1877             }
       
  1878         else
       
  1879             {
       
  1880             iStartFrame = iStartFrame - iResourceHandler->iPadStartFrames;
       
  1881             }
       
  1882         
       
  1883         iBufferStartPoint = (TUint32)(iStartFrame * iFrameLength * iResourceHandler->iSamplingRate * 2);
       
  1884         
       
  1885         RUBY_DEBUG1( "Padding start frames: %d", iResourceHandler->iPadEndFrames );
       
  1886 
       
  1887         iBufferEndPoint = (TUint32)((iEndFrame + iResourceHandler->iPadEndFrames) * iFrameLength *
       
  1888             iResourceHandler->iSamplingRate * 2);
       
  1889         
       
  1890         if ( iBufferEndPoint >= iStartPoint - 1 ) // Do not pad beyond recorded point
       
  1891             {
       
  1892             iBufferEndPoint = iStartPoint - 1; // Set to the last byte recorded
       
  1893             }
       
  1894         
       
  1895         if ( iBufferEndPoint > iStopPoint ) // Cannot pad more than buffer size allows
       
  1896             {
       
  1897             iBufferEndPoint = iStopPoint;
       
  1898             }
       
  1899         
       
  1900         StateTransition(EDASRTrain);
       
  1901         iDevSound->Stop();
       
  1902         iDevSoundState = ESoundDeviceStopped;
       
  1903         }
       
  1904     
       
  1905     // It is possible that the frontend has completed processing while the audio
       
  1906     // buffer is not yet full.
       
  1907     // The recording can be stopped as the frontend has detected end of utterance.
       
  1908     
       
  1909     else if ( iDevASRState == EDASRRecognitionSampling )
       
  1910         {
       
  1911         
       
  1912         if ( ( aResultCode != KErrNone ) && ( aResultCode != KErrTooLong ) )
       
  1913             {
       
  1914             RUBY_DEBUG1( "CSRSAlgorithmManager::EouDetected(): Recognition ERROR %d", aResultCode );
       
  1915 
       
  1916             StateTransition( EDASRIdle );
       
  1917             iRecognitionAlgMgr->Cancel();
       
  1918             iDevSound->Stop();
       
  1919             iDevASRObserver->DevASREvent( EDevASRRecognize, TranslateError( aResultCode ) );
       
  1920             return;
       
  1921             }
       
  1922         
       
  1923         StateTransition( EDASRRecognition );
       
  1924       
       
  1925         iDevSound->Stop();
       
  1926        
       
  1927         iDevSoundState = ESoundDeviceStopped;
       
  1928         }
       
  1929     else // We are in the wrong state to handle this event
       
  1930         {
       
  1931         RUBY_DEBUG0( "CSRSAlgorithmManager::EouDetected(): WRONG STATE" );
       
  1932         return;
       
  1933         }
       
  1934     
       
  1935     iDevASRObserver->DevASREvent( EDevASREouDetected, KErrNone );
       
  1936     }
       
  1937 
       
  1938 
       
  1939 // -----------------------------------------------------------------------------
       
  1940 // CSRSAlgorithmManager::TrainComplete
       
  1941 // The training backend has finished processing feature vectors. A Model is
       
  1942 // created.
       
  1943 // -----------------------------------------------------------------------------
       
  1944 //
       
  1945 void CSRSAlgorithmManager::TrainComplete( TInt aResultCode )
       
  1946     {
       
  1947     RUBY_DEBUG1( "CSRSAlgorithmManager::TrainComplete(%d)", aResultCode );
       
  1948 
       
  1949     if ( iDevASRState == EDASRTrain )
       
  1950         {
       
  1951         // Training is complete, DevASR can return to Idle state.
       
  1952         StateTransition( EDASRIdle );
       
  1953 
       
  1954         iDevASRObserver->DevASREvent( EDevASRTrain, TranslateError( aResultCode ) );
       
  1955         }
       
  1956     }
       
  1957 
       
  1958 
       
  1959 // -----------------------------------------------------------------------------
       
  1960 // CSRSAlgorithmManager::UnloadRuleComplete
       
  1961 // Rule has been blacklisted successfully or otherwise.
       
  1962 // -----------------------------------------------------------------------------
       
  1963 //
       
  1964 void CSRSAlgorithmManager::UnloadRuleComplete( TInt aResultCode )
       
  1965     {
       
  1966     RUBY_DEBUG1( "CSRSAlgorithmManager::UnloadRuleComplete(%d)", aResultCode );
       
  1967     
       
  1968     iDevASRObserver->DevASREvent( EDevASRUnloadRule, aResultCode );
       
  1969     }
       
  1970 
       
  1971 
       
  1972 // -----------------------------------------------------------------------------
       
  1973 // CSRSAlgorithmManager::RecognitionComplete
       
  1974 // The recognition backend has finished processing feature vectors. A recognition
       
  1975 // result is created.
       
  1976 // -----------------------------------------------------------------------------
       
  1977 //
       
  1978 void CSRSAlgorithmManager::RecognitionComplete( TInt aResultCode )
       
  1979     {
       
  1980     RUBY_DEBUG1( "CSRSAlgorithmManager::RecognitionComplete(%d)", aResultCode );
       
  1981 
       
  1982     // Recognition is complete, DevASR can return to pre-recognition state
       
  1983     if ( StateTransition( EDASRPreRecognition ) )
       
  1984         {
       
  1985         // Mark that sampling can be ended if not stopped yet
       
  1986         iEndFlag = ETrue;
       
  1987 		iDevASRObserver->DevASREvent( EDevASRRecognize, TranslateError( aResultCode ) );
       
  1988         }
       
  1989     else
       
  1990         {
       
  1991         if ( iDevASRState == EDASRRecognitionSampling )
       
  1992             {
       
  1993             StateTransition( EDASRRecognition );
       
  1994             iDevSound->Stop();
       
  1995 			iDevSoundState = ESoundDeviceStopped;
       
  1996             iDevASRObserver->DevASREvent( EDevASREouDetected, KErrNone );
       
  1997             StateTransition( EDASRPreRecognition );
       
  1998             iDevASRObserver->DevASREvent( EDevASRRecognize, TranslateError( aResultCode ) );
       
  1999             }
       
  2000         }
       
  2001     }
       
  2002 
       
  2003 
       
  2004 // -----------------------------------------------------------------------------
       
  2005 // RecAlgMgr Observer MIXIN impl ends
       
  2006 // -----------------------------------------------------------------------------
       
  2007 
       
  2008 // -----------------------------------------------------------------------------
       
  2009 // MVmAlgMgrObserver MIXIN impl starts
       
  2010 // -----------------------------------------------------------------------------
       
  2011 
       
  2012 // -----------------------------------------------------------------------------
       
  2013 // CSRSAlgorithmManager::CombineComplete
       
  2014 // Grammar combination is ready.
       
  2015 // -----------------------------------------------------------------------------
       
  2016 //
       
  2017 void CSRSAlgorithmManager::CombineComplete( HBufC8* aResult, TInt aError )
       
  2018     {
       
  2019     // Forward call to reco alg manager
       
  2020     iRecognitionAlgMgr->CombineComplete( aResult, aError );
       
  2021     }
       
  2022 
       
  2023 
       
  2024 // -----------------------------------------------------------------------------
       
  2025 // MVmAlgMgrObserver MIXIN impl ends
       
  2026 // -----------------------------------------------------------------------------
       
  2027 
       
  2028 
       
  2029 // -----------------------------------------------------------------------------
       
  2030 // Active object implementation begins
       
  2031 // -----------------------------------------------------------------------------
       
  2032 
       
  2033 
       
  2034 // -----------------------------------------------------------------------------
       
  2035 // CSRSAlgorithmManager::RunL
       
  2036 // Handle the requested function asynchronously.
       
  2037 // result is created.
       
  2038 // -----------------------------------------------------------------------------
       
  2039 //
       
  2040 void CSRSAlgorithmManager::RunL()
       
  2041     {
       
  2042 
       
  2043     switch ( iRequestFunction )
       
  2044         {
       
  2045         case KRecord:
       
  2046             TRAPD( err, HandleRecordL() );
       
  2047             if ( err )
       
  2048                 {
       
  2049                 iRecognitionAlgMgr->Cancel();
       
  2050                 StateTransition( EDASRIdle );
       
  2051                 iDevASRObserver->DevASREvent( EDevASRRecord, TranslateError( err ) );
       
  2052                 }
       
  2053             break;
       
  2054 
       
  2055         case KPlay:
       
  2056 
       
  2057             TRAPD( err1, HandlePlayL() );
       
  2058             if ( err1 )
       
  2059                 {
       
  2060                 StateTransition( EDASRIdle );
       
  2061                 iDevASRObserver->DevASREvent( EDevASRPlay, TranslateError( err1 ) );
       
  2062                 }
       
  2063             break;
       
  2064 
       
  2065         default:
       
  2066             break;
       
  2067         };
       
  2068     }
       
  2069 
       
  2070 
       
  2071 // -----------------------------------------------------------------------------
       
  2072 // CSRSAlgorithmManager::Ready
       
  2073 // Utility function to post a request complete.
       
  2074 // result is created.
       
  2075 // -----------------------------------------------------------------------------
       
  2076 //
       
  2077 void CSRSAlgorithmManager::Ready( const TInt aStatus )
       
  2078     {
       
  2079     TRequestStatus* stat = &iStatus;
       
  2080     User::RequestComplete( stat, aStatus );
       
  2081     SetActive();
       
  2082     }
       
  2083 
       
  2084 
       
  2085 // -----------------------------------------------------------------------------
       
  2086 // Active object impl ends
       
  2087 // -----------------------------------------------------------------------------
       
  2088 
       
  2089 
       
  2090 // -----------------------------------------------------------------------------
       
  2091 // CSRSAlgorithmManager::DevASRState
       
  2092 // Utility function print out the state of DevASR for unit testing.
       
  2093 // result is created.
       
  2094 // -----------------------------------------------------------------------------
       
  2095 //
       
  2096 void CSRSAlgorithmManager::DevASRState()
       
  2097     {
       
  2098 
       
  2099 #ifdef _DEBUG
       
  2100     switch ( iDevASRState )
       
  2101         {
       
  2102         case EDASRIdle:
       
  2103             RUBY_DEBUG1( "DevASR is: [%d] - EDASRIdle", iDevASRState );
       
  2104             break;
       
  2105 
       
  2106         case EDASRInitTrain:
       
  2107             RUBY_DEBUG1( "DevASR is: [%d] - EDASRInitTrain", iDevASRState );
       
  2108             break;
       
  2109 
       
  2110         case EDASRTrain:
       
  2111             RUBY_DEBUG1( "DevASR is: [%d] - EDASRTrain", iDevASRState );
       
  2112             break;
       
  2113 
       
  2114         case EDASRRecognitionSampling:
       
  2115             RUBY_DEBUG1( "DevASR is: [%d] - EDASRRecognitionSampling", iDevASRState );
       
  2116             break;
       
  2117 
       
  2118         case EDASRTrainSampling:
       
  2119             RUBY_DEBUG1( "DevASR is: [%d] - EDASRTrainSampling", iDevASRState );
       
  2120             break;
       
  2121 
       
  2122         case EDASRPlayback:
       
  2123             RUBY_DEBUG1( "DevASR is: [%d] - EDASRPlayback", iDevASRState );
       
  2124             break;
       
  2125 
       
  2126         case EDASRPreRecognition:
       
  2127             RUBY_DEBUG1( "DevASR is: [%d] - EDASRPreRecognition", iDevASRState );
       
  2128             break;
       
  2129 
       
  2130         case EDASRRecognitionPlayback:
       
  2131             RUBY_DEBUG1( "DevASR is: [%d] - EDASRRecognitionPlayback", iDevASRState );
       
  2132             break;
       
  2133 
       
  2134         case EDASRInitRecognition:
       
  2135             RUBY_DEBUG1( "DevASR is: [%d] - EDASRInitRecognition", iDevASRState );
       
  2136             break;
       
  2137 
       
  2138         case EDASRRecognition:
       
  2139             RUBY_DEBUG1( "DevASR is: [%d] - EDASRRecognition", iDevASRState );
       
  2140             break;
       
  2141 
       
  2142         case EDASRCancel:
       
  2143             RUBY_DEBUG1( "DevASR is: [%d] - EDASRCancel", iDevASRState );
       
  2144         default:
       
  2145             RUBY_DEBUG1( "DevASR is: [%d] - Unknown", iDevASRState );
       
  2146             break;
       
  2147         };
       
  2148 #endif
       
  2149 
       
  2150     }