Update contrib.
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // SpeechRecognitionUtility.cpp
19 #include <mmf/common/mmfcontroller.h>
20 #include <mmf/common/speechrecognitioncustomcommands.h>
21 #include <mmf/common/speechrecognitiondatacommon.h>
23 #include <speechrecognitionutility.h>
24 #include "SpeechRecognitionUtilityBody.h"
26 //_LIT(KMMFSpeechRecognitionUtilityPanic,"Speech Recognition Utility Bad State");
29 KMMFSpeechRecognitionUtilityPanicBadState,
30 KMMFSpeechRecognitionUtilityPanicUnrecognisedEvent
33 CSpeechRecognitionUtility::CBody* CSpeechRecognitionUtility::CBody::NewL( TUid aClientUid, MSpeechRecognitionUtilityObserver& aSpeechRecognitionUtilityObserver)
35 CSpeechRecognitionUtility::CBody* self = CSpeechRecognitionUtility::CBody::NewLC(aClientUid, aSpeechRecognitionUtilityObserver);
36 CleanupStack::Pop(self);
40 CSpeechRecognitionUtility::CBody* CSpeechRecognitionUtility::CBody::NewLC(TUid aClientUid, MSpeechRecognitionUtilityObserver& aSpeechRecognitionUtilityObserver)
42 CSpeechRecognitionUtility::CBody* self = new(ELeave) CSpeechRecognitionUtility::CBody(aClientUid, aSpeechRecognitionUtilityObserver);
43 CleanupStack::PushL(self);
48 void CSpeechRecognitionUtility::CBody::ConstructL()
50 iAudioPriority = EMdaPriorityNormal;
51 iTrainPreference = EMdaPriorityPreferenceNone;
52 iRecognitionPreference = EMdaPriorityPreferenceNone;
53 iPlaybackPreference = EMdaPriorityPreferenceNone;
55 iPrioritySettings.iPriority = iAudioPriority;
56 iPrioritySettings.iPref = iTrainPreference;
58 iControllerEventMonitor = CMMFControllerEventMonitor::NewL(*this, iMMFController);
60 RMMFControllerImplInfoArray controllers;
61 CleanupResetAndDestroyPushL(controllers);
63 CMMFControllerPluginSelectionParameters* cSelect = CMMFControllerPluginSelectionParameters::NewLC();
65 // Select the media IDs to allow
66 RArray<TUid> mediaIds;
67 CleanupClosePushL(mediaIds);
68 User::LeaveIfError(mediaIds.Append(KUidMediaTypeASR));
69 cSelect->SetMediaIdsL(mediaIds,CMMFPluginSelectionParameters::EAllowOnlySuppliedMediaIds);
70 CleanupStack::PopAndDestroy(&mediaIds);
72 cSelect->ListImplementationsL(controllers);
74 //[ Open and configure a controller]
75 iControllerEventMonitor->Cancel();
76 iMMFController.Close();
77 //[check controller array is not empty]
78 if( ! controllers.Count() )
79 User::Leave( KErrNotFound );
81 //[ simply open the first controller in the array ]
82 User::LeaveIfError( iMMFController.Open(controllers[0]->Uid(), iPrioritySettings));
83 iControllerEventMonitor->Start();
85 iSrCustomCommands.SetClientUid(iClientUid);
87 CleanupStack::PopAndDestroy(2, &controllers);//controllers, cSelect
91 CSpeechRecognitionUtility::CBody::CBody(TUid aClientUid, MSpeechRecognitionUtilityObserver& aSpeechRecognitionUtilityObserver)
92 : iSrCustomCommands(iMMFController),
93 iSpeechRecognitionUtilityObserver(&aSpeechRecognitionUtilityObserver),
94 iClientUid(aClientUid)
98 CSpeechRecognitionUtility::CBody::~CBody()
100 if (iControllerEventMonitor)
102 iControllerEventMonitor->Cancel();
103 delete iControllerEventMonitor;
105 iMMFController.Close();
108 TInt CSpeechRecognitionUtility::CBody::GetEngineProperties(const RArray<TInt>& aPropertyIDs, RArray<TInt>& aPropertyValues)
110 aPropertyValues.Reset();
111 return iSrCustomCommands.GetEngineProperties(aPropertyIDs, aPropertyValues);
114 TInt CSpeechRecognitionUtility::CBody::AddPronunciation(TLexiconID aLexiconID, TModelBankID aModelBankID, TModelID aModelID, TPronunciationID& aPronunciationID)
119 TInt err = iSrCustomCommands.AddPronunciation(aLexiconID, aModelBankID, aModelID, aPronunciationID);
122 iAsyncCallBusy=ETrue;
126 TInt CSpeechRecognitionUtility::CBody::AddRule(TGrammarID aGrammarID, TLexiconID aLexiconID, TPronunciationID aPronunciationID, TRuleID& aRuleID)
131 TInt err = iSrCustomCommands.AddRule(aGrammarID, aLexiconID, aPronunciationID, aRuleID);
134 iAsyncCallBusy=ETrue;
139 void CSpeechRecognitionUtility::CBody::Cancel()
141 iSrCustomCommands.Cancel();
142 // Now cancel has been called, so remove any pointers storing references to arrays to be filled
143 // These arrays are not owned by this object, so they must not be destroyed
144 iPronunciationIDs = NULL;
147 iAsyncCallBusy = EFalse;
150 TInt CSpeechRecognitionUtility::CBody::CommitChanges()
155 return iSrCustomCommands.CommitChanges();
158 TInt CSpeechRecognitionUtility::CBody::CreateGrammar(TGrammarID& aGrammarID)
162 TInt err = iSrCustomCommands.CreateGrammar(aGrammarID);
165 iAsyncCallBusy=ETrue;
169 TInt CSpeechRecognitionUtility::CBody::CreateLexicon(TLexiconID& aLexiconID)
173 TInt err = iSrCustomCommands.CreateLexicon(aLexiconID);
176 iAsyncCallBusy=ETrue;
180 TInt CSpeechRecognitionUtility::CBody::LoadModels(TModelBankID aModelBankID)
185 TInt err = iSrCustomCommands.LoadModels(aModelBankID);
188 iAsyncCallBusy=ETrue;
193 TInt CSpeechRecognitionUtility::CBody::LoadGrammar(TGrammarID aGrammarID)
198 TInt err = iSrCustomCommands.LoadGrammar(aGrammarID);
201 iAsyncCallBusy=ETrue;
207 TInt CSpeechRecognitionUtility::CBody::LoadLexicon(TLexiconID aLexiconID)
212 TInt err = iSrCustomCommands.LoadLexicon(aLexiconID);
215 iAsyncCallBusy=ETrue;
222 TInt CSpeechRecognitionUtility::CBody::GetUtteranceDuration(TModelBankID aModelBankID, TModelID aModelID, TTimeIntervalMicroSeconds32& aDuration)
227 TInt err = iSrCustomCommands.GetUtteranceDuration(aModelBankID, aModelID, aDuration);
230 iAsyncCallBusy=ETrue;
236 TInt CSpeechRecognitionUtility::CBody::PlayUtterance(TModelBankID aModelBankID, TModelID aModelID)
241 iPrioritySettings.iPriority = iAudioPriority;
242 iPrioritySettings.iPref = iPlaybackPreference;
244 TInt err = iMMFController.SetPrioritySettings(iPrioritySettings);
247 err = iSrCustomCommands.PlayUtterance(aModelBankID, aModelID);
250 iAsyncCallBusy=ETrue;
255 TInt CSpeechRecognitionUtility::CBody::GetModelCount(TModelBankID aModelBankID, TInt& aModelCount)
260 TInt err = iSrCustomCommands.GetModelCount(aModelBankID, aModelCount);
263 iAsyncCallBusy=ETrue;
268 TInt CSpeechRecognitionUtility::CBody::StartRecSession(TRecognitionMode aMode)
273 return iSrCustomCommands.StartRecSession(aMode);
276 TInt CSpeechRecognitionUtility::CBody::EndRecSession()
281 return iSrCustomCommands.EndRecSession();
284 TInt CSpeechRecognitionUtility::CBody::Recognize(CSDClientResultSet& aResultSet)
289 ASSERT(iResultSet==NULL); // this should be NULL unless we have problems
291 iPrioritySettings.iPriority = iAudioPriority;
292 iPrioritySettings.iPref = iRecognitionPreference;
293 TInt err = iMMFController.SetPrioritySettings(iPrioritySettings);
296 err = iSrCustomCommands.Recognize(aResultSet);
300 iResultSet = &aResultSet;
301 iAsyncCallBusy = ETrue;
306 TInt CSpeechRecognitionUtility::CBody::Record(TTimeIntervalMicroSeconds32 aRecordTime)
311 TInt err = iSrCustomCommands.Record(aRecordTime);
314 iAsyncCallBusy=ETrue;
319 TInt CSpeechRecognitionUtility::CBody::RemoveGrammar(TGrammarID aGrammarID)
324 TInt err = iSrCustomCommands.RemoveGrammar(aGrammarID);
327 iAsyncCallBusy = ETrue;
331 TInt CSpeechRecognitionUtility::CBody::RemovePronunciation(TLexiconID aLexiconID, TPronunciationID aPronunciationID)
336 TInt err = iSrCustomCommands.RemovePronunciation(aLexiconID, aPronunciationID);
339 iAsyncCallBusy = ETrue;
343 TInt CSpeechRecognitionUtility::CBody::RemoveLexicon(TLexiconID aLexiconID)
348 TInt err = iSrCustomCommands.RemoveLexicon(aLexiconID);
351 iAsyncCallBusy = ETrue;
355 TInt CSpeechRecognitionUtility::CBody::RemoveModel(TModelBankID aModelBankID, TModelID aModelID)
360 TInt err = iSrCustomCommands.RemoveModel(aModelBankID, aModelID);
363 iAsyncCallBusy = ETrue;
368 TInt CSpeechRecognitionUtility::CBody::RemoveRule(TGrammarID aGrammarID, TRuleID aRuleID)
373 TInt err = iSrCustomCommands.RemoveRule(aGrammarID, aRuleID);
376 iAsyncCallBusy = ETrue;
381 TInt CSpeechRecognitionUtility::CBody::Train(TModelBankID aModelBankID, TModelID& aModelID)
386 iPrioritySettings.iPriority = iAudioPriority;
387 iPrioritySettings.iPref = iTrainPreference;
388 TInt err = iMMFController.SetPrioritySettings(iPrioritySettings);
391 err = iSrCustomCommands.Train(aModelBankID, aModelID);
394 iAsyncCallBusy = ETrue;
399 TInt CSpeechRecognitionUtility::CBody::UnloadRule(TGrammarID aGrammarID, TRuleID aRuleID)
404 TInt err = iSrCustomCommands.UnloadRule(aGrammarID, aRuleID);
407 iAsyncCallBusy = ETrue;
413 void CSpeechRecognitionUtility::CBody::SetEventHandler(MSpeechRecognitionUtilityObserver* aSpeechRecognitionUtilityObserver)
415 iSpeechRecognitionUtilityObserver = aSpeechRecognitionUtilityObserver;
419 TInt CSpeechRecognitionUtility::CBody::GetAllPronunciationIDs(TLexiconID aLexiconID, RArray <TPronunciationID>& aPronunciationIDs)
424 ASSERT(iPronunciationIDs==NULL);
426 TInt err = iSrCustomCommands.GetAllPronunciationIDs(aLexiconID);
430 iPronunciationIDs = &aPronunciationIDs;
431 iPronunciationIDs->Reset();
432 iAsyncCallBusy = ETrue;
438 TInt CSpeechRecognitionUtility::CBody::GetAllModelIDs(TModelBankID aModelBankID, RArray <TModelID>& aModelIDs)
443 ASSERT(iModelIDs==NULL);
445 TInt err = iSrCustomCommands.GetAllModelIDs(aModelBankID);
449 iModelIDs = &aModelIDs;
451 iAsyncCallBusy = ETrue;
457 TInt CSpeechRecognitionUtility::CBody::GetAllRuleIDs(TGrammarID aGrammarID, RArray <TRuleID>& aRuleIDs)
462 ASSERT(iRuleIDs==NULL);
464 TInt err = iSrCustomCommands.GetAllRuleIDs(aGrammarID);
468 iRuleIDs = &aRuleIDs;
470 iAsyncCallBusy = ETrue;
476 TInt CSpeechRecognitionUtility::CBody::GetAllClientLexiconIDs(RArray <TLexiconID>& aLexiconIDs)
481 ASSERT(iLexiconIDs==NULL);
483 TInt err = iSrCustomCommands.GetAllClientLexiconIDs();
487 iLexiconIDs = &aLexiconIDs;
488 iLexiconIDs->Reset();
489 iAsyncCallBusy = ETrue;
495 TInt CSpeechRecognitionUtility::CBody::GetAllClientModelBankIDs(RArray <TModelBankID>& aModelBankIDs)
500 ASSERT(iModelBankIDs==NULL);
502 TInt err = iSrCustomCommands.GetAllClientModelBankIDs();
506 iModelBankIDs = &aModelBankIDs;
507 iModelBankIDs->Reset();
508 iAsyncCallBusy = ETrue;
514 TInt CSpeechRecognitionUtility::CBody::GetAllClientGrammarIDs(RArray <TGrammarID>& aGrammarIDs)
519 ASSERT(iGrammarIDs==NULL);
521 TInt err = iSrCustomCommands.GetAllClientGrammarIDs();
525 iGrammarIDs = &aGrammarIDs;
526 iGrammarIDs->Reset();
527 iAsyncCallBusy = ETrue;
533 TInt CSpeechRecognitionUtility::CBody::GetAllLexiconIDs(RArray <TLexiconID>& aLexiconIDs)
538 ASSERT(iLexiconIDs==NULL);
540 TInt err = iSrCustomCommands.GetAllLexiconIDs();
544 iLexiconIDs = &aLexiconIDs;
545 iLexiconIDs->Reset();
546 iAsyncCallBusy = ETrue;
552 TInt CSpeechRecognitionUtility::CBody::GetAllModelBankIDs(RArray <TModelBankID>& aModelBankIDs)
557 ASSERT(iModelBankIDs==NULL);
559 TInt err = iSrCustomCommands.GetAllModelBankIDs();
563 iModelBankIDs = &aModelBankIDs;
564 iModelBankIDs->Reset();
565 iAsyncCallBusy = ETrue;
571 TInt CSpeechRecognitionUtility::CBody::GetAllGrammarIDs(RArray <TGrammarID>& aGrammarIDs)
576 ASSERT(iGrammarIDs==NULL);
578 TInt err = iSrCustomCommands.GetAllGrammarIDs();
582 iGrammarIDs = &aGrammarIDs;
583 iGrammarIDs->Reset();
584 iAsyncCallBusy = ETrue;
590 TInt CSpeechRecognitionUtility::CBody::GetRuleValidity(TGrammarID aGrammarID, TRuleID aRuleID, TBool& aValid)
595 TInt err = iSrCustomCommands.GetRuleValidity(aGrammarID, aRuleID, aValid);
598 iAsyncCallBusy = ETrue;
603 TInt CSpeechRecognitionUtility::CBody::CreateModelBank(TModelBankID& aModelBankID)
607 TInt err = iSrCustomCommands.CreateModelBank(aModelBankID);
609 iAsyncCallBusy = ETrue;
613 TInt CSpeechRecognitionUtility::CBody::RemoveModelBank(TModelBankID aModelBankID)
618 TInt err = iSrCustomCommands.RemoveModelBank(aModelBankID);
620 iAsyncCallBusy = ETrue;
624 TInt CSpeechRecognitionUtility::CBody::GetAvailableStorage(TInt& aAvailableStorage)
629 TInt err = iSrCustomCommands.GetAvailableStorage(aAvailableStorage);
632 iAsyncCallBusy = ETrue;
637 TInt CSpeechRecognitionUtility::CBody::LoadEngineParameters(const RArray<TInt>& aParameterId, const RArray<TInt>& aParameterValue)
642 return iSrCustomCommands.LoadEngineParameters(aParameterId, aParameterValue);
645 TInt CSpeechRecognitionUtility::CBody::SetAudioPriority(TInt aPriority, TInt aTrainPreference, TInt aPlaybackPreference, TInt aRecognitionPreference)
647 iAudioPriority = aPriority;
648 iTrainPreference = (TMdaPriorityPreference)aTrainPreference;
649 iPlaybackPreference = (TMdaPriorityPreference)aPlaybackPreference;
650 iRecognitionPreference = (TMdaPriorityPreference)aRecognitionPreference;
655 void CSpeechRecognitionUtility::CBody::HandleEvent(const TMMFEvent& aEvent)
657 TBool cancelled = EFalse;
658 TBool asyncCallComplete = ETrue;
660 TUid event = aEvent.iEventType;
661 TInt errorCode = aEvent.iErrorCode;
666 case KUidAsrEventGetAllPronunciationIDsVal :
668 if (iPronunciationIDs)
670 if (errorCode==KErrNone)
671 TRAP(errorCode, iSrCustomCommands.GetPronunciationIDArrayL(*iPronunciationIDs));
672 // async operation complete, so get rid of reference to array
673 iPronunciationIDs = NULL;
679 case KUidAsrEventGetAllRuleIDsVal :
683 if (errorCode==KErrNone)
684 TRAP(errorCode, iSrCustomCommands.GetRuleIDArrayL(*iRuleIDs));
685 // async operation complete, so get rid of reference to array
692 case KUidAsrEventGetAllModelIDsVal :
696 if (errorCode==KErrNone)
697 TRAP(errorCode, iSrCustomCommands.GetModelIDArrayL(*iModelIDs));
698 // async operation complete, so get rid of reference to array
705 case KUidAsrEventGetAllClientModelBankIDsVal :
706 case KUidAsrEventGetAllModelBankIDsVal :
710 if (errorCode==KErrNone)
711 TRAP(errorCode, iSrCustomCommands.GetModelBankIDArrayL(*iModelBankIDs));
712 // async operation complete, so get rid of reference to array
713 iModelBankIDs = NULL;
719 case KUidAsrEventGetAllClientGrammarIDsVal :
720 case KUidAsrEventGetAllGrammarIDsVal :
724 if (errorCode==KErrNone)
725 TRAP(errorCode, iSrCustomCommands.GetGrammarIDArrayL(*iGrammarIDs));
726 // async operation complete, so get rid of reference to array
733 case KUidAsrEventGetAllClientLexiconIDsVal :
734 case KUidAsrEventGetAllLexiconIDsVal :
738 if (errorCode==KErrNone)
739 TRAP(errorCode, iSrCustomCommands.GetLexiconIDArrayL(*iLexiconIDs));
740 // async operation complete, so get rid of reference to array
748 case KUidAsrEventRecognitionVal :
752 if (errorCode==KErrNone)
753 TRAP(errorCode, iSrCustomCommands.GetResultSetL(*iResultSet));
760 case KUidAsrEventTrainVal:
762 case KUidAsrEventPlayStartedVal:
763 case KUidAsrEventRecordStartedVal:
764 asyncCallComplete = EFalse;
770 if (event == KMMFErrorCategoryControllerGeneralError)
772 // clear these, as the error callback
773 iPronunciationIDs = NULL;
778 iModelBankIDs = NULL;
780 if (asyncCallComplete)
781 iAsyncCallBusy = EFalse;
783 if (iSpeechRecognitionUtilityObserver && !cancelled)
784 iSpeechRecognitionUtilityObserver->MsruoEvent(event, errorCode);